MediaWiki:Common.js: Difference between revisions

From FamilySearch Wiki
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


/** Test if an element has a certain class
(function() {
* Description: Uses regular expressions and caching for better performance.
    var css = document.createElement('link');
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
    css.rel = 'stylesheet';
*/
    css.href = '//familysearch.org/hf/hf.css';
var hasClass = (function() {
    var head = document.querySelector('head');
     var reCache = {};
    head.insertBefore(css, head.childNodes[0]);
     return function(element, className) {
     var body = document.querySelector('body');
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
     body.insertBefore(document.createElement('header'), body.childNodes[0]);
     };
    body.appendChild(document.createElement('footer'));
})();
 
(function() {
    var meta = document.createElement('meta');
    meta.name = 'hf_base_url';
    meta.content = 'https://familysearch.org';
    var head = document.querySelector('head');
     head.insertBefore(meta, head.childNodes[0]);
})();
})();


(function() {
    var fs = document.createElement('script');
    fs.type = 'text/javascript';
    fs.async = true;
    var base_url = "https://familysearch.org";
    fs.src = 'https://familysearch.org/hf/hf.js' + (base_url === "" ? "" : "?base_url=" + base_url);
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(fs, s);
})();
function getCookie(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value !== null) ? unescape(value[1]) : null;
}
setTimeout(function() {
    var templeCookie = getCookie('fs-highconf') || getCookie('fs-templeinfo');
    if (templeCookie && templeCookie.indexOf('true') > -1) {
        var body = document.querySelector('body');
        body.classList.add('templeMember');
    }
}, 2000);
/* Fix for double title */
(function(){
    var mainTitle = document.querySelector('#firstHeading');
    var secondTitle = document.querySelector('#mw-content-text > h1');
    if(mainTitle && secondTitle){
        mainTitle.hidden = true;
    }
})();
/**
* Test if an element has a certain class
* @deprecated:  Use $(element).hasClass() instead.
*/
mw.log.deprecate( window, 'hasClass', function ( element, className ) {
    return $( element ).hasClass( className );
}, 'Use jQuery.hasClass() instead' );


/**
/**
  * Collapsible tables
  * Collapsible tables
  *
  *
  * @version 2.0.2 (2014-03-14)
* 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
  * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
  * @author [[User:R. Koot]]
  * @author [[User:R. Koot]]
Line 23: Line 69:
  * is supported in MediaWiki core.
  * is supported in MediaWiki core.
  */
  */
/*global $, mw */
 
var autoCollapse = 2;
var autoCollapse = 2;
var collapseCaption = 'hide';
var collapseCaption = 'hide';
var expandCaption = 'show';
var expandCaption = 'show';
var tableIndex = 0;


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


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


        var Rows = Table.rows;
    var Rows = Table.rows;
        var i;
    var i;


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


    function createClickHandler(tableIndex) {
function createClickHandler( tableIndex ) {
        return function(e) {
    return function ( e ) {
            e.preventDefault();
        e.preventDefault();
            collapseTable(tableIndex);
        collapseTable( tableIndex );
        };
    };
     }
}
 
