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@26084 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
mistic100 2013-12-22 15:59:55 +00:00
parent e1c2ba81b1
commit 12878437cd

View file

@ -1,222 +1,165 @@
/** (function() {
* Cookie plugin var session_storage = window.sessionStorage || {};
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses: var menubar=jQuery("#menubar"),
*/ menuswitcher,
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options=jQuery.extend({},options);options.expires=-1;} content=jQuery("#the_page > .content"),
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;} pcontent=jQuery("#content"),
expires='; expires='+date.toUTCString();} imageInfos=jQuery("#imageInfos"),
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;}}} infoswitcher,
return cookieValue;}}; theImage=jQuery("#theImage"),
comments=jQuery("#thePicturePage #comments"),
if (jQuery.cookie('picture-menu') == 'visible') { comments_button,
jQuery("head").append("<style type=\"text/css\">#content.contentWithMenu, #the_page > .content {margin-left:240px;}</style>"); commentsswitcher,
} else { comments_add,
jQuery("head").append("<style type=\"text/css\">#the_page #menubar {display:none;} #content.contentWithMenu, #the_page > .content {margin-left:35px;}</style>"); comments_top_offset = 0;
}
if (session_storage['picture-menu'] == 'visible') {
function hideMenu(delay) { jQuery("head").append('<style>#content.contentWithMenu, #the_page > .content {margin-left:240px;}</style>');
var menubar=jQuery("#menubar"); }
var menuswitcher=jQuery("#menuSwitcher"); else {
var content=jQuery("#the_page > .content"); jQuery("head").append('<style>#the_page #menubar {display:none;} #content.contentWithMenu, #the_page > .content {margin-left:35px;}</style>');
var pcontent=jQuery("#content"); }
menubar.hide(delay); function hideMenu(delay) {
menuswitcher.addClass("menuhidden").removeClass("menushown"); menubar.hide(delay);
content.addClass("menuhidden").removeClass("menushown"); menuswitcher.addClass("menuhidden").removeClass("menushown");
pcontent.addClass("menuhidden").removeClass("menushown"); content.addClass("menuhidden").removeClass("menushown");
jQuery.cookie('picture-menu', 'hidden', {path: "/"}); pcontent.addClass("menuhidden").removeClass("menushown");
session_storage['picture-menu'] = 'hidden';
} }
function showMenu(delay) { function showMenu(delay) {
menubar.show(delay);
var menubar=jQuery("#menubar"); menuswitcher.addClass("menushown").removeClass("menuhidden");
var menuswitcher=jQuery("#menuSwitcher"); content.addClass("menushown").removeClass("menuhidden");
var content=jQuery("#the_page > .content"); pcontent.addClass("menushown").removeClass("menuhidden");
var pcontent=jQuery("#content"); session_storage['picture-menu'] = 'visible';
}
menubar.show(delay);
menuswitcher.addClass("menushown").removeClass("menuhidden"); function hideInfo(delay) {
content.addClass("menushown").removeClass("menuhidden"); imageInfos.hide(delay);
pcontent.addClass("menushown").removeClass("menuhidden"); infoswitcher.addClass("infohidden").removeClass("infoshown");
jQuery.cookie('picture-menu', 'visible', {path: "/"}); theImage.addClass("infohidden").removeClass("infoshown");
session_storage['side-info'] = 'hidden';
} }
function hideInfo(delay) { function showInfo(delay) {
imageInfos.show(delay);
var imageInfos=jQuery("#imageInfos"); infoswitcher.addClass("infoshown").removeClass("infohidden");
var infoswitcher=jQuery("#infoSwitcher"); theImage.addClass("infoshown").removeClass("infohidden");
var theImage=jQuery("#theImage"); session_storage['side-info'] = 'visible';
}
imageInfos.hide(delay);
infoswitcher.addClass("infohidden").removeClass("infoshown"); function commentsToggle() {
theImage.addClass("infohidden").removeClass("infoshown"); if (comments.hasClass("commentshidden")) {
jQuery.cookie('side-info', 'hidden', {path: "/"}); comments.removeClass("commentshidden").addClass("commentsshown");
comments_button.addClass("comments_toggle_off").removeClass("comments_toggle_on");;
} session_storage['comments'] = 'visible';
function showInfo(delay) { comments_top_offset = comments_add.offset().top - parseFloat(comments_add.css('marginTop').replace(/auto/, 0));
}
var imageInfos=jQuery("#imageInfos"); else {
var infoswitcher=jQuery("#infoSwitcher"); comments.addClass("commentshidden").removeClass("commentsshown");
var theImage=jQuery("#theImage"); comments_button.addClass("comments_toggle_on").removeClass("comments_toggle_off");;
session_storage['comments'] = 'hidden';
imageInfos.show(delay); }
infoswitcher.addClass("infoshown").removeClass("infohidden"); }
theImage.addClass("infoshown").removeClass("infohidden");
jQuery.cookie('side-info', 'visible', {path: "/"}); jQuery(function(){
// side-menu show/hide
} if (menubar.length == 1 && p_main_menu!="disabled") {
menuswitcher=jQuery("#menuSwitcher");
jQuery("document").ready(function(jQuery){
menuswitcher.html('<div class="switchArrow">&nbsp;</div>');
// side-menu show/hide
if (session_storage['picture-menu'] == 'hidden' || p_main_menu == 'off') {
var sidemenu = jQuery.cookie('picture-menu'); hideMenu(0);
var menubar=jQuery("#menubar"); }
else {
if (menubar.length == 1 && p_main_menu!="disabled") { showMenu(0);
}
jQuery("#menuSwitcher").html("<div class=\"switchArrow\">&nbsp;</div>");
menuswitcher.click(function(e){
// if cookie says the menu is hiding, keep it hidden! if (menubar.is(":hidden")) {
if (sidemenu == 'hidden') { showMenu(0);
hideMenu(0); }
} else if (sidemenu == 'visible') { else {
showMenu(0); hideMenu(0);
} else if (p_main_menu == 'off') { }
hideMenu(0); e.preventDefault();
} else { });
showMenu(0); }
}
// info show/hide
jQuery("#menuSwitcher").click(function(){ if (imageInfos.length == 1 && p_pict_descr!="disabled") {
if (jQuery("#menubar").is(":hidden")) { infoswitcher=jQuery("#infoSwitcher");
showMenu(0);
return false; infoswitcher.html('<div class="switchArrow">&nbsp;</div>');
} else {
hideMenu(0); if (session_storage['side-info'] == 'hidden' || p_pict_descr == 'off') {
return false; hideInfo(0);
} }
}); else {
showInfo(0);
} }
// info show/hide infoswitcher.click(function(e){
if (imageInfos.is(":hidden")) {
var sideinfo = jQuery.cookie('side-info'); showInfo(0);
var imageInfos=jQuery("#imageInfos"); }
else {
if (imageInfos.length == 1 && p_pict_descr!="disabled") { hideInfo(0);
}
jQuery("#infoSwitcher").html("<div class=\"switchArrow\">&nbsp;</div>"); e.preventDefault();
});
// if cookie says the menu is hiding, keep it hidden! }
if (sideinfo == 'hidden') {
hideInfo(0); // comments show/hide
} else if (sideinfo == 'visible') { if (comments.length == 1 && p_pict_comment!="disabled") {
showInfo(0); commentsswitcher=jQuery("#commentsSwitcher");
} else if (p_pict_descr == 'off') { comments_button=jQuery("#comments h3");
hideInfo(0); comments_add=jQuery('#commentAdd');
} else {
showInfo(0); commentsswitcher.html('<div class="switchArrow">&nbsp;</div>');
}
if (comments_button.length == 0) {
jQuery("#infoSwitcher").click(function(){ jQuery("#addComment").before("<h3>Comments</h3>");
if (jQuery("#imageInfos").is(":hidden")) { comments_button=jQuery("#comments h3");
showInfo(0); }
return false;
} else { if (session_storage['comments'] == 'hidden' || p_pict_comment == 'off') {
hideInfo(0); comments.addClass("commentshidden");
return false; comments_button.addClass("comments_toggle comments_toggle_on");
} }
}); else {
comments.addClass("commentsshown");
} comments_button.addClass("comments_toggle comments_toggle_off");
}
// comments show/hide
comments_button.click(commentsToggle);
var commentsswicther=jQuery("#commentsSwitcher"); commentsswitcher.click(commentsToggle);
var comments=jQuery("#thePicturePage #comments");
jQuery(window).scroll(function (event) {
commentsswicther.html("<div class=\"switchArrow\">&nbsp;</div>"); if (comments_top_offset==0) return;
if (comments.length == 1 && p_pict_comment!="disabled") { // what the y position of the scroll is
var comments_button=jQuery("#comments h3"); var y = jQuery(this).scrollTop();
if (comments_button.length == 0) { // whether that's below the form
jQuery("#addComment").before("<h3>Comments</h3>"); if (y >= comments_top_offset) {
comments_button=jQuery("#comments h3"); // if so, ad the fixed class
} comments_add.addClass('fixed');
}
if (jQuery.cookie('comments') == 'hidden') { else {
comments.addClass("commentshidden"); // otherwise remove it
comments_button.addClass("comments_toggle").addClass("comments_toggle_on"); comments_add.removeClass('fixed');
} else if (jQuery.cookie('comments') == 'visible') { }
comments.addClass("commentsshown"); });
comments_button.addClass("comments_toggle").addClass("comments_toggle_off");
} else if (p_pict_comment == 'off') { if (comments_add.is(":visible")) {
comments.addClass("commentshidden"); comments_top_offset = comments_add.offset().top - parseFloat(comments_add.css('marginTop').replace(/auto/, 0));
comments_button.addClass("comments_toggle").addClass("comments_toggle_on"); }
} else { }
comments.addClass("commentsshown"); });
comments_button.addClass("comments_toggle").addClass("comments_toggle_off"); }());
}
comments_button.click(function() { commentsToggle() });
commentsswicther.click(function() { commentsToggle() });
if (jQuery('#commentAdd').is(":visible")) {
var top = jQuery('#commentAdd').offset().top - parseFloat(jQuery('#commentAdd').css('marginTop').replace(/auto/, 0));
jQuery(window).scroll(function (event) {
// what the y position of the scroll is
var y = jQuery(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
jQuery('#commentAdd').addClass('fixed');
} else {
// otherwise remove it
jQuery('#commentAdd').removeClass('fixed');
}
});
}
}
});
function commentsToggle() {
var comments=jQuery("#thePicturePage #comments");
var comments_button=jQuery("#comments h3");
if (comments.hasClass("commentshidden")) {
comments.removeClass("commentshidden").addClass("commentsshown");
comments_button.addClass("comments_toggle_off").removeClass("comments_toggle_on");;
jQuery.cookie('comments', 'visible', {path: "/"});
var top = jQuery('#commentAdd').offset().top - parseFloat(jQuery('#commentAdd').css('marginTop').replace(/auto/, 0));
jQuery(window).scroll(function (event) {
// what the y position of the scroll is
var y = jQuery(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
jQuery('#commentAdd').addClass('fixed');
} else {
// otherwise remove it
jQuery('#commentAdd').removeClass('fixed');
}
});
} else {
comments.addClass("commentshidden").removeClass("commentsshown");
comments_button.addClass("comments_toggle_on").removeClass("comments_toggle_off");;
jQuery.cookie('comments', 'hidden', {path: "/"});
}
}