MediaWiki:Linjekart.js

   

Fra Villmark

(Forskjeller mellom versjoner)
Gå til: navigasjon, søk
Admin (Diskusjon | bidrag)
(Ny side: /* Test if an element has a certain class ************************************** * * Description: Uses regular expressions and caching for better performance. * Maintainers: [[User:Mi…)

Nåværende revisjon fra 22. jul 2009 kl. 23:40

/* Test if an element has a certain class **************************************
  *
  * Description: Uses regular expressions and caching for better performance.
  * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
  */
 
 var hasClass = (function () {
     var reCache = {};
     return function (element, className) {
         return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
     };
 })();

 var aNode = new Array();
 var aOffset = new Array();
 var aGallery = new Array();
 var mainTable;
 var rightGallery;
 var bottomGallery;

 function positionTop(object)
 {
     var pos = 0;
     for (var par = object; par.offsetParent; par = par.offsetParent)
         pos += par.offsetTop;
     return pos;
 }

 function positionLeft(object)
 {
     var pos = 0;
     for (var par = object; par.offsetParent; par = par.offsetParent)
         pos += par.offsetLeft;
     return pos;
 }

 function reorgGalleries()
 {
     var bottomOffset = positionTop(mainTable) + mainTable.offsetHeight;
     var reduce = 0;
     var expand = 0;

     for ( var i = 0, TC; i < aNode.length; i++) {
         if ( aOffset[i] > bottomOffset) {
             if ( aNode[i].parentNode == rightGallery)
                 expand++;
         } else
             if ( aNode[i].parentNode.parentNode.parentNode == bottomGallery) {
                 rightGallery.insertBefore( aNode[i], mainTable );
                 reduce++;
             }
     }

     if (reduce > 0)
         for (var i = 0; i < aGallery.length - reduce; i++)
             aGallery[i].appendChild( aGallery[i + reduce].firstChild);

     if (expand > 0)
         for (var i = aGallery.length; i > 0; i--)
             aGallery[i - 1].appendChild( i > expand ? aGallery[i - expand - 1].firstChild
                                                     : aNode[aNode.length - aGallery.length + i - 1]);
 }

 function createGalleries()
 {
     var Divs = document.getElementsByTagName( "div" );
     mainTable = 0;

     for ( var i = 0; i < Divs.length; i++ ) {
         if ( hasClass( Divs[i], "flytegalleri" ) ) {
             Divs[i].normalize();
             rightGallery = Divs[i];

             for ( var chi = Divs[i].firstChild, rightOffset = 0; chi.nextSibling; chi = chi.nextSibling ) {
                 if ( hasClass( chi, "thumb" ) ) {
                     aNode[aNode.length] = chi;
                     aOffset[aOffset.length] = chi.offsetTop + chi.offsetHeight;

                     if (! rightOffset)
                         rightOffset = positionLeft(chi) + chi.offsetWidth;
                 }

                 if ( ! mainTable && chi.tagName == "TABLE" )
                     mainTable = chi;

                 if ( hasClass( chi, "flytegalleri" ) && aNode.length > 0 ) {
                     var bottomOffset = positionTop(chi);
                     var galleryRow = 0;
                     var TR = chi.insertRow(galleryRow++);
                     bottomGallery = chi.firstChild;

                     for ( var j = 0, k = 0, g = 0, test=0, TC; j < aNode.length; j++ ) {
                         if ( aOffset[j] > bottomOffset) {
                             TC = TR.insertCell(k++);
                             TC.appendChild( aNode[j] );
                             aGallery[g++] = TC;

                             if (test + TC.offsetWidth > rightOffset) {
                                 TR = chi.insertRow(galleryRow++);
                                 k = 0;
                                 test = 0;
                             } else
                                 test = positionLeft(TC) + TC.offsetWidth;
                         }
                     }
                     break;
                 }
             }
         }
     }
 }

 /** Collapsible tables *********************************************************
  *
  *  Description: Allows tables to be collapsed, showing only the header. See
  *               [[Wikipedia:NavFrame]].
  */

 /* Show/hide table sections *********************************
  *
  * Description: Shows or hides predefined sections of collapsible tables.
  * Maintainers: [[User:BjørnN]]
  */

 function leftCollapseTable(tableIndex, rowIndex)
 {
     var Tables = document.getElementsByTagName( "table" );
     var TableRows = Tables[tableIndex].rows;
     var TD = Tables[tableIndex+1].rows[0].getElementsByTagName( "td" );
     var collapse = false;
     var change = false;
     var started = false;
     var collapsedView = false;
     var detailView = false;

     for ( var j = 0; j < TableRows.length; j++ ) {
       var TR = TableRows[j];

       switch (rowIndex) {
         case 0:
           switch (TR.className) {
             case "startCollapse":
               TR.style.display = "none";
               break;
             case "endCollapse":
               break;
             default:
               TR.style.display = "";
           }
           detailView = true;
           break;
         case -1:
           switch (TR.className) {
             case "startCollapse":
               TR.style.display = "";
               collapse = true;
               break;
             case "endCollapse":
               collapse = false;
               break;
             default:
               if (collapse) {
                 TR.style.display = "none";
               }
           }
           collapsedView = true;
           break;
         default:
           if (j == rowIndex) {
             change = true;
           }

           if (TR.className == "endCollapse") {
             change = false;
           }

           if (change) {
             if (TR.style.display == "none") {
               TR.style.display = "";
             } else {
               TR.style.display = "none";
             }
           }

           if (TR.className == "startCollapse") {
               if (! started) {
                   if (TR.style.display == "none") {
                       detailView |= true
                   } else {
                       collapsedView |= true
                   }
		  started = true;
               }
           } else {
  	      started = false;
           }
       }
     }

     if (collapsedView) {
         TD[0].getElementsByTagName( "span" )[0].style.display = "";
     } else {
         TD[0].getElementsByTagName( "span" )[0].style.display = "none";
     }

     if (detailView) {
         TD[1].getElementsByTagName( "span" )[0].style.display = "";
     } else {
         TD[1].getElementsByTagName( "span" )[0].style.display = "none";
     }

     reorgGalleries();
 }

 /* Create a button *********************************
  *
  * Description: Creates folder style navigation buttons for collapsible tables.
  * Based on function createCollapseButton, maintained by [[:en:User:R. Koot]] 
  * Maintainers: [[User:BjørnN]]
  */

 function createLeftCollapseButton(tableIndex, rowIndex, caption, buttonTitle)
 {
     var Button     = document.createElement( "span" );
     var ButtonLink = document.createElement( "a" );
     var ButtonText = document.createTextNode( caption );

     Button.style.fontWeight = "normal";
     Button.style.width = "6em";

     ButtonLink.setAttribute( "title", buttonTitle );
     ButtonLink.setAttribute( "href", "javascript:leftCollapseTable(" + tableIndex + ", " + rowIndex + ");" );
     ButtonLink.appendChild( ButtonText );

     Button.appendChild( document.createTextNode( "[" ) );
     Button.appendChild( ButtonLink );
     Button.appendChild( document.createTextNode( "]" ) );

     return Button;
 }

 /* Create show/hide buttons for table sections *********************************
  *
  * Description: Creates folder style navigation buttons for collapsible tables.
  * Maintainers: [[User:BjørnN]]
  */

 function createLeftCollapseButtons()
 {
     var Tables = document.getElementsByTagName( "table" );

     for ( var i = 0; i < Tables.length; i++ ) {
         if ( hasClass( Tables[i], "withAlternateSections" ) ) {
             var started = false;
             var displayRow = true;
             var sectionStart = 0;
             var TableRows = Tables[i].rows;

             var TR = Tables[i+1].rows[0];

             var detailCaption = "Vis alt";
             var overviewCaption = "Vis oversikt";
             var showCaption = "+";
             var hideCaption = "–";
             var showTitle = "Vis mer";
             var hideTitle = "Vis mindre";

             var aTD = TR.getElementsByTagName( "td" ); // fetch parameter cache
             try { detailCaption = aTD[1].firstChild.data; } catch (e) { ; }
             try { overviewCaption = aTD[2].firstChild.data; } catch (e) { ; }
             try { showCaption = aTD[3].firstChild.data; } catch (e) { ; }
             try { hideCaption = aTD[4].firstChild.data; } catch (e) { ; }
             try { showTitle = aTD[5].firstChild.data; } catch (e) { ; }
             try { hideTitle = aTD[6].firstChild.data; } catch (e) { ; }

             var TD = TR.insertCell(0); // execute after all the aTD statemens - it changes the index reference
             var Button = createLeftCollapseButton(i, 0, detailCaption, detailCaption);

             TD.appendChild(Button);

             TD = TR.insertCell(1);
             Button = createLeftCollapseButton(i, -1, overviewCaption, overviewCaption);
             Button.style.display = "none";
             TD.appendChild(Button);

             for ( var j = 0; j < TableRows.length; j++ ) {
                 var TR = TableRows[j];
                 var TD = TR.insertCell(0);

                 switch (TR.className) {
                   case "startCollapse":
                     if (! started) {
                        sectionStart = j;
              	        Button = createLeftCollapseButton(i, sectionStart, showCaption, showTitle);
                        TD.appendChild(Button);
		        started = true;
		        displayRow = false;
              	     }
              	     TR.style.display = "";
                     break;
                   case "endCollapse":
		     displayRow = true;
                     break;
                   default:
                     if (started) {
  		       Button = createLeftCollapseButton(i, sectionStart, hideCaption, hideTitle);
                       TD.appendChild(Button);
  		       started = false;
               	       TR.style.display = "none";
                     } else {
                       if (! displayRow)
                	 TR.style.display = "none";
                     }
		 }
             }
         }
     }
     createGalleries();
 }

 addOnloadHook( createLeftCollapseButtons );
Personlige verktøy