Diferencia entre revisiones de «MediaWiki:Common.js»

De WikiEducator
Saltar a: navegación, buscar
(copy from WikiEducator.org)
 
(copy IFrame convenience functions from en.WikiEducator.org)
Línea 101: Línea 101:
 
   
 
   
 
  addOnloadHook( createCollapseButtons );
 
  addOnloadHook( createCollapseButtons );
 +
 +
function WEAddStyles(a) {
 +
  var ss = document.createElement('style');
 +
  ss.type = 'text/css';
 +
  ss.media = 'screen';
 +
  ss.title = 'WE';
 +
  if (ss.stylesheet) ss.stylesheet.cssText = a; //IE
 +
  else ss.appendChild(document.createTextNode(a));
 +
  document.getElementsByTagName('head')[0].appendChild(ss);
 +
}
 +
 +
addOnloadHook(function() {
 +
  /* hide some navigation and other unnecessary elements if displayed in an iFrame */
 +
  if (window.self != window.top) {
 +
    var content = document.getElementById('content');
 +
    if (content) {
 +
      content.style.margin = 0;
 +
      content.style.borderStyle = 'none';
 +
    }
 +
    var removals = ['column-one', 'siteNotice', 'contentSub', 'siteSub', 'catlinks', 'page-base', 'head-base', 'head', 'panel', 'footer-info-lastmod', 'footer-info-viewcount', 'footer-places', 'footer-icon-poweredby', 'f-poweredbyico', 'lastmod', 'viewcount', 'privacy', 'about', 'disclaimer'];
 +
    for (var i=0; i<removals.length; i++) {
 +
      var g = document.getElementById(removals[i])
 +
      if (g) {
 +
        g.style.display = 'none';
 +
      }
 +
    }
 +
    document.body.style.background='none';
 +
    var divs = document.getElementsByTagName('div');
 +
    for (i=0; i<divs.length; i++) {
 +
      if (divs[i].className.match(/navigation/i)) {
 +
        divs[i].style.display = 'none';
 +
      }
 +
    }
 +
    g = document.getElementById('footer');
 +
    if (g) {
 +
      g.style.marginLeft = 0;
 +
    }
 +
    var foot = document.getElementById('f-list') || document.getElementById('footer-info');
 +
    if (foot) {
 +
      var li = document.createElement('li');
 +
      g = "http://WikiEducator.org/" + wgPageName;
 +
      li.innerHTML = 'Retrieved from <a href="' + g + '">' + g + '</a>';
 +
      li.style.display = 'block';
 +
      foot.insertBefore(li, document.getElementById('copyright') || document.getElementById('footer-info-copyright'));
 +
    }
 +
    var q = {};
 +
    var qs = document.location.search;
 +
    qs = qs.substring(1).toLowerCase().split('&');
 +
    for (i=0; i<qs.length; i++) {
 +
      g = qs[i].split('=');
 +
      if (g[0] == 'links') {
 +
        var largs = g[1].split('|');
 +
        for (var j=0; j<largs.length; j++) {
 +
          q[largs[j]] = true;
 +
        }
 +
        var hilight = q.highlight ? ' background: yellow;' : '';
 +
        var hover = q.hover ? ' background: yellow;' : '';
 +
        if (q.none || q.highlight || q.hover) {
 +
          WEAddStyles('a.link {text-decoration: none; padding: 0px; background: none; ' + hilight + '} a.visited {text-decoration: none;' + hilight +'} a:active {text-decoration: none;' + hilight + '} a:hover {text-decoration: none;' + hilight + hover + '}');
 +
        }
 +
        var as = document.getElementsByTagName('a');
 +
        for (j=0; j<as.length; j++) {
 +
          var href = as[j].getAttribute('href');
 +
          if (href) {
 +
            if (q.none || q.show || q.hover || q.highlight) {
 +
              as[j].setAttribute('onclick', 'return false;');
 +
            }
 +
            if (q.window) {
 +
              as[j].setAttribute('target', '_WE');
 +
            }
 +
          }
 +
        }
 +
      }
 +
    }
 +
    // if postMessage is supported, try to let the parent know our size
 +
    if (parent.postMessage) {
 +
      var ht = content.offsetHeight;
 +
      var ftr = document.getElementById('footer');
 +
      if (ftr) { ht = ht + ftr.offsetHeight; }
 +
      var loc = document.location.href;
 +
      parent.postMessage(ht + '^' + loc, "*");
 +
    }
 +
  }
 +
});
 +
 +
/* add Links button with permanent link, short link, and iframe snippets for each
 +
* 2010-12-21 http://WikiEducator.org/User:JimTittsler
 +
* short links derived from [[commons:Bugzilla:021572]] -- [[commons:meta:user:Platonides]]
 +
*/
 +
