Skip to content

Commit 212e689

Browse files
committed
progress refactoring
1 parent 6f39626 commit 212e689

File tree

271 files changed

+13922
-3722
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

271 files changed

+13922
-3722
lines changed

dist/esm/Doz.js renamed to cjs-src/Doz.js

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
1-
import bind from "./utils/bind.js";
2-
import createInstance from "./component/createInstance.js";
3-
import { TAG, REGEX, ALREADY_WALKED } from "./constants.js";
4-
import toLiteralString from "./utils/toLiteralString.js";
5-
import plugin from "./plugin/index.js";
6-
import directive from "./directives/index.js";
7-
import makeSureAttach from "./component/makeSureAttach.js";
1+
const bind = require('./utils/bind');
2+
const createInstance = require('./component/createInstance');
3+
const {TAG, REGEX, ALREADY_WALKED} = require('./constants');
4+
const toLiteralString = require('./utils/toLiteralString');
5+
const plugin = require('./plugin');
6+
const directive = require('./directives');
7+
const makeSureAttach = require('./component/makeSureAttach');
88
let appCounter = 0;
9+
910
class Doz {
11+
1012
constructor(cfg = {}) {
13+
1114
this.baseTemplate = `<${TAG.APP}></${TAG.APP}>`;
15+
1216
if (REGEX.IS_ID_SELECTOR.test(cfg.root)) {
1317
cfg.root = document.getElementById(cfg.root.substring(1));
1418
}
19+
1520
if (REGEX.IS_ID_SELECTOR.test(cfg.template)) {
1621
cfg.template = document.getElementById(cfg.template.substring(1));
1722
cfg.template = cfg.template.innerHTML;
1823
}
24+
1925
if (!(cfg.root instanceof HTMLElement || cfg.root instanceof ShadowRoot)) {
2026
throw new TypeError('root must be an HTMLElement or an valid ID selector like #example-root');
2127
}
28+
2229
if (!cfg.mainComponent && !(cfg.template instanceof HTMLElement || typeof cfg.template === 'string' || typeof cfg.template === 'function')) {
2330
throw new TypeError('template must be a string or an HTMLElement or a function or an valid ID selector like #example-template');
2431
}
32+
2533
if (cfg.root.hasChildNodes) {
26-
const appNode = cfg.root.firstElementChild; // document.querySelector(TAG.APP);
34+
const appNode = cfg.root.firstElementChild;// document.querySelector(TAG.APP);
2735
// This fix double app rendering in SSR
2836
makeSureAttach(appNode);
2937
if (appNode && !appNode._dozAttach[ALREADY_WALKED]) {
3038
appNode.parentNode.removeChild(appNode);
3139
}
3240
}
41+
3342
this.cfg = Object.assign({}, {
3443
components: [],
3544
shared: {},
@@ -41,6 +50,7 @@ class Doz {
4150
autoDraw: true,
4251
enableExternalTemplate: false
4352
}, cfg);
53+
4454
Object.defineProperties(this, {
4555
_lastUId: {
4656
value: 0,
@@ -72,6 +82,7 @@ class Doz {
7282
cb.call(cb._instance);
7383
}
7484
});
85+
7586
this._onAppReadyCB = [];
7687
}
7788
},
@@ -139,13 +150,17 @@ class Doz {
139150
},
140151
mount: {
141152
value: function (template, root, parent = this._tree) {
153+
142154
if (typeof root === 'string') {
143155
root = document.querySelector(root);
144156
}
157+
145158
root = root || parent._rootElement;
159+
146160
if (!(root instanceof HTMLElement)) {
147161
throw new TypeError('root must be an HTMLElement or an valid selector like #example-root');
148162
}
163+
149164
const contentStr = this.cfg.enableExternalTemplate ? eval('`' + toLiteralString(template) + '`') : template;
150165
const autoCmp = {
151166
tag: TAG.MOUNT,
@@ -157,6 +172,7 @@ class Doz {
157172
}
158173
}
159174
};
175+
160176
return createInstance({
161177
root,
162178
template: `<${TAG.MOUNT}></${TAG.MOUNT}>`,
@@ -169,34 +185,36 @@ class Doz {
169185
enumerable: true
170186
}
171187
});
188+
172189
if (Array.isArray(this.cfg.components)) {
173190
this.cfg.components.forEach(cmp => {
174191
if (typeof cmp === 'object' && typeof cmp.tag === 'string' && typeof cmp.cfg === 'object') {
175192
this._components[cmp.tag] = cmp;
176193
}
177194
});
178-
}
179-
else if (typeof this.cfg.components === 'object') {
195+
} else if (typeof this.cfg.components === 'object') {
180196
Object.keys(this.cfg.components).forEach(objName => {
181197
this._components[objName] = {
182198
tag: objName,
183199
cfg: this.cfg.components[objName]
184-
};
200+
}
185201
});
186202
}
203+
187204
plugin.load(this);
188205
directive.callAppInit(this);
206+
189207
if (this.cfg.mainComponent) {
190208
this._tree = createInstance({
191209
mountMainComponent: true,
192210
root: this.cfg.root,
193211
component: this.cfg.mainComponent,
194212
app: this,
195213
innerHTML: this.cfg.innerHTML
196-
}); // || [];
214+
});// || [];
215+
197216
//console.log(this._tree)
198-
}
199-
else {
217+
} else {
200218
this._components[TAG.APP] = {
201219
tag: TAG.APP,
202220
cfg: {
@@ -209,70 +227,89 @@ class Doz {
209227
}
210228
}
211229
};
230+
212231
Object.keys(cfg).forEach(p => {
213232
if (!['template', 'root'].includes(p))
214233
this._components[TAG.APP].cfg[p] = cfg[p];
215234
});
235+
216236
}
237+
217238
//Apply listeners
218239
if (this.cfg.listeners) {
219240
Object.keys(this.cfg.listeners).forEach(event => {
220241
//console.log(event)
221-
this.on(event, this.cfg.listeners[event]);
222-
});
242+
this.on(event, this.cfg.listeners[event])
243+
})
223244
}
245+
224246
if (!this.cfg.mainComponent && this.cfg.autoDraw)
225247
this.draw();
248+
226249
this.canAppReady();
227250
}
251+
228252
canAppReady() {
229253
if (this._onAppComponentsMounted.size) {
230254
setTimeout(() => {
231255
this.canAppReady();
232-
});
233-
}
234-
else {
256+
})
257+
} else {
235258
this._callAppReady();
236259
this.emit('ready', this);
237260
}
238261
}
262+
239263
draw() {
264+
240265
if (!this.cfg.autoDraw)
241266
this.cfg.root.innerHTML = '';
242267
this._tree = createInstance({
243268
root: this.cfg.root,
244269
template: this.baseTemplate,
245270
app: this
246-
}); // || [];
271+
});// || [];
272+
247273
return this;
248274
}
275+
249276
get mainComponent() {
250277
return this._tree;
251278
}
279+
252280
on(event, callback) {
253281
if (typeof event !== 'string')
254282
throw new TypeError('Event must be a string');
283+
255284
if (typeof callback !== 'function')
256285
throw new TypeError('Callback must be a function');
286+
257287
if (!this._onAppCB[event]) {
258288
this._onAppCB[event] = [];
259289
}
290+
260291
this._onAppCB[event].push(callback);
292+
261293
return this;
262294
}
295+
263296
emit(event, ...args) {
264297
if (this._onAppCB[event]) {
265298
this._onAppCB[event].forEach(func => {
266299
func.apply(this, args);
267300
});
268301
}
302+
269303
if (this.onAppEmit) {
270-
this.onAppEmit(event, ...args);
304+
this.onAppEmit(event, ...args)
271305
}
306+
272307
return this;
273308
}
309+
274310
generateUId() {
275311
return this.appId + '-' + (++this._lastUId);
276312
}
277313
}
278-
export default Doz;
314+
315+
module.exports = Doz;
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import Doz from "./Doz.js";
1+
const Doz = require('./Doz');
2+
23
function appCreate(root, component, options) {
4+
35
let cfg = Object.assign({
46
root,
57
mainComponent: component,
68
byAppCreate: true
79
}, options);
10+
811
return new Doz(cfg);
912
}
10-
export default appCreate;
13+
14+
module.exports = appCreate;
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import data from "./data.js";
1+
const data = require('./data');
2+
23
/**
34
* Register a component to global
45
* @param cmp
56
*/
67
function registerComponent(cmp) {
8+
79
const tag = cmp.tag.toUpperCase();
10+
811
if (Object.prototype.hasOwnProperty.call(data.components, tag))
912
console.warn('Doz', `component ${tag} overwritten`);
13+
1014
data.components[tag] = cmp;
1115
}
16+
1217
/**
1318
* Remove all global components
1419
*/
@@ -17,6 +22,7 @@ function removeAll() {
1722
//data.plugins = [];
1823
//data.directives = {};
1924
}
25+
2026
/**
2127
* Get component from global
2228
* @param tag
@@ -26,55 +32,59 @@ function getComponent(tag) {
2632
tag = tag.toUpperCase();
2733
return data.components[tag];
2834
}
35+
2936
/**
3037
* Register a plugin to global
3138
* @param plugin
3239
*/
3340
function registerPlugin(plugin) {
3441
data.plugins.push(plugin);
3542
}
43+
3644
/**
3745
* Register a directive to global
3846
* @param name
3947
* @param cfg
4048
*/
4149
function registerDirective(name, cfg = {}) {
50+
4251
if (typeof name !== 'string') {
4352
throw new TypeError('Doz directive name must be a string');
4453
}
54+
4555
if (typeof cfg !== 'object' || !cfg) {
4656
throw new TypeError('Doz directive config must be an object');
4757
}
58+
4859
if (name[0] === ':') {
4960
cfg._onlyDozComponent = true;
5061
name = name.substr(1);
5162
}
63+
5264
name = name.toLowerCase();
5365
let namePart = [];
5466
if (name.indexOf('-') !== -1) {
5567
namePart = name.split('-');
5668
name = namePart[0];
5769
namePart.shift();
5870
}
71+
5972
cfg.name = name;
6073
cfg._keyArguments = namePart.map(item => item.substr(1)); // remove $
74+
6175
if (Object.prototype.hasOwnProperty.call(data.directives, name))
6276
console.warn('Doz', `directive ${name} overwritten`);
77+
6378
data.directives[name] = cfg;
6479
if (!data.directivesKeys.includes(name))
6580
data.directivesKeys.push(name);
6681
}
67-
export { registerComponent };
68-
export { registerPlugin };
69-
export { getComponent };
70-
export { registerDirective };
71-
export { removeAll };
72-
export { data };
73-
export default {
82+
83+
module.exports = {
7484
registerComponent,
7585
registerPlugin,
7686
getComponent,
7787
registerDirective,
7888
removeAll,
7989
data
80-
};
90+
};

dist/esm/component/Base.js renamed to cjs-src/component/Base.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
class Base {
2+
23
constructor(opt = {}) {
34
opt.cmp = opt.cmp || {
45
tag: opt.tag,
56
cfg: {}
67
};
8+
79
opt.app = opt.app || {};
10+
811
Object.defineProperties(this, {
912
//Private
1013
_opt: {
@@ -94,6 +97,7 @@ class Base {
9497
_componentsMap: {
9598
value: new Map()
9699
},
100+
97101
//Public
98102
tag: {
99103
value: opt.cmp.tag,
@@ -195,4 +199,5 @@ class Base {
195199
});
196200
}
197201
}
198-
export default Base;
202+
203+
module.exports = Base;

0 commit comments

Comments
 (0)