-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSSP.js
More file actions
179 lines (145 loc) · 3.99 KB
/
SSP.js
File metadata and controls
179 lines (145 loc) · 3.99 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* SSP - The Simple Singleton Pattern | A solution to many problems
* @author Dennis Calazans
* @maintainer Rodolfo Dias
* @version v0.1.0
* @link http://simplesingletonpattern.com
* @license BSD
*/
var SSP = SSP || {};
/**
* SSP's Module Namespace
* @type {String}
*/
SSP._nameSpace = "SSP";
/**
* Method delegation
*
* Using this method,
* you can create an anonymous function able to invoke a method
* inside an determined scope
*
* @param {Object} scope Scope Object
* @param {function} method The method that will be invoked
* @return {function} Delegated method
*/
SSP.delegate = function(scope, method) {
var argumentsLength = arguments.length, methodArguments, i;
if(argumentsLength > 2) {
methodArguments = [];
for(i = 2; i < argumentsLength; i++) {
methodArguments.push(arguments[i]);
}
return function() {
method.apply(scope, methodArguments);
}
} else {
return function() {
method.apply(scope,arguments);
};
}
};
/**
* Initialize all (arguments.length == 0) or several modules under SSP
* @param object [Module,[...]]
* @return void
*/
SSP.init = function(Module) {
var modulesLength = arguments.length;
if(Module !== undefined && Module !== SSP) {
SSP.setUp();
}
if(modulesLength === 0 || modulesLength === 1) {
this.readModule(Module || SSP);
} else if(modulesLength > 1) {
SSP.readModule.apply(this, arguments);
}
};
//Main method
SSP.setUp = function() {};
/**
* Look for child Modules
*/
SSP.readModule = function(Module) {
var node, i=0;
if(arguments.length > 1) {
for(;i<arguments.length;i++)
SSP.readModule(arguments[i]);
return;
}
if(typeof Module != "object")
return false;
if(Module.hasOwnProperty('setUp') && typeof Module.setUp == "function")
Module.setUp();
else
return false;
for(node in Module) {
if(Module.hasOwnProperty(node) === true) {
if(Module[node] !== null && typeof Module[node] == "object") {
//Auxiliar function to recover parent Module
Module[node]['parent'] = function() {
return Module;
};
//Mudule nameSpace definition
Module[node]['_nameSpace'] = (Module['_nameSpace'] || "SSP") + '.'+node;
SSP.readModule(Module[node]);
}
}
}
return false;
};
/**
* Call SSP.init method through a given namespace string
* @param {String} namespace Module's namespace
* @return {Object} null
*/
SSP.initModuleByNamespace = function(namespace) {
var argumentsLength, iArguments, argumentsArray;
argumentsArray = [];
for(iArguments = 0; iArguments < argumentsLength; iArguments++)
argumentsArray.push(SSP.getByNamespace(arguments[iArguments]));
SSP.init.apply(this, argumentsArray);
}
/**
* Return an object by a given namespace
* @param {String} namespace Target's namespace. Example: SSP.Audio._url
* @return {Object} Namespace's Target
*/
SSP.getByNamespace = function(namespace) {
var spaces, spacesLength, iSpace, space, target;
spaces = namespace.split(".");
if(spaces.shift() != "SSP")
return null;
spacesLength = spaces.length;
target = this;
for(iSpace = 0; iSpace < spacesLength; iSpace++) {
space = spaces[iSpace];
if(target.hasOwnProperty(space))
target = target[space];
}
return target;
}
/**
* Invoke a method by it's namespace
* @param String nameSpace Method's namespace
* @param Array params Array of parameters
* @return object Returned value of the method
*/
SSP.applyByNamespace = function(nameSpace, params) {
var node, nodes, scopes, target, i;
if(params !== undefined && params instanceof Array === false)
params = [params];
scopes = [window];
nodes = nameSpace.split('.');
for(i=0;i<nodes.length;i++) {
node = nodes[i];
if(scopes[i].hasOwnProperty(node))
scopes.push(scopes[i][node]);
}
target = scopes.pop();
if(typeof target === 'function') {
return target.apply(scopes.pop(), params || []);
} else {
return target;
}
}