|
|
(170 intermediate revisions by the same user not shown) |
Line 2: |
Line 2: |
| | | |
| /** | | /** |
− | * Tests if a particular element possesses a class. | + | * Determines if a particular element possesses a class. |
| */ | | */ |
| var hasClass = (function () { | | var hasClass = (function () { |
Line 12: |
Line 12: |
| | | |
| /** | | /** |
− | * Custom getElementsByClassName by Snook + Nyman | + | * Determines if a given value is a number. |
| */ | | */ |
− | function getElementsByClassName(oElm, strTagName, oClassNames){ | + | function isNumeric(value) { |
− | var arrElements = (strTagName == '*' && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
| + | return new RegExp(/(^\d+$)|(^\d+\.\d+$)/).test(value); |
− | var arrReturnElements = new Array();
| + | |
− | var arrRegExpClassNames = new Array();
| + | |
− | | + | |
− | if (typeof oClassNames == 'object') {
| + | |
− | for (var i = 0; i < oClassNames.length; i++) {
| + | |
− | arrRegExpClassNames[arrRegExpClassNames.length] = new RegExp('(^|\\s)' + oClassNames[i].replace(/\-/g, '\\-') + '(\\s|$)');
| + | |
− | }
| + | |
− | } else {
| + | |
− | arrRegExpClassNames[arrRegExpClassNames.length] = new RegExp('(^|\\s)' + oClassNames.replace(/\-/g, '\\-') + '(\\s|$)');
| + | |
− | }
| + | |
− | | + | |
− | var oElement;
| + | |
− | var bMatchesAll;
| + | |
− | | + | |
− | for (var j = 0; j < arrElements.length; j++) {
| + | |
− | oElement = arrElements[j];
| + | |
− | bMatchesAll = true;
| + | |
− | | + | |
− | for (var k = 0; k < arrRegExpClassNames.length; k++) {
| + | |
− | if(!arrRegExpClassNames[k].test(oElement.className)) {
| + | |
− | bMatchesAll = false;
| + | |
− | break;
| + | |
− | }
| + | |
− | }
| + | |
− | | + | |
− | if (bMatchesAll) {
| + | |
− | arrReturnElements[arrReturnElements.length] = oElement;
| + | |
− | }
| + | |
− | }
| + | |
− | | + | |
− | return arrReturnElements;
| + | |
| } | | } |
| | | |
| /** | | /** |
− | * Code for collapsible tables. | + | * Removes all children from an element. |
| */ | | */ |
− | var collapseCaption = 'hide';
| + | function removeChildren(element) { |
− | var expandCaption = 'show';
| + | while (element.hasChildNodes()) { |
− | | + | element.removeChild(element.firstChild); |
− | function collapseTable(tableIndex) { | + | |
− | var button = document.getElementById('collapseButton' + tableIndex); | + | |
− | var table = document.getElementById('collapsibleTable' + tableIndex);
| + | |
− | | + | |
− | if (!table || !button) {
| + | |
− | return false; | + | |
− | }
| + | |
− | | + | |
− | var rows = table.rows;
| + | |
− | | + | |
− | if (button.firstChild.data == collapseCaption) {
| + | |
− | for (var i = 1; i < rows.length; i++) {
| + | |
− | rows[i].style.display = 'none';
| + | |
− | }
| + | |
− | button.firstChild.data = expandCaption;
| + | |
− | } else {
| + | |
− | for (var i = 1; i < rows.length; i++) {
| + | |
− | rows[i].style.display = rows[0].style.display;
| + | |
− | }
| + | |
− | button.firstChild.data = collapseCaption;
| + | |
| } | | } |
| } | | } |
| | | |
− | function createCollapseButtons() {
| + | /** |
− | var tableIndex = 0;
| + | * Import more specific scripts. |
− | var navigationBoxes = new Object();
| + | */ |
− | var tables = getElementsByClassName(document, 'table', 'collapses');
| + | importScript('MediaWiki:Collapsible.js'); |
| + | importScript('MediaWiki:Calculator.js'); |
| | | |
− | for (var i = 0; i < tables.length; i++) {
| + | if (wgAction == 'edit' || wgAction == 'submit') |
− | var headerRow = tables[i].getElementsByTagName('tr')[0];
| + | { |
− | if (!headerRow) {
| + | importScript('MediaWiki:Editbar.js'); |
− | continue;
| + | |
− | }
| + | |
− | | + | |
− | var header = headerRow.getElementsByTagName('th')[0];
| + | |
− | if (!header) {
| + | |
− | continue;
| + | |
− | }
| + | |
− | | + | |
− | navigationBoxes[tableIndex] = tables[i];
| + | |
− | tables[i].setAttribute('id', 'collapsibleTable' + tableIndex);
| + | |
− | | + | |
− | var spacer = document.createElement('div');
| + | |
− | var button = document.createElement('span');
| + | |
− | var buttonLink = document.createElement('a');
| + | |
− | var buttonText = document.createTextNode(collapseCaption);
| + | |
− | | + | |
− | spacer.style.cssFloat = 'left';
| + | |
− | spacer.style.styleFloat = 'left';
| + | |
− | spacer.style.textAlign = 'left';
| + | |
− | spacer.style.width = '35px';
| + | |
− | spacer.appendChild(document.createTextNode('\u00a0'));
| + | |
− | header.insertBefore(spacer, header.childNodes[0]);
| + | |
− | | + | |
− | button.style.cssFloat = 'right';
| + | |
− | button.style.styleFloat = 'right';
| + | |
− | button.style.fontSize = '11px';
| + | |
− | button.style.fontWeight = 'normal';
| + | |
− | button.style.textAlign = 'right';
| + | |
− | button.style.width = '35px';
| + | |
− | | + | |
− | buttonLink.style.color = '#FFFFFF';
| + | |
− | buttonLink.id = 'collapseButton' + tableIndex;
| + | |
− | buttonLink.href = 'javascript:collapseTable(' + tableIndex + ');';
| + | |
− | buttonLink.appendChild(buttonText);
| + | |
− | | + | |
− | button.appendChild(document.createTextNode('['));
| + | |
− | button.appendChild(buttonLink);
| + | |
− | button.appendChild(document.createTextNode(']'));
| + | |
− | header.insertBefore(button, header.childNodes[0]);
| + | |
− | | + | |
− | tableIndex++;
| + | |
− | }
| + | |
− | | + | |
− | for (var i = 0; i < tableIndex; i++) {
| + | |
− | if (hasClass(navigationBoxes[i], 'collapsed') || (tableIndex >= 2 && hasClass(navigationBoxes[i], 'autocollapse'))) {
| + | |
− | collapseTable(i);
| + | |
− | }
| + | |
− | else if (hasClass(navigationBoxes[i], 'innercollapse')) {
| + | |
− | var element = navigationBoxes[i];
| + | |
− | while (element = element.parentNode) {
| + | |
− | if (hasClass(element, 'outercollapse')) {
| + | |
− | collapseTable(i);
| + | |
− | break;
| + | |
− | }
| + | |
− | }
| + | |
− | }
| + | |
− | }
| + | |
| } | | } |
− |
| |
− | addOnloadHook(createCollapseButtons);
| |
− |
| |
− | /**
| |
− | * Code for sorting tables.
| |
− | */
| |