mixins.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define('mixins', [
  6. 'module'
  7. ], function (module) {
  8. 'use strict';
  9. var rjsMixins;
  10. /**
  11. * Checks if specified string contains
  12. * a plugin spacer '!' substring.
  13. *
  14. * @param {String} name - Name, path or alias of a module.
  15. * @returns {Boolean}
  16. */
  17. function hasPlugin(name) {
  18. return !!~name.indexOf('!');
  19. }
  20. /**
  21. * Adds 'mixins!' prefix to the specified string.
  22. *
  23. * @param {String} name - Name, path or alias of a module.
  24. * @returns {String} Modified name.
  25. */
  26. function addPlugin(name) {
  27. return 'mixins!' + name;
  28. }
  29. /**
  30. * Removes base url from the provided string.
  31. *
  32. * @param {String} url - Url to be processed.
  33. * @param {Object} config - Contexts' configuration object.
  34. * @returns {String} String without base url.
  35. */
  36. function removeBaseUrl(url, config) {
  37. var baseUrl = config.baseUrl || '',
  38. index = url.indexOf(baseUrl);
  39. if (~index) {
  40. url = url.substring(baseUrl.length - index);
  41. }
  42. return url;
  43. }
  44. /**
  45. * Extracts url (without baseUrl prefix)
  46. * from a modules' name.
  47. *
  48. * @param {String} name - Name, path or alias of a module.
  49. * @param {Object} config - Contexts' configuartion.
  50. * @returns {String}
  51. */
  52. function getPath(name, config) {
  53. var url = require.toUrl(name);
  54. return removeBaseUrl(url, config);
  55. }
  56. /**
  57. * Checks if specified string represents a relative path (../).
  58. *
  59. * @param {String} name - Name, path or alias of a module.
  60. * @returns {Boolean}
  61. */
  62. function isRelative(name) {
  63. return !!~name.indexOf('./');
  64. }
  65. /**
  66. * Iterativly calls mixins passing to them
  67. * current value of a 'target' parameter.
  68. *
  69. * @param {*} target - Value to be modified.
  70. * @param {...Function} mixins
  71. * @returns {*} Modified 'target' value.
  72. */
  73. function applyMixins(target) {
  74. var mixins = Array.prototype.slice.call(arguments, 1);
  75. mixins.forEach(function (mixin) {
  76. target = mixin(target);
  77. });
  78. return target;
  79. }
  80. rjsMixins = {
  81. /**
  82. * Loads specified module along with its' mixins.
  83. *
  84. * @param {String} name - Module to be loaded.
  85. */
  86. load: function (name, req, onLoad, config) {
  87. var path = getPath(name, config),
  88. mixins = this.getMixins(path),
  89. deps = [name].concat(mixins);
  90. req(deps, function () {
  91. onLoad(applyMixins.apply(null, arguments));
  92. });
  93. },
  94. /**
  95. * Retrieves list of mixins associated with a specified module.
  96. *
  97. * @param {String} path - Path to the module (without base url).
  98. * @returns {Array} An array of paths to mixins.
  99. */
  100. getMixins: function (path) {
  101. var config = module.config() || {},
  102. mixins;
  103. // fix for when urlArgs is set
  104. if (path.indexOf('?') !== -1) {
  105. path = path.substring(0, path.indexOf('?'));
  106. }
  107. mixins = config[path] || {};
  108. return Object.keys(mixins).filter(function (mixin) {
  109. return mixins[mixin] !== false;
  110. });
  111. },
  112. /**
  113. * Checks if specified module has associated with it mixins.
  114. *
  115. * @param {String} path - Path to the module (without base url).
  116. * @returns {Boolean}
  117. */
  118. hasMixins: function (path) {
  119. return this.getMixins(path).length;
  120. },
  121. /**
  122. * Modifies provided names perpending to them
  123. * the 'mixins!' plugin prefix if it's necessary.
  124. *
  125. * @param {(Array|String)} names - Module names, paths or aliases.
  126. * @param {Object} context - Current requirejs context.
  127. * @returns {Array|String}
  128. */
  129. processNames: function (names, context) {
  130. var config = context.config;
  131. /**
  132. * Prepends 'mixin' plugin to a single name.
  133. *
  134. * @param {String} name
  135. * @returns {String}
  136. */
  137. function processName(name) {
  138. var path = getPath(name, config);
  139. if (!hasPlugin(name) && (isRelative(name) || rjsMixins.hasMixins(path))) {
  140. return addPlugin(name);
  141. }
  142. return name;
  143. }
  144. return typeof names !== 'string' ?
  145. names.map(processName) :
  146. processName(names);
  147. }
  148. };
  149. return rjsMixins;
  150. });
  151. require([
  152. 'mixins'
  153. ], function (mixins) {
  154. 'use strict';
  155. var originalRequire = window.require,
  156. originalDefine = window.define,
  157. contexts = originalRequire.s.contexts,
  158. defContextName = '_',
  159. hasOwn = Object.prototype.hasOwnProperty,
  160. getLastInQueue;
  161. getLastInQueue =
  162. '(function () {' +
  163. 'var queue = globalDefQueue,' +
  164. 'item = queue[queue.length - 1];' +
  165. '' +
  166. 'return item;' +
  167. '})();';
  168. /**
  169. * Returns property of an object if
  170. * it's not defined in it's prototype.
  171. *
  172. * @param {Object} obj - Object whose property should be retrieved.
  173. * @param {String} prop - Name of the property.
  174. * @returns {*} Value of the property or false.
  175. */
  176. function getOwn(obj, prop) {
  177. return hasOwn.call(obj, prop) && obj[prop];
  178. }
  179. /**
  180. * Overrides global 'require' method adding to it dependencies modfication.
  181. */
  182. window.require = function (deps, callback, errback, optional) {
  183. var contextName = defContextName,
  184. context,
  185. config;
  186. if (!Array.isArray(deps) && typeof deps !== 'string') {
  187. config = deps;
  188. if (Array.isArray(callback)) {
  189. deps = callback;
  190. callback = errback;
  191. errback = optional;
  192. } else {
  193. deps = [];
  194. }
  195. }
  196. if (config && config.context) {
  197. contextName = config.context;
  198. }
  199. context = getOwn(contexts, contextName);
  200. if (!context) {
  201. context = contexts[contextName] = require.s.newContext(contextName);
  202. }
  203. if (config) {
  204. context.configure(config);
  205. }
  206. deps = mixins.processNames(deps, context);
  207. return context.require(deps, callback, errback);
  208. };
  209. /**
  210. * Overrides global 'define' method adding to it dependencies modfication.
  211. */
  212. window.define = function (name, deps, callback) { // eslint-disable-line no-unused-vars
  213. var context = getOwn(contexts, defContextName),
  214. result = originalDefine.apply(this, arguments),
  215. queueItem = require.exec(getLastInQueue),
  216. lastDeps = queueItem && queueItem[1];
  217. if (Array.isArray(lastDeps)) {
  218. queueItem[1] = mixins.processNames(lastDeps, context);
  219. }
  220. return result;
  221. };
  222. /**
  223. * Copy properties of original 'require' method.
  224. */
  225. Object.keys(originalRequire).forEach(function (key) {
  226. require[key] = originalRequire[key];
  227. });
  228. /**
  229. * Copy properties of original 'define' method.
  230. */
  231. Object.keys(originalDefine).forEach(function (key) {
  232. define[key] = originalDefine[key];
  233. });
  234. window.requirejs = window.require;
  235. });