MediaWiki:Navbox.js
Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.
- Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
- Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
- Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
/* Adapt a list structure and use it as a navigational aid **************************************
*
* Description: Gives a list some additional structuring and a collapse/expand functionality
* Maintainers: [[User:Jeblad]]
*/
// build an opaque function object
(function(mw, $, undefined){
// get the locally stored objects, clear out the objects on errors
var max = 128;
var values = {};
try {
values = JSON.parse( window.localStorage.getItem("navbox-items-value") || '{}' );
}
catch (e) {
try {
window.localStorage.setItem("navbox-items-value", '{}' );
} catch(e) {}
}
// private method for local storage
function store(key, idx, state) {
// only check whats the state
if (state === undefined)
return (values[key] ? values[key][idx] : null);
// build a copy where we start with our value and skip any later duplicates
var obj = {};
obj[key] = [];
obj[key][idx] = state;
var count = 0;
for (var x in values) {
if (x === key) {
if (!obj[x]) obj[x] = [];
for (var y in values[x])
if (!obj[x][y])
obj[x][y]=values[x][y];
}
else
obj[x]=values[x];
if (max<count++)
continue;
}
values = obj;
// rebuild indexes for local storage
var a = [];
for (var x in values) {
var b = [];
for (var y in values[x])
b.push('"'+values[x][y]+'"');
a.push('"'+x+'":['+b.join(',')+']');
}
try {
window.localStorage.setItem("navbox-items-value", '{'+a.join(',')+'}' );
} catch(e) {}
// return our set state
return state;
}
// worker process
$(function() {
// cache our navboxes
var navbox = $('div.statbox');
// we're running so remove no-script class
navbox
.removeClass('no-script');
// identify lead ins
navbox
.find('dd')
.find('ul,ol')
.each(function(i, el) {
var obj = el.previousSibling;
while (obj && !/\S/.test(obj.nodeValue))
obj = obj.previousSibling;
if (obj)
$(el)
.addClass('lead-in');
});
// is collapsible navigation enabled?
if (mw.config.get('wgVectorEnabledModules') && mw.config.get('wgVectorEnabledModules')['collapsiblenav']) {
// identify collapsible navigations
navbox
.addClass('collapsible');
// identify collapsed state and set up handlers
navbox
.find('dt')
.each(function(i, el){
var obj = el.nextSibling;
while (obj && obj.nodeType != 1)
obj = obj.nextSibling;
if (obj && obj.nodeName) {
$(el)
.addClass(function(index, currentClass){
var matches = $(el).closest('.navbox').attr('class').match(/navbox-([-\w]+)/);
var idx = $(this).closest('dl').prevAll('dl').length; // not sure
if (matches && 1<matches.length && store(matches[1], idx) == 'expanded') {
$(el)
.nextUntil('dt')
.css('display', 'block');
return 'expanded';
}
else return 'collapsed';
})
.wrapInner('<span class="title"/>')
.click(function(){
var matches = $(this).closest('.navbox').attr('class').match(/navbox-([-\w]+)/);
var idx = $(this).closest('dl').prevAll('dl').length; // not sure
$(this)
.closest('dt')
.toggleClass('collapsed expanded')
.each(function(i, el){
if (matches && 1<matches.length)
store(matches[1], idx, $(el).hasClass('collapsed') ? 'collapsed' : 'expanded');
})
.nextUntil('dt')
.slideToggle();
});
}
})
}
});
})(mediaWiki, jQuery);