MediaWiki:Common.js: различия между версиями

Материал из Noobtype — единственного источника Истины во Вселенной
Перейти к навигации Перейти к поиску
(Новая: /* Размещённый здесь код JavaScript будет загружен всем пользователям при обращении к какой-либо странице *...)
 
Нет описания правки
Строка 250: Строка 250:
}
}
addOnloadHook(processNSFWGalleryItems);
addOnloadHook(processNSFWGalleryItems);
//[edit] zero section
function editZeroSection(){
var body = document.getElementById('bodyContent')
if (!body || window.disable_zero_section) return
var h2s = body.getElementsByTagName('H2')
var h2 = h2s[0]
if (!h2) return
if (h2.parentNode.id == 'toctitle') h2 = h2s[1]
if (!h2) return
var span = h2.firstChild
if (!span || span.className != 'editsection') return
var zero = span.cloneNode(true)
body.insertBefore(zero, body.firstChild)
var a = zero.getElementsByTagName('a')[0]
if (a.href.indexOf('&section=T') == -1 ) a.title = a.title.replace(/:.*$/,': 0')
else a.title = 'Править секцию: 0'
a.setAttribute('href', wgScript + '?title='+wgPageName + '&action=edit&section=0')
}


/* </source> */
/* </source> */

Версия от 06:16, 28 мая 2008

/* Размещённый здесь код JavaScript будет загружен всем пользователям при обращении к какой-либо странице */
/* Размещённый здесь код JavaScript будет загружен всем пользователям при обращении к какой-либо странице
<source javascript> */

var auto_comment = 0;

if (document.URL.indexOf("action=edit") > 0 || document.URL.indexOf("action=submit") > 0)
{
       if (wgCanonicalNamespace != "Special")
       {
               document.write('<script type="text/javascript" src="/index.php' +
               '?title=MediaWiki:Onlyifediting.js&action=raw' +
               '&ctype=text/javascript&dontcountme=s"></script>');
       }
}

/* hasClass from en-wiki */
 
 var hasClass = (function () {
     var reCache = {};
     return function (element, className) {
         return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
     };
 })();

/** Collapsible tables *********************************************************
  *
  *  Description: Allows tables to be collapsed, showing only the header. See
  *               [[Wikipedia:NavFrame]].
  *  Maintainers: UNMAINTAINED
  */
 
 var autoCollapse = 2;
 var collapseCaption = "скрыть";
 var expandCaption = "показать";
 
 function collapseTable( tableIndex )
 {
     var Button = document.getElementById( "collapseButton" + tableIndex );
     var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
     if ( !Table || !Button ) {
         return false;
     }
 
     var Rows = Table.getElementsByTagName( "tr" ); 
 
     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;
     var NavigationBoxes = new Object();
     var Tables = document.getElementsByTagName( "table" );
 
     for ( var i = 0; i < Tables.length; i++ ) {
         if ( hasClass( Tables[i], "collapsible" ) ) {
             NavigationBoxes[ tableIndex ] = Tables[i];
             Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
             var Button     = document.createElement( "span" );
             var ButtonLink = document.createElement( "a" );
             var ButtonText = document.createTextNode( collapseCaption );
 
             Button.style.styleFloat = "right";
             Button.style.cssFloat = "right";
             Button.style.fontWeight = "normal";
             Button.style.textAlign = "right";
             Button.style.width = "6em";
 
             ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
             ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
             ButtonLink.appendChild( ButtonText );
 
             Button.appendChild( document.createTextNode( "[" ) );
             Button.appendChild( ButtonLink );
             Button.appendChild( document.createTextNode( "]" ) );
 
             var Header = Tables[i].getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
             /* only add button and increment count if there is a header row to work with */
             if (Header) {
                 Header.insertBefore( Button, Header.childNodes[0] );
                 tableIndex++;
             }
         }
     }
 
     for ( var i = 0;  i < tableIndex; i++ ) {
         if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
             collapseTable( i );
         }
     }
 }
 
 addOnloadHook( createCollapseButtons );

