PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/yui/3.10.0pr1/oop/oop-debug.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 407 lines | 174 code | 44 blank | 189 comment | 47 complexity | ad7b71d591abb23790f0488692bd6e7e MD5 | raw file
  1. YUI.add('oop', function (Y, NAME) {
  2. /**
  3. Adds object inheritance and manipulation utilities to the YUI instance. This
  4. module is required by most YUI components.
  5. @module oop
  6. **/
  7. var L = Y.Lang,
  8. A = Y.Array,
  9. OP = Object.prototype,
  10. CLONE_MARKER = '_~yuim~_',
  11. hasOwn = OP.hasOwnProperty,
  12. toString = OP.toString;
  13. function dispatch(o, f, c, proto, action) {
  14. if (o && o[action] && o !== Y) {
  15. return o[action].call(o, f, c);
  16. } else {
  17. switch (A.test(o)) {
  18. case 1:
  19. return A[action](o, f, c);
  20. case 2:
  21. return A[action](Y.Array(o, 0, true), f, c);
  22. default:
  23. return Y.Object[action](o, f, c, proto);
  24. }
  25. }
  26. }
  27. /**
  28. Augments the _receiver_ with prototype properties from the _supplier_. The
  29. receiver may be a constructor function or an object. The supplier must be a
  30. constructor function.
  31. If the _receiver_ is an object, then the _supplier_ constructor will be called
  32. immediately after _receiver_ is augmented, with _receiver_ as the `this` object.
  33. If the _receiver_ is a constructor function, then all prototype methods of
  34. _supplier_ that are copied to _receiver_ will be sequestered, and the
  35. _supplier_ constructor will not be called immediately. The first time any
  36. sequestered method is called on the _receiver_'s prototype, all sequestered
  37. methods will be immediately copied to the _receiver_'s prototype, the
  38. _supplier_'s constructor will be executed, and finally the newly unsequestered
  39. method that was called will be executed.
  40. This sequestering logic sounds like a bunch of complicated voodoo, but it makes
  41. it cheap to perform frequent augmentation by ensuring that suppliers'
  42. constructors are only called if a supplied method is actually used. If none of
  43. the supplied methods is ever used, then there's no need to take the performance
  44. hit of calling the _supplier_'s constructor.
  45. @method augment
  46. @param {Function|Object} receiver Object or function to be augmented.
  47. @param {Function} supplier Function that supplies the prototype properties with
  48. which to augment the _receiver_.
  49. @param {Boolean} [overwrite=false] If `true`, properties already on the receiver
  50. will be overwritten if found on the supplier's prototype.
  51. @param {String[]} [whitelist] An array of property names. If specified,
  52. only the whitelisted prototype properties will be applied to the receiver, and
  53. all others will be ignored.
  54. @param {Array|any} [args] Argument or array of arguments to pass to the
  55. supplier's constructor when initializing.
  56. @return {Function} Augmented object.
  57. @for YUI
  58. **/
  59. Y.augment = function (receiver, supplier, overwrite, whitelist, args) {
  60. var rProto = receiver.prototype,
  61. sequester = rProto && supplier,
  62. sProto = supplier.prototype,
  63. to = rProto || receiver,
  64. copy,
  65. newPrototype,
  66. replacements,
  67. sequestered,
  68. unsequester;
  69. args = args ? Y.Array(args) : [];
  70. if (sequester) {
  71. newPrototype = {};
  72. replacements = {};
  73. sequestered = {};
  74. copy = function (value, key) {
  75. if (overwrite || !(key in rProto)) {
  76. if (toString.call(value) === '[object Function]') {
  77. sequestered[key] = value;
  78. newPrototype[key] = replacements[key] = function () {
  79. return unsequester(this, value, arguments);
  80. };
  81. } else {
  82. newPrototype[key] = value;
  83. }
  84. }
  85. };
  86. unsequester = function (instance, fn, fnArgs) {
  87. // Unsequester all sequestered functions.
  88. for (var key in sequestered) {
  89. if (hasOwn.call(sequestered, key)
  90. && instance[key] === replacements[key]) {
  91. instance[key] = sequestered[key];
  92. }
  93. }
  94. // Execute the supplier constructor.
  95. supplier.apply(instance, args);
  96. // Finally, execute the original sequestered function.
  97. return fn.apply(instance, fnArgs);
  98. };
  99. if (whitelist) {
  100. Y.Array.each(whitelist, function (name) {
  101. if (name in sProto) {
  102. copy(sProto[name], name);
  103. }
  104. });
  105. } else {
  106. Y.Object.each(sProto, copy, null, true);
  107. }
  108. }
  109. Y.mix(to, newPrototype || sProto, overwrite, whitelist);
  110. if (!sequester) {
  111. supplier.apply(to, args);
  112. }
  113. return receiver;
  114. };
  115. /**
  116. * Copies object properties from the supplier to the receiver. If the target has
  117. * the property, and the property is an object, the target object will be
  118. * augmented with the supplier's value.
  119. *
  120. * @method aggregate
  121. * @param {Object} receiver Object to receive the augmentation.
  122. * @param {Object} supplier Object that supplies the properties with which to
  123. * augment the receiver.
  124. * @param {Boolean} [overwrite=false] If `true`, properties already on the receiver
  125. * will be overwritten if found on the supplier.
  126. * @param {String[]} [whitelist] Whitelist. If supplied, only properties in this
  127. * list will be applied to the receiver.
  128. * @return {Object} Augmented object.
  129. */
  130. Y.aggregate = function(r, s, ov, wl) {
  131. return Y.mix(r, s, ov, wl, 0, true);
  132. };
  133. /**
  134. * Utility to set up the prototype, constructor and superclass properties to
  135. * support an inheritance strategy that can chain constructors and methods.
  136. * Static members will not be inherited.
  137. *
  138. * @method extend
  139. * @param {function} r the object to modify.
  140. * @param {function} s the object to inherit.
  141. * @param {object} px prototype properties to add/override.
  142. * @param {object} sx static properties to add/override.
  143. * @return {object} the extended object.
  144. */
  145. Y.extend = function(r, s, px, sx) {
  146. if (!s || !r) {
  147. Y.error('extend failed, verify dependencies');
  148. }
  149. var sp = s.prototype, rp = Y.Object(sp);
  150. r.prototype = rp;
  151. rp.constructor = r;
  152. r.superclass = sp;
  153. // assign constructor property
  154. if (s != Object && sp.constructor == OP.constructor) {
  155. sp.constructor = s;
  156. }
  157. // add prototype overrides
  158. if (px) {
  159. Y.mix(rp, px, true);
  160. }
  161. // add object overrides
  162. if (sx) {
  163. Y.mix(r, sx, true);
  164. }
  165. return r;
  166. };
  167. /**
  168. * Executes the supplied function for each item in
  169. * a collection. Supports arrays, objects, and
  170. * NodeLists
  171. * @method each
  172. * @param {object} o the object to iterate.
  173. * @param {function} f the function to execute. This function
  174. * receives the value, key, and object as parameters.
  175. * @param {object} c the execution context for the function.
  176. * @param {boolean} proto if true, prototype properties are
  177. * iterated on objects.
  178. * @return {YUI} the YUI instance.
  179. */
  180. Y.each = function(o, f, c, proto) {
  181. return dispatch(o, f, c, proto, 'each');
  182. };
  183. /**
  184. * Executes the supplied function for each item in
  185. * a collection. The operation stops if the function
  186. * returns true. Supports arrays, objects, and
  187. * NodeLists.
  188. * @method some
  189. * @param {object} o the object to iterate.
  190. * @param {function} f the function to execute. This function
  191. * receives the value, key, and object as parameters.
  192. * @param {object} c the execution context for the function.
  193. * @param {boolean} proto if true, prototype properties are
  194. * iterated on objects.
  195. * @return {boolean} true if the function ever returns true,
  196. * false otherwise.
  197. */
  198. Y.some = function(o, f, c, proto) {
  199. return dispatch(o, f, c, proto, 'some');
  200. };
  201. /**
  202. Deep object/array copy. Function clones are actually wrappers around the
  203. original function. Array-like objects are treated as arrays. Primitives are
  204. returned untouched. Optionally, a function can be provided to handle other data
  205. types, filter keys, validate values, etc.
  206. **Note:** Cloning a non-trivial object is a reasonably heavy operation, due to
  207. the need to recursively iterate down non-primitive properties. Clone should be
  208. used only when a deep clone down to leaf level properties is explicitly
  209. required. This method will also
  210. In many cases (for example, when trying to isolate objects used as hashes for
  211. configuration properties), a shallow copy, using `Y.merge()` is normally
  212. sufficient. If more than one level of isolation is required, `Y.merge()` can be
  213. used selectively at each level which needs to be isolated from the original
  214. without going all the way to leaf properties.
  215. @method clone
  216. @param {object} o what to clone.
  217. @param {boolean} safe if true, objects will not have prototype items from the
  218. source. If false, they will. In this case, the original is initially
  219. protected, but the clone is not completely immune from changes to the source
  220. object prototype. Also, cloned prototype items that are deleted from the
  221. clone will result in the value of the source prototype being exposed. If
  222. operating on a non-safe clone, items should be nulled out rather than
  223. deleted.
  224. @param {function} f optional function to apply to each item in a collection; it
  225. will be executed prior to applying the value to the new object.
  226. Return false to prevent the copy.
  227. @param {object} c optional execution context for f.
  228. @param {object} owner Owner object passed when clone is iterating an object.
  229. Used to set up context for cloned functions.
  230. @param {object} cloned hash of previously cloned objects to avoid multiple
  231. clones.
  232. @return {Array|Object} the cloned object.
  233. **/
  234. Y.clone = function(o, safe, f, c, owner, cloned) {
  235. var o2, marked, stamp;
  236. // Does not attempt to clone:
  237. //
  238. // * Non-typeof-object values, "primitive" values don't need cloning.
  239. //
  240. // * YUI instances, cloning complex object like YUI instances is not
  241. // advised, this is like cloning the world.
  242. //
  243. // * DOM nodes (#2528250), common host objects like DOM nodes cannot be
  244. // "subclassed" in Firefox and old versions of IE. Trying to use
  245. // `Object.create()` or `Y.extend()` on a DOM node will throw an error in
  246. // these browsers.
  247. //
  248. // Instad, the passed-in `o` will be return as-is when it matches one of the
  249. // above criteria.
  250. if (!L.isObject(o) ||
  251. Y.instanceOf(o, YUI) ||
  252. (o.addEventListener || o.attachEvent)) {
  253. return o;
  254. }
  255. marked = cloned || {};
  256. switch (L.type(o)) {
  257. case 'date':
  258. return new Date(o);
  259. case 'regexp':
  260. // if we do this we need to set the flags too
  261. // return new RegExp(o.source);
  262. return o;
  263. case 'function':
  264. // o2 = Y.bind(o, owner);
  265. // break;
  266. return o;
  267. case 'array':
  268. o2 = [];
  269. break;
  270. default:
  271. // #2528250 only one clone of a given object should be created.
  272. if (o[CLONE_MARKER]) {
  273. return marked[o[CLONE_MARKER]];
  274. }
  275. stamp = Y.guid();
  276. o2 = (safe) ? {} : Y.Object(o);
  277. o[CLONE_MARKER] = stamp;
  278. marked[stamp] = o;
  279. }
  280. Y.each(o, function(v, k) {
  281. if ((k || k === 0) && (!f || (f.call(c || this, v, k, this, o) !== false))) {
  282. if (k !== CLONE_MARKER) {
  283. if (k == 'prototype') {
  284. // skip the prototype
  285. // } else if (o[k] === o) {
  286. // this[k] = this;
  287. } else {
  288. this[k] =
  289. Y.clone(v, safe, f, c, owner || o, marked);
  290. }
  291. }
  292. }
  293. }, o2);
  294. if (!cloned) {
  295. Y.Object.each(marked, function(v, k) {
  296. if (v[CLONE_MARKER]) {
  297. try {
  298. delete v[CLONE_MARKER];
  299. } catch (e) {
  300. v[CLONE_MARKER] = null;
  301. }
  302. }
  303. }, this);
  304. marked = null;
  305. }
  306. return o2;
  307. };
  308. /**
  309. * Returns a function that will execute the supplied function in the
  310. * supplied object's context, optionally adding any additional
  311. * supplied parameters to the beginning of the arguments collection the
  312. * supplied to the function.
  313. *
  314. * @method bind
  315. * @param {Function|String} f the function to bind, or a function name
  316. * to execute on the context object.
  317. * @param {object} c the execution context.
  318. * @param {any} args* 0..n arguments to include before the arguments the
  319. * function is executed with.
  320. * @return {function} the wrapped function.
  321. */
  322. Y.bind = function(f, c) {
  323. var xargs = arguments.length > 2 ?
  324. Y.Array(arguments, 2, true) : null;
  325. return function() {
  326. var fn = L.isString(f) ? c[f] : f,
  327. args = (xargs) ?
  328. xargs.concat(Y.Array(arguments, 0, true)) : arguments;
  329. return fn.apply(c || fn, args);
  330. };
  331. };
  332. /**
  333. * Returns a function that will execute the supplied function in the
  334. * supplied object's context, optionally adding any additional
  335. * supplied parameters to the end of the arguments the function
  336. * is executed with.
  337. *
  338. * @method rbind
  339. * @param {Function|String} f the function to bind, or a function name
  340. * to execute on the context object.
  341. * @param {object} c the execution context.
  342. * @param {any} args* 0..n arguments to append to the end of
  343. * arguments collection supplied to the function.
  344. * @return {function} the wrapped function.
  345. */
  346. Y.rbind = function(f, c) {
  347. var xargs = arguments.length > 2 ? Y.Array(arguments, 2, true) : null;
  348. return function() {
  349. var fn = L.isString(f) ? c[f] : f,
  350. args = (xargs) ?
  351. Y.Array(arguments, 0, true).concat(xargs) : arguments;
  352. return fn.apply(c || fn, args);
  353. };
  354. };
  355. }, '@VERSION@', {"requires": ["yui-base"]});