-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
245 lines (211 loc) · 5.7 KB
/
index.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
'use strict';
/**
* @module
*/
// ----------------------------------------
// Imports
// ----------------------------------------
import $ from 'jquery';
// ----------------------------------------
// Private
// ----------------------------------------
let instance = null;
export class ModuleDynamicImport {
constructor (settings) {
/** @private */
this._PromiseFn = settings.PromiseFn || null;
if (this._PromiseFn === null) {
throw new Error('ModuleDynamicImport Error! You must specify "Promise" property!');
}
/**
* @private
*/
this._selector = settings.selector || '.js-import';
/**
* @enum {ModuleDynamicImportModules}
* @private
*/
this._modules = settings.modules || {};
/**
* @private
*/
this._pendingCssClass = settings.pendingCssClass || '_import-pending';
/**
* @private
*/
this._loadedCssClass = settings.loadedCssClass || '_import-loaded';
/**
* @private
*/
this._executedCssClass = settings.executedCssClass || '_import-executed';
/**
* @private
*/
this._debug = settings.debug || false;
}
importModule (moduleName, $container = $(document)) {
if (!this._modules.hasOwnProperty(moduleName)) {
this._log('warn', `module "${moduleName}" is not declared`);
return this._resolveWithErrors(moduleName);
}
const $elements = this._getElements($container);
if (!$elements.length) {
return Promise.resolve();
}
return this._import(moduleName, $elements, $container);
}
importAll ($container = $(document), awaitAll = true) {
const $elements = this._getElements($container);
if (!$elements.length) {
return Promise.resolve();
}
const imports = [];
for (let moduleName in this._modules) {
if (this._modules.hasOwnProperty(moduleName)) {
imports.push(this._import(moduleName, $elements, $container));
}
}
if (awaitAll) {
return Promise.all(imports);
}
return Promise.resolve();
}
static get eventPendingName () {
return 'moduleDynamicImportPending';
}
static get eventLoadedName () {
return 'moduleDynamicImportLoaded';
}
static get eventExecutedName () {
return 'moduleDynamicImportExecuted';
}
static instance () {
if (instance === null) {
singleton.create();
}
return instance;
}
static create (settings = {}) {
if (instance instanceof ModuleDynamicImport) {
instance._log('warn', 'ModuleDynamicImport is already created');
return instance;
}
instance = new ModuleDynamicImport(settings);
return instance;
}
// ----
/**
* @param {string} moduleName
* @return {Promise}
* @private
*/
_resolveWithErrors (moduleName) {
console.warn(`ModuleDynamicImport WARN! Module "${moduleName}" resolved width errors!!!`);
return Promise.resolve();
}
/**
* @param {string} moduleName
* @param {jQuery} $elements
* @param {jQuery} $container
* @return {Promise}
* @private
*/
_import (moduleName, $elements, $container) {
/** @type ModuleDynamicImportModules */
const module = this._modules[moduleName];
if (!module.hasOwnProperty('__moduleName')) {
Object.defineProperty(module, '__moduleName', {
value: moduleName,
writable: false,
configurable: false
});
}
if (module && module.moduleFile && module.filterSelector) {
const $moduleElements = $elements.filter(module.filterSelector);
if (!$moduleElements.length) {
return Promise.resolve();
}
if (typeof module.importCondition === 'function' && module.__importConditionAllowed !== true) {
const result = module.importCondition($moduleElements, $container);
if (result === false) {
this._log('info', `module "${moduleName}" skipped by ".importCondition()"`);
return Promise.resolve();
}
}
module.__importConditionAllowed = true;
this._log('info', `module "${moduleName}" pending`);
this._markAsPending($moduleElements);
return this._PromiseFn(module.moduleFile)
.then(importedModule => {
if (typeof importedModule.default !== 'function') {
this._log('warn', `imported module "${moduleName}" - must export default method`);
return this._resolveWithErrors(moduleName);
}
this._markAsLoaded($moduleElements);
this._log('info', `module "${moduleName}" executing`);
importedModule.default($moduleElements);
this._markAsExecuted($moduleElements);
})
.catch(err => {
console.error(err);
return this._resolveWithErrors(moduleName);
});
} else {
this._log('warn', `${moduleName} is incorrect! it must have "moduleFile" and "filterSelector" properties`);
return this._resolveWithErrors(moduleName);
}
}
/**
* @param {jQuery} $container
* @return {jQuery} $container
* @private
*/
_getElements ($container) {
const $elements = $container.find(this._selector);
if (!$elements.length) {
this._log('info', `No import elements with selector "${this._selector}"`);
}
return $elements;
}
/**
* @param {jQuery} $elements
* @private
*/
_markAsPending ($elements) {
if (this._pendingCssClass) {
$elements.addClass(this._pendingCssClass);
}
$elements.trigger(ModuleDynamicImport.eventPendingName);
}
/**
* @param {jQuery} $elements
* @private
*/
_markAsLoaded ($elements) {
if (this._loadedCssClass) {
$elements.addClass(this._loadedCssClass);
}
$elements.trigger(ModuleDynamicImport.eventLoadedName);
}
/**
* @param {jQuery} $elements
* @private
*/
_markAsExecuted ($elements) {
if (this._executedCssClass) {
$elements.addClass(this._executedCssClass);
}
$elements.trigger(ModuleDynamicImport.eventExecutedName);
}
/**
* @param {string} type
* @param {string} msg
* @param {...*} [data]
* @private
*/
_log (type, msg, ...data) {
if (this._debug) {
console[type](`ModuleDynamicImport ${type}: ${msg}`, ...data);
}
}
}