forked from atesgoral/requirejs-namespace-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamespace.js
117 lines (97 loc) · 4.22 KB
/
namespace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*!
* @license RequireJS Namespace Plugin, Copyright (c) 2013 Ates Goral
* @version 0.0.1
* Loads modules into a namespace
*/
define(["module"], function(module) {
return {
load: function(name, req, onload, config) {
var localConfig = module.config(),
configObj = localConfig[name],
modules = [],
path = [name],
type = Object.prototype.toString.call(configObj),
// baseURL = type === '[object Object]' ? configObj.baseURL : name,
paths = [],
reiterate = function(configObj, propnames) {
for (var key in configObj) {
if (configObj.hasOwnProperty(key)) {
var mods = configObj[key].paths;
if (!mods) {
if (!propnames) {
propnames = [];
}
propnames.push(key);
return reiterate(configObj[key], propnames);
}
for (var i = 0, limit = mods.length; i < limit; i++) {
var module_prefix = propnames && propnames.length ? (propnames.join("/") + "/") : '';
modules.push(module_prefix + key + "/" + mods[i]);
}
insertPaths(configObj[key].paths, configObj[key].baseURL);
}
}
};
if (!configObj) {
var err = new Error("Module configuration is missing");
err.namespaceModule = name;
onload.error(err);
return;
}
var insertPaths = function(modules, baseURL) {
for (var i = 0, limit = modules.length; i < limit; i++) {
var index = modules[i].indexOf('!')
if (modules[i] != undefined) {
if (index !== -1) {
var plugin = modules[i].substr(0, index + 1),
moduleString = modules[i].substr(index + 1);
paths.push(plugin + baseURL + '/' + moduleString);
} else {
paths.push(baseURL + '/' + modules[i]);
}
}
};
}
if (configObj.baseURL) {
modules = configObj.paths;
insertPaths(modules, configObj.baseURL);
} else {
if (type === '[object String]') {
modules = configObj.split(",");
insertPaths(modules, name);
} else if (type === '[object Object]') {
reiterate(configObj);
}
}
req(paths, function() {
var namespace = {},
args = arguments;
for (var i = 0; i < modules.length; i++) {
var module = modules[i],
idx = i,
paths = module.split('/'),
paths_len = paths.length;
if (!paths_len) continue;
for (var l = 0, n = namespace; l < paths_len; l++) {
var path = paths[l],
index_plugin = path.indexOf('!');
// remove a marcacao de plugin: `json!`, `text!`, etc...
if (index_plugin >= 0) {
path = paths[l] = path.substring(index_plugin + 1, path.indexOf('.'));
}
// cria sub-objetos caso nao existam
if (!n[path]) {
n[path] = {};
}
if (l < paths_len - 1) {
// não é o ultimo indice do array
n = n[path];
}
}
n[paths[paths_len - 1]] = args[idx];
};
onload(namespace);
});
}
};
});