/
symphony.collapsible.js
307 lines (258 loc) · 9.46 KB
/
symphony.collapsible.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
/**
* @package assets
*/
(function($, Symphony) {
// Saves the value into the local storage at the specified storage key.
var save = function (storage, value) {
// Put in a try/catch in case something goes wrong (no space, privileges etc)
// Always put try/catches into their own function to prevent callers from
// going into un-optimized hell
try {
window.localStorage[storage] = value;
}
catch(e) {
window.onerror(e.message);
}
};
/**
* Create collapsible elements.
*
* @name $.symphonyCollapsible
* @class
*
* @param {Object} options An object specifying containing the attributes specified below
* @param {String} [options.items='.instance'] Selector to find collapsible items within the container
* @param {String} [options.handles='.header:first'] Selector to find clickable handles to trigger interaction
* @param {String} [options.content='.content'] Selector to find hideable content area
* @param {Boolean} [options.save_state=true] Stores states of instances using local storage
* @param {String} [options.storage='symphony.collapsible.area.page.id'] Namespace used for local storage
* @param {Integer} [options.delay=250] Time delay for animations
*
* @example
var collapsible = $('#duplicator').symphonyCollapsible({
items: '.instance',
handles: '.header span'
});
collapsible.collapseAll();
*/
$.fn.symphonyCollapsible = function(options) {
var objects = this,
settings = {
items: '.instance',
handles: '.frame-header',
content: '.content',
ignore: '.ignore',
save_state: true,
storage: 'symphony.collapsible.' + Symphony.Context.get('context-id'),
delay: 250
};
$.extend(settings, options);
/*-----------------------------------------------------------------------*/
objects.each(function collapsible(index) {
var object = $(this),
storage = settings.storage + '.' + index + '.collapsed';
var getDuration = function (duration) {
return $.isNumeric(duration) ? duration : settings.delay;
};
/*---------------------------------------------------------------------
Events
---------------------------------------------------------------------*/
var collapseItem = function collapse(item, duration) {
var heightMin = 0;
// Customization point
item.trigger('collapsebefore.collapsible', settings);
heightMin = item.data('heightMin');
// Check duration
if(duration !== 0) {
item.addClass('js-animate');
item.trigger('collapsestart.collapsible');
}
// Collapse item
item.addClass('collapsed');
item.css('max-height', heightMin);
if(duration !== 0) {
setTimeout(function() {
item.trigger('animationend.collapsible');
item.trigger('animationend.duplicator');
}, duration);
}
};
// Collapse item
object.on('collapse.collapsible', settings.items, function collapse(event, duration) {
var item = $(this);
collapseItem(item, getDuration(duration));
});
// Collapse all items
object.on('collapseall.collapsible', function collapseAll(event) {
var items = object.find(settings.items + ':not(.collapsed)'),
visibles = Symphony.Utilities.inSight(items),
invisibles = $(),
scrollTop = $(window).scrollTop(),
visibleIndex = visibles.eq(0).index(),
visibleCollapsedHeight = 0;
// Find items that will be visible after collapse
while (visibleIndex < items.length && visibleCollapsedHeight < window.innerHeight) {
var currentItem = items.eq(visibleIndex);
visibles = visibles.add(currentItem);
visibleCollapsedHeight += currentItem.data('heightMin');
visibleIndex++;
}
visibles.each(function () { collapseItem($(this), settings.delay); });
setTimeout(function collapseAllInvisibleEnd() {
var first = visibles.eq(0);
var firstOffset = first.offset().top;
// update invisible accordingly
invisibles = items.not(visibles);
invisibles.each(function () { collapseItem($(this), 0); });
if (firstOffset > 0 && scrollTop > object.offset().top) {
// scroll back to where we were,
// which is last scroll position + delta of first visible item
$(window).scrollTop(scrollTop + (first.offset().top - firstOffset));
}
invisibles.trigger('animationend.collapsible');
}, settings.delay + 100);
});
// Expand item
var expandItem = function (item, duration) {
var heightMax = 0;
// Customization point
item.trigger('expandbefore.collapsible', settings);
heightMax = item.data('heightMax');
// Check duration
if(duration !== 0) {
item.addClass('js-animate');
item.trigger('expandstart.collapsible');
}
// Collapse item
item.removeClass('collapsed');
item.css('max-height', heightMax);
if(duration !== 0) {
setTimeout(function() {
item.trigger('animationend.collapsible');
}, duration);
}
};
object.on('expand.collapsible', settings.items, function expand(event, duration) {
var item = $(this);
expandItem(item, getDuration(duration));
});
// Expand all items
object.on('expandall.collapsible', function expandAll(event) {
var items = object.find(settings.items + '.collapsed'),
visibles = Symphony.Utilities.inSight(items).filter('*:lt(4)'),
invisibles = items.not(visibles),
scrollTop = $(window).scrollTop();
visibles.addClass('js-animate-all'); // prevent focus
visibles.each(function () { expandItem($(this), settings.delay); });
setTimeout(function expandAllInvisible() {
var first = visibles.eq(0);
var firstOffset = first.offset().top;
invisibles.addClass('js-animate-all'); // prevent focus
invisibles.each(function () { expandItem($(this), 0); });
invisibles.trigger('animationend.collapsible');
// if we are past the first item
if (firstOffset > 0 && scrollTop > object.offset().top) {
// scroll back to where we were,
// which is last scroll position + delta of first visible item
$(window).scrollTop(scrollTop + (first.offset().top - firstOffset));
}
}, settings.delay + 100);
});
// Finish animations
object.on('animationend.collapsible', settings.items, function finish(event) {
var item = $(this);
// Trigger events
if(item.is('.collapsed')) {
item.trigger('collapsestop.collapsible');
}
else {
item.trigger('expandstop.collapsible');
}
// clean up
item.removeClass('js-animate js-animate-all');
});
// Toggle single item
object.on('click.collapsible', settings.handles, function toggle(event) {
var handle = $(this),
item = handle.closest(settings.items);
if(!handle.is(settings.ignore) && !$(event.target).is(settings.ignore) && !item.is('.locked')) {
// Expand
if(item.is('.collapsed')) {
expandItem(item, settings.delay);
}
// Collapse
else {
collapseItem(item, settings.delay);
}
}
});
// Save states
var saveTimer = 0;
object.on('collapsestop.collapsible expandstop.collapsible store.collapsible', settings.items, function saveState(event) {
if(settings.save_state === true && Symphony.Support.localStorage === true) {
// save it to local storage, delayed, once
clearTimeout(saveTimer);
saveTimer = setTimeout(function () {
var collapsed = object.find(settings.items).map(function(index) {
if($(this).is('.collapsed')) {
return index;
};
}).get().join(',');
save(storage, collapsed);
}, settings.delay);
}
});
// Restore states
object.on('restore.collapsible', function restoreState(event) {
if(settings.save_state === true && Symphony.Support.localStorage === true && window.localStorage[storage]) {
$.each(window.localStorage[storage].split(','), function(index, value) {
var collapsed = object.find(settings.items).eq(value);
if(collapsed.has('.invalid').length == 0) {
collapseItem(collapsed, 0);
}
});
}
});
// Refresh state storage after ordering
object.on('orderstop.orderable', function refreshOrderedState(event) {
object.find(settings.items).trigger('store.collapsible');
});
// Refresh state storage after deleting and instance
object.on('destructstop.duplicator', settings.items, function refreshState() {
$(this).trigger('store.collapsible');
});
// Update sizes
object.on('updatesize.collapsible', settings.items, function updateSizes() {
var item = $(this),
min = item.find(settings.handles).outerHeight(true),
max = min + item.find(settings.content).outerHeight(true);
item.data('heightMin', min);
item.data('heightMax', max);
});
// Set sizes
object.on('setsize.collapsible', settings.items, function setSizes() {
var item = $(this);
var heightMin = item.data('heightMin');
var heightMax = item.data('heightMax');
item.css({
'min-height': heightMin,
'max-height': heightMax
});
});
/*---------------------------------------------------------------------
Initialisation
---------------------------------------------------------------------*/
// Prepare interface
object.addClass('collapsible').find(settings.items).each(function() {
var item = $(this);
item.addClass('instance');
item.trigger('updatesize.collapsible');
item.trigger('setsize.collapsible');
});
// Restore states
object.trigger('restore.collapsible');
});
/*-----------------------------------------------------------------------*/
return objects;
};
})(window.jQuery, window.Symphony);