-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpod.js
331 lines (280 loc) · 11.8 KB
/
pod.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Pod
* AngularJS [P]lugin [O]riented [D]evelopment Framework
*
* @author smakalande
* @version 1.0
*
*/
let POD_DEBUG = false;
angular.module('pod', [])
.factory('constants', function() {
return {
paramPrefix: 'pod-param-',
targetElement: 'pod-target',
podName: 'podName',
podType: 'podType',
args: 'podArgs',
postFunc: 'podPost',
podSort: 'podSort'
};
})
.factory('utils', function() {
return {
toSpinalCase: function(camelCaseString) {
return camelCaseString.replace(/([^A-Z])([A-Z])/g, '$1-$2').toLowerCase();
}
};
})
.factory('$podLink', ['utils', 'constants','$rootScope', '$stateRegistry', function(utils, constants, $rootScope, $stateRegistry) {
let link = {
states: []
};
function getLinkEntry(moduleLink, type, key) {
let keySegments = key.split('.');
if(keySegments.length >= 1) {
let s1 = keySegments[0];
if(keySegments.length >= 2) {
let s2 = keySegments[1];
if(link[s1]) {
return link[s1][type][adjust(s2, true)];
}
} else {
return moduleLink[type][s1] || moduleLink[type][adjust(s1, true)];
}
}
}
function adjust(key, isGlobal) {
return isGlobal ? '+' + key : key;
}
return {
register: function(moduleName) {
link[moduleName] = {
data: {},
subjects: {}
};
let moduleLink = link[moduleName];
return {
data: {
set: function(key, value, isGlobal) {
key = adjust(key, isGlobal);
moduleLink.data[key] = value;
},
get: function(key) {
return getLinkEntry(moduleLink, 'data', key);
}
},
cache: {
set: function(key, value) {
localStorage.setItem(key, value);
},
get: function(key) {
return localStorage.getItem(key);
}
},
events: {
pub: function(event, data) {
$rootScope.$broadcast(moduleName + '.' + event, data);
},
sub: function(event, callback, myScope) {
let scope = myScope || $rootScope;
let eventSegments = event.split('.');
if(eventSegments.length >= 2) {
scope.$on(event, callback);
} else {
scope.$on(moduleName + '.' + event, callback);
}
}
},
subjects: {
new: function(key, initialValue, isGlobal) {
key = adjust(key, isGlobal);
moduleLink.subjects[key] = new Rx.BehaviorSubject(initialValue ? initialValue : null);
return moduleLink.subjects[key];
},
next: function(key, next) {
let subject = getLinkEntry(moduleLink, 'subjects', key);
if(subject) {
subject.next(next);
}
},
sub: function(key, next, controller, error, complete) {
let subject = getLinkEntry(moduleLink, 'subjects', key);
if(subject) {
let nextToJS = function(data) {
next(data ? data : null);
};
let subscription = subject.subscribe(nextToJS, error, complete);
POD_DEBUG && console.log('Subscribing to [' + key + ']:', controller);
if(controller) {
if(controller.$on) {
controller.$on("$destroy", function() {
POD_DEBUG && console.log('Unsubscribing to [' + key + ']:', controller);
subscription.unsubscribe();
});
} else {
controller.$onDestroy = function() {
POD_DEBUG && console.log('Unsubscribing to [' + key + ']:', controller);
subscription.unsubscribe();
};
}
}
return subscription;
}
}
},
state: {
add: function(state, stateDef, isShared) {
if(isShared) {
link.states.map(s => {
$stateRegistry.register(angular.extend({}, stateDef, {
name: s.state + '.' + state
}));
link.states.push({
state: s.state + '.' + state,
isShared: false
});
});
}
function setSharedChildStates(parentState, maxLevels, currentLevel) {
let level = currentLevel || 0;
link.states.filter(s => s.isShared).map(s => {
let newState = parentState + '.' + s.state;
if(parentState.indexOf(s.state) === -1 && link.states.filter(s => s.state === newState).length === 0) {
$stateRegistry.register(angular.extend({}, s.def, {
name: parentState + '.' + s.state
}));
link.states.push({
state: parentState + '.' + s.state,
isShared: false
});
if(level < maxLevels) {
level++;
setSharedChildStates(newState, maxLevels, level);
}
}
});
};
setSharedChildStates(state, 1)
$stateRegistry.register(angular.extend({}, stateDef, {
name: state
}));
if(isShared) {
link.states.push({
state: state,
isShared: true,
def: stateDef
});
} else {
link.states.push({
state: state,
isShared: false
});
}
POD_DEBUG && console.log(link.states);
}
}
};
}
};
}])
.factory('$pod', ['utils', 'constants', function(utils, constants) {
let registry;
return {
init: function (app, pluginModules) {
registry = {};
let allModules = app.requires.concat([app.name]);
for(moduleName of allModules) {
let allComponents = angular.module(moduleName)._invokeQueue.filter(item => 'component' === item[1]);
for(comp of allComponents) {
let podType = comp[2][1][constants.podType];
if(podType) {
registry[podType] = registry[podType] || [];
registry[podType].push(angular.extend({}, comp[2][1][constants.args], {
name: comp[2][0],
sort: comp[2][1][constants.podSort] || 0
}));
}
}
}
},
getRegistry: function() {
return registry;
},
getRegisteredComponents: function(podType, podName) {
if(podName) {
return registry[podType].filter(comp => comp.name === podName);
} else {
return registry[podType];
}
},
loadStyleUrls: function(styleUrls) {
for(url of styleUrls) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}
}
};
}])
.directive('pod', ['$compile', '$pod', 'constants', 'utils', function($compile, $pod, constants, utils) {
function load(scope, element, attrs, ctrl, transclude) {
let availableComponents = $pod.getRegisteredComponents(scope[constants.podType]);
if(scope[constants.podName] !== undefined) {
availableComponents = availableComponents.filter(component => component.name === scope[constants.podName]);
}
if(!availableComponents) {
return;
}
availableComponents.sort(function(componentA, componentB) {
return componentA.sort > componentB.sort;
});
let attributeString = '';
for (let attributeName in attrs) {
let spinalAttributeName = utils.toSpinalCase(attributeName);
if (attrs.hasOwnProperty(attributeName) && spinalAttributeName.startsWith(constants.paramPrefix)) {
attributeString += ' ' + spinalAttributeName.substring(constants.paramPrefix.length, spinalAttributeName.length) + '="' + attrs[attributeName] + '"';
}
}
element.empty();
availableComponents.forEach(function(component, index) {
let template = '<' + utils.toSpinalCase(component.name) + attributeString + ' />';
let compiled = $compile(template)(scope.$parent);
transclude(function(transcludeElements, transcludeScope) {
transcludeScope.pod = component;
transcludeScope.pod.index = index;
let targetElement = transcludeElements.find(constants.targetElement);
if(targetElement.length === 1) {
targetElement.replaceWith(compiled);
element.append(transcludeElements);
} else {
element.append(compiled);
}
if(scope[constants.postFunc] !== undefined) {
scope[constants.postFunc](transcludeScope.pod);
}
});
});
}
let scopeBindings = {};
scopeBindings[constants.podType] = '=';
scopeBindings[constants.podName] = '=';
scopeBindings[constants.postFunc] = '=';
return {
restrict: 'E',
scope: scopeBindings,
transclude: true,
link: function(scope, element, attrs, ctrl, transclude) {
load(scope, element, attrs, ctrl, transclude);
if(scope[constants.podName] !== undefined) {
scope.$watch(constants.podName, function(newValue, oldValue) {
if (newValue !== undefined && oldValue !== newValue) {
load(scope, element, attrs, ctrl, transclude);
}
});
}
}
};
}]);