function createCollapseButtons( $content ) {
     var NavigationBoxes = {};
    var $Tables = $content.find( 'table' );
    var i;


     function createCollapseButtons() {
     $Tables.each( function( i, table ) {
         var tableIndex = 0;
         if ( $(table).hasClass( 'collapsible' ) ) {
        var NavigationBoxes = {};
        var Tables = document.getElementsByTagName('table');
        var i;


        for (i = 0; i < Tables.length; i++) {
             /* only add button and increment count if there is a header row to work with */
             if ($(Tables[i]).hasClass('collapsible')) {
            var HeaderRow = table.getElementsByTagName( 'tr' )[0];
                /* only add button and increment count if there is a header row to work with */
            if ( !HeaderRow ) {
                var HeaderRow = Tables[i].getElementsByTagName('tr')[0];
                return;
                if (!HeaderRow) {
            }
                    continue;
            var Header = table.getElementsByTagName( 'th' )[0];
                }
            if ( !Header ) {
                var Header = HeaderRow.getElementsByTagName('th')[0];
                return;
                if (!Header) {
            }
                    continue;
                }


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


                var Button = document.createElement('span');
            var Button     = document.createElement( 'span' );
                var ButtonLink = document.createElement('a');
            var ButtonLink = document.createElement( 'a' );
                var ButtonText = document.createTextNode(collapseCaption);
            var ButtonText = document.createTextNode( collapseCaption );
                // TODO: Declare styles in [[MediaWiki:Gadget-collapsibleTables.css]]
            // Styles are declared in [[MediaWiki:Common.css]]
                // Button.className = 'collapseButton';
            Button.className = 'collapseButton';
                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.style.color = Header.style.color;
                ButtonLink.setAttribute('id', 'collapseButton' + tableIndex);
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
                ButtonLink.setAttribute('href', '#');
            ButtonLink.setAttribute( 'href', '#' );
                $(ButtonLink).on('click', createClickHandler(tableIndex));
            $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
                ButtonLink.appendChild(ButtonText);
            ButtonLink.appendChild( ButtonText );


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


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


        for (i = 0; i < tableIndex; i++) {
    for ( i = 0; i < tableIndex; i++ ) {
            if ($(NavigationBoxes[i]).hasClass('collapsed') ||
        if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
                (tableIndex >= autoCollapse && $(NavigationBoxes[i]).hasClass('autocollapse'))
            ( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )
             ) {
        ) {
                 collapseTable(i);
            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);
mw.hook( 'wikipage.content' ).add( createCollapseButtons );
}());


/**
/**
  * Dynamic Navigation Bars. See [[Wikipedia:NavFrame]]
  * Dynamic Navigation Bars (experimental)
* Based on script from en.wikipedia.org, 2008-09-15.
*
  * @source www.mediawiki.org/wiki/MediaWiki:Gadget-NavFrame.js
* Description: See [[Wikipedia:NavFrame]].
* @maintainer Helder.wiki, 2012–2013
  * Maintainers: UNMAINTAINED
* @maintainer Krinkle, 2013
  */
  */
(function() {
    // set up the words in your language
    var navigationBarHide = '[' + collapseCaption + ']';
    var navigationBarShow = '[' + expandCaption + ']';


    /**
/* set up the words in your language */
    * Shows and hides content and picture (if available) of navigation bars.
var NavigationBarHide = '[' + collapseCaption + ']';
    * @param {number} indexNavigationBar The index of navigation bar to be toggled
var NavigationBarShow = '[' + expandCaption + ']';
    * @param {jQuery.Event} e Event object
var indexNavigationBar = 0;
    */
    function toggleNavigationBar(indexNavigationBar, e) {
        var navChild,
            navToggle = document.getElementById('NavToggle' + indexNavigationBar),
            navFrame = document.getElementById('NavFrame' + indexNavigationBar);


        // Prevent browser from jumping to href "#"
/**
        e.preventDefault();
* Shows and hides content and picture (if available) of navigation bars
* Parameters:
*    indexNavigationBar: the index of navigation bar to be toggled
**/
window.toggleNavigationBar = function ( indexNavigationBar, event ) {
    var NavToggle = document.getElementById( 'NavToggle' + indexNavigationBar );
    var NavFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
    var NavChild;


        if (!navFrame || !navToggle) {
    if ( !NavFrame || !NavToggle ) {
            return false;
        return false;
        }
    }


        // If shown now
    /* if shown now */
        if (navToggle.firstChild.data == navigationBarHide) {
    if ( NavToggle.firstChild.data === NavigationBarHide ) {
            for (navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling) {
        for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
                if (hasClass(navChild, 'NavPic')) {
            if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
                    navChild.style.display = 'none';
                NavChild.style.display = 'none';
                }
                if (hasClass(navChild, 'NavContent')) {
                    navChild.style.display = 'none';
                }
             }
             }
            navToggle.firstChild.data = navigationBarShow;
        }
    NavToggle.firstChild.data = NavigationBarShow;


            // If hidden now
    /* if hidden now */
        } else if (navToggle.firstChild.data == navigationBarShow) {
    } else if ( NavToggle.firstChild.data === NavigationBarShow ) {
            for (navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling) {
        for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
                if ($(navChild).hasClass('NavPic') || $(navChild).hasClass('NavContent')) {
            if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
                    navChild.style.display = 'block';
                NavChild.style.display = 'block';
                }
             }
             }
            navToggle.firstChild.data = navigationBarHide;
         }
         }
        NavToggle.firstChild.data = NavigationBarHide;
     }
     }


     /**
     event.preventDefault();
    * Adds show/hide-button to navigation bars.
};
    * @param {jQuery} $content
 
    */
/* adds show/hide-button to navigation bars */
    function createNavigationBarToggleButton($content) {
function createNavigationBarToggleButton( $content ) {
        var i, j, navFrame, navToggle, navToggleText, navChild,
    var NavChild;
            indexNavigationBar = 0,
    /* iterate over all < div >-elements */
            navFrames = $content.find('div.NavFrame').toArray();
    var $divs = $content.find( 'div' );
    $divs.each( function ( i, NavFrame ) {
        /* if found a navigation bar */
        if ( $( NavFrame ).hasClass( 'NavFrame' ) ) {


        // Iterate over all (new) nav frames
        for (i = 0; i < navFrames.length; i++) {
            navFrame = navFrames[i];
            // If found a navigation bar
             indexNavigationBar++;
             indexNavigationBar++;
             navToggle = document.createElement('a');
             var NavToggle = document.createElement( 'a' );
             navToggle.className = 'NavToggle';
             NavToggle.className = 'NavToggle';
             navToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
             NavToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
             navToggle.setAttribute('href', '#');
             NavToggle.setAttribute( 'href', '#' );
             $(navToggle).on('click', $.proxy(toggleNavigationBar, null, indexNavigationBar));
             $( NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, window, indexNavigationBar ) );


             navToggleText = document.createTextNode(navigationBarHide);
             var isCollapsed = $( NavFrame ).hasClass( 'collapsed' );
             for (navChild = navFrame.firstChild; navChild !== null; navChild = navChild.nextSibling) {
             /**
                 if ($(navChild).hasClass('NavPic') || $(navChild).hasClass('NavContent')) {
            * Check if any children are already hidden.  This loop is here for backwards compatibility:
                     if (navChild.style.display == 'none') {
            * the old way of making NavFrames start out collapsed was to manually add style="display:none"
                         navToggleText = document.createTextNode(navigationBarShow);
            * to all the NavPic/NavContent elements.  Since this was bad for accessibility (no way to make
                        break;
            * the content visible without JavaScript support), the new recommended way is to add the class
            * "collapsed" to the NavFrame itself, just like with collapsible tables.
            */
            for ( NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling ) {
                 if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
                     if ( NavChild.style.display === 'none' ) {
                         isCollapsed = true;
                     }
                     }
                 }
                 }
             }
             }
            if ( isCollapsed ) {
                for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
                    if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
                        NavChild.style.display = 'none';
                    }
                }
            }
            var NavToggleText = document.createTextNode( isCollapsed ? NavigationBarShow : NavigationBarHide );
            NavToggle.appendChild( NavToggleText );


            navToggle.appendChild(navToggleText);
             /* Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked) */
             // 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++ ) {
             for (j = 0; j < navFrame.childNodes.length; j++) {
                 if ( $( NavFrame.childNodes[j] ).hasClass( 'NavHead' ) ) {
                 if ($(navFrame.childNodes[j]).hasClass('NavHead')) {
                     NavToggle.style.color = NavFrame.childNodes[j].style.color;
                     navFrame.childNodes[j].appendChild(navToggle);
                    NavFrame.childNodes[j].appendChild( NavToggle );
                 }
                 }
             }
             }
             navFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
             NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
         }
         }
     }
     } );
 
    mw.hook('wikipage.content').add(createNavigationBarToggleButton);
}());
 
