User:Ferretwings/Toggle.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/**
* Toggle
* Works with Template:Toggle to create a section of the article that
* can be toggled to show/hide
* Uses cookies to store the state of the toggle
*
* @version 2.0.0
* @author Tierrie
*/
$(document).ready(function(){
console.log("script: Toggle script loaded");
if(typeof($.cookie) === undefined) {
mw.loader.using(['jquery.cookie']);
}
var cookie_id = 'toggle_';
function removeCookie(key) {
setCookie(key);
}
function setCookie(key, value) {
if(value=="undefined" || value == "null") value = null;
$.cookie(cookie_id + key, value, { expires: 150, path: '/' });
}
function getCookie(key) {
return $.cookie(cookie_id + key);
}
$('.toggle_banner').click( function() {
var id = $(this).parent().attr('class').match(/toggle_id_[\d\w]+/)[0].split('toggle_id_')[1];
if( $('.toggle_id_'+id+' .toggle_wrn').css('display') == 'none') {
$('.toggle_id_'+id+' .toggle_wrn').fadeIn(200, function() {
$(this).show(200);
});
$('.toggle_id_'+id+' .toggle_txt').hide(0);
setCookie(id, 'hide');
} else {
$('.toggle_id_'+id+' .toggle_wrn').fadeOut(200, function() {
$(this).hide(200);
});
$('.toggle_id_'+id+' .toggle_txt').delay(200).show(0);
setCookie(id, 'show');
}
});
var toggle_on_page = {};
$('.sp').each( function() {
var id = $(this).attr('class').match(/sp_id_[\d\w]+/)[0].split('sp_id_')[1];
toggle_on_page[id] = undefined;
});
for (var id in sp_on_page) {
if (getCookie(id) === 'show') {
$('.toggle_id_'+id+' .toggle_wrn').hide(0);
$('.toggle_id_'+id+' .toggle_txt').show(0);
} else if (getCookie(id) === 'hide') {
$('.toggle_id_'+id+' .toggle_wrn').show(0);
$('.toggle_id_'+id+' .toggle_txt').hide(0);
// if no cookies are set, check to see if the warning is displayed by default
} else if ($('.toggle_id_'+id+' .toggle_wrn').attr('display') == 'none') {
$('.toggle_id_'+id+' .toggle_wrn').hide(0);
$('.toggle_id_'+id+' .toggle_txt').show(0);
} else {
$('.toggle_id_'+id+' .toggle_wrn').show(0);
$('.toggle_id_'+id+' .toggle_txt').hide(0);
}
}
});