4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * Control collection, this class contains control instances and it enables you to
13 * perform actions on all the contained items. This is very similar to how jQuery works.
16 * someCollection.show().disabled(true);
18 * @class tinymce.ui.Collection
20 define("tinymce/ui/Collection", [
22 "tinymce/ui/Selector",
24 ], function(Tools, Selector, Class) {
27 var Collection, proto, push = Array.prototype.push, slice = Array.prototype.slice;
31 * Current number of contained control instances.
39 * Constructor for the collection.
43 * @param {Array} items Optional array with items to add.
45 init: function(items) {
52 * Adds new items to the control collection.
55 * @param {Array} items Array if items to add to collection.
56 * @return {tinymce.ui.Collection} Current collection instance.
58 add: function(items) {
61 // Force single item into array
62 if (!Tools.isArray(items)) {
63 if (items instanceof Collection) {
64 self.add(items.toArray());
66 push.call(self, items);
69 push.apply(self, items);
76 * Sets the contents of the collection. This will remove any existing items
77 * and replace them with the ones specified in the input array.
80 * @param {Array} items Array with items to set into the Collection.
81 * @return {tinymce.ui.Collection} Collection instance.
83 set: function(items) {
84 var self = this, len = self.length, i;
90 for (i = self.length; i < len; i++) {
98 * Filters the collection item based on the specified selector expression or selector function.
101 * @param {String} selector Selector expression to filter items by.
102 * @return {tinymce.ui.Collection} Collection containing the filtered items.
104 filter: function(selector) {
105 var self = this, i, l, matches = [], item, match;
107 // Compile string into selector expression
108 if (typeof(selector) === "string") {
109 selector = new Selector(selector);
111 match = function(item) {
112 return selector.match(item);
115 // Use selector as matching function
119 for (i = 0, l = self.length; i < l; i++) {
127 return new Collection(matches);
131 * Slices the items within the collection.
134 * @param {Number} index Index to slice at.
135 * @param {Number} len Optional length to slice.
136 * @return {tinymce.ui.Collection} Current collection.
139 return new Collection(slice.apply(this, arguments));
143 * Makes the current collection equal to the specified index.
146 * @param {Number} index Index of the item to set the collection to.
147 * @return {tinymce.ui.Collection} Current collection.
149 eq: function(index) {
150 return index === -1 ? this.slice(index) : this.slice(index, +index + 1);
154 * Executes the specified callback on each item in collection.
157 * @param {function} callback Callback to execute for each item in collection.
158 * @return {tinymce.ui.Collection} Current collection instance.
160 each: function(callback) {
161 Tools.each(this, callback);
167 * Returns an JavaScript array object of the contents inside the collection.
170 * @return {Array} Array with all items from collection.
172 toArray: function() {
173 return Tools.toArray(this);
177 * Finds the index of the specified control or return -1 if it isn't in the collection.
180 * @param {Control} ctrl Control instance to look for.
181 * @return {Number} Index of the specified control or -1.
183 indexOf: function(ctrl) {
184 var self = this, i = self.length;
187 if (self[i] === ctrl) {
196 * Returns a new collection of the contents in reverse order.
199 * @return {tinymce.ui.Collection} Collection instance with reversed items.
201 reverse: function() {
202 return new Collection(Tools.toArray(this).reverse());
206 * Returns true/false if the class exists or not.
209 * @param {String} cls Class to check for.
210 * @return {Boolean} true/false state if the class exists or not.
212 hasClass: function(cls) {
213 return this[0] ? this[0].hasClass(cls) : false;
217 * Sets/gets the specific property on the items in the collection. The same as executing control.<property>(<value>);
220 * @param {String} name Property name to get/set.
221 * @param {Object} value Optional object value to set.
222 * @return {tinymce.ui.Collection} Current collection instance or value of the first item on a get operation.
224 prop: function(name, value) {
225 var self = this, undef, item;
227 if (value !== undef) {
228 self.each(function(item) {
239 if (item && item[name]) {
245 * Executes the specific function name with optional arguments an all items in collection if it exists.
247 * @example collection.exec("myMethod", arg1, arg2, arg3);
249 * @param {String} name Name of the function to execute.
250 * @param {Object} ... Multiple arguments to pass to each function.
251 * @return {tinymce.ui.Collection} Current collection.
253 exec: function(name) {
254 var self = this, args = Tools.toArray(arguments).slice(1);
256 self.each(function(item) {
258 item[name].apply(item, args);
266 * Remove all items from collection and DOM.
269 * @return {tinymce.ui.Collection} Current collection.
282 * Fires the specified event by name and arguments on the control. This will execute all
283 * bound event handlers.
286 * @param {String} name Name of the event to fire.
287 * @param {Object} args Optional arguments to pass to the event.
288 * @return {tinymce.ui.Collection} Current collection instance.
290 // fire: function(event, args) {}, -- Generated by code below
293 * Binds a callback to the specified event. This event can both be
294 * native browser events like "click" or custom ones like PostRender.
296 * The callback function will have two parameters the first one being the control that received the event
297 * the second one will be the event object either the browsers native event object or a custom JS object.
300 * @param {String} name Name of the event to bind. For example "click".
301 * @param {String/function} callback Callback function to execute ones the event occurs.
302 * @return {tinymce.ui.Collection} Current collection instance.
304 // on: function(name, callback) {}, -- Generated by code below
307 * Unbinds the specified event and optionally a specific callback. If you omit the name
308 * parameter all event handlers will be removed. If you omit the callback all event handles
309 * by the specified name will be removed.
312 * @param {String} name Optional name for the event to unbind.
313 * @param {function} callback Optional callback function to unbind.
314 * @return {tinymce.ui.Collection} Current collection instance.
316 // off: function(name, callback) {}, -- Generated by code below
319 * Shows the items in the current collection.
322 * @return {tinymce.ui.Collection} Current collection instance.
324 // show: function() {}, -- Generated by code below
327 * Hides the items in the current collection.
330 * @return {tinymce.ui.Collection} Current collection instance.
332 // hide: function() {}, -- Generated by code below
335 * Sets/gets the text contents of the items in the current collection.
338 * @return {tinymce.ui.Collection} Current collection instance or text value of the first item on a get operation.
340 // text: function(value) {}, -- Generated by code below
343 * Sets/gets the name contents of the items in the current collection.
346 * @return {tinymce.ui.Collection} Current collection instance or name value of the first item on a get operation.
348 // name: function(value) {}, -- Generated by code below
351 * Sets/gets the disabled state on the items in the current collection.
354 * @return {tinymce.ui.Collection} Current collection instance or disabled state of the first item on a get operation.
356 // disabled: function(state) {}, -- Generated by code below
359 * Sets/gets the active state on the items in the current collection.
362 * @return {tinymce.ui.Collection} Current collection instance or active state of the first item on a get operation.
364 // active: function(state) {}, -- Generated by code below
367 * Sets/gets the selected state on the items in the current collection.
370 * @return {tinymce.ui.Collection} Current collection instance or selected state of the first item on a get operation.
372 // selected: function(state) {}, -- Generated by code below
375 * Sets/gets the selected state on the items in the current collection.
378 * @return {tinymce.ui.Collection} Current collection instance or visible state of the first item on a get operation.
380 // visible: function(state) {}, -- Generated by code below
383 * Adds a class to all items in the collection.
386 * @param {String} cls Class to add to each item.
387 * @return {tinymce.ui.Collection} Current collection instance.
389 // addClass: function(cls) {}, -- Generated by code below
392 * Removes the specified class from all items in collection.
394 * @method removeClass
395 * @param {String} cls Class to remove from each item.
396 * @return {tinymce.ui.Collection} Current collection instance.
398 // removeClass: function(cls) {}, -- Generated by code below
401 // Extend tinymce.ui.Collection prototype with some generated control specific methods
402 Tools.each('fire on off show hide addClass removeClass append prepend before after reflow'.split(' '), function(name) {
403 proto[name] = function() {
404 var args = Tools.toArray(arguments);
406 this.each(function(ctrl) {
408 ctrl[name].apply(ctrl, args);
416 // Extend tinymce.ui.Collection prototype with some property methods
417 Tools.each('text name disabled active selected checked visible parent value data'.split(' '), function(name) {
418 proto[name] = function(value) {
419 return this.prop(name, value);
423 // Create class based on the new prototype
424 Collection = Class.extend(proto);
426 // Stick Collection into Selector to prevent circual references
427 Selector.Collection = Collection;