@aegis-framework/pandora 0.1.7 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Component.js DELETED
@@ -1,502 +0,0 @@
1
- /* eslint-disable no-unused-vars */
2
- import { callAsync, deserializeCSS } from './Util';
3
-
4
- /**
5
- * A component represents a custom HTML element, and has all of its functionality
6
- * as well as its general structure and representation self contained on it.
7
- *
8
- * @class Component
9
- */
10
- export class Component extends HTMLElement {
11
-
12
- /**
13
- * _tag {String} - The tag name for the component
14
- *
15
- * @static
16
- */
17
- static _tag;
18
-
19
- static get tag () {
20
- if (typeof this._tag === 'undefined') {
21
- let tag = this.name;
22
- const matches = tag.match (/([A-Z])/g);
23
- if (matches !== null) {
24
- for (const match of matches) {
25
- tag = tag.replace (match, `-${match}`.toLowerCase ());
26
- }
27
- }
28
- this._tag = tag.slice (1);
29
- }
30
- return this._tag;
31
- }
32
-
33
- static set tag (value) {
34
- this._tag = value;
35
- }
36
-
37
- /**
38
- * These are the types that can be set as properties on the HTML code of the
39
- * element.
40
- */
41
- static _explicitPropTypes = ['boolean', 'string', 'number'];
42
-
43
- /**
44
- *
45
- *
46
- * @static
47
- */
48
- static _template = undefined;
49
-
50
- static template (html = null, context = null) {
51
- if (html !== null) {
52
- this._template = html;
53
- document.querySelectorAll(this.tag).forEach((instance) => {
54
- if (instance._isReady) {
55
- instance.forceRender ();
56
- }
57
- });
58
- } else {
59
-
60
- // Check if no parameters were set but the HTML is still a function to be called
61
- if (typeof this._template === 'function') {
62
- return this._template.call (context);
63
- }
64
-
65
- // If this is reached, the HTML was just a string
66
- return this._template;
67
- }
68
- }
69
-
70
- constructor () {
71
- super ();
72
- this._children = this.innerHTML.trim ();
73
-
74
- // State Object for the component
75
- this._state = {};
76
-
77
- // Props Object for the component
78
- this._props = {};
79
-
80
- // List of callbacks to run once the component has been mounted successfully
81
- this._ready = [];
82
-
83
- this._connected = false;
84
- this._isReady = false;
85
-
86
- this._style = {};
87
-
88
- this._styleElement = null;
89
- }
90
-
91
- /**
92
- * width - Determines the real (computed) width of the element
93
- *
94
- * @return {int} - Computed Width of the element on pixels
95
- */
96
- get width () {
97
- return parseInt (getComputedStyle (this).width.replace ('px', ''));
98
- }
99
-
100
- set width (value) {
101
- this.style.width = value;
102
- }
103
-
104
- /**
105
- * height - Determines the real (computed) height of the element
106
- *
107
- * @return {int} - Computed height of the element on pixels
108
- */
109
- get height () {
110
- return parseInt (getComputedStyle(this).height.replace ('px', ''));
111
- }
112
-
113
- set height (value) {
114
- this.style.height = value;
115
- }
116
-
117
- get static () {
118
- return new Proxy (this.constructor, {});
119
- }
120
-
121
- set static (value) {
122
- throw new Error ('Component static properties cannot be reassigned.');
123
- }
124
-
125
- get props () {
126
- return new Proxy (this, {
127
- get: (target, key) => {
128
- if (this.hasAttribute (key)) {
129
- let value = this.getAttribute (key);
130
- if (typeof value === 'string') {
131
- if (value === 'false') {
132
- value = false;
133
- } else if (value === 'true' || value === '') {
134
- value = true;
135
- } else if (!isNaN (value)) {
136
- if (value.indexOf ('.') > 0) {
137
- value = parseFloat (value);
138
- } else {
139
- value = parseInt (value);
140
- }
141
- }
142
- }
143
- return value;
144
- } else if (key in this._props) {
145
- return this._props[key];
146
- }
147
- return null;
148
- },
149
- set: (target, key, value) => {
150
- throw new Error ('Component props should be set using the `setProps` function.');
151
- }
152
- });
153
- }
154
-
155
- set props (value) {
156
- if (this._connected === false) {
157
- this._props = Object.assign ({}, this._props, value);
158
- } else {
159
- throw new Error ('Component props cannot be directly assigned. Use the `setProps` function instead.');
160
- }
161
- }
162
-
163
- get state () {
164
- return new Proxy (this._state, {
165
- get: (target, key) => {
166
- return target[key];
167
- },
168
- set: (target, key, value) => {
169
- if (this._connected === false) {
170
- return target[key] = value;
171
- } else {
172
- throw new Error ('Component state should be set using the `setState` function instead.');
173
- }
174
-
175
- }
176
- });
177
- }
178
-
179
- set state (value) {
180
- if (this._connected === false) {
181
- this._state = Object.assign ({}, this._state, value);
182
- } else {
183
- throw new Error ('Component state should be set using the `setState` function instead.');
184
- }
185
- }
186
-
187
- get dom () {
188
- return this;
189
- }
190
-
191
- set dom (value) {
192
- throw new Error ('Component DOM can not be overwritten.');
193
- }
194
-
195
- /**
196
- * register - Register the component as a custom HTML element
197
- * using the component's tag as the actual element tag.
198
- *
199
- * This action cannot be reverted nor the controller class for
200
- * the element can be changed.
201
- */
202
- static register () {
203
- window.customElements.define (this.tag, this);
204
- }
205
-
206
- /**
207
- * template - A simple function providing access to the basic HTML
208
- * structure of the component.
209
- *
210
- * @param {function|string} html - A string or function that renders the
211
- * component into a valid HTML structure.
212
- *
213
- * @returns {void|string} - Void or the HTML structure in a string
214
- */
215
- template (html = null) {
216
- return this.static.template (html, this);
217
- }
218
-
219
- _createStyleElement () {
220
- const sharedStyle = document.body.querySelector (`style#${this.static.tag}`);
221
-
222
- if (sharedStyle !== null) {
223
- this._styleElement = sharedStyle;
224
- }
225
-
226
- if (!(this._styleElement instanceof HTMLStyleElement)) {
227
- this._styleElement = document.createElement ('style');
228
- this._styleElement.id = this.static.tag;
229
- document.body.prepend (this._styleElement);
230
- }
231
- }
232
-
233
- setStyle (style, reset = false) {
234
- this._createStyleElement ();
235
-
236
- if (typeof style === 'object') {
237
- if (reset === false) {
238
- this._style = Object.assign ({}, this._style, style);
239
- } else {
240
- this._style = Object.assign ({}, style);
241
- }
242
- this._styleElement.innerHTML = deserializeCSS (this._style, this.static.tag);
243
- } else if (typeof style === 'string') {
244
- if (reset === false) {
245
- this._styleElement.innerHTML += style;
246
- } else {
247
- this._styleElement.innerHTML = style;
248
- }
249
- }
250
-
251
- return this._style;
252
- }
253
-
254
- setState (state) {
255
- if (typeof state === 'object') {
256
- const oldState = Object.assign ({}, this._state);
257
-
258
- this._state = Object.assign ({}, this._state, state);
259
-
260
- for (const key of Object.keys (state)) {
261
- this.updateCallback (key, oldState[key], this._state[key], 'state', oldState, this._state);
262
- }
263
- } else {
264
- throw new TypeError(`A state must be an object. Received ${typeof state}.`);
265
- }
266
- }
267
-
268
- setProps (props) {
269
- if (typeof props === 'object') {
270
- const oldProps = Object.assign ({}, this._props);
271
-
272
- this._props = Object.assign ({}, this._props, props);
273
-
274
- for (const key of Object.keys (props)) {
275
- this.updateCallback (key, oldProps[key], this._props[key], 'props', oldProps, this._props);
276
- }
277
- this._setPropAttributes (true);
278
- } else {
279
- throw new TypeError(`Props must be an object. Received ${typeof state}.`);
280
- }
281
- }
282
-
283
- _setPropAttributes (update = false) {
284
- for (const key of Object.keys (this._props)) {
285
- const value = this._props[key];
286
- if (this.static._explicitPropTypes.indexOf (typeof value) > -1) {
287
- if (update === true) {
288
- this.setAttribute (key, this._props[key]);
289
- } else {
290
- this._props[key] = this.props[key];
291
- this.setAttribute (key, this.props[key]);
292
- }
293
- }
294
- }
295
- }
296
-
297
- /*
298
- * =========================
299
- * Update Cycle
300
- * =========================
301
- */
302
-
303
- willUpdate (origin, property, oldValue, newValue, oldObject, newObject) {
304
- return Promise.resolve ();
305
- }
306
-
307
- update (origin, property, oldValue, newValue, oldObject, newObject) {
308
- return Promise.resolve ();
309
- }
310
-
311
- didUpdate (origin, property, oldValue, newValue, oldObject, newObject) {
312
- return Promise.resolve ();
313
- }
314
-
315
- onStateUpdate (property, oldValue, newValue, oldObject, newObject) {
316
- return Promise.resolve ();
317
- }
318
-
319
- onPropsUpdate (property, oldValue, newValue, oldObject, newObject) {
320
- return Promise.resolve ();
321
- }
322
-
323
- /*
324
- * =========================
325
- * Mount Cycle
326
- * =========================
327
- */
328
-
329
- willMount () {
330
- return Promise.resolve ();
331
- }
332
-
333
- didMount () {
334
- return Promise.resolve ();
335
- }
336
-
337
- /*
338
- * =========================
339
- * Unmount Cycle
340
- * =========================
341
- */
342
-
343
- willUnmount () {
344
- return Promise.resolve ();
345
- }
346
-
347
- unmount () {
348
- return Promise.resolve ();
349
- }
350
-
351
- didUnmount () {
352
- return Promise.resolve ();
353
- }
354
-
355
- /*
356
- * =========================
357
- * Render Cycle
358
- * =========================
359
- */
360
-
361
- /**
362
- * Forces the component to be rendered again.
363
- *
364
- * @returns {string|Promise<string>} - The HTML to render on the component
365
- */
366
- forceRender () {
367
- return this._render ();
368
- }
369
-
370
- /**
371
- * This function is the one that defines the HTML that will be rendered
372
- * inside the component. Since some content may need to be loaded before the
373
- * component is rendered, this function can also return a promise.
374
- *
375
- * @returns {string|Promise<string>} - The HTML to render on the component
376
- */
377
- render () {
378
- return '';
379
- }
380
-
381
- _render () {
382
- let render = this.render;
383
-
384
- // Check if a template has been set to this component, and if that's the
385
- // case, use that instead of the render function to render the component's
386
- // HTML code.
387
- if (this.static._template !== null) {
388
- render = this.template;
389
- }
390
-
391
- // Call the render function asynchronously and set the HTML from it to the
392
- // component.
393
- return callAsync (render, this).then ((html) => {
394
- const slot = this.dom.querySelector ('slot');
395
-
396
- if (typeof html === 'string') {
397
- html = html.trim ();
398
-
399
- if (html === '') {
400
- return;
401
- }
402
- }
403
-
404
- if (html === null || typeof html === 'undefined') {
405
- return;
406
- }
407
-
408
- if (slot !== null) {
409
- slot.replaceWith (html);
410
- } else {
411
- this.innerHTML = html;
412
-
413
- if (this._children !== '' && html.indexOf(this._children) === -1) {
414
- this.innerHTML += this._children;
415
- }
416
- }
417
- });
418
- }
419
-
420
- connectedCallback () {
421
- // Set the state as connected
422
- this._connected = true;
423
-
424
- // Add a data property with the tag of the component
425
- this.dataset.component = this.static.tag;
426
-
427
- // Check if a template for this component was set. The contents on this
428
- // if block will only be run once.
429
- if (typeof this.static._template === 'undefined') {
430
-
431
- // Check if there is an HTML template for this component
432
- const template = document.querySelector (`template#${this.static.tag}`);
433
-
434
- if (template !== null) {
435
- // If there is, set is as the template for the component
436
- this.template (template.innerHTML);
437
- } else {
438
- // If not, set is as null
439
- this.static._template = null;
440
- }
441
- }
442
-
443
- // Set the initial prop attributes for the component using the given
444
- // props
445
- this._setPropAttributes ();
446
-
447
- // Start the Mount Cycle
448
- return this.willMount ().then (() => {
449
-
450
- return this._render ().then (() => {
451
- return this.didMount ().then (() => {
452
-
453
- this._isReady = true;
454
- for (const callback of this._ready) {
455
- callback.call (this);
456
- }
457
- });
458
- });
459
- });
460
- }
461
-
462
- /**
463
- * Adds a callback to be run once the component has been mounted successfully
464
- *
465
- * @param {function} callback - Callback to run once the component is ready
466
- */
467
- ready (callback) {
468
- this._ready.push (callback);
469
- }
470
-
471
-
472
- disconnectedCallback () {
473
- return this.willUnmount ().then (() => {
474
- return this.unmount ().then (() => {
475
- return this.didUnmount ();
476
- });
477
- });
478
- }
479
-
480
- updateCallback (property, oldValue, newValue, origin = 'props', oldObject = {}, newObject = {}) {
481
- return this.willUpdate (origin, property, oldValue, newValue, oldObject, newObject).then (() => {
482
- return this.update (origin, property, oldValue, newValue, oldObject, newObject).then (() => {
483
- let promise;
484
- if (origin === 'state') {
485
- promise = this.onStateUpdate (property, oldValue, newValue, oldObject, newObject);
486
- } else {
487
- promise = this.onPropsUpdate (property, oldValue, newValue, oldObject, newObject);
488
- }
489
- return promise.then (() => {
490
- return this.didUpdate (origin, property, oldValue, newValue, oldObject, newObject);
491
- });
492
- });
493
- }).catch ((e) => {
494
- console.error (e);
495
- // Component should not update
496
- });
497
- }
498
-
499
- attributeChangedCallback (property, oldValue, newValue) {
500
-
501
- }
502
- }
package/src/Registry.js DELETED
@@ -1,74 +0,0 @@
1
- /* eslint-disable no-unused-vars */
2
- import { Component } from './Component';
3
-
4
- export class Registry {
5
-
6
- static components = {};
7
-
8
- static register (tag, component) {
9
- if (typeof this.components[tag] === 'undefined') {
10
- this.components[tag] = component;
11
-
12
- const components = this.components;
13
- window.customElements.define (tag, new Proxy (Component, {
14
- getPrototypeOf (target) {
15
- return Reflect.getPrototypeOf (target);
16
- },
17
- getOwnPropertyDescriptor (target, propertyKey) {
18
- return Reflect.getOwnPropertyDescriptor (components[tag]);
19
- },
20
- construct (target, args) {
21
- return document.createElement (components[tag].tag);
22
- },
23
- get (target, property, receiver) {
24
- return Reflect.get (components[tag], property, receiver);
25
- },
26
- set (target, property, value, receiver) {
27
- return Reflect.set (components[tag], property, value, receiver);
28
- },
29
- apply (target, receiver, args) {
30
- return Reflect.apply (components[tag], receiver, args);
31
- }
32
- }));
33
- this.components[tag]._registered = true;
34
- } else {
35
- throw new Error ('A component with this tag has already been registered. Use the evolve() function to modify the component.');
36
- }
37
- }
38
-
39
- static evolve (tag, component) {
40
- if (typeof this.components[tag] === 'undefined') {
41
- throw new Error ('No component with this tag has been registered. Cannot Evolve.');
42
- } else {
43
- const previousState = Object.assign ({} , this.components[tag].state);
44
- const previousProps = Object.assign ({} , this.components[tag].props);
45
-
46
- this.components[tag] = component;
47
-
48
- this.components[tag]._registered = true;
49
- }
50
- }
51
-
52
- static instances (tag, callback = null) {
53
- if (typeof this.components[tag] !== 'undefined') {
54
- if (typeof callback === 'function') {
55
- document.querySelectorAll (tag).forEach (callback);
56
- } else {
57
- return document.querySelectorAll (tag);
58
- }
59
- } else {
60
- throw new Error ('No component with the provided tag has been registered.');
61
- }
62
- }
63
-
64
- static instantiate (tag, props) {
65
- if (this._registered === false) {
66
- this.register ();
67
- }
68
-
69
- const element = document.createElement (this.components[tag]);
70
- element._setProps (props);
71
-
72
- return element;
73
- }
74
- }
@@ -1,47 +0,0 @@
1
- import { Component } from './Component';
2
- import { callAsync } from './Util';
3
-
4
- /**
5
- * @class ShadowComponent
6
- */
7
- export class ShadowComponent extends Component {
8
- constructor (...props) {
9
- super (...props);
10
-
11
- this._shadowDOM = this.attachShadow ({ mode: 'open' });
12
- }
13
-
14
- _createStyleElement () {
15
- if (!(this._styleElement instanceof HTMLStyleElement)) {
16
- this._styleElement = document.createElement ('style');
17
- this.dom.prepend (this._styleElement);
18
- }
19
- }
20
-
21
- _render () {
22
- let render = this.render;
23
-
24
- // Check if a template has been set to this component, and if that's the
25
- // case, use that instead of the render function to render the component's
26
- // HTML code.
27
- if (this.static._template !== null) {
28
- render = this.template;
29
- }
30
-
31
- // Call the render function asynchronously and set the HTML from it to the
32
- // component.
33
- return callAsync (render, this).then ((html) => {
34
- this._shadowDOM.innerHTML = '';
35
- if (this._styleElement instanceof HTMLStyleElement) {
36
- this._shadowDOM.appendChild (this._styleElement);
37
- }
38
- this._shadowDOM.innerHTML += html;
39
- });
40
- }
41
-
42
- get dom () {
43
- return this._shadowDOM;
44
- }
45
-
46
-
47
- }
package/src/Util.js DELETED
@@ -1,66 +0,0 @@
1
- /**
2
- *
3
- * @param {*} callable
4
- * @param {*} context
5
- * @param {...any} args
6
- */
7
- export function callAsync (callable, context, ...args) {
8
- try {
9
- // Call the provided function using the context and arguments given
10
- const result = callable.apply (context, args);
11
-
12
- // Check if the function returned a simple value or a Promise
13
- if (result instanceof Promise) {
14
- return result;
15
- } else {
16
- return Promise.resolve (result);
17
- }
18
- } catch (e) {
19
- return Promise.reject (e);
20
- }
21
- }
22
-
23
- /**
24
- *
25
- * @param {*} object
26
- * @param {*} encapsulation
27
- * @param {*} level
28
- */
29
- export function deserializeCSS (object, encapsulation = '', level = 0, ) {
30
- const keys = Object.keys (object);
31
-
32
- let css = '';
33
-
34
- for (const key of keys) {
35
- if (typeof object[key] === 'object') {
36
- if (encapsulation) {
37
- if (key.indexOf ('&') === 0) {
38
- css += `${key.replace(/&/g, encapsulation)} {\n`;
39
- } else {
40
- css += `${encapsulation} ${key} {\n`;
41
- }
42
- } else {
43
- css += `${key} {\n`;
44
- }
45
-
46
- const properties = Object.keys (object[key]);
47
- for (const property of properties) {
48
- css += '\t'.repeat (level);
49
- if (typeof object[key][property] === 'object') {
50
- const temp = {};
51
- temp[property] = object[key][property];
52
-
53
- css += deserializeCSS (temp, encapsulation, level + 1);
54
- } else {
55
- css += `\t${property}: ${object[key][property]};\n`;
56
- }
57
- }
58
- css += '}\n';
59
- } else {
60
- css += '\t'.repeat (level);
61
- css += `\t${key}: ${object[key]};\n`;
62
- }
63
- }
64
-
65
- return css;
66
- }