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