-
Notifications
You must be signed in to change notification settings - Fork 1
/
symphony.stage.js
437 lines (332 loc) · 12.1 KB
/
symphony.stage.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* STAGE
* for Symphony
*
* @author: Nils Hörrmann, http://nilshoerrmann.de
* @source: http://github.com/nilshoerrmann/stage
*/
/*-----------------------------------------------------------------------------
Language strings
-----------------------------------------------------------------------------*/
Symphony.Language.add({
'Browse': false,
'Create New': false,
'Remove Item': false,
'There are currently no items available. Perhaps you want create one first?': false,
'Click here to create a new item.': false,
'Load items': false,
'No items found.': false
});
/*-----------------------------------------------------------------------------
Stage plugin
-----------------------------------------------------------------------------*/
jQuery.fn.symphonyStage = function(custom_settings) {
// Get objects
var objects = this;
// Get settings
var settings = {
items: 'li:not(.template):not(.empty)', //
source: false, // A stage source, e. g. a select box
queue: 'div.queue input', // Handle for queue
queue_ajax: false, // AJAX options for queue
draggable: true, // Can items be dragged?
droppable: false, // Can items be dropped?
dragclick: jQuery.noop(), // Click function for draggable items
constructable: true, // Allow construction of new instances?
destructable: true, // Allow destruction of instances?
searchable: true, // Allow searching of queue?
minimum: 0, // Do not allow instances to be removed below this limit.
maximum: 1000, // Do not allow instances to be added above this limit.
speed: 'fast', // Control the speed of any animations
queue_speed: 'normal',
delay_initialize: false
};
jQuery.extend(settings, custom_settings);
/*-------------------------------------------------------------------------
Draggable
-------------------------------------------------------------------------*/
if(settings.draggable) objects.find('ul.selection').symphonyDraggable({
handles: false,
click: settings.dragclick
});
/*-------------------------------------------------------------------------
Stage
-------------------------------------------------------------------------*/
objects = objects.map(function() {
// This object
var object = this;
// Construct a new item
var construct = function(item) {
object.trigger('constructstart');
var value = item.attr('value');
// Remove empty selection message
object.find('ul.selection li.empty').slideUp('fast');
// Add queue selection
var queue_item = object.find('div.queue li[value=' + value + ']');
queue_item.addClass('selected');
// Add stage selection
var stage_item = queue_item.clone().removeClass('selected').hide();
stage_item.insertBefore(object.find('ul.selection li.empty')).slideDown(settings.speed);
object.stage.addDestructor(stage_item);
// Prevent clicks on layout anchors
object.find('a.file, a.image').click(function(event) {
event.preventDefault();
});
// Add source selection
if(settings.source) {
sync(item);
}
object.trigger('constructstop');
};
// Destruct an item
var destruct = function(item) {
object.trigger('destructstart', item);
var value = item.attr('value');
// Remove stage selection
object.find('ul.selection li[value=' + value + ']').slideUp(settings.speed, function() {
jQuery(this).remove();
});
// Remove queue selection
object.find('div.queue li[value=' + value + ']').removeClass('selected');
// Remove source selection
if(settings.source) {
sync(item, 'destruct');
}
// Add empty selection message
var selection = object.find('ul.selection').find(settings.items);
if(selection.size() <= 1) {
object.find('ul.selection li.empty').slideDown(settings.speed);
}
object.trigger('destructstop');
};
// Synchronize source list
var sync = function(item, action) {
var source = jQuery(settings.source);
var id = item.attr('value');
var selection = source.find('option[value=' + id + ']');
// Destruct item
if(action == 'destruct') {
// Item exists in source list
if(selection.size() > 0) {
source.find('option[value=' + id + ']').removeAttr('selected');
}
// Item does not exists in source list
else {
jQuery('<option value="' + id + '">New Item</option>').appendTo(source);
}
}
// Construct item
else {
// Item exists in source list
if(selection.size() > 0) {
source.find('option[value=' + id + ']').attr('selected', 'selected');
}
// Item does not exists in source list
else {
jQuery('<option value="' + id + '" selected="selected">New Item</option>').appendTo(source);
}
}
// Make sure source is enabled
if(source.find('option').size() > 0) {
source.removeAttr('disabled');
}
}
// Make selection clickable
var select = function(item) {
var item = jQuery(item);
// Deselect
if(item.hasClass('selected')) {
if(settings.destructable) {
destruct(item);
}
}
// Select
else {
// Store old value
var old_value = settings.source.val();
// Construct new item
construct(item);
// Single selects
if(!settings.source.attr('multiple') && old_value != 0) {
var old = object.find('ul.selection li[value=' + old_value + ']');
destruct(old);
}
}
}
/*-------------------------------------------------------------------*/
if(object instanceof jQuery === false) {
object = jQuery(object);
}
object.stage = {
initialize: function() {
var items = object.find(settings.items);
if(items.size() > 0) {
object.find('ul.selection li.empty').hide();
}
// Add queue
if(settings.searchable || settings.constructable) {
var queue = jQuery('<div class="queue" />');
if(settings.searchable) jQuery('<input type="search" placeholder="' + Symphony.Language.get('Browse') + ' …" class="browser" value="" />').appendTo(queue);
if(settings.constructable) jQuery('<button class="create">' + Symphony.Language.get('Create New') + '</button>').appendTo(queue);
object.find('ul:first').after(queue);
}
// Prevent clicks on layout anchors
object.find('a.file, a.image').click(function(event) {
event.preventDefault();
});
// Add destructors
object.stage.addDestructor(items);
// Open queue on click
object.find(settings.queue).bind('click', function(event) {
event.preventDefault();
event.stopPropagation();
object.stage.showQueue();
});
// Search
if(settings.searchable) {
object.find('div.queue .browser').bind('click keyup', object.stage.search);
}
// Events
object.bind('construct', function(event, item) {
construct(item);
});
object.bind('destruct', function(event, item) {
destruct(item);
});
object.bind('sync', function(event, item) {
sync(item);
});
object.find('ul.queue li:not(.message)').live('click', function(event) {
event.preventDefault();
event.stopPropagation();
select(event.currentTarget);
});
},
addDestructor: function(items) {
if(settings.destructable) {
jQuery('<a class="destructor">' + Symphony.Language.get('Remove Item') + '</a>').appendTo(items).click(function(event) {
var item = jQuery(event.target).parent('li');
destruct(item);
});
}
},
showQueue: function(event) {
var queue = object.find('div.queue');
// Append queue if it's not present yet
if(queue.find('ul li').size() == 0) {
// Append queue
queue.find('ul').remove();
var list = jQuery('<ul class="queue"></ul>').css('min-height', 50).appendTo(queue).slideDown('fast');
// Get queue content
if(settings.queue_ajax) {
jQuery.ajax(jQuery.extend({
async: false,
type: 'GET',
dataType: 'html',
success: function(result) {
list.find('li.loading').remove();
if(result != '') {
list.append(jQuery(result));
// Highlight items, add events
list.find('li').each(function(index, element) {
element = jQuery(element);
// Odd
if(index % 2 != 0) element.addClass('odd')
// Selected
var value = jQuery(element).attr('value');
if(object.find('ul:first li[value=' + value + ']').size() > 0) element.addClass('selected');
// Prevent clicks on layout anchors
element.find('a.file, a.image').click(function(event) {
event.preventDefault();
});
});
// Slide queue
list.slideDown(settings.queue_speed);
}
}
}, settings.queue_ajax));
}
// Empty queue information
if(queue.find('li').size() == 0) {
list.append(jQuery('<li class="message"><span>' + Symphony.Language.get('There are currently no items available. Perhaps you want create one first?') + ' <a class="create">' + Symphony.Language.get('Click here to create a new item.') + '</a></span></li>')).slideDown(settings.queue_speed);
}
// Reset minimum height
list.css('min-height', 0);
}
// Slide queue
else {
queue.find('ul').slideDown(settings.queue_speed);
}
// Automatically hide queue later
jQuery('body').bind('click', function(event) {
object.stage.hideQueue();
jQuery('body').unbind('click');
});
},
hideQueue: function() {
var queue = object.find('div.queue');
if(queue.find('ul').size() > 0) {
queue.find('ul').slideUp(settings.queue_speed);
queue.find('.browser').val('');
}
},
search: function(event) {
var search = jQuery.trim(jQuery(event.target).val()).toLowerCase().split(' ');
// Build search index
if(!this.search_index) {
this.search_index = object.find('div.queue li').map(function() {
return this.textContent.toLowerCase();
});
}
// Searching
var items = object.find('div.queue ul li');
if(search.length > 0 && search[0] != '') {
this.search_index.each(function(index, content) {
var found = true;
var item = items.filter(':nth(' + index + ')');
// Items have to match all search strings
jQuery.each(search, function(index, string) {
if(content.search(string) == -1) found = false;
});
// Show matching items
if(found) {
item.addClass('found').slideDown(settings.speed);
item.parent().find('li.none').slideUp(settings.speed, function(event) {
jQuery(this).remove();
});
}
// Hide other items
else {
item.slideUp(settings.speed);
}
});
// Found items
var found = items.removeClass('odd').filter('.found');
// None found
if(found.size() == 0 && items.parent().find('li.none').size() == 0) {
var none = jQuery('<li class="none"><span>' + Symphony.Language.get('No items found.') + '</span></li>');
items.parent().append(none).slideDown(settings.speed);
}
// Reset zebra style
found.each(function(index, item) {
item = jQuery(item).removeClass('found');
if(index % 2 != 0) item.addClass('odd');
});
}
// Not searching
else {
items.removeClass('odd').filter(':odd').addClass('odd');
items.slideDown(settings.speed);
items.parent().find('li.none').slideUp(settings.speed, function(event) {
jQuery(this).remove();
});
}
}
};
if(settings.delay_initialize !== true) {
object.stage.initialize();
}
return object;
});
return objects;
};