-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-namespace.js
146 lines (135 loc) · 4.95 KB
/
js-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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* @description js-namespace: A simple way to create and use namespaces in javascript. No dependencies.
* @author <a href="http://www.bitofnothing.com" alt="Kyle Thielk Personal Blog">Kyle Thielk</a>
*
**/
/**
* @license LICENSED UNDER THE MIT LICENSE
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function (window)
{
/**
* @private
* @type {Array}
*/
var rootNamespaces = [];
/**
* Retrieves a namespace from the global namespace and creates it if it does not exist.
*
* Sample Usage:
*
* var model = JsNamespace("Company.SubNamespace1");
* model.MyClass = function(){}
*
* OR
*
* JsNamespace("RootNamespace.SubNamespace1").MyClass = function(){}
*
* @param {String} namespaceString The name of the namespace i.e RootNamespace.SubNamespace1 .
* @return {Object} The object containing the namespace.
*/
var JsNamespace = function (namespaceString)
{
return new JsNamespace.fn.init(namespaceString);
};
/**
* @private
* @type {Object}
*/
JsNamespace.fn = {
/**
* See JsNamespace() for more thorough description.
* @param {String} namespaceString The string for the namespace i.e Company.SubNamespace.SubNamespace2.
* @return {Object} The object containing the namespace.
*/
init:function (namespaceString)
{
var partNames = namespaceString.split('.'),
parent = {},
currentPartName = '';
//Add the root object to the global namespace if it does not exist
if (!window[partNames[0]])
{
window[partNames[0]] = {};
window[partNames[0]].isNamespace = true;
parent = window[partNames[0]];
rootNamespaces[partNames[0]] = parent;
}
else
{
parent = window[partNames[0]];
}
//Remove root partName
partNames = partNames.slice(1);
for (var i = 0, length = partNames.length; i < length; i++)
{
currentPartName = partNames[i];
if(parent[currentPartName] && !parent[currentPartName]['isNamespace'])
{
throw "Cannot define a namespace inside a non-namespace object.";
}
parent[currentPartName] = parent[currentPartName] || {};
parent[currentPartName]['isNamespace'] = true;
parent = parent[currentPartName];
}
return parent;
}
};
/**
* In order to use
*
* JsNamespace("Company.SubNamespace1").MyClass = {}
* Company.SubNamespace1.MyClass.className
*
* we must first call this method after all namespaces have been defined.
*
* In general I recommend putting this in your onload, .ready() or equivalent
* event listener to ensure all javascript has been loaded before calling.
*
*/
JsNamespace.buildClassNames = function ()
{
for (var namespaceName in rootNamespaces)
{
JsNamespace.buildClassNamesForRoot(rootNamespaces[namespaceName], namespaceName);
}
};
/**
* @private
* @param {*=} parent Initial call to this method should be undefined.
* @param {*=} name The name so far. Undefined on initial call to this method.
*/
JsNamespace.buildClassNamesForRoot = function (parent, name)
{
if (parent && parent['isNamespace'] && parent['isNamespace'] == true)
{
for (var id in parent)
{
if (id != "isNamespace")
{
parent[id].className = name + "." + id;
}
}
for (var id in parent)
{
JsNamespace.buildClassNamesForRoot(parent[id], name + "." + id);
}
}
};
window.JsNamespace = JsNamespace;
})(window);