/** Dynamic Navigation Bars (experimental) *************************************
  *
  *  Description: See [[Wikipedia:NavFrame]].
  *  Maintainers: UNMAINTAINED
  */
 
  // set up the words in your language
  var NavigationBarHide = '[' + collapseCaption + ']';
  var NavigationBarShow = '[' + expandCaption + ']';
 
  // set up max count of Navigation Bars on page,
  // if there are more, all will be hidden
  // NavigationBarShowDefault = 0; // all bars will be hidden
  // NavigationBarShowDefault = 1; // on pages with more than 1 bar all bars will be hidden
  var NavigationBarShowDefault = autoCollapse;
 
 
  // shows and hides content and picture (if available) of navigation bars
  // Parameters:
  //     indexNavigationBar: the index of navigation bar to be toggled
  function toggleNavigationBar(indexNavigationBar, bForceShowHide)
  {
     var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
     var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 
     if (!NavFrame || !NavToggle) {
         return false;
     }

     if (bForceShowHide != undefined) {
       NavToggle.firstChild.data == bForceShowHide ? NavigationBarHide : NavigationBarShow;
     }

     // if shown now
     if (NavToggle.firstChild.data == NavigationBarHide) {
         for (
                 var NavChild = NavFrame.firstChild;
                 NavChild != null;
                 NavChild = NavChild.nextSibling
             ) {
             if ( hasClass( NavChild, 'NavPic' ) ) {
                 NavChild.style.display = 'none';
             }
             if ( hasClass( NavChild, 'NavContent') ) {
                 NavChild.style.display = 'none';
             }
         }
     NavToggle.firstChild.data = NavigationBarShow;
 
     // if hidden now
     } else if (NavToggle.firstChild.data == NavigationBarShow) {
         for (
                 var NavChild = NavFrame.firstChild;
                 NavChild != null;
                 NavChild = NavChild.nextSibling
             ) {
             if (hasClass(NavChild, 'NavPic')) {
                 NavChild.style.display = 'block';
             }
             if (hasClass(NavChild, 'NavContent')) {
                 NavChild.style.display = 'block';
             }
         }
     NavToggle.firstChild.data = NavigationBarHide;
     }
  }
 
  // adds show/hide-button to navigation bars
  function createNavigationBarToggleButton()
  {
     var indexNavigationBar = 0;
     // iterate over all < div >-elements 
     var divs = document.getElementsByTagName("div");
     for(var i = 0; NavFrame = divs[i]; i++) {
         // if found a navigation bar
         if (hasClass(NavFrame, "NavFrame")) {
             indexNavigationBar++;
             var NavToggle = document.createElement("a");
             NavToggle.className = 'NavToggle';
             NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
             NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
 
             var NavToggleText = document.createTextNode(NavigationBarHide);
             NavToggle.appendChild(NavToggleText);
             // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
             for(var j = 0; j < NavFrame.childNodes.length; j++) {
               if (hasClass(NavFrame.childNodes[j], "NavHead")) {
                 NavFrame.childNodes[j].appendChild(NavToggle);
               }
             }
             NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
             if (hasClass(NavFrame, "NavHideByDefault")) {
               toggleNavigationBar(indexNavigationBar, false);
             }
         }
     }
     // if more Navigation Bars found than Default: hide all
     /* if (NavigationBarShowDefault < indexNavigationBar) {
         for(var i=1; i<=indexNavigationBar; i++) {
             toggleNavigationBar(i);
         }
     } */
 
  }
 
  addOnloadHook( createNavigationBarToggleButton );

/** NSFW Gallery Items (experimental) *************************************
  *
  *  Description: none, see Parser.php and ImageGallery.php for "$nsfw" hack.
  *  Maintainers: [[User:drdaeman]]
  */

  function toggleChildVisibility(parent, visibility) {
    if (parent.hasChildNodes()) {
      var children = parent.childNodes;
      for (var i = 0; i < children.length; i++) {
        if (children[i].style) { children[i].style.visibility = visibility; }
      }
    }
  }

  function processNSFWGalleryItems() {
    var divs = document.getElementsByTagName("div");
    for(var i = 0; item = divs[i]; i++) {
      if (item.hasChildNodes() && hasClass(item, "nsfw-gallery-item")) {
        toggleChildVisibility(item, 'hidden');
        item.style.background = 'url(/skins/common/images/nsfw.png) no-repeat center center';
        item.style.cursor = 'pointer';
        item.onclick = function (e) {
          var target = (e.target) ? e.target : (window.event && event.srcElement) ? event.srcElement : false;
          if (target && hasClass(target, "nsfw-gallery-item")) {
            target.style.background = 'none';
            target.style.cursor = 'default';
            toggleChildVisibility(target, 'visible');
            target.className = target.className.replace("nsfw-gallery-item", "");
            if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; }
            return false;
          }
        }
      }
    }
  }
  addOnloadHook(processNSFWGalleryItems);
//[edit] zero section
function editZeroSection(){
 var body = document.getElementById('bodyContent')
 if (!body || window.disable_zero_section) return
 var h2s = body.getElementsByTagName('H2')
 var h2 = h2s[0]
 if (!h2) return
 if (h2.parentNode.id == 'toctitle') h2 = h2s[1]
 if (!h2) return
 var span = h2.firstChild
 if (!span || span.className != 'editsection') return
 var zero = span.cloneNode(true)
 body.insertBefore(zero, body.firstChild)
 var a = zero.getElementsByTagName('a')[0]
 if (a.href.indexOf('&section=T') == -1 )  a.title = a.title.replace(/:.*$/,': 0')
 else a.title = 'Править секцию: 0'
 a.setAttribute('href', wgScript + '?title='+wgPageName + '&action=edit&section=0')
}

/* </source> */