Collection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /**
  2. * Collection.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * Control collection, this class contains control instances and it enables you to
  12. * perform actions on all the contained items. This is very similar to how jQuery works.
  13. *
  14. * @example
  15. * someCollection.show().disabled(true);
  16. *
  17. * @class tinymce.ui.Collection
  18. */
  19. define("tinymce/ui/Collection", [
  20. "tinymce/util/Tools",
  21. "tinymce/ui/Selector",
  22. "tinymce/util/Class"
  23. ], function(Tools, Selector, Class) {
  24. "use strict";
  25. var Collection, proto, push = Array.prototype.push, slice = Array.prototype.slice;
  26. proto = {
  27. /**
  28. * Current number of contained control instances.
  29. *
  30. * @field length
  31. * @type Number
  32. */
  33. length: 0,
  34. /**
  35. * Constructor for the collection.
  36. *
  37. * @constructor
  38. * @method init
  39. * @param {Array} items Optional array with items to add.
  40. */
  41. init: function(items) {
  42. if (items) {
  43. this.add(items);
  44. }
  45. },
  46. /**
  47. * Adds new items to the control collection.
  48. *
  49. * @method add
  50. * @param {Array} items Array if items to add to collection.
  51. * @return {tinymce.ui.Collection} Current collection instance.
  52. */
  53. add: function(items) {
  54. var self = this;
  55. // Force single item into array
  56. if (!Tools.isArray(items)) {
  57. if (items instanceof Collection) {
  58. self.add(items.toArray());
  59. } else {
  60. push.call(self, items);
  61. }
  62. } else {
  63. push.apply(self, items);
  64. }
  65. return self;
  66. },
  67. /**
  68. * Sets the contents of the collection. This will remove any existing items
  69. * and replace them with the ones specified in the input array.
  70. *
  71. * @method set
  72. * @param {Array} items Array with items to set into the Collection.
  73. * @return {tinymce.ui.Collection} Collection instance.
  74. */
  75. set: function(items) {
  76. var self = this, len = self.length, i;
  77. self.length = 0;
  78. self.add(items);
  79. // Remove old entries
  80. for (i = self.length; i < len; i++) {
  81. delete self[i];
  82. }
  83. return self;
  84. },
  85. /**
  86. * Filters the collection item based on the specified selector expression or selector function.
  87. *
  88. * @method filter
  89. * @param {String} selector Selector expression to filter items by.
  90. * @return {tinymce.ui.Collection} Collection containing the filtered items.
  91. */
  92. filter: function(selector) {
  93. var self = this, i, l, matches = [], item, match;
  94. // Compile string into selector expression
  95. if (typeof(selector) === "string") {
  96. selector = new Selector(selector);
  97. match = function(item) {
  98. return selector.match(item);
  99. };
  100. } else {
  101. // Use selector as matching function
  102. match = selector;
  103. }
  104. for (i = 0, l = self.length; i < l; i++) {
  105. item = self[i];
  106. if (match(item)) {
  107. matches.push(item);
  108. }
  109. }
  110. return new Collection(matches);
  111. },
  112. /**
  113. * Slices the items within the collection.
  114. *
  115. * @method slice
  116. * @param {Number} index Index to slice at.
  117. * @param {Number} len Optional length to slice.
  118. * @return {tinymce.ui.Collection} Current collection.
  119. */
  120. slice: function() {
  121. return new Collection(slice.apply(this, arguments));
  122. },
  123. /**
  124. * Makes the current collection equal to the specified index.
  125. *
  126. * @method eq
  127. * @param {Number} index Index of the item to set the collection to.
  128. * @return {tinymce.ui.Collection} Current collection.
  129. */
  130. eq: function(index) {
  131. return index === -1 ? this.slice(index) : this.slice(index, +index + 1);
  132. },
  133. /**
  134. * Executes the specified callback on each item in collection.
  135. *
  136. * @method each
  137. * @param {function} callback Callback to execute for each item in collection.
  138. * @return {tinymce.ui.Collection} Current collection instance.
  139. */
  140. each: function(callback) {
  141. Tools.each(this, callback);
  142. return this;
  143. },
  144. /**
  145. * Returns an JavaScript array object of the contents inside the collection.
  146. *
  147. * @method toArray
  148. * @return {Array} Array with all items from collection.
  149. */
  150. toArray: function() {
  151. return Tools.toArray(this);
  152. },
  153. /**
  154. * Finds the index of the specified control or return -1 if it isn't in the collection.
  155. *
  156. * @method indexOf
  157. * @param {Control} ctrl Control instance to look for.
  158. * @return {Number} Index of the specified control or -1.
  159. */
  160. indexOf: function(ctrl) {
  161. var self = this, i = self.length;
  162. while (i--) {
  163. if (self[i] === ctrl) {
  164. break;
  165. }
  166. }
  167. return i;
  168. },
  169. /**
  170. * Returns a new collection of the contents in reverse order.
  171. *
  172. * @method reverse
  173. * @return {tinymce.ui.Collection} Collection instance with reversed items.
  174. */
  175. reverse: function() {
  176. return new Collection(Tools.toArray(this).reverse());
  177. },
  178. /**
  179. * Returns true/false if the class exists or not.
  180. *
  181. * @method hasClass
  182. * @param {String} cls Class to check for.
  183. * @return {Boolean} true/false state if the class exists or not.
  184. */
  185. hasClass: function(cls) {
  186. return this[0] ? this[0].hasClass(cls) : false;
  187. },
  188. /**
  189. * Sets/gets the specific property on the items in the collection. The same as executing control.<property>(<value>);
  190. *
  191. * @method prop
  192. * @param {String} name Property name to get/set.
  193. * @param {Object} value Optional object value to set.
  194. * @return {tinymce.ui.Collection} Current collection instance or value of the first item on a get operation.
  195. */
  196. prop: function(name, value) {
  197. var self = this, undef, item;
  198. if (value !== undef) {
  199. self.each(function(item) {
  200. if (item[name]) {
  201. item[name](value);
  202. }
  203. });
  204. return self;
  205. }
  206. item = self[0];
  207. if (item && item[name]) {
  208. return item[name]();
  209. }
  210. },
  211. /**
  212. * Executes the specific function name with optional arguments an all items in collection if it exists.
  213. *
  214. * @example collection.exec("myMethod", arg1, arg2, arg3);
  215. * @method exec
  216. * @param {String} name Name of the function to execute.
  217. * @param {Object} ... Multiple arguments to pass to each function.
  218. * @return {tinymce.ui.Collection} Current collection.
  219. */
  220. exec: function(name) {
  221. var self = this, args = Tools.toArray(arguments).slice(1);
  222. self.each(function(item) {
  223. if (item[name]) {
  224. item[name].apply(item, args);
  225. }
  226. });
  227. return self;
  228. },
  229. /**
  230. * Remove all items from collection and DOM.
  231. *
  232. * @method remove
  233. * @return {tinymce.ui.Collection} Current collection.
  234. */
  235. remove: function() {
  236. var i = this.length;
  237. while (i--) {
  238. this[i].remove();
  239. }
  240. return this;
  241. }
  242. /**
  243. * Fires the specified event by name and arguments on the control. This will execute all
  244. * bound event handlers.
  245. *
  246. * @method fire
  247. * @param {String} name Name of the event to fire.
  248. * @param {Object} args Optional arguments to pass to the event.
  249. * @return {tinymce.ui.Collection} Current collection instance.
  250. */
  251. // fire: function(event, args) {}, -- Generated by code below
  252. /**
  253. * Binds a callback to the specified event. This event can both be
  254. * native browser events like "click" or custom ones like PostRender.
  255. *
  256. * The callback function will have two parameters the first one being the control that received the event
  257. * the second one will be the event object either the browsers native event object or a custom JS object.
  258. *
  259. * @method on
  260. * @param {String} name Name of the event to bind. For example "click".
  261. * @param {String/function} callback Callback function to execute ones the event occurs.
  262. * @return {tinymce.ui.Collection} Current collection instance.
  263. */
  264. // on: function(name, callback) {}, -- Generated by code below
  265. /**
  266. * Unbinds the specified event and optionally a specific callback. If you omit the name
  267. * parameter all event handlers will be removed. If you omit the callback all event handles
  268. * by the specified name will be removed.
  269. *
  270. * @method off
  271. * @param {String} name Optional name for the event to unbind.
  272. * @param {function} callback Optional callback function to unbind.
  273. * @return {tinymce.ui.Collection} Current collection instance.
  274. */
  275. // off: function(name, callback) {}, -- Generated by code below
  276. /**
  277. * Shows the items in the current collection.
  278. *
  279. * @method show
  280. * @return {tinymce.ui.Collection} Current collection instance.
  281. */
  282. // show: function() {}, -- Generated by code below
  283. /**
  284. * Hides the items in the current collection.
  285. *
  286. * @method hide
  287. * @return {tinymce.ui.Collection} Current collection instance.
  288. */
  289. // hide: function() {}, -- Generated by code below
  290. /**
  291. * Sets/gets the text contents of the items in the current collection.
  292. *
  293. * @method text
  294. * @return {tinymce.ui.Collection} Current collection instance or text value of the first item on a get operation.
  295. */
  296. // text: function(value) {}, -- Generated by code below
  297. /**
  298. * Sets/gets the name contents of the items in the current collection.
  299. *
  300. * @method name
  301. * @return {tinymce.ui.Collection} Current collection instance or name value of the first item on a get operation.
  302. */
  303. // name: function(value) {}, -- Generated by code below
  304. /**
  305. * Sets/gets the disabled state on the items in the current collection.
  306. *
  307. * @method disabled
  308. * @return {tinymce.ui.Collection} Current collection instance or disabled state of the first item on a get operation.
  309. */
  310. // disabled: function(state) {}, -- Generated by code below
  311. /**
  312. * Sets/gets the active state on the items in the current collection.
  313. *
  314. * @method active
  315. * @return {tinymce.ui.Collection} Current collection instance or active state of the first item on a get operation.
  316. */
  317. // active: function(state) {}, -- Generated by code below
  318. /**
  319. * Sets/gets the selected state on the items in the current collection.
  320. *
  321. * @method selected
  322. * @return {tinymce.ui.Collection} Current collection instance or selected state of the first item on a get operation.
  323. */
  324. // selected: function(state) {}, -- Generated by code below
  325. /**
  326. * Sets/gets the selected state on the items in the current collection.
  327. *
  328. * @method visible
  329. * @return {tinymce.ui.Collection} Current collection instance or visible state of the first item on a get operation.
  330. */
  331. // visible: function(state) {}, -- Generated by code below
  332. /**
  333. * Adds a class to all items in the collection.
  334. *
  335. * @method addClass
  336. * @param {String} cls Class to add to each item.
  337. * @return {tinymce.ui.Collection} Current collection instance.
  338. */
  339. // addClass: function(cls) {}, -- Generated by code below
  340. /**
  341. * Removes the specified class from all items in collection.
  342. *
  343. * @method removeClass
  344. * @param {String} cls Class to remove from each item.
  345. * @return {tinymce.ui.Collection} Current collection instance.
  346. */
  347. // removeClass: function(cls) {}, -- Generated by code below
  348. };
  349. // Extend tinymce.ui.Collection prototype with some generated control specific methods
  350. Tools.each('fire on off show hide addClass removeClass append prepend before after reflow'.split(' '), function(name) {
  351. proto[name] = function() {
  352. var args = Tools.toArray(arguments);
  353. this.each(function(ctrl) {
  354. if (name in ctrl) {
  355. ctrl[name].apply(ctrl, args);
  356. }
  357. });
  358. return this;
  359. };
  360. });
  361. // Extend tinymce.ui.Collection prototype with some property methods
  362. Tools.each('text name disabled active selected checked visible parent value data'.split(' '), function(name) {
  363. proto[name] = function(value) {
  364. return this.prop(name, value);
  365. };
  366. });
  367. // Create class based on the new prototype
  368. Collection = Class.extend(proto);
  369. // Stick Collection into Selector to prevent circual references
  370. Selector.Collection = Collection;
  371. return Collection;
  372. });