function weSelectMe(node) {
 +
  var selection, range, doc, win;
 +
  if ((doc = node.ownerDocument) && (win=doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined') {
 +
    range = doc.createRange();
 +
    range.selectNode(node);
 +
    selection.removeAllRanges();
 +
    selection.addRange(range);
 +
  } else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())) { /* IE */
 +
    range.moveToElementText(node);
 +
    range.select();
 +
  }
 +
}
 +
 +
addOnloadHook( function() {
 +
  var plink = document.getElementById("t-permalink");
 +
  if (wgArticleId && plink) {
 +
    var permanent_link = wgScript + '?oldid=' + wgCurRevisionId;
 +
    var short_url = wgScript + '?curid=' + wgArticleId;
 +
    plink.innerHTML = ['<a href="#" title="Permanent and short URLs, IFrame snippets for this page" onClick="var weLinks=document.getElementById(\'weLinks\'); if (weLinks.style.display == \'none\') { weLinks.style.display = \'block\'; weSelectMe(document.getElementById(\'we_page_iframe\'));} else { weLinks.style.display = \'none\'; } return false;">Links</a>'
 +
      , '<ul id="weLinks" style="display:none;">'
 +
      , '<li><a href="' + permanent_link + '" title="Permanent link to this revision">Permanent Link</a></li>'
 +
      , '<li>IFrame snippet to this revision:<br>'
 +
      , '<span onClick="weSelectMe(this);">&lt;iframe src="' + wgServer + permanent_link + '" width="100%" height="300"&gt;&lt;/iframe&gt;</span></li>'
 +
      , '<li><a href="' + short_url + '" title="Reference page by article id">Short URL</a></li>'
 +
      , '<li>IFrame snippet to this page:<br>'
 +
      , '<span id="we_page_iframe" onClick="weSelectMe(this);">&lt;iframe src="' + wgServer + short_url + '" width="100%" height="300"&gt;&lt;/iframe&gt;</span></li>'
 +
      , '</ul>'
 +
    ].join('');
 +
  }
 +
} );
 +
 +
// BEGIN Dynamic Navigation Bars
 +
// NEEDS Enable multiple onload functions
 +
 +
// set up the words in your language
 +
var NavigationBarHide = 'Collapse';
 +
var NavigationBarShow = 'Expand';
 +
 +
// 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 = 0;
 +
 +
 +
// shows and hides content and picture (if available) of navigation bars
 +
// Parameters:
 +
//    indexNavigationBar: the index of navigation bar to be toggled
 +
function toggleNavigationBar(indexNavigationBar)
 +
{
 +
    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
 +
    var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 +
 +
    if (!NavFrame || !NavToggle) {
 +
        return false;
 +
    }
 +
 +
    // if shown now
 +
    if (NavToggle.firstChild.data == NavigationBarHide) {
 +
        for (
 +
                var NavChild = NavFrame.firstChild;
 +
                NavChild != null;
 +
                NavChild = NavChild.nextSibling
 +
            ) {
 +
            if (NavChild.className == 'NavPic') {
 +
                NavChild.style.display = 'none';
 +
            }
 +
            if (NavChild.className == 'NavContent') {
 +
                NavChild.style.display = 'none';
 +
            }
 +
            if (NavChild.className == 'NavToggle') {
 +
                NavChild.firstChild.data = NavigationBarShow;
 +
            }
 +
        }
 +
 +
    // if hidden now
 +
    } else if (NavToggle.firstChild.data == NavigationBarShow) {
 +
        for (
 +
                var NavChild = NavFrame.firstChild;
 +
                NavChild != null;
 +
                NavChild = NavChild.nextSibling
 +
            ) {
 +
            if (NavChild.className == 'NavPic') {
 +
                NavChild.style.display = 'block';
 +
            }
 +
            if (NavChild.className == 'NavContent') {
 +
                NavChild.style.display = 'block';
 +
            }
 +
            if (NavChild.className == 'NavToggle') {
 +
                NavChild.firstChild.data = NavigationBarHide;
 +
            }
 +
        }
 +
    }
 +
}
 +
 +
// adds show/hide-button to navigation bars
 +
function createNavigationBarToggleButton()
 +
{
 +
    var indexNavigationBar = 0;
 +
    // iterate over all < div >-elements
 +
    for(
 +
            var i=0;
 +
            NavFrame = document.getElementsByTagName("div")[i];
 +
            i++
 +
        ) {
 +
        // if found a navigation bar
 +
        if (NavFrame.className == "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);
 +
 +
            // add NavToggle-Button as first div-element
 +
            // in < div class="NavFrame" >
 +
            NavFrame.insertBefore(
 +
                NavToggle,
 +
                NavFrame.firstChild
 +
            );
 +
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
 +
        }
 +
    }
 +
    // if more Navigation Bars found than Default: hide all
 +
    if (NavigationBarShowDefault < indexNavigationBar) {
 +
        for(
 +
                var i=1;
 +
                i<=indexNavigationBar;
 +
                i++
 +
        ) {
 +
            toggleNavigationBar(i);
 +
        }
 +
    }
 +
 +
}
 +
