Collection.js
11.7 KB
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
438
/**
* Collection.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* Control collection, this class contains control instances and it enables you to
* perform actions on all the contained items. This is very similar to how jQuery works.
*
* @example
* someCollection.show().disabled(true);
*
* @class tinymce.ui.Collection
*/
define("tinymce/ui/Collection", [
"tinymce/util/Tools",
"tinymce/ui/Selector",
"tinymce/util/Class"
], function(Tools, Selector, Class) {
"use strict";
var Collection, proto, push = Array.prototype.push, slice = Array.prototype.slice;
proto = {
/**
* Current number of contained control instances.
*
* @field length
* @type Number
*/
length: 0,
/**
* Constructor for the collection.
*
* @constructor
* @method init
* @param {Array} items Optional array with items to add.
*/
init: function(items) {
if (items) {
this.add(items);
}
},
/**
* Adds new items to the control collection.
*
* @method add
* @param {Array} items Array if items to add to collection.
* @return {tinymce.ui.Collection} Current collection instance.
*/
add: function(items) {
var self = this;
// Force single item into array
if (!Tools.isArray(items)) {
if (items instanceof Collection) {
self.add(items.toArray());
} else {
push.call(self, items);
}
} else {
push.apply(self, items);
}
return self;
},
/**
* Sets the contents of the collection. This will remove any existing items
* and replace them with the ones specified in the input array.
*
* @method set
* @param {Array} items Array with items to set into the Collection.
* @return {tinymce.ui.Collection} Collection instance.
*/
set: function(items) {
var self = this, len = self.length, i;
self.length = 0;
self.add(items);
// Remove old entries
for (i = self.length; i < len; i++) {
delete self[i];
}
return self;
},
/**
* Filters the collection item based on the specified selector expression or selector function.
*
* @method filter
* @param {String} selector Selector expression to filter items by.
* @return {tinymce.ui.Collection} Collection containing the filtered items.
*/
filter: function(selector) {
var self = this, i, l, matches = [], item, match;
// Compile string into selector expression
if (typeof selector === "string") {
selector = new Selector(selector);
match = function(item) {
return selector.match(item);
};
} else {
// Use selector as matching function
match = selector;
}
for (i = 0, l = self.length; i < l; i++) {
item = self[i];
if (match(item)) {
matches.push(item);
}
}
return new Collection(matches);
},
/**
* Slices the items within the collection.
*
* @method slice
* @param {Number} index Index to slice at.
* @param {Number} len Optional length to slice.
* @return {tinymce.ui.Collection} Current collection.
*/
slice: function() {
return new Collection(slice.apply(this, arguments));
},
/**
* Makes the current collection equal to the specified index.
*
* @method eq
* @param {Number} index Index of the item to set the collection to.
* @return {tinymce.ui.Collection} Current collection.
*/
eq: function(index) {
return index === -1 ? this.slice(index) : this.slice(index, +index + 1);
},
/**
* Executes the specified callback on each item in collection.
*
* @method each
* @param {function} callback Callback to execute for each item in collection.
* @return {tinymce.ui.Collection} Current collection instance.
*/
each: function(callback) {
Tools.each(this, callback);
return this;
},
/**
* Returns an JavaScript array object of the contents inside the collection.
*
* @method toArray
* @return {Array} Array with all items from collection.
*/
toArray: function() {
return Tools.toArray(this);
},
/**
* Finds the index of the specified control or return -1 if it isn't in the collection.
*
* @method indexOf
* @param {Control} ctrl Control instance to look for.
* @return {Number} Index of the specified control or -1.
*/
indexOf: function(ctrl) {
var self = this, i = self.length;
while (i--) {
if (self[i] === ctrl) {
break;
}
}
return i;
},
/**
* Returns a new collection of the contents in reverse order.
*
* @method reverse
* @return {tinymce.ui.Collection} Collection instance with reversed items.
*/
reverse: function() {
return new Collection(Tools.toArray(this).reverse());
},
/**
* Returns true/false if the class exists or not.
*
* @method hasClass
* @param {String} cls Class to check for.
* @return {Boolean} true/false state if the class exists or not.
*/
hasClass: function(cls) {
return this[0] ? this[0].classes.contains(cls) : false;
},
/**
* Sets/gets the specific property on the items in the collection. The same as executing control.<property>(<value>);
*
* @method prop
* @param {String} name Property name to get/set.
* @param {Object} value Optional object value to set.
* @return {tinymce.ui.Collection} Current collection instance or value of the first item on a get operation.
*/
prop: function(name, value) {
var self = this, undef, item;
if (value !== undef) {
self.each(function(item) {
if (item[name]) {
item[name](value);
}
});
return self;
}
item = self[0];
if (item && item[name]) {
return item[name]();
}
},
/**
* Executes the specific function name with optional arguments an all items in collection if it exists.
*
* @example collection.exec("myMethod", arg1, arg2, arg3);
* @method exec
* @param {String} name Name of the function to execute.
* @param {Object} ... Multiple arguments to pass to each function.
* @return {tinymce.ui.Collection} Current collection.
*/
exec: function(name) {
var self = this, args = Tools.toArray(arguments).slice(1);
self.each(function(item) {
if (item[name]) {
item[name].apply(item, args);
}
});
return self;
},
/**
* Remove all items from collection and DOM.
*
* @method remove
* @return {tinymce.ui.Collection} Current collection.
*/
remove: function() {
var i = this.length;
while (i--) {
this[i].remove();
}
return this;
},
/**
* Adds a class to all items in the collection.
*
* @method addClass
* @param {String} cls Class to add to each item.
* @return {tinymce.ui.Collection} Current collection instance.
*/
addClass: function(cls) {
return this.each(function(item) {
item.classes.add(cls);
});
},
/**
* Removes the specified class from all items in collection.
*
* @method removeClass
* @param {String} cls Class to remove from each item.
* @return {tinymce.ui.Collection} Current collection instance.
*/
removeClass: function(cls) {
return this.each(function(item) {
item.classes.remove(cls);
});
}
/**
* Fires the specified event by name and arguments on the control. This will execute all
* bound event handlers.
*
* @method fire
* @param {String} name Name of the event to fire.
* @param {Object} args Optional arguments to pass to the event.
* @return {tinymce.ui.Collection} Current collection instance.
*/
// fire: function(event, args) {}, -- Generated by code below
/**
* Binds a callback to the specified event. This event can both be
* native browser events like "click" or custom ones like PostRender.
*
* The callback function will have two parameters the first one being the control that received the event
* the second one will be the event object either the browsers native event object or a custom JS object.
*
* @method on
* @param {String} name Name of the event to bind. For example "click".
* @param {String/function} callback Callback function to execute ones the event occurs.
* @return {tinymce.ui.Collection} Current collection instance.
*/
// on: function(name, callback) {}, -- Generated by code below
/**
* Unbinds the specified event and optionally a specific callback. If you omit the name
* parameter all event handlers will be removed. If you omit the callback all event handles
* by the specified name will be removed.
*
* @method off
* @param {String} name Optional name for the event to unbind.
* @param {function} callback Optional callback function to unbind.
* @return {tinymce.ui.Collection} Current collection instance.
*/
// off: function(name, callback) {}, -- Generated by code below
/**
* Shows the items in the current collection.
*
* @method show
* @return {tinymce.ui.Collection} Current collection instance.
*/
// show: function() {}, -- Generated by code below
/**
* Hides the items in the current collection.
*
* @method hide
* @return {tinymce.ui.Collection} Current collection instance.
*/
// hide: function() {}, -- Generated by code below
/**
* Sets/gets the text contents of the items in the current collection.
*
* @method text
* @return {tinymce.ui.Collection} Current collection instance or text value of the first item on a get operation.
*/
// text: function(value) {}, -- Generated by code below
/**
* Sets/gets the name contents of the items in the current collection.
*
* @method name
* @return {tinymce.ui.Collection} Current collection instance or name value of the first item on a get operation.
*/
// name: function(value) {}, -- Generated by code below
/**
* Sets/gets the disabled state on the items in the current collection.
*
* @method disabled
* @return {tinymce.ui.Collection} Current collection instance or disabled state of the first item on a get operation.
*/
// disabled: function(state) {}, -- Generated by code below
/**
* Sets/gets the active state on the items in the current collection.
*
* @method active
* @return {tinymce.ui.Collection} Current collection instance or active state of the first item on a get operation.
*/
// active: function(state) {}, -- Generated by code below
/**
* Sets/gets the selected state on the items in the current collection.
*
* @method selected
* @return {tinymce.ui.Collection} Current collection instance or selected state of the first item on a get operation.
*/
// selected: function(state) {}, -- Generated by code below
/**
* Sets/gets the selected state on the items in the current collection.
*
* @method visible
* @return {tinymce.ui.Collection} Current collection instance or visible state of the first item on a get operation.
*/
// visible: function(state) {}, -- Generated by code below
};
// Extend tinymce.ui.Collection prototype with some generated control specific methods
Tools.each('fire on off show hide append prepend before after reflow'.split(' '), function(name) {
proto[name] = function() {
var args = Tools.toArray(arguments);
this.each(function(ctrl) {
if (name in ctrl) {
ctrl[name].apply(ctrl, args);
}
});
return this;
};
});
// Extend tinymce.ui.Collection prototype with some property methods
Tools.each('text name disabled active selected checked visible parent value data'.split(' '), function(name) {
proto[name] = function(value) {
return this.prop(name, value);
};
});
// Create class based on the new prototype
Collection = Class.extend(proto);
// Stick Collection into Selector to prevent circual references
Selector.Collection = Collection;
return Collection;
});