//Adding Header/Footer to these pages
(function() {
    var css = document.createElement('link');
    css.rel = 'stylesheet';
    css.href = '//familysearch.org/hf/hf.css';
    var head = document.querySelector('head');
    head.insertBefore(css, head.childNodes[0]);
})();
 
(function() {
    var body = document.querySelector('body');
    body.innerHTML = '<div class="wikiContainer">' + body.innerHTML + '</div>';
    body.insertBefore(document.createElement('header'), body.childNodes[0]);
    body.appendChild(document.createElement('footer'));
})();
 
(function() {
    var meta = document.createElement('meta');
    meta.name = 'hf_base_url';
    meta.content = 'https://familysearch.org';
    var head = document.querySelector('head');
    head.insertBefore(meta, head.childNodes[0]);
})();
 
(function() {
    var fs = document.createElement('script');
    fs.type = 'text/javascript';
    fs.async = true;
    var base_url = "https://familysearch.org";
    fs.src = 'https://familysearch.org/hf/hf.js' + (base_url === "" ? "" : "?base_url=" + base_url);
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(fs, s);
})();
 
function getCookie(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value !== null) ? unescape(value[1]) : null;
}
}
setTimeout(function() {
    var templeCookie = getCookie('fs-highconf') || getCookie('fs-templeinfo');
    if (templeCookie && templeCookie.indexOf('true') > -1) {
        var body = document.querySelector('body');
        body.classList.add('templeMember');
    }
}, 2000);


