API Docs for: 3.18.1
Show:

File: base/js/BaseBuild.js

  1. /**
  2. * The base-build submodule provides Base.build functionality, which
  3. * can be used to create custom classes, by aggregating extensions onto
  4. * a main class.
  5. *
  6. * @module base
  7. * @submodule base-build
  8. * @for Base
  9. */
  10. var BaseCore = Y.BaseCore,
  11. Base = Y.Base,
  12. L = Y.Lang,
  13.  
  14. INITIALIZER = "initializer",
  15. DESTRUCTOR = "destructor",
  16. AGGREGATES = ["_PLUG", "_UNPLUG"],
  17.  
  18. build;
  19.  
  20. // Utility function used in `_buildCfg` to aggregate array values into a new
  21. // array from the sender constructor to the receiver constructor.
  22. function arrayAggregator(prop, r, s) {
  23. if (s[prop]) {
  24. r[prop] = (r[prop] || []).concat(s[prop]);
  25. }
  26. }
  27.  
  28. // Utility function used in `_buildCfg` to aggregate `_ATTR_CFG` array
  29. // values from the sender constructor into a new array on receiver's
  30. // constructor, and clear the cached hash.
  31. function attrCfgAggregator(prop, r, s) {
  32. if (s._ATTR_CFG) {
  33. // Clear cached hash.
  34. r._ATTR_CFG_HASH = null;
  35.  
  36. arrayAggregator.apply(null, arguments);
  37. }
  38. }
  39.  
  40. // Utility function used in `_buildCfg` to aggregate ATTRS configs from one
  41. // the sender constructor to the receiver constructor.
  42. function attrsAggregator(prop, r, s) {
  43. BaseCore.modifyAttrs(r, s.ATTRS);
  44. }
  45.  
  46. Base._build = function(name, main, extensions, px, sx, cfg) {
  47.  
  48. var build = Base._build,
  49.  
  50. builtClass = build._ctor(main, cfg),
  51. buildCfg = build._cfg(main, cfg, extensions),
  52.  
  53. _mixCust = build._mixCust,
  54.  
  55. dynamic = builtClass._yuibuild.dynamic,
  56.  
  57. i, l, extClass, extProto,
  58. initializer,
  59. destructor;
  60.  
  61. // Augment/Aggregate
  62. for (i = 0, l = extensions.length; i < l; i++) {
  63. extClass = extensions[i];
  64.  
  65. extProto = extClass.prototype;
  66.  
  67. initializer = extProto[INITIALIZER];
  68. destructor = extProto[DESTRUCTOR];
  69. delete extProto[INITIALIZER];
  70. delete extProto[DESTRUCTOR];
  71.  
  72. // Prototype, old non-displacing augment
  73. Y.mix(builtClass, extClass, true, null, 1);
  74.  
  75. // Custom Statics
  76. _mixCust(builtClass, extClass, buildCfg);
  77.  
  78. if (initializer) {
  79. extProto[INITIALIZER] = initializer;
  80. }
  81.  
  82. if (destructor) {
  83. extProto[DESTRUCTOR] = destructor;
  84. }
  85.  
  86. builtClass._yuibuild.exts.push(extClass);
  87. }
  88.  
  89. if (px) {
  90. Y.mix(builtClass.prototype, px, true);
  91. }
  92.  
  93. if (sx) {
  94. Y.mix(builtClass, build._clean(sx, buildCfg), true);
  95. _mixCust(builtClass, sx, buildCfg);
  96. }
  97.  
  98. builtClass.prototype.hasImpl = build._impl;
  99.  
  100. if (dynamic) {
  101. builtClass.NAME = name;
  102. builtClass.prototype.constructor = builtClass;
  103.  
  104. // Carry along the reference to `modifyAttrs()` from `main`.
  105. builtClass.modifyAttrs = main.modifyAttrs;
  106. }
  107.  
  108. return builtClass;
  109. };
  110.  
  111. build = Base._build;
  112.  
  113. Y.mix(build, {
  114.  
  115. _mixCust: function(r, s, cfg) {
  116.  
  117. var aggregates,
  118. custom,
  119. statics,
  120. aggr,
  121. l,
  122. i;
  123.  
  124. if (cfg) {
  125. aggregates = cfg.aggregates;
  126. custom = cfg.custom;
  127. statics = cfg.statics;
  128. }
  129.  
  130. if (statics) {
  131. Y.mix(r, s, true, statics);
  132. }
  133.  
  134. if (aggregates) {
  135. for (i = 0, l = aggregates.length; i < l; i++) {
  136. aggr = aggregates[i];
  137. if (!r.hasOwnProperty(aggr) && s.hasOwnProperty(aggr)) {
  138. r[aggr] = L.isArray(s[aggr]) ? [] : {};
  139. }
  140. Y.aggregate(r, s, true, [aggr]);
  141. }
  142. }
  143.  
  144. if (custom) {
  145. for (i in custom) {
  146. if (custom.hasOwnProperty(i)) {
  147. custom[i](i, r, s);
  148. }
  149. }
  150. }
  151.  
  152. },
  153.  
  154. _tmpl: function(main) {
  155.  
  156. function BuiltClass() {
  157. BuiltClass.superclass.constructor.apply(this, arguments);
  158. }
  159. Y.extend(BuiltClass, main);
  160.  
  161. return BuiltClass;
  162. },
  163.  
  164. _impl : function(extClass) {
  165. var classes = this._getClasses(), i, l, cls, exts, ll, j;
  166. for (i = 0, l = classes.length; i < l; i++) {
  167. cls = classes[i];
  168. if (cls._yuibuild) {
  169. exts = cls._yuibuild.exts;
  170. ll = exts.length;
  171.  
  172. for (j = 0; j < ll; j++) {
  173. if (exts[j] === extClass) {
  174. return true;
  175. }
  176. }
  177. }
  178. }
  179. return false;
  180. },
  181.  
  182. _ctor : function(main, cfg) {
  183.  
  184. var dynamic = (cfg && false === cfg.dynamic) ? false : true,
  185. builtClass = (dynamic) ? build._tmpl(main) : main,
  186. buildCfg = builtClass._yuibuild;
  187.  
  188. if (!buildCfg) {
  189. buildCfg = builtClass._yuibuild = {};
  190. }
  191.  
  192. buildCfg.id = buildCfg.id || null;
  193. buildCfg.exts = buildCfg.exts || [];
  194. buildCfg.dynamic = dynamic;
  195.  
  196. return builtClass;
  197. },
  198.  
  199. _cfg : function(main, cfg, exts) {
  200. var aggr = [],
  201. cust = {},
  202. statics = [],
  203. buildCfg,
  204. cfgAggr = (cfg && cfg.aggregates),
  205. cfgCustBuild = (cfg && cfg.custom),
  206. cfgStatics = (cfg && cfg.statics),
  207. c = main,
  208. i,
  209. l;
  210.  
  211. // Prototype Chain
  212. while (c && c.prototype) {
  213. buildCfg = c._buildCfg;
  214. if (buildCfg) {
  215. if (buildCfg.aggregates) {
  216. aggr = aggr.concat(buildCfg.aggregates);
  217. }
  218. if (buildCfg.custom) {
  219. Y.mix(cust, buildCfg.custom, true);
  220. }
  221. if (buildCfg.statics) {
  222. statics = statics.concat(buildCfg.statics);
  223. }
  224. }
  225. c = c.superclass ? c.superclass.constructor : null;
  226. }
  227.  
  228. // Exts
  229. if (exts) {
  230. for (i = 0, l = exts.length; i < l; i++) {
  231. c = exts[i];
  232. buildCfg = c._buildCfg;
  233. if (buildCfg) {
  234. if (buildCfg.aggregates) {
  235. aggr = aggr.concat(buildCfg.aggregates);
  236. }
  237. if (buildCfg.custom) {
  238. Y.mix(cust, buildCfg.custom, true);
  239. }
  240. if (buildCfg.statics) {
  241. statics = statics.concat(buildCfg.statics);
  242. }
  243. }
  244. }
  245. }
  246.  
  247. if (cfgAggr) {
  248. aggr = aggr.concat(cfgAggr);
  249. }
  250.  
  251. if (cfgCustBuild) {
  252. Y.mix(cust, cfg.cfgBuild, true);
  253. }
  254.  
  255. if (cfgStatics) {
  256. statics = statics.concat(cfgStatics);
  257. }
  258.  
  259. return {
  260. aggregates: aggr,
  261. custom: cust,
  262. statics: statics
  263. };
  264. },
  265.  
  266. _clean : function(sx, cfg) {
  267. var prop, i, l, sxclone = Y.merge(sx),
  268. aggregates = cfg.aggregates,
  269. custom = cfg.custom;
  270.  
  271. for (prop in custom) {
  272. if (sxclone.hasOwnProperty(prop)) {
  273. delete sxclone[prop];
  274. }
  275. }
  276.  
  277. for (i = 0, l = aggregates.length; i < l; i++) {
  278. prop = aggregates[i];
  279. if (sxclone.hasOwnProperty(prop)) {
  280. delete sxclone[prop];
  281. }
  282. }
  283.  
  284. return sxclone;
  285. }
  286. });
  287.  
  288. /**
  289. * <p>
  290. * Builds a custom constructor function (class) from the
  291. * main function, and array of extension functions (classes)
  292. * provided. The NAME field for the constructor function is
  293. * defined by the first argument passed in.
  294. * </p>
  295. * <p>
  296. * The cfg object supports the following properties
  297. * </p>
  298. * <dl>
  299. * <dt>dynamic &#60;boolean&#62;</dt>
  300. * <dd>
  301. * <p>If true (default), a completely new class
  302. * is created which extends the main class, and acts as the
  303. * host on which the extension classes are augmented.</p>
  304. * <p>If false, the extensions classes are augmented directly to
  305. * the main class, modifying the main class' prototype.</p>
  306. * </dd>
  307. * <dt>aggregates &#60;String[]&#62;</dt>
  308. * <dd>An array of static property names, which will get aggregated
  309. * on to the built class, in addition to the default properties build
  310. * will always aggregate as defined by the main class' static _buildCfg
  311. * property.
  312. * </dd>
  313. * </dl>
  314. *
  315. * @method build
  316. * @deprecated Use the more convenient Base.create and Base.mix methods instead
  317. * @static
  318. * @param {Function} name The name of the new class. Used to define the NAME property for the new class.
  319. * @param {Function} main The main class on which to base the built class
  320. * @param {Function[]} extensions The set of extension classes which will be
  321. * augmented/aggregated to the built class.
  322. * @param {Object} cfg Optional. Build configuration for the class (see description).
  323. * @return {Function} A custom class, created from the provided main and extension classes
  324. */
  325. Base.build = function(name, main, extensions, cfg) {
  326. return build(name, main, extensions, null, null, cfg);
  327. };
  328.  
  329. /**
  330. * Creates a new class (constructor function) which extends the base class passed in as the second argument,
  331. * and mixes in the array of extensions provided.
  332. *
  333. * Prototype properties or methods can be added to the new class, using the px argument (similar to Y.extend).
  334. *
  335. * Static properties or methods can be added to the new class, using the sx argument (similar to Y.extend).
  336. *
  337. * **NOTE FOR COMPONENT DEVELOPERS**: Both the `base` class, and `extensions` can define static a `_buildCfg`
  338. * property, which acts as class creation meta-data, and drives how special static properties from the base
  339. * class, or extensions should be copied, aggregated or (custom) mixed into the newly created class.
  340. *
  341. * The `_buildCfg` property is a hash with 3 supported properties: `statics`, `aggregates` and `custom`, e.g:
  342. *
  343. * // If the Base/Main class is the thing introducing the property:
  344. *
  345. * MyBaseClass._buildCfg = {
  346. *
  347. * // Static properties/methods to copy (Alias) to the built class.
  348. * statics: ["CopyThisMethod", "CopyThisProperty"],
  349. *
  350. * // Static props to aggregate onto the built class.
  351. * aggregates: ["AggregateThisProperty"],
  352. *
  353. * // Static properties which need custom handling (e.g. deep merge etc.)
  354. * custom: {
  355. * "CustomProperty" : function(property, Receiver, Supplier) {
  356. * ...
  357. * var triggers = Receiver.CustomProperty.triggers;
  358. * Receiver.CustomProperty.triggers = triggers.concat(Supplier.CustomProperty.triggers);
  359. * ...
  360. * }
  361. * }
  362. * };
  363. *
  364. * MyBaseClass.CopyThisMethod = function() {...};
  365. * MyBaseClass.CopyThisProperty = "foo";
  366. * MyBaseClass.AggregateThisProperty = {...};
  367. * MyBaseClass.CustomProperty = {
  368. * triggers: [...]
  369. * }
  370. *
  371. * // Or, if the Extension is the thing introducing the property:
  372. *
  373. * MyExtension._buildCfg = {
  374. * statics : ...
  375. * aggregates : ...
  376. * custom : ...
  377. * }
  378. *
  379. * This way, when users pass your base or extension class to `Y.Base.create` or `Y.Base.mix`, they don't need to
  380. * know which properties need special handling. `Y.Base` has a buildCfg which defines `ATTRS` for custom mix handling
  381. * (to protect the static config objects), and `Y.Widget` has a buildCfg which specifies `HTML_PARSER` for
  382. * straight up aggregation.
  383. *
  384. * @method create
  385. * @static
  386. * @param {String} name The name of the newly created class. Used to define the NAME property for the new class.
  387. * @param {Function} main The base class which the new class should extend.
  388. * This class needs to be Base or a class derived from base (e.g. Widget).
  389. * @param {Function[]} extensions The list of extensions which will be mixed into the built class.
  390. * @param {Object} px The set of prototype properties/methods to add to the built class.
  391. * @param {Object} sx The set of static properties/methods to add to the built class.
  392. * @return {Function} The newly created class.
  393. */
  394. Base.create = function(name, base, extensions, px, sx) {
  395. return build(name, base, extensions, px, sx);
  396. };
  397.  
  398. /**
  399. * <p>Mixes in a list of extensions to an existing class.</p>
  400. * @method mix
  401. * @static
  402. * @param {Function} main The existing class into which the extensions should be mixed.
  403. * The class needs to be Base or a class derived from Base (e.g. Widget)
  404. * @param {Function[]} extensions The set of extension classes which will mixed into the existing main class.
  405. * @return {Function} The modified main class, with extensions mixed in.
  406. */
  407. Base.mix = function(main, extensions) {
  408.  
  409. if (main._CACHED_CLASS_DATA) {
  410. main._CACHED_CLASS_DATA = null;
  411. }
  412.  
  413. return build(null, main, extensions, null, null, {dynamic:false});
  414. };
  415.  
  416. /**
  417. * The build configuration for the Base class.
  418. *
  419. * Defines the static fields which need to be aggregated when the Base class
  420. * is used as the main class passed to the
  421. * <a href="#method_Base.build">Base.build</a> method.
  422. *
  423. * @property _buildCfg
  424. * @type Object
  425. * @static
  426. * @final
  427. * @private
  428. */
  429. BaseCore._buildCfg = {
  430. aggregates: AGGREGATES.concat(),
  431.  
  432. custom: {
  433. ATTRS : attrsAggregator,
  434. _ATTR_CFG : attrCfgAggregator,
  435. _NON_ATTRS_CFG: arrayAggregator
  436. }
  437. };
  438.  
  439. // Makes sure Base and BaseCore use separate `_buildCfg` objects.
  440. Base._buildCfg = {
  441. aggregates: AGGREGATES.concat(),
  442.  
  443. custom: {
  444. ATTRS : attrsAggregator,
  445. _ATTR_CFG : attrCfgAggregator,
  446. _NON_ATTRS_CFG: arrayAggregator
  447. }
  448. };
  449.