/
drupalBlockToggle.js
153 lines (115 loc) · 3.62 KB
/
drupalBlockToggle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Our drupalBlockToggle jQuery plugin.
* It allows the contents of menu blocks to be hidden/unhidden by clicking
* on the title.
*
* @author Douglas Muth <http://www.dmuth.org/>
*/
jQuery.fn.drupalBlockToggle = function() {
//
// Take a hint after Python here, since "this" is reserved in Javascript
// Also keep in mind that this isn't an actual object. More like
// a namespace.
//
var self = jQuery.fn.drupalBlockToggle;
//
// Make all headers clickable so that they can be hidden/shown
//
$("#sidebar-left").find("h2.title").css("cursor", "pointer");
$("#sidebar-left").find("h2.title").click(function() {self.toggleMenu(this)});
$("#sidebar-right").find("h2.title").css("cursor", "pointer");
$("#sidebar-right").find("h2.title").click(function() {self.toggleMenu(this)});
$("#footer-left").find("h2.title").css("cursor", "pointer");
$("#footer-left").find("h2.title").click(function() {self.toggleMenu(this)});
$("#footer-middle").find("h2.title").css("cursor", "pointer");
$("#footer-middle").find("h2.title").click(function() {self.toggleMenu(this)});
$("#footer-right").find("h2.title").css("cursor", "pointer");
$("#footer-right").find("h2.title").click(function() {self.toggleMenu(this)});
self.status = self.getStatus();
//
// Loop through each of our titles, and hide the contents
//
$("#sidebar-left").find("h2.title").each(function() {self.initBlock(this)});
$("#sidebar-right").find("h2.title").each(function() {self.initBlock(this)});
$("#footer-left").find("h2.title").each(function() {self.initBlock(this)});
$("#footer-middle").find("h2.title").each(function() {self.initBlock(this)});
$("#footer-right").find("h2.title").each(function() {self.initBlock(this)});
};
/**
*
* Retrive our status cookie, split it up, and save the
* values as keys
*
* @return array An array of our block status where the keys
* are the IDs of blocks that are to be hidden.
*/
jQuery.fn.drupalBlockToggle.getStatus = function() {
var retval = {};
var cookie = $.cookie("block-status");
if (cookie) {
cookie = cookie.split(",");
for (key in cookie) {
retval[cookie[key]] = true;
}
}
return(retval);
}; // End of getStatus()
/**
* Initialize a specific block, possibly setting it to already be collapsed.
*
* @param obj e The HTML entity. Equivilient to "this" in jQuery.
*/
jQuery.fn.drupalBlockToggle.initBlock = function(e) {
var self = jQuery.fn.drupalBlockToggle;
var id = $(e).parent().attr("id");
var title = $(e);
if (self.status[id]) {
title.siblings().hide();
}
} // End of initBlock()
/**
* This function is called whenver a menu header is clicked.
*
* @param obj e The HTML entity. Equivilient to "this" in jQuery.
*/
jQuery.fn.drupalBlockToggle.toggleMenu = function(e) {
var self = jQuery.fn.drupalBlockToggle;
var id = $(e).parent().attr("id");
var title = $(e);
$(e).siblings().slideToggle("normal", function() {
var display = $(this).css("display");
if (display == "none") {
self.status[id] = true;
} else {
delete (self.status[id]);
}
//
// Figure out what's going on in Safari
//
// alert(id);
var tmp = self.serialize(self.status);
var params = {"expires": 30, "path": "/"};
$.cookie("block-status", tmp, params);
});
}; // End of toggleMenu()
/**
* Serialize the keys in our array.
*
* @return string A comma-delimited string of keys.
*/
jQuery.fn.drupalBlockToggle.serialize = function(data) {
var retval = "";
for (key in data) {
if (retval) {
retval += ",";
}
retval += key;
}
return(retval);
}; // End of serialize()
//
// Stuff to execute when the page is done loading.
//
$(document).ready(function(){
jQuery.fn.drupalBlockToggle();
});