MediaWiki:Common.js

Материал из Энциклопедия МИФИ

Перейти к: навигация, поиск
(Попробую другой код.)
Строка 46: Строка 46:
-
//Collapsiblе: [[ВП:СБ]]
+
/** Collapsible tables *********************************************************
-
 
+
  *
-
var NavigationBarShowDefault = 2
+
  *  Description: Allows tables to be collapsed, showing only the header. See
-
var NavigationBarHide = '[скрыть]'
+
  *              [[:en:Wikipedia:NavFrame]].
-
var NavigationBarShow = '[показать]'
+
  *  Maintainers: [[:en:User:R. Koot]]
-
 
+
  */
-
var hasClass = (function (){
+
-
  var reCache = {}
+
var autoCollapse = 2;
-
return function (element, className){
+
var collapseCaption = "Скрыть";
-
  return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className)
+
var expandCaption = "Показать";
-
  }
+
-
})()
+
function collapseTable( tableIndex )
-
 
+
  {
-
function collapsibleTables(){
+
    var Button = document.getElementById( "collapseButton" + tableIndex );
-
  var Table, HRow,  HCell, btn, a, tblIdx = 0, colTables = []
+
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
-
var allTables = document.getElementsByTagName('table')
+
-
  for (var i=0; Table = allTables[i]; i++){
+
    if ( !Table || !Button ) {
-
  if (!hasClass(Table, 'collapsible')) continue
+
        return false;
-
  if (!(HRow=Table.rows[0])) continue
+
    }
-
  if (!(HCell=HRow.getElementsByTagName('th')[0])) continue
+
   
-
  Table.id = 'collapsibleTable' + tblIdx
+
    var Rows = Table.getElementsByTagName( "tr" );
-
  btn = document.createElement('span')
+
   
-
  btn.style.cssText = 'float:right; font-weight:normal; font-size:smaller'
+
    if ( Button.firstChild.data == collapseCaption ) {
-
  a = document.createElement('a')
+
        for ( var i = 1; i < Rows.length; i++ ) {
-
  a.id = 'collapseButton' + tblIdx
+
            Rows[i].style.display = "none";
-
  a.href = 'javascript:collapseTable(' + tblIdx + ');'
+
        }
-
  a.style.color = HCell.style.color
+
        Button.firstChild.data = expandCaption;
-
  a.appendChild(document.createTextNode(NavigationBarHide))
+
    } else {
-
  btn.appendChild(a)
+
        for ( var i = 1; i < Rows.length; i++ ) {
-
  HCell.insertBefore(btn, HCell.childNodes[0])
+
            Rows[i].style.display = Rows[0].style.display;
-
  colTables[tblIdx++] = Table
+
        }
 +
        Button.firstChild.data = collapseCaption;
 +
    }
  }
  }
-
  for (var i=0; i < tblIdx; i++)
+
   
-
  if ((tblIdx > NavigationBarShowDefault && hasClass(colTables[i], 'autocollapse')) || hasClass(colTables[i], 'collapsed'))
+
function createCollapseButtons()
-
    collapseTable(i)
+
{
-
}
+
    var tableIndex = 0;
-
 
+
    var NavigationBoxes = new Object();
-
function collapseTable (idx){
+
    var Tables = document.getElementsByTagName( "table" );
-
  var Table = document.getElementById('collapsibleTable' + idx)
+
-
var btn = document.getElementById('collapseButton' + idx)
+
    for ( var i = 0; i < Tables.length; i++ ) {
-
if (!Table || !btn) return false
+
        if ( hasClass( Tables[i], "collapsible" ) ) {
-
  var Rows = Table.rows
+
            NavigationBoxes[ tableIndex ] = Tables[i];
-
var isShown = (btn.firstChild.data == NavigationBarHide)
+
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
-
btn.firstChild.data = isShown ?  NavigationBarShow : NavigationBarHide
+
   
-
var disp = isShown ? 'none' : Rows[0].style.display
+
            var Button    = document.createElement( "span" );
-
for (var i=1; i < Rows.length; i++)
+
            var ButtonLink = document.createElement( "a" );
-
    Rows[i].style.display = disp
+
            var ButtonText = document.createTextNode( collapseCaption );
-
}
+
   
-
 
+
            Button.style.styleFloat = "right";
-
function collapsibleDivs(){
+
            Button.style.cssFloat = "right";
-
var navIdx = 0, colNavs = [], i, NavFrame
+
            Button.style.fontWeight = "normal";
-
var divs = document.getElementById('content').getElementsByTagName('div')
+
            Button.style.textAlign = "right";
-
  for (i=0; NavFrame = divs[i]; i++) {
+
            Button.style.width = "6em";
-
  if (!hasClass(NavFrame, 'NavFrame')) continue
+
-
  NavFrame.id = 'NavFrame' + navIdx
+
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
-
  var a = document.createElement('a')
+
            ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
-
  a.className = 'NavToggle'
+
            ButtonLink.appendChild( ButtonText );
-
  a.id = 'NavToggle' + navIdx
+
   
-
  a.href = 'javascript:collapseDiv(' + navIdx + ');'
+
            Button.appendChild( document.createTextNode( "[" ) );
-
  a.appendChild(document.createTextNode(NavigationBarHide))
+
            Button.appendChild( ButtonLink );
-
  for (var j=0; j < NavFrame.childNodes.length; j++)
+
            Button.appendChild( document.createTextNode( "]" ) );
-
    if (hasClass(NavFrame.childNodes[j], 'NavHead'))
+
-
      NavFrame.childNodes[j].appendChild(a)
+
            var Header = Tables[i].getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
-
  colNavs[navIdx++] = NavFrame
+
            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 );
 +
        }
 +
    }
  }
  }
