-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules-in-namespace.js
115 lines (89 loc) · 3.54 KB
/
modules-in-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
(function (root) {
"use strict";
// The constructor for a new namespace
var Namespace = function (options) {
var defaultOptions = {
// Specifies the namespace's root name
rootName: "NSModuleRoot",
// Specifies the module's names delimiter
delimiter: ".",
// Turns on a simple console debugging
debug: false,
// Adds internal properties to the newly created modules, under
// the _namespace_ property:
// name: The modules name (the last part of it)
// prefix: Preceding modules, if any
// fullName: Prefix + name
addProperties: false,
// Adds parent-child relation to the newly created modules, so
// that you can access the child module from its parent,
// without the need of using the namespace object at all
addParentChildRelation: true
};
this.opt = this.defaults(defaultOptions, options);
this.namespace = new this.NSModule(this.opt.rootName, this.opt);
};
if (root.Namespace) {
throw "root.Namespace already polluted";
} else {
root.Namespace = Namespace;
}
// Simple class for modules
// Options are addProperties, debug. The descriptions stands the same
// as for Namespace.
Namespace.prototype.NSModule = function (fullName, options) {
if (options.addProperties) {
var nameSplitted = fullName.split(options.delimiter);
var lastPart = nameSplitted.pop();
var prefix = nameSplitted.join(options.delimiter);
this._namespace_ = {
name: lastPart,
prefix: prefix,
fullName: fullName
};
}
if (options.debug) {
console.log("NSModule created " + fullName, this);
}
};
// Creates one module and adds it to the namespace
Namespace.prototype.createModule = function (fullName, name, parentModule) {
if (!this.namespace[fullName]) {
this.namespace[fullName] = new this.NSModule(fullName, this.opt);
}
if (this.opt.addParentChildRelation) {
parentModule[name] = parentModule[name] || this.namespace[fullName];
}
};
// Creates multiple parent-child-related modules and adds them to
// the namespace
Namespace.prototype.use = function (fullName) {
var nameSplitted = fullName.split(this.opt.delimiter);
var parentModule = this.namespace;
var currentFullName = "";
var currentName = "";
// Create every module specified in "name", if it doesn't exist yet
for (var i = 0; i < nameSplitted.length; i++) {
currentFullName += nameSplitted[i];
currentName = nameSplitted[i];
this.createModule(currentFullName, currentName, parentModule);
parentModule = parentModule[currentName];
currentFullName += this.opt.delimiter;
}
// Return the newly created module
return this.namespace[fullName];
};
// Add a getter for the root module for convenience
Namespace.prototype.getRoot = function () {
return this.namespace;
};
Namespace.prototype.defaults = function (defaultOptions, options) {
options = options || {};
for (var key in defaultOptions) {
if (options.hasOwnProperty(key)) {
defaultOptions[key] = options[key];
}
}
return defaultOptions;
};
})(this);