/* Fix for double title */
mw.hook( 'wikipage.content' ).add( createNavigationBarToggleButton );
(function(){
    var mainTitle = document.querySelector('#firstHeading');
    var secondTitle = document.querySelector('#mw-content-text > h1');
    if(mainTitle && secondTitle){
        mainTitle.hidden = true;
    }
})();

Revision as of 13:51, 31 March 2016

/* Any JavaScript here will be loaded for all users on every page load. */

(function() {
    var css = document.createElement('link');
    css.rel = 'stylesheet';
    css.href = '//familysearch.org/hf/hf.css';
    var head = document.querySelector('head');
    head.insertBefore(css, head.childNodes[0]);
    var body = document.querySelector('body');
    body.insertBefore(document.createElement('header'), body.childNodes[0]);
    body.appendChild(document.createElement('footer'));
})();

(function() {
    var meta = document.createElement('meta');
    meta.name = 'hf_base_url';
    meta.content = 'https://familysearch.org';
    var head = document.querySelector('head');
    head.insertBefore(meta, head.childNodes[0]);
})();

(function() {
    var fs = document.createElement('script');
    fs.type = 'text/javascript';
    fs.async = true;
    var base_url = "https://familysearch.org";
    fs.src = 'https://familysearch.org/hf/hf.js' + (base_url === "" ? "" : "?base_url=" + base_url);
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(fs, s);
})();
function getCookie(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value !== null) ? unescape(value[1]) : null;
}
setTimeout(function() {
    var templeCookie = getCookie('fs-highconf') || getCookie('fs-templeinfo');
    if (templeCookie && templeCookie.indexOf('true') > -1) {
        var body = document.querySelector('body');
        body.classList.add('templeMember');
    }
}, 2000);
/* Fix for double title */
(function(){
    var mainTitle = document.querySelector('#firstHeading');
    var secondTitle = document.querySelector('#mw-content-text > h1');
    if(mainTitle && secondTitle){
        mainTitle.hidden = true;
    }
})();
/**
 * Test if an element has a certain class
 * @deprecated:  Use $(element).hasClass() instead.
 */
