MediaWiki:Common.js

Frå Setesdalswiki
Hopp til navigering Hopp til søk

Merk: Etter lagring vil det kanskje vera naudsynt at nettlesaren slettar mellomlageret sitt for at endringane skal tre i kraft.

  • Firefox og Safari: Haldt nede Shift medan du klikkar på Oppdater, eller trykk anten Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
  • Google Chrome: Trykk Ctrl-Shift-R (⌘-Shift-R på Mac)
  • Internet Explorer og Edge: Haldt nede Ctrl medan du klikkar Oppdater, eller trykk Ctrl-F5
  • Opera: Tøm mellomlageret i Verktøy → Innstillingar
/* Javascript i denne fila vil gjelde for alle drakter. */

/** Dynamic Navigation Bars (experimental) *************************************
 *
 *  Description: See [[Wikipedia:NavFrame]].
 *  Maintainers: UNMAINTAINED
 */
 
// set up the words in your language
var collapseCaption, expandCaption;
var NavigationBarHide = '[' + (collapseCaption || 'skjul') + ']';
var NavigationBarShow = '[' + (expandCaption || 'vis') + ']';
 
// 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).hasClass('NavPic' ) ) {
                NavChild.style.display = 'none';
            }
            if ( $(NavChild).hasClass('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 ($(NavChild).hasClass('NavPic')) {
                NavChild.style.display = 'block';
            }
            if ($(NavChild).hasClass('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 ($(NavFrame).hasClass("NavFrame")) {
 
            indexNavigationBar++;
            var NavToggle = document.createElement("a");
            NavToggle.className = 'NavToggle';
            NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
            NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
 
            var isCollapsed = hasClass( NavFrame, "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 (var NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling) {
                if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
                    if ( NavChild.style.display == 'none' ) {
                        isCollapsed = true;
                    }
                }
            }
            if (isCollapsed) {
                for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
                    if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, '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 (hasClass(NavFrame.childNodes[j], "NavHead")) {
                    NavFrame.childNodes[j].appendChild(NavToggle);
                }
            }
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
        }
    }
}
 
$( createNavigationBarToggleButton );

/** Translate #REDIRECT
 *
 *  Description: A workaround for bug 24114, "Redirect toolbar button doesn't insert localized version of #REDIRECT"
 *  Original: http://meta.wikimedia.org/wiki/2010_Wikimedia_design_and_feature_change/Feedback/Archive_1#REDIRECT_button
 *  Maintainers: [[:no:User:Sidhekin]]
 *  Change:
 *   must bind the function earlier to get the wanted effect
 *   added try-catch but not sure if its necessary, the structures will always be defined
 *   jeblad, 2011-05-12
 */
(function(mw, $) {
    if ( window.wgWikiEditorEnabledModules && wgWikiEditorEnabledModules.toolbar ) {
        try {
            $( '#wpTextbox1' ).bind( 'wikiEditor-toolbar-buildSection-advanced', function( event, section ) {
                 section.groups.insert.tools.redirect.action.options.pre = '#OMDIRIGERING [[';
            } );
         }
         catch (e) {
            // Woopsie, die without a notice
         }
    }
})(mediaWiki, jQuery);


/**
 * Description: Modify parts of the toolbar to better suit this projects needs
 * Maintainer:
 */
