-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcore.js
300 lines (250 loc) · 8.25 KB
/
core.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
// Web component and service registry
'use strict';
/**
* Web container for all components and services.
*/
var web = (function web() {
/**
* Declare & define web and private variables
* @private
*/
var web = {};
var serviceRegistry = {};
var componentDefaultOptions = {};
var componentRegistry = {};
var componentInstances = {};
// ------- SERVICE HANDLING -------
/**
* Define a new service
* @method registerService
* @param {String} name
* @param {Object} service
*/
web.registerService = function registerService(name, service) {
if (typeof serviceRegistry[name] === 'undefined') {
serviceRegistry[name] = service;
}
};
/**
* Get the service by name
* @param {String} name
* @method getService
* @return {Object} service interface
*/
web.getService = function getService(name) {
// Service not registered
if (!web.hasService(name)) {
web.emitError('Service with the name: ' + name + ' is not registered.');
return;
}
return serviceRegistry[name];
};
/**
* Checks if a service with this name exist
* @param {String} name
* @method hasService
*/
web.hasService = function hasService(name) {
return (typeof serviceRegistry[name] !== 'undefined');
};
/**
* Call a service method by specified parameters
* @param {string} name
* @param {string} method
* @param {string} args
* @method callService
* @return {Object} service function return value
*/
web.callService = function callService(name, method, args) {
var service = web.getService(name);
if (!service) {
return;
}
return service[method].apply(service, args);
};
/**
* Call services functions with specified parameters
* @param {Array} services
* @method callServices
* @return {Array} results
*/
web.callServices = function callServices(services) {
var results = [];
// Cycle through registered services and call the battery of methods with args
services.forEach(function(service) {
results.push(web.callService(service.name, service.func, service.args));
});
return results;
};
// ------- COMPONENT HANDLING -------
/**
* Starts a new component from the component blueprint
* @param {String} name
* @param {String} id
* @param {Object} options
* @method startComponent
* @return {Object} instance
*/
web.startComponent = function startComponent(name, id, options) {
var instance;
var Component;
// Check if component type is available
if (!web.hasComponent(name)) {
web.emitError('Component with the type ' + name + ' is not registered.');
return;
}
// Instantiate object from base component and init it
Component = web.getBaseComponent(name);
if (Component.length === 0) {
console.warn('@sulu/web: Component "' + name + '" with initialize is deprecated and should be upgraded.');
instance = new Component();
instance.initialize(web.getElement(id), Object.assign({}, componentDefaultOptions[name], options));
} else {
instance = new Component(web.getElement(id), Object.assign({}, componentDefaultOptions[name], options));
}
// Add component instance to registry
componentInstances[id] = instance;
return instance;
};
/**
* Starts components instantiation from the registry
* @param {Array} components
* @method startComponents
*/
web.startComponents = function startComponents(components) {
components.forEach(function(component) {
try {
web.startComponent(component.name, component.id, component.options);
} catch (error) {
web.emitError('Component "' + component.name + '" start failed for "' + component.id + '":', error);
}
});
};
/**
* Checks if a component with this name exist
* @param {String} name
* @method hasComponent
*/
web.hasComponent = function hasComponent(name) {
return (typeof componentRegistry[name] !== 'undefined');
};
/**
* Get the component dom element.
* @param {String} id
* @return {HTMLElement}
* @method getElement
* @private
*/
web.getElement = function(id) {
return document.getElementById(id);
};
/**
* Removes its DOM element
* @param {String} id
* @method removeElement
* @private
*/
web.removeElement = function removeElement(id) {
web.getElement(id).remove();
};
/**
* Get the base component by type name
* @method getBaseComponent
* @param {String} name
* @return {Object}
*/
web.getBaseComponent = function getBaseComponent(name) {
return componentRegistry[name];
};
/**
* Get a base component from the Component Factory and enrich it
* with custom component properties
* @param {String} name
* @param {Object} component
* @param {Object} defaultOptions
* @method registerComponent
*/
web.registerComponent = function registerComponent(name, component, defaultOptions) {
var instance;
componentDefaultOptions[name] = defaultOptions || {};
if (typeof component === 'object') {
// When component is a object create function to allow new on it.
instance = function() {};
Object.assign(instance.prototype, component);
componentRegistry[name] = instance;
return;
}
componentRegistry[name] = component;
};
/**
* Get a Component instance by id
* @param {String} id component instance identifier defined with the ’startComponent’ method
* @method getComponent
* @return {Object} component instance
*/
web.getComponent = function getComponent(id) {
if (typeof componentInstances[id] === 'undefined') {
web.emitError('Component instance with id ' + id + ' not found.');
return;
}
return componentInstances[id];
};
/**
* Facade for destroying a component
* @param {Object|String} component Could be an instance or the component id
* @method removeComponent
* @return {Object}
*/
web.removeComponent = function removeComponent(component) {
if (typeof component === 'string') {
web.destroyComponent(component);
return;
}
web.destroyComponent(component.id);
};
/**
* Destroys a component and removes its DOM element
* @param {String} id
* @method destroyComponent
* @private
*/
web.destroyComponentInstance = function destroyComponentInstance(id) {
if (typeof componentInstances[id] === 'undefined') {
web.emitError('Component with id ' + id + ' could not be destroyed, it was not found.');
return;
}
// Calls pre-destroy function of component
if (typeof componentInstances[id].preDestroy === 'function') {
componentInstances[id].preDestroy();
}
// Remove DOM element of component instance
web.removeElement(id);
// Delete instance from our registry
delete componentInstances[id];
};
/**
* Handle error messages
* @method emitError
*/
web.emitError = function emitError() {
/* eslint-disable no-console */
if (console && console.error) {
console.error.apply(null, arguments);
}
/* eslint-enable no-console */
};
return {
registerComponent: web.registerComponent,
registerService: web.registerService,
startComponents: web.startComponents,
callServices: web.callServices,
startComponent: web.startComponent,
callService: web.callService,
getService: web.getService,
getComponent: web.getComponent,
hasComponent: web.hasComponent,
hasService: web.hasService,
removeComponent: web.removeComponent,
};
})();
module.exports = window.web = web;