/
symphony.duplicator.js
345 lines (283 loc) · 10.2 KB
/
symphony.duplicator.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* @package assets
*/
(function($, Symphony) {
/**
* Duplicators are advanced lists used throughout the
* Symphony backend to manage repeatable content.
*
* @name $.symphonyDuplicator
* @class
*
* @param {Object} options An object specifying containing the attributes specified below
* @param {String} [options.instances='> li:not(.template)'] Selector to find children to use as instances
* @param {String} [options.templates='> li.template'] Selector to find children to use as templates
* @param {String} [options.headers='> :first-child'] Selector to find the header part of each instance
* @param {String} [options.perselect=false] Default option for the selector
* @param {Boolean} [options.orderable=false] Can instances be ordered
* @param {Boolean} [options.collapsible=false] Can instances be collapsed
* @param {Boolean} [options.constructable=true] Allow construction of new instances
* @param {Boolean} [options.destructable=true] Allow destruction of instances
* @param {Integer} [optionss.minimum=0] Do not allow instances to be removed below this limit
* @param {Integer} [options.maximum=1000] Do not allow instances to be added above this limit,
* @param {Integer} [options.delay=250'] Time delay for animations
*
* @example
$('.duplicator').symphonyDuplicator({
orderable: true,
collapsible: true
});
*/
$.fn.symphonyDuplicator = function(options) {
var objects = this,
settings = {
instances: '> li:not(.template)',
templates: '> li.template',
headers: '> :first-child',
preselect: false,
orderable: false,
collapsible: false,
constructable: true,
destructable: true,
save_state: true,
minimum: 0,
maximum: 1000,
delay: 250
};
$.extend(settings, options);
/*-----------------------------------------------------------------------*/
// Language strings
Symphony.Language.add({
'Add item': false,
'Remove item': false
});
/*-----------------------------------------------------------------------*/
objects.each(function duplicators() {
var duplicator = $(this),
list = duplicator.find('> ol'),
apply = $('<fieldset class="apply" />'),
selector = $('<select />'),
constructor = $('<button type="button" class="constructor" />'),
instances, templates, items, headers;
// Initialise duplicator components
duplicator.addClass('duplicator').addClass('empty');
instances = list.find(settings.instances).addClass('instance');
templates = list.find(settings.templates).addClass('template');
items = instances.add(templates);
headers = items.find(settings.headers).addClass('frame-header');
constructor.text(list.attr('data-add') || Symphony.Language.get('Add item'));
/*---------------------------------------------------------------------
Events
---------------------------------------------------------------------*/
// Construct instances
apply.on('click.duplicator', '.constructor:not(.disabled)', function construct(event, speed) {
var instance = templates.filter('[data-type="' + $(this).parent().find('select').val() + '"]').clone(true),
heightMin, heightMax;
event.preventDefault();
// Prepare instance
instance
.trigger('constructstart.duplicator')
.appendTo(list);
// Duplicator is not empty
duplicator.removeClass('empty');
// Show instance
instance
.trigger('constructshow.duplicator');
// Update collapsible sizes
instance.trigger('updatesize.collapsible');
instance.trigger('setsize.collapsible');
setTimeout(function() {
instance.trigger('animationend.duplicator');
}, settings.delay);
});
// Destruct instances
duplicator.on('click.duplicator', '.destructor:not(.disabled)', function destruct(event) {
var instance = $(this).closest('.instance');
// Remove instance
instance
.trigger('collapse.collapsible')
.trigger('destructstart.duplicator')
.addClass('destructed');
setTimeout(function() {
instance.trigger('animationend.duplicator');
}, settings.delay);
});
// Finish animations
duplicator.on('animationend.duplicator', '.instance', function finish() {
var instance = $(this).removeClass('js-animate');
// Trigger events
if(instance.is('.destructed')) {
instance.remove();
// Check if duplicator is empty
if(duplicator.find('.instance').length == 0) {
duplicator.addClass('empty');
}
instance.trigger('destructstop.duplicator');
}
else {
instance.trigger('constructstop.duplicator');
}
// Update collapsible states
if(settings.collapsible) {
instance.trigger('store.collapsible');
}
});
// Lock constructor
duplicator.on('constructstop.duplicator', '.instance', function lockConstructor() {
if(duplicator.find('.instance').length >= settings.maximum) {
constructor.addClass('disabled');
}
});
// Unlock constructor
duplicator.on('destructstart.duplicator', '.instance', function unlockConstructor() {
if(duplicator.find('.instance').length <= settings.maximum) {
constructor.removeClass('disabled');
}
});
// Lock destructor
duplicator.on('destructstart.duplicator', '.instance', function lockDestructor() {
if(duplicator.find('.instance').length - 1 == settings.minimum) {
duplicator.find('a.destructor').addClass('disabled');
}
});
// Unlock destructor
duplicator.on('constructstop.duplicator', '.instance', function unlockDestructor() {
if(duplicator.find('.instance').length > settings.minimum) {
duplicator.find('a.destructor').removeClass('disabled');
}
});
// Lock unique instances
duplicator.on('constructstop.duplicator', '.instance', function lockUnique(event) {
var instance = $(this);
if(instance.is('.unique')) {
selector.find('option[value="' + instance.attr('data-type') + '"]').attr('disabled', true);
// Preselect first available instance
selector.find('option').prop('selected', false).filter(':not(:disabled):first').prop('selected', true);
// All selected
if(selector.find('option:not(:disabled)').length == 0) {
selector.attr('disabled', 'disabled');
}
}
});
// Unlock unique instances
duplicator.on('destructstart.duplicator', '.instance', function unlockUnique(event) {
var instance = $(this),
option;
if(instance.is('.unique')) {
option = selector.attr('disabled', false).find('option[value="' + instance.attr('data-type') + '"]').attr('disabled', false);
// Preselect instance if it's the only active one
if(selector.find('option:not(:disabled)').length == 1) {
option.prop('selected', true);
}
}
});
// Build field indexes
duplicator.on('constructstop.duplicator refresh.duplicator', '.instance', function buildIndexes(event) {
var instance = $(this),
position = duplicator.find('.instance').index(instance);
// Loop over named fields
instance.find('*[name]').each(function() {
var field = $(this),
exp = /\[\-?[0-9]+\]/,
name = field.attr('name');
// Set index
if(exp.test(name)) {
field.attr('name', name.replace(exp, '[' + position + ']'));
}
});
});
// Refresh field indexes
duplicator.on('orderstop.orderable', function refreshIndexes(event) {
duplicator.find('.instance').trigger('refresh.duplicator');
});
/*---------------------------------------------------------------------
Initialisation
---------------------------------------------------------------------*/
// Wrap content, if needed
headers.each(function wrapContent() {
var header = $(this);
if (!header.next('.content').length) {
header.nextAll().wrapAll( $('<div />').attr('class','content') );
}
});
// Constructable interface
if(settings.constructable === true) {
duplicator.addClass('constructable');
apply.append($('<div />').append(selector)).append(constructor);
apply.appendTo(duplicator);
// Populate selector
templates.detach().each(function createTemplates() {
var template = $(this),
title = $.trim(template.find(settings.headers).attr('data-name'))
|| $.trim(template.find(settings.headers).text()),
value = $.trim(template.attr('data-type'));
template.trigger('constructstart.duplicator');
// Check type connection
if(!value) {
value = title;
template.attr('data-type', value);
}
// Append options
$('<option />', {
text: title,
value: value
}).appendTo(selector);
// Check uniqueness
template.trigger('constructstop.duplicator');
}).removeClass('template').addClass('instance');
}
// Select default
if(settings.preselect != false) {
selector.find('option[value="' + settings.preselect + '"]').prop('selected', true);
}
// Single template
if(templates.length <= 1) {
apply.addClass('single');
// Single unique template
if(templates.is('.unique')) {
if(instances.length == 0) {
constructor.trigger('click.duplicator', [0]);
}
apply.hide();
}
}
// Destructable interface
if(settings.destructable === true) {
duplicator.addClass('destructable');
headers.append(
$('<a />')
.attr('class', 'destructor')
.text(list.attr('data-remove') || Symphony.Language.get('Remove item'))
);
}
// Collapsible interface
if(settings.collapsible) {
duplicator.symphonyCollapsible({
items: '.instance',
handles: '.frame-header',
ignore: '.destructor',
save_state: settings.save_state,
delay: settings.delay
});
}
// Orderable interface
if(settings.orderable) {
duplicator.symphonyOrderable({
items: '.instance',
handles: '.frame-header'
});
}
// Catch errors
instances.filter(':has(.invalid)').addClass('conflict');
// Initialise existing instances
instances.trigger('constructstop.duplicator');
instances.find('input[name*="[label]"]').trigger('keyup.duplicator');
// Check for existing instances
if(instances.length > 0) {
duplicator.removeClass('empty');
}
});
/*-----------------------------------------------------------------------*/
return objects;
};
})(window.jQuery, window.Symphony);