(function(mw, $, undefined) {
    //if ( $.fn.wikiEditor !== undefined ) {

    // customization for the beta toolbar
    var customizeBetaToolbar = function () {

        // Execute on load
        $( function() {
    
            // just to get rid of some errors
            function label(msg, txt, before, after) {
                var str = '';
                if (before === undefined ? true : before) str += "{" + "{";
                str += msg;
                if (after === undefined ? true : after) str += "}" + "}";
                if (txt) str += " – " + txt;
                return str;
            }
            // just to get rid of some errors
            function pre(msg, txt, before) {
                var str = '';
                if (before === undefined ? true : before) str += "{" + "{";
                str += msg;
                if (txt) str += txt;
                return str;
            }
            // just to get rid of some errors
            function peri(msg, txt, before, after) {
                var str = '';
                if (before === undefined ? true : before) str += "{" + "{";
                str += msg;
                if (txt) str += txt;
                if (after === undefined ? true : after) str += "}" + "}";
                return str;
            }
    
            // get timestamp for templates
            var now = new Date();
            var timestamp = now.getUTCFullYear() + '-' + (now.getUTCMonth()<9 ? '0' : '') + (now.getUTCMonth()+1) + '-' + (now.getUTCDate()<9 ? '0' : '') + now.getUTCDate();

            // add quote signs to the format group in the main section
            $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
                'section': 'main',
                'group': 'format',
                'tools': {
                    'quote': {
                        label: 'Anførselstegn',
                        type: 'button',
                        icon: '//upload.wikimedia.org/wikipedia/commons/a/ac/Norwegian_quote_sign.png',
                        action: {
                            type: 'encapsulate',
                            options: {
                                pre: "«",
                                //peri: "«»",
                                post: "»"
                            }
                        }
                    }
                }
            });
    
            // add quote signs to the format group in the main section
            $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
                'section': 'main',
                'group': 'format',
                'tools': {
                    'link': {
                        label: 'Lenkeparenteser',
                        type: 'button',
                        icon: '//upload.wikimedia.org/wikipedia/commons/b/ba/Norwegian_link_sign.png',
                        action: {
                            type: 'encapsulate',
                            options: {
                                pre: "[[",
                                //peri: "[[]]",
                                post: "]]"
                            }
                        }
                    }
                }
            });
    
            // add quote signs to the format group in the main section
            $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
                'section': 'main',
                'group': 'format',
                'tools': {
                    'link': {
                        label: 'Malparenteser',
                        type: 'button',
                        icon: '//upload.wikimedia.org/wikipedia/commons/4/4a/Norwegian_template_sign.png',
                        action: {
                            type: 'encapsulate',
                            options: {
                                pre: "{{",
                                //peri: "{{}}",
                                post: "}}"
                            }
                        }
                    }
                }
            });
    
            // add an article mark menu in the advanced section
            $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
                'section': 'advanced',
                groups: {
                    'heading': {
                        tools: {
                            'heading': {
                                label: 'Vedlikehold',
                                type: 'select',
                                list: {
                                    'template-neutral' : {
                                        label: label('Nøytralitet', 'artikkelen gir ikke en nøytral fremstilling'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('Nøytralitet', '|ts=' + timestamp + '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-verifiable' : {
                                        label: label('Verifiserbar', 'artikkelen er ikke verifiserbar'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('Verifiserbar', '|ts=' + timestamp + '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-research' : {
                                        label: label('Forskning', 'artikkelen inneholder originalforskning'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('Forskning', '|ts=' + timestamp + '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-accuracy' : {
                                        label: label('Nøyaktighet', 'artikkelen har ikke en nøyaktig fremstilling'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('Nøyaktighet', '|ts=' + timestamp + '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-spelling' : {
                                        label: label('Språkvask', 'artikkelens språk trenger en gjennomgang'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('Språkvask', '|ts=' + timestamp + '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-format' : {
                                        label: label('Formatering', 'artikkelen trenger wikifisering'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('Formatering', '|ts=' + timestamp + '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-workinprogress' : {
                                        label: label('Arbeid pågår', 'artikkelen er under arbeid'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('Arbeid pågår', '|ts=' + timestamp + '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });

            // add a template menu in the advanced sectionined' ) {
            $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
                'section': 'advanced',
                groups: {
                    'heading': {
                        tools: {
                            'heading': {
                                label: 'Maler',
                                type: 'select',
                                list: {
                                    'template-columns' : {
                                        label: label('kolonner', 'etterfølgende liste fordeles over flere kolonner'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('kolonner', '|'),
                                                //peri: peri('columns'),
                                                post: '}}',
                                                ownline: true
                                            }
                                        }
                                    },
                                    'template-sorted-columns' : {
                                        label: label('sorterte kolonner', 'etterfølgende liste fordeles over flere sorterbare kolonner'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('sorterte kolonner', '|tittel='),
                                                //peri: peri('columns'),
                                                post: '}}',
                                                ownline: true
                                            }
                                        }
                                    },
                                    'template-nowrap' : {
                                        label: label('nowrap', 'hindre linjeskift i tekst og tall'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('nowrap', '|'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-formatnum' : {
                                        label: label('formatnum', 'tall med mellomrom og desimalkomma'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('formatnum', ':'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    },
                                    'template-defaultsort' : {
                                        label: label('DEFAULTSORT', 'sortering på etternavn og lignende'),
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: pre('DEFAULTSORT', ':'),
                                                post: '}}',
                                                ownline: false
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
            
            // add a titles menu in the advanced section
            $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
                'section': 'advanced',
                groups: {
                    'heading': {
                        tools: {
                            'heading': {
                                label: 'Titler',
                                type: 'select',
                                list: {
                                    'titles-see-also' : {
                                        label: '== Se også == – interne lenker til andre prosjekter',
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: '== Se også ==',
                                                ownline: true
                                            }
                                        }
                                    },
                                    'titles-references' : {
                                        label: '== Referanser == – oppgitte referanser i teksten',
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: "== Referanser ==\n<references>\n",
                                                post: "\n</references>",
                                                ownline: true
                                            }
                                        }
                                    },
                                    'titles-literature' : {
                                        label: '== Litteratur == – oppgitt litteratur',
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: '== Litteratur ==',
                                                ownline: true
                                            }
                                        }
                                    },
                                    'titles-links' : {
                                        label: '== Eksterne lenker == – lenker til nettsteder',
                                        action: {
                                            type: 'encapsulate',
                                            options: {
                                                pre: '== Eksterne lenker ==',
                                                ownline: true
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        });
    };
    /* Check if we are in edit mode and the required modules are available and then customize the toolbar */
    if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1 ) {
        mw.loader.using( 'user.options', function () {
        	if ( mw.user.options.get('usebetatoolbar') ) {
    			mw.loader.using( 'ext.wikiEditor.toolbar', function () {
    				$(customizeBetaToolbar);
    			});
    		}
            //else {
        //			$(customizeOrigToolbar);
          //  }
    	});
    }

})(mediaWiki, jQuery);

/**
 * Description: Adapt multiple columns to the actual size of the bodyContent div.
 * Maintainers:
 */
(function(mw, $) {
    // counter for number of columns
    var count = 0;
    // width of the #bodyContent and its token
    var widths = [
        { size : 450,  name : 'multicol-xx-small' },
        { size : 700,  name : 'multicol-x-small' },
        { size : 950,  name : 'multicol-small' },
        { size : 1200, name : 'multicol-normal' },
        { size : 1500, name : 'multicol-large' },
        { size : 1800, name : 'multicol-x-large' },
        { size : 2100, name : 'multicol-xx-large' }
    ];
    // selectors for default override of large lists
    var selectors = [
        '#bodyContent h2:has(#Referanser) + ol.large.references',
        '#bodyContent h2:has(#Litteratur) + ul.large',
        '#bodyContent h2:has(#Eksterne_lenker) + ul.large',
        '#bodyContent h2:has(#Kilder) + ul.large',
        '#bodyContent h2:has(#Se_ogs\\.C3\\.A5) + ul.large',
        '#bodyContent h2:has(#Referanser) + ul.large',
        '#bodyContent h2:has(#Litteratur) + ol.large',
        '#bodyContent h2:has(#Eksterne_lenker) + ol.large',
        '#bodyContent h2:has(#Kilder) + ol.large',
        '#bodyContent h2:has(#Se_ogs\\.C3\\.A5) + ol.large'
    ];
    // calculate number of columns and set its token
    function calc() {
        var found = 0;
        var body = $('#bodyContent');
        var width = body.width();
        // remove any previous definitions
        // this is necessary for resize to work
        for (var i = 0; i<widths.length; i++)
            body.removeClass(widths[i].name);
        // if the width is to small we set a special token and exits
        if (width < widths[0].size) {
            body.addClass('multicol-xx-small');
            body.addClass('multicol-verysmall');
            return;
        }
        // test for increasing sizes and set the token
        for (var i = 1; i<widths.length; i++) {
            count++;
            if (width < widths[i].size) {
                body.addClass( widths[i].name);
                found++;
                break;
            }
        }
        // if we didn't find a matching size we set a special token
        if (!found) {
            body.addClass('multicol-xx-large');
            body.addClass('multicol-verylarge');
        }
        // unless we had a too smalll width we set a common token
        body.addClass('multicol-all');
    }
    // set it all up
    $(function() {
        calc();                 // only on ready
        $(window).resize(calc); // only on resize
        // identify large lists
        count *= 2;               // twice the number of columns
        $('#bodyContent ol, #bodyContent ul').each(function(i, el) {
            if (count < el.children.length) $(el).addClass('large');
        });
        // override for long lists in specific sections, add later
        //$(selectors.join(',')).before('<div class="multicol" style="display:none" />');
    });
})(mediaWiki, jQuery);


/* Test if an element has a certain class **************************************
 *
 * Description: Uses regular expressions and caching for better performance.
 * Note that use of this function indicates that the code isnt up to date.
 * 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);
    };
})();

/** Navbox
 * Description: Collapsible navigational boxes
 * Note: Interim solution.
 * Maintainer:
 */
mw.loader.load(mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=MediaWiki:Navbox.js&action=raw&ctype=text/javascript&maxage=21600&smaxage=86400');
 
 
/** Infobokser
 * Description: Vis/skjul, samt samle spesielt merkede bokser
 * Note: Det er på vei en adskillig bedre løsning som standardfunksjonalitet
 */
mw.loader.load(mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=MediaWiki:Visskjul.js&action=raw&ctype=text/javascript&maxage=21600&smaxage=86400');



/** Toolbar
 * Toolbarbuttons for links etc
*/

// Check if we're editing a page.
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
	// Add a hook handler.
	mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
		// Configure a new toolbar entry on the given $textarea jQuery object.
		$textarea.wikiEditor( 'addToToolbar', {

                'section': 'main',
                'group': 'format',
                'tools': {
                    'quote': {
                        label: 'Anførselstegn',
                        type: 'button',
                        icon: '//upload.wikimedia.org/wikipedia/commons/a/ac/Norwegian_quote_sign.png',
                        action: {
                            type: 'encapsulate',
                            options: {
                                pre: "«",
                                //peri: "«»",
                                post: "»"
                            }
                        }
                    }
                }
            });

$textarea.wikiEditor( 'addToToolbar', {
			
                'section': 'main',
                'group': 'format',
                'tools': {
                    'link': {
                        label: 'Lenkeparenteser',
                        type: 'button',
                        icon: '//upload.wikimedia.org/wikipedia/commons/b/ba/Norwegian_link_sign.png',
                        action: {
                            type: 'encapsulate',
                            options: {
                                pre: "[[",
                                //peri: "[[]]",
                                post: "]]"
                            }
                        }
                    }
                }
           
		} );
$textarea.wikiEditor( 'addToToolbar', {
                'section': 'main',
                'group': 'format',
                'tools': {
                    'link': {
                        label: 'Malparenteser',
                        type: 'button',
                        icon: '//upload.wikimedia.org/wikipedia/commons/4/4a/Norwegian_template_sign.png',
                        action: {
                            type: 'encapsulate',
                            options: {
                                pre: "{{",
                                //peri: "{{}}",
                                post: "}}"
                            }
                        }
                    }
                }
            });


	} );
}