addOnloadHook(createNavigationBarToggleButton);

Revisión de 13:05 18 ago 2011

/* Cualquier código JavaScript escrito aquí se cargará para todos los usuarios en cada carga de página. */
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* 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);
     };
 })();
 
 
/** Collapsible tables *********************************************************
  *
  *  Description: Allows tables to be collapsed, showing only the header. See
  *               [[Wikipedia:NavFrame]].
  *  Maintainers: [[User:R. Koot]]
  */
 
 var autoCollapse = 2;
 var collapseCaption = "hide";
 var expandCaption = "show";
 
 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;
     var NavigationBoxes = new Object();
     var Tables = document.getElementsByTagName( "table" );
 
     for ( var i = 0; i < Tables.length; i++ ) {
         if ( hasClass( Tables[i], "collapsible" ) ) {
 
             /* only add button and increment count if there is a header row to work with */
             var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
             if (!HeaderRow) continue;
             var Header = HeaderRow.getElementsByTagName( "th" )[0];
             if (!Header) continue;
 
             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.style.color = Header.style.color;
             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( "]" ) );
 
             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 );
 
function WEAddStyles(a) {
  var ss = document.createElement('style');
  ss.type = 'text/css';
  ss.media = 'screen';
  ss.title = 'WE';
  if (ss.stylesheet) ss.stylesheet.cssText = a; //IE
  else ss.appendChild(document.createTextNode(a));
  document.getElementsByTagName('head')[0].appendChild(ss);
}
 
addOnloadHook(function() {
  /* hide some navigation and other unnecessary elements if displayed in an iFrame */
  if (window.self != window.top) {
    var content = document.getElementById('content');
    if (content) {
      content.style.margin = 0;
      content.style.borderStyle = 'none';
    }
    var removals = ['column-one', 'siteNotice', 'contentSub', 'siteSub', 'catlinks', 'page-base', 'head-base', 'head', 'panel', 'footer-info-lastmod', 'footer-info-viewcount', 'footer-places', 'footer-icon-poweredby', 'f-poweredbyico', 'lastmod', 'viewcount', 'privacy', 'about', 'disclaimer'];
    for (var i=0; i<removals.length; i++) {
      var g = document.getElementById(removals[i])
      if (g) {
        g.style.display = 'none';
      }
    }
    document.body.style.background='none';
    var divs = document.getElementsByTagName('div');
    for (i=0; i<divs.length; i++) {
      if (divs[i].className.match(/navigation/i)) {
        divs[i].style.display = 'none';
      }
    }
    g = document.getElementById('footer');
    if (g) {
      g.style.marginLeft = 0;
    }
    var foot = document.getElementById('f-list') || document.getElementById('footer-info');
    if (foot) {
      var li = document.createElement('li');
      g = "http://WikiEducator.org/" + wgPageName;
      li.innerHTML = 'Retrieved from <a href="' + g + '">' + g + '</a>';
      li.style.display = 'block';
      foot.insertBefore(li, document.getElementById('copyright') || document.getElementById('footer-info-copyright'));
    }
    var q = {};
    var qs = document.location.search;
    qs = qs.substring(1).toLowerCase().split('&');
    for (i=0; i<qs.length; i++) {
      g = qs[i].split('=');
      if (g[0] == 'links') {
        var largs = g[1].split('|');
        for (var j=0; j<largs.length; j++) {
          q[largs[j]] = true;
        }
        var hilight = q.highlight ? ' background: yellow;' : '';
        var hover = q.hover ? ' background: yellow;' : '';
        if (q.none || q.highlight || q.hover) {
          WEAddStyles('a.link {text-decoration: none; padding: 0px; background: none; ' + hilight + '} a.visited {text-decoration: none;' + hilight +'} a:active {text-decoration: none;' + hilight + '} a:hover {text-decoration: none;' + hilight + hover + '}');
        }
        var as = document.getElementsByTagName('a');
        for (j=0; j<as.length; j++) {
          var href = as[j].getAttribute('href');
          if (href) {
            if (q.none || q.show || q.hover || q.highlight) {
              as[j].setAttribute('onclick', 'return false;');
            }
            if (q.window) {
              as[j].setAttribute('target', '_WE');
            }
          }
        }
      }
    }
    // if postMessage is supported, try to let the parent know our size
    if (parent.postMessage) {
      var ht = content.offsetHeight;
      var ftr = document.getElementById('footer');
      if (ftr) { ht = ht + ftr.offsetHeight; }
      var loc = document.location.href;
      parent.postMessage(ht + '^' + loc, "*");
    }
  }
});
 
