-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-expand.js
309 lines (264 loc) · 11.4 KB
/
simple-expand.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
===============================================================
jQuery plugin to expand/collapse a content element when a
expander element is clicked. When expanding/collapsing the plug-in
also toggles a class on the element.
See https://github.com/redhotsly/simple-expand
===============================================================
Copyright (C) 2012 Sylvain Hamel
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
===============================================================
*/
/*globals $:false, window:false*/
(function ($) {
"use strict";
// SimpleExpand
function SimpleExpand() {
var that = this;
that.defaults = {
// hideMode
// -----------
// Specifies method to hide the content element.
//
// Default: fadeToggle
//
// Values:
// - fadeToggle: Use jquery.fadeToggle()
// - basic: Use jquery.toggle()
// - css: relies on user provided css to show/hide. you can define
// classes for "collapsed" and "expanded" classes.
// - a function : custom toggle function. The function receives 3 arguments
// expander: the element that triggered the toggle
// targets: the items to toggle
// expanded: true if expanding; false if collapsing
//
// If un an unknown value is specified, the plug-in reverts to "css".
'hideMode': 'fadeToggle',
// searchMode
// -----------
// Specifies the defaut value for data-expander-target-search
// when none is specified on the expander element.
//
// Default: parent
//
// Values:
// - parent: go up the expander's parents hierarchy searching
// each parent's childens looking for a target
//
// - absolute : finds a target globally in the document (useful when
// matching an id)
//
// - relative : finds a target nested inside the expander
//
// If un an unknown value is specified, no targets will be found.
'defaultSearchMode': 'parent',
// defaultTarget
// -----------
// Specifies the defaut value for data-expander-target when
// none is specified on the expander element.
//
// Default: .content
'defaultTarget': '.content',
// throwOnMissingTarget
// -----------
// Specifies whether the plug-in throws an exception if it
// cannot find a target for the expander
//
// Default: true
'throwOnMissingTarget': true,
// keepStateInCookie
// -----------
// Specifies whether the plug-in keeps the expended/collapsed state
// in a cookie for the next time.
//
// Default: false
//
// Notes:
// - This only works for expanders with an Id attribute.
// - Make sure you load the jQuery cookie plug-in (https://github.com/carhartl/jquery-cookie/)
// before simple-expand is loaded.
//
'keepStateInCookie': false,
'cookieName': 'simple-expand'
};
that.settings = {};
$.extend(that.settings, that.defaults);
// Search in the children of the 'parent' element for an element that matches 'filterSelector'
// but don't search deeper if a 'stopAtSelector' element is met.
// See this question to better understand what this does.
// http://stackoverflow.com/questions/10902077/how-to-select-children-elements-but-only-one-level-deep-with-jquery
that.findLevelOneDeep = function (parent, filterSelector, stopAtSelector) {
return parent.find(filterSelector).filter(function () {
return !$(this).parentsUntil(parent, stopAtSelector).length;
});
};
// Hides targets
that.setInitialState = function (expander, targets) {
var isExpanded = that.readState(expander);
if (isExpanded) {
expander.removeClass("collapsed").addClass("expanded");
that.show(targets);
} else {
expander.removeClass("expanded").addClass("collapsed");
that.hide(targets);
}
};
that.hide = function (targets) {
if (that.settings.hideMode === "fadeToggle") {
targets.hide();
} else if (that.settings.hideMode === "basic") {
targets.hide();
}
};
that.show = function (targets) {
if (that.settings.hideMode === "fadeToggle") {
targets.show();
} else if (that.settings.hideMode === "basic") {
targets.show();
}
};
// assert that $.cookie if 'keepStateInCookie' option is enabled
that.checkKeepStateInCookiePreconditions = function () {
if (that.settings.keepStateInCookie && $.cookie === undefined){
throw new Error("simple-expand: keepStateInCookie option requires $.cookie to be defined.");
}
};
// returns the cookie
that.readCookie = function () {
var jsonString = $.cookie(that.settings.cookieName);
if ( jsonString === null || jsonString === '' ){
return {};
}
else{
return JSON.parse(jsonString);
}
};
// gets state for the expander from cookies
that.readState = function (expander) {
if (!that.settings.keepStateInCookie){
return false;
}
var id = expander.attr('Id');
if (id === undefined){
return;
}
var cookie = that.readCookie();
var isExpanded = cookie[id] === true || false;
return isExpanded;
};
// save states of the item in the cookies
that.saveState = function (expander, isExpanded) {
if (!that.settings.keepStateInCookie){
return;
}
var id = expander.attr('Id');
if (id === undefined){
return;
}
var cookie = that.readCookie();
cookie[id] = isExpanded;
$.cookie(that.settings.cookieName, JSON.stringify(cookie), { raw: true, path:window.location.pathname });
};
// Toggles the targets and sets the 'collapsed' or 'expanded'
// class on the expander
that.toggle = function (expander, targets) {
var isExpanded = that.toggleCss(expander);
if (that.settings.hideMode === "fadeToggle") {
targets.fadeToggle(150);
} else if (that.settings.hideMode === "basic") {
targets.toggle();
} else if ($.isFunction(that.settings.hideMode)) {
that.settings.hideMode(expander, targets, isExpanded);
}
that.saveState(expander, isExpanded);
// prevent default to stop browser from scrolling to: href="#"
return false;
};
// Toggles using css
that.toggleCss = function (expander) {
if (expander.hasClass("expanded")) {
expander.toggleClass("collapsed expanded");
return false;
}
else {
expander.toggleClass("expanded collapsed");
return true;
}
};
// returns the targets for the given expander
that.findTargets = function (expander, searchMode, targetSelector) {
// find the targets using the specified searchMode
var targets = [];
if (searchMode === "absolute") {
targets = $(targetSelector);
}
else if (searchMode === "relative") {
targets = that.findLevelOneDeep(expander, targetSelector, targetSelector);
}
else if (searchMode === "parent") {
// Search the expander's parents recursively until targets are found.
var parent = expander.parent();
do {
targets = that.findLevelOneDeep(parent, targetSelector, targetSelector);
// No targets found, prepare for next iteration...
if (targets.length === 0) {
parent = parent.parent();
}
} while (targets.length === 0 && parent.length !== 0);
}
return targets;
};
that.activate = function (jquery, options) {
$.extend(that.settings, options);
that.checkKeepStateInCookiePreconditions();
// Plug-in entry point
//
// For each expander:
// search targets
// hide targets
// register to targets' click event to toggle them on click
jquery.each(function () {
var expander = $(this);
var targetSelector = expander.attr("data-expander-target") || that.settings.defaultTarget;
var searchMode = expander.attr("data-expander-target-search") || that.settings.defaultSearchMode;
var targets = that.findTargets(expander, searchMode, targetSelector);
// no elements match the target selector
// there is nothing we can do
if (targets.length === 0) {
if (that.settings.throwOnMissingTarget) {
throw "simple-expand: Targets not found";
}
return this;
}
that.setInitialState(expander, targets);
// hook the click on the expander
expander.click(function () {
return that.toggle(expander, targets);
});
});
};
}
// export SimpleExpand
window.SimpleExpand = SimpleExpand;
// expose SimpleExpand as a jQuery plugin
$.fn.simpleexpand = function (options) {
var instance = new SimpleExpand();
instance.activate(this, options);
return this;
};
}(jQuery));