mw.log.deprecate( window, 'hasClass', function ( element, className ) {
    return $( element ).hasClass( className );
}, 'Use jQuery.hasClass() instead' );

/**
 * 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 = 'hide';
var expandCaption = 'show';
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 );

/**
 * Dynamic Navigation Bars (experimental)
 *
 * Description: See [[Wikipedia:NavFrame]].
 * Maintainers: UNMAINTAINED
 */

/* set up the words in your language */
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
var indexNavigationBar = 0;

/**
 * Shows and hides content and picture (if available) of navigation bars
 * Parameters:
 *     indexNavigationBar: the index of navigation bar to be toggled
 **/
window.toggleNavigationBar = function ( indexNavigationBar, event ) {
    var NavToggle = document.getElementById( 'NavToggle' + indexNavigationBar );
    var NavFrame = document.getElementById( 'NavFrame' + indexNavigationBar );
    var NavChild;

    if ( !NavFrame || !NavToggle ) {
        return false;
    }

    /* if shown now */
    if ( NavToggle.firstChild.data === NavigationBarHide ) {
        for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
            if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
                NavChild.style.display = 'none';
            }
        }
    NavToggle.firstChild.data = NavigationBarShow;

    /* if hidden now */
    } else if ( NavToggle.firstChild.data === NavigationBarShow ) {
        for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
            if ( $( NavChild ).hasClass( 'NavContent' ) || $( NavChild ).hasClass( 'NavPic' ) ) {
                NavChild.style.display = 'block';
            }
        }
        NavToggle.firstChild.data = NavigationBarHide;
    }

    event.preventDefault();
};

/* adds show/hide-button to navigation bars */
function createNavigationBarToggleButton( $content ) {
    var NavChild;
    /* iterate over all < div >-elements */
    var $divs = $content.find( 'div' );
    $divs.each( function ( i, NavFrame ) {
        /* if found a navigation bar */
        if ( $( NavFrame ).hasClass( 'NavFrame' ) ) {

            indexNavigationBar++;
            var NavToggle = document.createElement( 'a' );
            NavToggle.className = 'NavToggle';
            NavToggle.setAttribute( 'id', 'NavToggle' + indexNavigationBar );
            NavToggle.setAttribute( 'href', '#' );
            $( NavToggle ).on( 'click', $.proxy( window.toggleNavigationBar, window, indexNavigationBar ) );

            var isCollapsed = $( NavFrame ).hasClass( 'collapsed' );
            /**
             * Check if any children are already hidden.  This loop is here for backwards compatibility:
             * the old way of making NavFrames start out collapsed was to manually add style="display:none"
             * to all the NavPic/NavContent elements.  Since this was bad for accessibility (no way to make
             * the content visible without JavaScript support), the new recommended way is to add the class
             * "collapsed" to the NavFrame itself, just like with collapsible tables.
             */
            for ( NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling ) {
                if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
                    if ( NavChild.style.display === 'none' ) {
                        isCollapsed = true;
                    }
                }
            }
            if ( isCollapsed ) {
                for ( NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling ) {
                    if ( $( NavChild ).hasClass( 'NavPic' ) || $( NavChild ).hasClass( 'NavContent' ) ) {
                        NavChild.style.display = 'none';
                    }
                }
            }
            var NavToggleText = document.createTextNode( isCollapsed ? NavigationBarShow : 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 ( $( NavFrame.childNodes[j] ).hasClass( 'NavHead' ) ) {
                    NavToggle.style.color = NavFrame.childNodes[j].style.color;
                    NavFrame.childNodes[j].appendChild( NavToggle );
                }
            }
            NavFrame.setAttribute( 'id', 'NavFrame' + indexNavigationBar );
        }
    } );
}

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