source: trunk/themes/elegant/scripts_pp.js @ 26084

Last change on this file since 26084 was 26084, checked in by mistic100, 10 years ago

rewrite elegant javascript : use session storage instead of cookies, factorize jQuery variables, remove unecessary event handlers, wrap into anonymous function

File size: 5.1 KB
Line 
1(function() {
2  var session_storage = window.sessionStorage || {};
3
4  var menubar=jQuery("#menubar"),
5      menuswitcher,
6      content=jQuery("#the_page > .content"),
7      pcontent=jQuery("#content"),
8      imageInfos=jQuery("#imageInfos"),
9      infoswitcher,
10      theImage=jQuery("#theImage"),
11      comments=jQuery("#thePicturePage #comments"),
12      comments_button,
13      commentsswitcher,
14      comments_add,
15      comments_top_offset = 0;
16
17  if (session_storage['picture-menu'] == 'visible') {
18    jQuery("head").append('<style>#content.contentWithMenu, #the_page > .content {margin-left:240px;}</style>');
19  }
20  else {
21    jQuery("head").append('<style>#the_page #menubar {display:none;} #content.contentWithMenu, #the_page > .content {margin-left:35px;}</style>');
22  }
23
24  function hideMenu(delay) {
25    menubar.hide(delay);
26    menuswitcher.addClass("menuhidden").removeClass("menushown");
27    content.addClass("menuhidden").removeClass("menushown");
28    pcontent.addClass("menuhidden").removeClass("menushown");
29    session_storage['picture-menu'] = 'hidden';
30  }
31
32  function showMenu(delay) {
33    menubar.show(delay);
34    menuswitcher.addClass("menushown").removeClass("menuhidden");
35    content.addClass("menushown").removeClass("menuhidden");
36    pcontent.addClass("menushown").removeClass("menuhidden");
37    session_storage['picture-menu'] = 'visible';
38  }
39
40  function hideInfo(delay) {
41    imageInfos.hide(delay);
42    infoswitcher.addClass("infohidden").removeClass("infoshown");
43    theImage.addClass("infohidden").removeClass("infoshown");
44    session_storage['side-info'] = 'hidden';
45  }
46
47  function showInfo(delay) {
48    imageInfos.show(delay);
49    infoswitcher.addClass("infoshown").removeClass("infohidden");
50    theImage.addClass("infoshown").removeClass("infohidden");
51    session_storage['side-info'] = 'visible';
52  }
53
54  function commentsToggle() {
55    if (comments.hasClass("commentshidden")) {
56        comments.removeClass("commentshidden").addClass("commentsshown");
57        comments_button.addClass("comments_toggle_off").removeClass("comments_toggle_on");;
58        session_storage['comments'] = 'visible';
59
60        comments_top_offset = comments_add.offset().top - parseFloat(comments_add.css('marginTop').replace(/auto/, 0));
61      }
62      else {
63        comments.addClass("commentshidden").removeClass("commentsshown");
64        comments_button.addClass("comments_toggle_on").removeClass("comments_toggle_off");;
65        session_storage['comments'] = 'hidden';
66      }
67  }
68
69  jQuery(function(){
70    // side-menu show/hide
71    if (menubar.length == 1 && p_main_menu!="disabled") {
72      menuswitcher=jQuery("#menuSwitcher");
73
74      menuswitcher.html('<div class="switchArrow">&nbsp;</div>');
75
76      if (session_storage['picture-menu'] == 'hidden' || p_main_menu == 'off') {
77        hideMenu(0);
78      }
79      else {
80        showMenu(0);
81      }
82
83      menuswitcher.click(function(e){
84        if (menubar.is(":hidden")) {
85          showMenu(0);
86        }
87        else {
88          hideMenu(0);
89        }
90        e.preventDefault();
91      });
92    }
93
94    // info show/hide
95    if (imageInfos.length == 1 && p_pict_descr!="disabled") {
96      infoswitcher=jQuery("#infoSwitcher");
97
98      infoswitcher.html('<div class="switchArrow">&nbsp;</div>');
99
100      if (session_storage['side-info'] == 'hidden' || p_pict_descr == 'off') {
101        hideInfo(0);
102      }
103      else {
104        showInfo(0);
105      }
106
107      infoswitcher.click(function(e){
108        if (imageInfos.is(":hidden")) {
109          showInfo(0);
110        }
111        else {
112          hideInfo(0);
113        }
114        e.preventDefault();
115      });
116    }
117
118    // comments show/hide
119    if (comments.length == 1 && p_pict_comment!="disabled") {
120      commentsswitcher=jQuery("#commentsSwitcher");
121      comments_button=jQuery("#comments h3");
122      comments_add=jQuery('#commentAdd');
123
124      commentsswitcher.html('<div class="switchArrow">&nbsp;</div>');
125
126      if (comments_button.length == 0) {
127        jQuery("#addComment").before("<h3>Comments</h3>");
128        comments_button=jQuery("#comments h3");
129      }
130
131      if (session_storage['comments'] == 'hidden' || p_pict_comment == 'off') {
132        comments.addClass("commentshidden");
133        comments_button.addClass("comments_toggle comments_toggle_on");
134      }
135      else {
136        comments.addClass("commentsshown");
137        comments_button.addClass("comments_toggle comments_toggle_off");
138      }
139
140      comments_button.click(commentsToggle);
141      commentsswitcher.click(commentsToggle);
142
143      jQuery(window).scroll(function (event) {
144        if (comments_top_offset==0) return;
145
146        // what the y position of the scroll is
147        var y = jQuery(this).scrollTop();
148
149        // whether that's below the form
150        if (y >= comments_top_offset) {
151          // if so, ad the fixed class
152          comments_add.addClass('fixed');
153        }
154        else {
155          // otherwise remove it
156          comments_add.removeClass('fixed');
157        }
158      });
159
160      if (comments_add.is(":visible")) {
161        comments_top_offset = comments_add.offset().top - parseFloat(comments_add.css('marginTop').replace(/auto/, 0));
162      }
163    }
164  });
165}());
Note: See TracBrowser for help on using the repository browser.