跳转到内容

MediaWiki:Common.js

计算器百科,非营利的计算器专业知识百科。
春上冰月留言 | 贡献2016年5月22日 (日) 13:44的版本 (debug)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
/* 这里的任何JavaScript将为所有用户在每次页面载入时加载。 */


mw.loader.using(['mediawiki.util', 'ext.gadget.site-lib'], function () {
  (function ($, mw) {

/**
 * 简繁处理,判断用户设置的语言并返回相应的简体/繁体
 * console 异常显示有脚本需要此函数
 * @param String $simplifiedChinese 简体中文
 * @param String $traditionalChinese 繁体中文
 * @return 根据用户的语言设置返回相应的文字,zh 为简体中文
 * @auther Haruue Icymoon
 */
/*
function wgULS($simplifiedChinese, $traditionalChinese) {
    var language = mw.config.get("wgUserLanguage");
    if (language == "zh" || language == "zh-hans" || language == "zh-cn" || language == "zh-sg") {
    	return $simplifiedChinese;
    } else {
    	return $traditionalChinese;
    }
}
*/

/**
 * Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
 * 
 * Based on script from en.wikipedia.org, 2008-09-15.
 *
 * @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
 * @maintainer Helder.wiki, 2012–2013
 * @maintainer Krinkle, 2013
 * @maintainer Fantasticfears, 2013-2014
 */
( function () {

var collapseCaption = wgULS('隐藏', '隱藏');
var expandCaption = wgULS('显示', '顯示');

var navigationBarHide = collapseCaption + '▲';
var navigationBarShow = expandCaption + '▼';
 
/**
 * Shows and hides content and picture (if available) of navigation bars.
 *
 * @param {number} indexNavigationBar The index of navigation bar to be toggled
 * @param {jQuery.Event} e Event object
 */
function toggleNavigationBar( indexNavigationBar, e ) {
        var toggle = $( '#NavToggle' + indexNavigationBar ),
                frame = $( '#NavFrame' + indexNavigationBar ),
                isFrameCollapsed;

        if ( !frame || !toggle ) {
                return false;
        }

	isFrameCollapsed = frame.hasClass( 'collapsed' );
        if ( isFrameCollapsed ) {
                frame.find( '> .NavPic, > .NavContent, > .toogleShow' ).each( function() {
                        $( this ).css( 'display', 'block' );
                });
                frame.find( '> .toggleHide' ).each( function() {
                        $( this ).css( 'display', 'none' );
                });
                toggle.text( navigationBarHide );
                frame.removeClass( 'collapsed' );
        } else {
                frame.find( '> .NavPic, > .NavContent, > .toogleShow' ).each( function() {
                        $( this ).css( 'display', 'none' );
                });
                frame.find( '> .toggleHide' ).each( function() {
                        $( this ).css( 'display', 'block' );
                });
                toggle.text( navigationBarShow );
                frame.addClass( 'collapsed' );
        }
}

/**
 * Adds show/hide-button to navigation bars.
 *
 * @param {jQuery} $content
 */
function createNavigationBarToggleButton( $content ) {
        // Iterate over all (new) nav frames
        $content.find( 'div.NavFrame' ).each( function( indexNavigationBar ) {
                var frame = $( this ).attr( 'id', 'NavFrame' + indexNavigationBar );
                // If found a navigation bar
                var navToggle = $( '<span class="NavToggle" id="NavToggle' + indexNavigationBar + '"></span>' );
                frame.find( '> .NavHead' ).each( function() {
                        $( this ).on( 'click', $.proxy( toggleNavigationBar, null, indexNavigationBar ) );
                	return false;
		});
                if ( frame.hasClass( 'collapsed' ) ) {
                        frame.find( '> .NavPic, > .NavContent, > .toggleHide' ).each( function() {
                                $( this ).css( 'display', 'none' );
                        });
                } else {
                        frame.find( '> .toggleShow' ).each( function() {
                                $( this ).css( 'display', 'none' );
                        });
                }

                var showNavigationBarHide = true;
                frame.find( '> .NavPic, > .NavContent' ).each( function() {
                        if ( $( this ).css( 'display' ) === 'none' ) {
                                showNavigationBarHide = false;
                                return false;
                        }
                });

                navToggle.text( showNavigationBarHide? navigationBarHide: navigationBarShow );

                frame.find( '> .NavHead' ).each( function() {
                        $( this ).append( navToggle );
                        return false;
                });
        });
}

        mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );

}());

/**
 * Collapsible tables
 *
 * Allows tables to be collapsed, showing only the header. See [[Wikipedia:NavFrame]].
 *
 * @version 2.0.3 (2014-03-14)
 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
 * @author [[User:R. Koot]]
 * @author [[User:Krinkle]]
 * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
 * is supported in MediaWiki core.
 */

var autoCollapse = 2;
var collapseCaption = wgULS('隐藏', '隱藏');
var expandCaption = wgULS('显示', '顯示');
var tableIndex = 0;

function collapseTable( tableIndex ) {
    var Button = document.getElementById( 'collapseButton' + tableIndex );
    var Table = document.getElementById( 'collapsibleTable' + tableIndex );

    if ( !Table || !Button ) {
        return false;
    }

    var Rows = Table.rows;
    var i;

    if ( Button.firstChild.data === collapseCaption ) {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = 'none';
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = Rows[0].style.display;
        }
        Button.firstChild.data = collapseCaption;
    }
}

function createClickHandler( tableIndex ) {
    return function ( e ) {
        e.preventDefault();
        collapseTable( tableIndex );
    };
}

function createCollapseButtons( $content ) {
    var NavigationBoxes = {};
    var $Tables = $content.find( 'table' );
    var i;

    $Tables.each( function( i, table ) {
        if ( $(table).hasClass( 'collapsible' ) ) {

            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = table.getElementsByTagName( 'tr' )[0];
            if ( !HeaderRow ) {
                return;
            }
            var Header = table.getElementsByTagName( 'th' )[0];
            if ( !Header ) {
                return;
            }

            NavigationBoxes[ tableIndex ] = table;
            table.setAttribute( 'id', 'collapsibleTable' + tableIndex );

            var Button     = document.createElement( 'span' );
            var ButtonLink = document.createElement( 'a' );
            var ButtonText = document.createTextNode( collapseCaption );
            // Styles are declared in [[MediaWiki:Common.css]]
            Button.className = 'collapseButton';

            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
            ButtonLink.setAttribute( 'href', '#' );
            $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
            ButtonLink.appendChild( ButtonText );

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

            Header.insertBefore( Button, Header.firstChild );
            tableIndex++;
        }
    } );

    for ( i = 0;  i < tableIndex; i++ ) {
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
            ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )
        ) {
            collapseTable( i );
        }
        else if ( $( NavigationBoxes[i] ).hasClass ( 'innercollapse' ) ) {
            var element = NavigationBoxes[i];
            while ((element = element.parentNode)) {
                if ( $( element ).hasClass( 'outercollapse' ) ) {
                    collapseTable ( i );
                    break;
                }
            }
        }
    }
}

mw.hook( 'wikipage.content' ).add( createCollapseButtons );

})});