rewrite elegant javascript : use session storage instead of cookies, factorize jQuery variables, remove unecessary event handlers, wrap into anonymous function
git-svn-id: http://piwigo.org/svn/trunk@26085 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
parent
12878437cd
commit
fd4b73efa3
1 changed files with 57 additions and 83 deletions
|
@ -1,83 +1,57 @@
|
|||
/**
|
||||
* Cookie plugin
|
||||
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
|
||||
* Dual licensed under the MIT and GPL licenses:
|
||||
*/
|
||||
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options=jQuery.extend({},options);options.expires=-1;}
|
||||
var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}
|
||||
expires='; expires='+date.toUTCString();}
|
||||
var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}
|
||||
return cookieValue;}};
|
||||
|
||||
if (jQuery.cookie('page-menu') == 'hidden') {
|
||||
jQuery("head").append("<style type=\"text/css\">#the_page #menubar {display:none;} #content.contentWithMenu, #the_page > .content {margin-left:35px;}</style>");
|
||||
} else {
|
||||
jQuery("head").append("<style type=\"text/css\">#content.contentWithMenu, #the_page > .content {margin-left:240px;}</style>");
|
||||
}
|
||||
|
||||
function hideMenu(delay) {
|
||||
var menubar=jQuery("#menubar");
|
||||
var menuswitcher=jQuery("#menuSwitcher");
|
||||
var content=jQuery("#the_page > .content");
|
||||
var pcontent=jQuery("#content");
|
||||
|
||||
menubar.hide(delay);
|
||||
menuswitcher.addClass("menuhidden").removeClass("menushown");
|
||||
content.addClass("menuhidden").removeClass("menushown");
|
||||
pcontent.addClass("menuhidden").removeClass("menushown");
|
||||
jQuery.cookie('page-menu', 'hidden', {path: "/"});
|
||||
|
||||
}
|
||||
|
||||
function showMenu(delay) {
|
||||
|
||||
var menubar=jQuery("#menubar");
|
||||
var menuswitcher=jQuery("#menuSwitcher");
|
||||
var content=jQuery("#the_page > .content");
|
||||
var pcontent=jQuery("#content");
|
||||
|
||||
menubar.show(delay);
|
||||
menuswitcher.addClass("menushown").removeClass("menuhidden");
|
||||
content.addClass("menushown").removeClass("menuhidden");
|
||||
pcontent.addClass("menushown").removeClass("menuhidden");
|
||||
jQuery.cookie('page-menu', 'visible', {path: "/"});
|
||||
|
||||
}
|
||||
|
||||
jQuery("document").ready(function(jQuery){
|
||||
|
||||
var sidemenu = jQuery.cookie('page-menu');
|
||||
var menubar=jQuery("#menubar");
|
||||
|
||||
if (menubar.length == 1 && p_main_menu!="disabled") {
|
||||
|
||||
jQuery("#menuSwitcher").html("<div class=\"switchArrow\"> </div>");
|
||||
|
||||
// if cookie says the menu is hiding, keep it hidden!
|
||||
if (sidemenu == 'hidden') {
|
||||
hideMenu(0);
|
||||
} else if (sidemenu == 'visible') {
|
||||
showMenu(0);
|
||||
} else if (p_main_menu == 'off') {
|
||||
hideMenu(0);
|
||||
} else {
|
||||
showMenu(0);
|
||||
}
|
||||
|
||||
jQuery("#menuSwitcher").click(function(){
|
||||
if (jQuery("#menubar").is(":hidden")) {
|
||||
showMenu(0);
|
||||
return false;
|
||||
} else {
|
||||
hideMenu(0);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (menubar.length == 1 && p_main_menu=="disabled") {
|
||||
showMenu(0);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
(function() {
|
||||
var session_storage = window.sessionStorage || {};
|
||||
|
||||
var menubar=jQuery("#menubar"),
|
||||
menuswitcher=jQuery("#menuSwitcher"),
|
||||
content=jQuery("#the_page > .content"),
|
||||
pcontent=jQuery("#content");
|
||||
|
||||
if (session_storage['page-menu'] == 'hidden') {
|
||||
jQuery("head").append('<style>#the_page #menubar {display:none;} #content.contentWithMenu, #the_page > .content {margin-left:35px;}</style>');
|
||||
}
|
||||
else {
|
||||
jQuery("head").append('<style>#content.contentWithMenu, #the_page > .content {margin-left:240px;}</style>');
|
||||
}
|
||||
|
||||
function hideMenu(delay) {
|
||||
menubar.hide(delay);
|
||||
menuswitcher.addClass("menuhidden").removeClass("menushown");
|
||||
content.addClass("menuhidden").removeClass("menushown");
|
||||
pcontent.addClass("menuhidden").removeClass("menushown");
|
||||
session_storage['page-menu'] = 'hidden';
|
||||
}
|
||||
|
||||
function showMenu(delay) {
|
||||
menubar.show(delay);
|
||||
menuswitcher.addClass("menushown").removeClass("menuhidden");
|
||||
content.addClass("menushown").removeClass("menuhidden");
|
||||
pcontent.addClass("menushown").removeClass("menuhidden");
|
||||
session_storage['page-menu'] = 'visible';
|
||||
}
|
||||
|
||||
jQuery(function(){
|
||||
if (menubar.length == 1 && p_main_menu!="disabled") {
|
||||
menuswitcher.html('<div class="switchArrow"> </div>');
|
||||
|
||||
if (session_storage['page-menu'] == 'hidden' || p_main_menu == 'off') {
|
||||
hideMenu(0);
|
||||
}
|
||||
else {
|
||||
showMenu(0);
|
||||
}
|
||||
|
||||
menuswitcher.click(function(e){
|
||||
if (menubar.is(":hidden")) {
|
||||
showMenu(0);
|
||||
}
|
||||
else {
|
||||
hideMenu(0);
|
||||
}
|
||||
e.preventDefault();
|
||||
});
|
||||
}
|
||||
else if (menubar.length == 1 && p_main_menu=="disabled") {
|
||||
showMenu(0);
|
||||
}
|
||||
});
|
||||
}());
|
Loading…
Reference in a new issue