-
  for (i=0; i < navIdx; i++)
+
   
-
  if ((navIdx > NavigationBarShowDefault && !hasClass(colNavs[i], 'expanded')) || hasClass(colNavs[i], 'collapsed'))
+
  addOnloadHook( createCollapseButtons );
-
    collapseDiv(i)
+
-
}
+
-
 
+
-
function collapseDiv(idx) {
+
-
  var div = document.getElementById('NavFrame' + idx)
+
-
var btn = document.getElementById('NavToggle' + idx)
+
-
if (!div || !btn) return false
+
-
var isShown = (btn.firstChild.data == NavigationBarHide)
+
-
btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide
+
-
var disp = isShown ? 'none' : 'block'
+
-
for (var child = div.firstChild; child != null;  child = child.nextSibling)
+
-
  if (hasClass(child, 'NavPic') || hasClass(child, 'NavContent'))
+
-
      child.style.display = disp
+
-
}
+

Версия 13:16, 25 августа 2009

/* Размещённый здесь JavaScript код будет загружаться всем пользователям при обращении к каждой странице */
function addWikifButton(){
 var toolbar = document.getElementById('toolbar')
 var textbox = document.getElementById('wpTextbox1')
 if (!textbox || !toolbar) return
 var i = document.createElement('img')
 i.src = 'http://upload.wikimedia.org/wikisource/ru/d/d1/Button-wikifikator.png'
 i.alt = i.title = 'Викификатор'
 i.onclick = Wikify
 i.style.cursor = 'pointer'
 toolbar.appendChild(i)
}
if (wgAction == 'edit' || wgAction == 'submit'){
 document.write('<script type="text/javascript" src="http://ru.wikipedia.org/w/index.php?title=MediaWiki:Wikificator.js&action=raw&ctype=text/javascript"><\/script>')
// document.write('<script type="text/javascript" src="http://wiki.mephist.ru/wiki?title=MediaWiki:Wikificator.js&action=raw&ctype=text/javascript"><\/script>')
 addOnloadHook(addWikifButton)
}
 
function addImageMapButton(){
 var toolbar = document.getElementById('toolbar')
 var textbox = document.getElementById('wpTextbox1')
 if (!textbox || !toolbar) return
 
 var addimdiv = document.createElement('div')
 addimdiv.id = 'AddImgMapDiv'
 addimdiv.style.display='none'
 
 toolbar.appendChild(addimdiv)
 
 var i = document.createElement('img')
 i.src = 'http://upload.wikimedia.org/wikipedia/en/1/12/Button_gallery.png'
 i.alt = i.title = 'Вставка картинки с разметкой'
 i.onclick = showImageMapEditor
 i.style.cursor = 'pointer'
 toolbar.appendChild(i)
 
}
if (wgAction == 'edit' || wgAction == 'submit'){
 document.write('<script type="text/javascript" src="/extensions/ImageMap/ImageMapEdit.js"><\/script>')
 document.write('<link rel="stylesheet" type="text/css" href="/extensions/ImageMap/ImageMapEdit.css" />')
 
// document.write('<script type="text/javascript" src="http://wiki.mephist.ru/wiki?title=MediaWiki:Wikificator.js&action=raw&ctype=text/javascript"><\/script>')
// addOnloadHook(addImageMapButton)
hookEvent("load", addImageMapButton)
}
 
 
 /** Collapsible tables *********************************************************
  *
  *  Description: Allows tables to be collapsed, showing only the header. See
  *               [[:en:Wikipedia:NavFrame]].
  *  Maintainers: [[:en:User:R. Koot]]
  */
 
 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];
             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 );
Помощь