/* add Links button with permanent link, short link, and iframe snippets for each
 * 2010-12-21 http://WikiEducator.org/User:JimTittsler
 * short links derived from [[commons:Bugzilla:021572]] -- [[commons:meta:user:Platonides]]
 */
function weSelectMe(node) {
  var selection, range, doc, win;
  if ((doc = node.ownerDocument) && (win=doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined') {
    range = doc.createRange();
    range.selectNode(node);
    selection.removeAllRanges();
    selection.addRange(range);
  } else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())) { /* IE */
    range.moveToElementText(node);
    range.select();
  }
}
 
addOnloadHook( function() {
  var plink = document.getElementById("t-permalink");
  if (wgArticleId && plink) {
    var permanent_link = wgScript + '?oldid=' + wgCurRevisionId;
    var short_url = wgScript + '?curid=' + wgArticleId;
    plink.innerHTML = ['<a href="#" title="Permanent and short URLs, IFrame snippets for this page" onClick="var weLinks=document.getElementById(\'weLinks\'); if (weLinks.style.display == \'none\') { weLinks.style.display = \'block\'; weSelectMe(document.getElementById(\'we_page_iframe\'));} else { weLinks.style.display = \'none\'; } return false;">Links</a>'
      , '<ul id="weLinks" style="display:none;">'
      , '<li><a href="' + permanent_link + '" title="Permanent link to this revision">Permanent Link</a></li>'
      , '<li>IFrame snippet to this revision:<br>'
      , '<span onClick="weSelectMe(this);">&lt;iframe src="' + wgServer + permanent_link + '" width="100%" height="300"&gt;&lt;/iframe&gt;</span></li>'
      , '<li><a href="' + short_url + '" title="Reference page by article id">Short URL</a></li>'
      , '<li>IFrame snippet to this page:<br>'
      , '<span id="we_page_iframe" onClick="weSelectMe(this);">&lt;iframe src="' + wgServer + short_url + '" width="100%" height="300"&gt;&lt;/iframe&gt;</span></li>'
      , '</ul>'
    ].join('');
   }
} );
 
 // BEGIN Dynamic Navigation Bars
 // NEEDS Enable multiple onload functions 
 
 // set up the words in your language
 var NavigationBarHide = 'Collapse';
 var NavigationBarShow = 'Expand';
 
 // 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 = 0;
 
 
 // shows and hides content and picture (if available) of navigation bars
 // Parameters:
 //     indexNavigationBar: the index of navigation bar to be toggled
 function toggleNavigationBar(indexNavigationBar)
 {
    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
    var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 
    if (!NavFrame || !NavToggle) {
        return false;
    }
 
    // if shown now
    if (NavToggle.firstChild.data == NavigationBarHide) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if (NavChild.className == 'NavPic') {
                NavChild.style.display = 'none';
            }
            if (NavChild.className == 'NavContent') {
                NavChild.style.display = 'none';
            }
            if (NavChild.className == 'NavToggle') {
                NavChild.firstChild.data = NavigationBarShow;
            }
        }
 
    // if hidden now
    } else if (NavToggle.firstChild.data == NavigationBarShow) {
        for (
                var NavChild = NavFrame.firstChild;
                NavChild != null;
                NavChild = NavChild.nextSibling
            ) {
            if (NavChild.className == 'NavPic') {
                NavChild.style.display = 'block';
            }
            if (NavChild.className == 'NavContent') {
                NavChild.style.display = 'block';
            }
            if (NavChild.className == 'NavToggle') {
                NavChild.firstChild.data = NavigationBarHide;
            }
        }
    }
 }
 
 // adds show/hide-button to navigation bars
 function createNavigationBarToggleButton()
 {
    var indexNavigationBar = 0;
    // iterate over all < div >-elements
    for(
            var i=0; 
            NavFrame = document.getElementsByTagName("div")[i]; 
            i++
        ) {
        // if found a navigation bar
        if (NavFrame.className == "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);
 
            // add NavToggle-Button as first div-element 
            // in < div class="NavFrame" >
            NavFrame.insertBefore(
                NavToggle,
                NavFrame.firstChild
            );
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
        }
    }
    // if more Navigation Bars found than Default: hide all
    if (NavigationBarShowDefault < indexNavigationBar) {
        for(
                var i=1; 
                i<=indexNavigationBar; 
                i++
        ) {
            toggleNavigationBar(i);
        }
    }
 
 }
addOnloadHook(createNavigationBarToggleButton);