User:Erutuon/scripts/changeCaseOrder.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, it will take 5-10 minutes before the changes take effect. You may also have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// <nowiki>

/* jshint eqeqeq: true */
/* globals mw, $ */

'use strict';

$(function () {
	// Each object contains a canonical name, code, and an array of
	// row headers.
	// The array will be used to sort the table rows. List row headers in the order
	// that you want them to display.
	// If a row does not have its header listed in the array, it will appear
	// before all the headers in the array.
	// For Ancient Greek noun declension tables, this means that the "Case / #"
	// row will appear directly above the "Nominative" row.
	// Headers are converted to lowercase before this list is used.
	var languages = [
		{
			name: "Ancient Greek",
			code: "grc",
			headers: [ "nominative", "vocative", "accusative", "genitive", "dative", "notes:", "derived forms" ]
		},
		{
			name: "Latin",
			code: "la",
			headers: [ "nominative", "vocative", "accusative", "genitive", "dative", "ablative" ],
		},
		{
			name: "German",
			code: "de",
			headers: [ "nominative", "accusative", "dative", "genitive", "ablative", "vocative" ],
		},
	];
	
	function rearrange(language) {
		var languageName = language.name, languageCode = language.code, rowOrder = language.headers;
		
		var declensionTables = $(".inflection-table-" + languageCode + " tbody");
		
		// Get text of first header in row.
		var getCompareValue = function (row) {
			var headerText = $(row).children("th").first().text().trim().toLowerCase();
			return rowOrder.indexOf(headerText);
		};
		
		declensionTables.each(function () {
			$(this).replaceWith(function() {
				return $(this)
					.children()
					.sort(function (a, b) {
						return getCompareValue(a) - getCompareValue(b);
					});
			});
		});
	}
	
	languages.forEach(rearrange);
});

// </nowiki>