Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

389 строки
9.2 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. var storageShim = {
  6. _data: {},
  7. /**
  8. * Sets value of the specified item.
  9. *
  10. * @param {String} key - Key of the property.
  11. * @param {*} value - Properties' value.
  12. */
  13. setItem: function (key, value) {
  14. 'use strict';
  15. this._data[key] = value + '';
  16. },
  17. /**
  18. * Retrieves specified item.
  19. *
  20. * @param {String} key - Key of the property to be retrieved.
  21. */
  22. getItem: function (key) {
  23. 'use strict';
  24. return this._data[key];
  25. },
  26. /**
  27. * Removes specified item.
  28. *
  29. * @param {String} key - Key of the property to be removed.
  30. */
  31. removeItem: function (key) {
  32. 'use strict';
  33. delete this._data[key];
  34. },
  35. /**
  36. * Removes all items.
  37. */
  38. clear: function () {
  39. 'use strict';
  40. this._data = {};
  41. }
  42. };
  43. define('buildTools', [
  44. ], function () {
  45. 'use strict';
  46. var storage,
  47. storeName = 'buildDisabled';
  48. try {
  49. storage = window.localStorage;
  50. } catch (e) {
  51. storage = storageShim;
  52. }
  53. return {
  54. isEnabled: storage.getItem(storeName) === null,
  55. /**
  56. * Removes base url from the provided string
  57. *
  58. * @param {String} url - Url to be processed.
  59. * @param {Object} config - RequiereJs config object.
  60. * @returns {String} String without base url.
  61. */
  62. removeBaseUrl: function (url, config) {
  63. var urlParts,
  64. baseUrlParts,
  65. baseUrl = config.baseUrl || '',
  66. index = url.indexOf(baseUrl);
  67. if (~index) {
  68. url = url.substring(baseUrl.length - index);
  69. } else {
  70. baseUrlParts = baseUrl.split('/');
  71. baseUrlParts = baseUrlParts.slice(0, -5); // slice area/vendor/theme/locale/empty chunk
  72. baseUrl = baseUrlParts.join('/');
  73. url = url.substring(baseUrl.length);
  74. urlParts = url.split('/');
  75. urlParts = urlParts.slice(5); // slice empty chunk/area/vendor/theme/locale/
  76. url = urlParts.join('/');
  77. }
  78. return url;
  79. },
  80. /**
  81. * Enables build usage.
  82. */
  83. on: function () {
  84. storage.removeItem(storeName);
  85. location.reload();
  86. },
  87. /**
  88. * Disables build usage.
  89. */
  90. off: function () {
  91. storage.setItem(storeName, 'true');
  92. location.reload();
  93. }
  94. };
  95. });
  96. /**
  97. * Module responsible for collecting statistics
  98. * data regarding modules that have been loader via bundle.
  99. */
  100. define('statistician', [
  101. ], function () {
  102. 'use strict';
  103. var storage,
  104. stringify = JSON.stringify.bind(JSON);
  105. try {
  106. storage = window.localStorage;
  107. } catch (e) {
  108. storage = storageShim;
  109. }
  110. /**
  111. * Removes duplicated entries of array, returning new one.
  112. *
  113. * @param {Array} arr
  114. * @returns {Array}
  115. */
  116. function uniq(arr) {
  117. return arr.filter(function (entry, i) {
  118. return arr.indexOf(entry) >= i;
  119. });
  120. }
  121. /**
  122. * Takes first array passed, removes all
  123. * entries which further arrays contain.
  124. *
  125. * @returns {Array} Modified array
  126. */
  127. function difference() {
  128. var args = Array.prototype.slice.call(arguments),
  129. target = args.splice(0, 1)[0];
  130. return target.filter(function (entry) {
  131. return !args.some(function (arr) {
  132. return !!~arr.indexOf(entry);
  133. });
  134. });
  135. }
  136. /**
  137. * Stringifies 'data' parameter and sets it under 'key' namespace to localStorage.
  138. *
  139. * @param {*} data
  140. * @param {String} key
  141. */
  142. function set(data, key) {
  143. storage.setItem(key, stringify(data));
  144. }
  145. /**
  146. * Gets item from localStorage by 'key' parameter, JSON.parse's it if defined.
  147. * Else, returns empty array.
  148. *
  149. * @param {String} key
  150. * @returns {Array}
  151. */
  152. function getModules(key) {
  153. var plain = storage.getItem(key);
  154. return plain ? JSON.parse(plain) : [];
  155. }
  156. /**
  157. * Concats 'modules' array with one that was previously stored by 'key' parameter
  158. * in localStorage, removes duplicated entries from resulting array and writes
  159. * it to 'key' namespace of localStorage via 'set' function.
  160. *
  161. * @param {Array} modules
  162. * @param {String} key
  163. */
  164. function storeModules(modules, key) {
  165. var old = getModules(key);
  166. set(uniq(old.concat(modules)), key);
  167. }
  168. /**
  169. * Creates Blob, writes passed data to it, then creates ObjectURL string
  170. * with blob data. In parallel, creates 'a' element, writes resulting ObjectURL
  171. * to it's href property and fileName parameter as it's download prop.
  172. * Clicks on 'a' and cleans up file data.
  173. *
  174. * @param {String} fileName
  175. * @param {Object} data
  176. */
  177. function upload(fileName, data) {
  178. var a = document.createElement('a'),
  179. blob,
  180. url;
  181. a.style = 'display: none';
  182. document.body.appendChild(a);
  183. blob = new Blob([JSON.stringify(data)], {
  184. type: 'octet/stream'
  185. });
  186. url = window.URL.createObjectURL(blob);
  187. a.href = url;
  188. a.download = fileName;
  189. a.click();
  190. window.URL.revokeObjectURL(url);
  191. }
  192. return {
  193. /**
  194. * Stores keys of 'modules' object to localStorage under 'all' namespace.
  195. *
  196. * @param {Object} modules
  197. */
  198. collect: function (modules) {
  199. storeModules(Object.keys(modules), 'all');
  200. },
  201. /**
  202. * Wraps 'module' in empty array and stores it to localStorage by 'used' namespace.
  203. *
  204. * @param {String} module
  205. */
  206. utilize: function (module) {
  207. storeModules([module], 'used');
  208. },
  209. /**
  210. * Returns modules, stores under 'all' namespace in localStorage via
  211. * getModules function.
  212. *
  213. * @return {Array}
  214. */
  215. getAll: function () {
  216. return getModules('all');
  217. },
  218. /**
  219. * Returns modules, stores under 'used' namespace in localStorage via
  220. * getModules function.
  221. *
  222. * @return {Array}
  223. */
  224. getUsed: function () {
  225. return getModules('used');
  226. },
  227. /**
  228. * Returns difference between arrays stored under 'all' and 'used'.
  229. *
  230. * @return {Array}
  231. */
  232. getUnused: function () {
  233. var all = getModules('all'),
  234. used = getModules('used');
  235. return difference(all, used);
  236. },
  237. /**
  238. * Clears "all" and "used" namespaces of localStorage.
  239. */
  240. clear: function () {
  241. storage.removeItem('all');
  242. storage.removeItem('used');
  243. },
  244. /**
  245. * Create blob containing stats data and download it
  246. */
  247. export: function () {
  248. upload('Magento Bundle Statistics', {
  249. used: this.getUsed(),
  250. unused: this.getUnused(),
  251. all: this.getAll()
  252. });
  253. }
  254. };
  255. });
  256. /**
  257. * Extension of a requirejs 'load' method
  258. * to load files from a build object.
  259. */
  260. define('jsbuild', [
  261. 'module',
  262. 'buildTools',
  263. 'statistician'
  264. ], function (module, tools, statistician) {
  265. 'use strict';
  266. var build = module.config() || {};
  267. if (!tools.isEnabled) {
  268. return;
  269. }
  270. require._load = require.load;
  271. statistician.collect(build);
  272. /**
  273. * Overrides requirejs main loading method to provide
  274. * support of scripts initialization from a bundle object.
  275. *
  276. * @param {Object} context
  277. * @param {String} moduleName
  278. * @param {String} url
  279. */
  280. require.load = function (context, moduleName, url) {
  281. var relative = tools.removeBaseUrl(url, context.config),
  282. data = build[relative];
  283. if (data) {
  284. statistician.utilize(relative);
  285. new Function(data)();
  286. context.completeLoad(moduleName);
  287. } else {
  288. require._load.apply(require, arguments);
  289. }
  290. };
  291. });
  292. /**
  293. * Extension of a requirejs text plugin
  294. * to load files from a build object.
  295. */
  296. define('text', [
  297. 'module',
  298. 'buildTools',
  299. 'mage/requirejs/text'
  300. ], function (module, tools, text) {
  301. 'use strict';
  302. var build = module.config() || {};
  303. if (!tools.isEnabled) {
  304. return text;
  305. }
  306. text._load = text.load;
  307. /**
  308. * Overrides load method of a 'text' plugin to provide support
  309. * of loading files from a build object.
  310. *
  311. * @param {String} name
  312. * @param {Function} req
  313. * @param {Function} onLoad
  314. * @param {Object} config
  315. */
  316. text.load = function (name, req, onLoad, config) {
  317. var url = req.toUrl(name),
  318. relative = tools.removeBaseUrl(url, config),
  319. data = build[relative];
  320. data ?
  321. onLoad(data) :
  322. text._load.apply(text, arguments);
  323. };
  324. return text;
  325. });