@symbiotejs/symbiote 1.4.1 → 1.4.2

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.
File without changes
package/package.json CHANGED
@@ -15,18 +15,17 @@
15
15
  "files": [
16
16
  "build/symbiote.js",
17
17
  "build/symbiote.min.js",
18
- "build/symbiote.base.js",
19
- "build/symbiote.base.min.js",
20
- "build/types.d.ts",
18
+ "build/BaseComponent.min.js",
19
+ "build/symbiote.d.ts",
21
20
  "README.md",
22
21
  "LICENSE"
23
22
  ],
24
23
  "module": "./build/symbiote.js",
25
- "types": "./build/types.d.ts",
24
+ "types": "./build/symbiote.d.ts",
26
25
  "publishConfig": {
27
26
  "access": "public"
28
27
  },
29
- "version": "1.4.1",
28
+ "version": "1.4.2",
30
29
  "description": "Symbiote.js",
31
30
  "author": "hello@symbiotejs.org",
32
31
  "license": "MIT",
@@ -1,620 +0,0 @@
1
- // core/Data.js
2
- function cloneObj(obj) {
3
- let clone = (o) => {
4
- var _a;
5
- for (let prop in o) {
6
- if (((_a = o[prop]) == null ? void 0 : _a.constructor) === Object) {
7
- o[prop] = clone(o[prop]);
8
- }
9
- }
10
- return { ...o };
11
- };
12
- return clone(obj);
13
- }
14
- var Data = class {
15
- constructor(src) {
16
- this.uid = Symbol();
17
- this.name = src.name || null;
18
- if (src.schema.constructor === Object) {
19
- this.store = cloneObj(src.schema);
20
- } else {
21
- this._storeIsProxy = true;
22
- this.store = src.schema;
23
- }
24
- this.callbackMap = Object.create(null);
25
- }
26
- static warn(actionName, prop) {
27
- console.warn(`Symbiote Data: cannot ${actionName}. Prop name: ` + prop);
28
- }
29
- read(prop) {
30
- if (!this._storeIsProxy && !this.store.hasOwnProperty(prop)) {
31
- Data.warn("read", prop);
32
- return null;
33
- }
34
- return this.store[prop];
35
- }
36
- has(prop) {
37
- return this._storeIsProxy ? this.store[prop] !== void 0 : this.store.hasOwnProperty(prop);
38
- }
39
- add(prop, val, rewrite = true) {
40
- if (!rewrite && Object.keys(this.store).includes(prop)) {
41
- return;
42
- }
43
- this.store[prop] = val;
44
- if (this.callbackMap[prop]) {
45
- this.callbackMap[prop].forEach((callback) => {
46
- callback(this.store[prop]);
47
- });
48
- }
49
- }
50
- pub(prop, val) {
51
- if (!this._storeIsProxy && !this.store.hasOwnProperty(prop)) {
52
- Data.warn("publish", prop);
53
- return;
54
- }
55
- this.add(prop, val);
56
- }
57
- multiPub(updObj) {
58
- for (let prop in updObj) {
59
- this.pub(prop, updObj[prop]);
60
- }
61
- }
62
- notify(prop) {
63
- if (this.callbackMap[prop]) {
64
- this.callbackMap[prop].forEach((callback) => {
65
- callback(this.store[prop]);
66
- });
67
- }
68
- }
69
- sub(prop, callback, init = true) {
70
- if (!this._storeIsProxy && !this.store.hasOwnProperty(prop)) {
71
- Data.warn("subscribe", prop);
72
- return null;
73
- }
74
- if (!this.callbackMap[prop]) {
75
- this.callbackMap[prop] = /* @__PURE__ */ new Set();
76
- }
77
- this.callbackMap[prop].add(callback);
78
- if (init) {
79
- callback(this.store[prop]);
80
- }
81
- return {
82
- remove: () => {
83
- this.callbackMap[prop].delete(callback);
84
- if (!this.callbackMap[prop].size) {
85
- delete this.callbackMap[prop];
86
- }
87
- },
88
- callback
89
- };
90
- }
91
- remove() {
92
- delete Data.globalStore[this.uid];
93
- }
94
- static registerLocalCtx(schema) {
95
- let state = new Data({
96
- schema
97
- });
98
- Data.globalStore[state.uid] = state;
99
- return state;
100
- }
101
- static registerNamedCtx(ctxName, schema) {
102
- let state = Data.globalStore[ctxName];
103
- if (state) {
104
- console.warn('State: context name "' + ctxName + '" already in use');
105
- } else {
106
- state = new Data({
107
- name: ctxName,
108
- schema
109
- });
110
- Data.globalStore[ctxName] = state;
111
- }
112
- return state;
113
- }
114
- static clearNamedCtx(ctxName) {
115
- delete Data.globalStore[ctxName];
116
- }
117
- static getNamedCtx(ctxName, notify = true) {
118
- return Data.globalStore[ctxName] || (notify && console.warn('State: wrong context name - "' + ctxName + '"'), null);
119
- }
120
- };
121
- Data.globalStore = Object.create(null);
122
-
123
- // core/dictionary.js
124
- var DICT = Object.freeze({
125
- BIND_ATTR: "set",
126
- ATTR_BIND_PRFX: "@",
127
- EXT_DATA_CTX_PRFX: "*",
128
- NAMED_DATA_CTX_SPLTR: "/",
129
- CTX_NAME_ATTR: "ctx-name",
130
- CSS_CTX_PROP: "--ctx-name",
131
- EL_REF_ATTR: "ref",
132
- AUTO_TAG_PRFX: "sym"
133
- });
134
-
135
- // utils/UID.js
136
- var CHARS = "1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
137
- var CHLENGTH = CHARS.length - 1;
138
- var UID = class {
139
- static generate(pattern = "XXXXXXXXX-XXX") {
140
- let uid = "";
141
- for (let i = 0; i < pattern.length; i++) {
142
- uid += pattern[i] === "-" ? pattern[i] : CHARS.charAt(Math.random() * CHLENGTH);
143
- }
144
- return uid;
145
- }
146
- };
147
-
148
- // core/tpl-processors.js
149
- function slotProcessor(fr, fnCtx) {
150
- if (fnCtx.renderShadow) {
151
- return;
152
- }
153
- let slots = [...fr.querySelectorAll("slot")];
154
- if (fnCtx.__initChildren.length && slots.length) {
155
- let slotMap = {};
156
- slots.forEach((slot) => {
157
- let slotName = slot.getAttribute("name");
158
- if (slotName) {
159
- slotMap[slotName] = {
160
- slot,
161
- fr: document.createDocumentFragment()
162
- };
163
- } else {
164
- slotMap.__default__ = {
165
- slot,
166
- fr: document.createDocumentFragment()
167
- };
168
- }
169
- });
170
- fnCtx.__initChildren.forEach((child) => {
171
- var _a;
172
- let slotName = (_a = child.getAttribute) == null ? void 0 : _a.call(child, "slot");
173
- if (slotName) {
174
- child.removeAttribute("slot");
175
- slotMap[slotName].fr.appendChild(child);
176
- } else if (slotMap.__default__) {
177
- slotMap.__default__.fr.appendChild(child);
178
- }
179
- });
180
- Object.values(slotMap).forEach((mapObj) => {
181
- mapObj.slot.parentNode.insertBefore(mapObj.fr, mapObj.slot);
182
- mapObj.slot.remove();
183
- });
184
- } else {
185
- fnCtx.innerHTML = "";
186
- }
187
- }
188
- function refProcessor(fr, fnCtx) {
189
- [...fr.querySelectorAll(`[${DICT.EL_REF_ATTR}]`)].forEach((el) => {
190
- let refName = el.getAttribute(DICT.EL_REF_ATTR);
191
- fnCtx.ref[refName] = el;
192
- el.removeAttribute(DICT.EL_REF_ATTR);
193
- });
194
- }
195
- function domSetProcessor(fr, fnCtx) {
196
- [...fr.querySelectorAll(`[${DICT.BIND_ATTR}]`)].forEach((el) => {
197
- let subStr = el.getAttribute(DICT.BIND_ATTR);
198
- let keyValsArr = subStr.split(";");
199
- keyValsArr.forEach((keyValStr) => {
200
- if (!keyValStr) {
201
- return;
202
- }
203
- let kv = keyValStr.split(":").map((str) => str.trim());
204
- let prop = kv[0];
205
- let isAttr;
206
- if (prop.indexOf(DICT.ATTR_BIND_PRFX) === 0) {
207
- isAttr = true;
208
- prop = prop.replace(DICT.ATTR_BIND_PRFX, "");
209
- }
210
- let valKeysArr = kv[1].split(",").map((valKey) => {
211
- return valKey.trim();
212
- });
213
- let isDeep, parent, lastStep, dive;
214
- if (prop.includes(".")) {
215
- isDeep = true;
216
- let propPath = prop.split(".");
217
- dive = () => {
218
- parent = el;
219
- propPath.forEach((step, idx) => {
220
- if (idx < propPath.length - 1) {
221
- parent = parent[step];
222
- } else {
223
- lastStep = step;
224
- }
225
- });
226
- };
227
- dive();
228
- }
229
- for (let valKey of valKeysArr) {
230
- fnCtx.sub(valKey, (val) => {
231
- if (isAttr) {
232
- if ((val == null ? void 0 : val.constructor) === Boolean) {
233
- val ? el.setAttribute(prop, "") : el.removeAttribute(prop);
234
- } else {
235
- el.setAttribute(prop, val);
236
- }
237
- } else if (isDeep) {
238
- if (parent) {
239
- parent[lastStep] = val;
240
- } else {
241
- window.setTimeout(() => {
242
- dive();
243
- parent[lastStep] = val;
244
- });
245
- }
246
- } else {
247
- el[prop] = val;
248
- }
249
- });
250
- }
251
- });
252
- el.removeAttribute(DICT.BIND_ATTR);
253
- });
254
- }
255
- var OPEN_TOKEN = "{{";
256
- var CLOSE_TOKEN = "}}";
257
- var SKIP_ATTR = "skip-text";
258
- function getTextNodesWithTokens(el) {
259
- let node;
260
- let result = [];
261
- let walk = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, {
262
- acceptNode: (txt) => {
263
- var _a;
264
- return !((_a = txt.parentElement) == null ? void 0 : _a.hasAttribute(SKIP_ATTR)) && txt.textContent.includes(OPEN_TOKEN) && txt.textContent.includes(CLOSE_TOKEN) && 1;
265
- }
266
- });
267
- while (node = walk.nextNode()) {
268
- result.push(node);
269
- }
270
- return result;
271
- }
272
- var txtNodesProcessor = function(fr, fnCtx) {
273
- let txtNodes = getTextNodesWithTokens(fr);
274
- txtNodes.forEach((txtNode) => {
275
- let tokenNodes = [];
276
- let offset;
277
- while (txtNode.textContent.includes(CLOSE_TOKEN)) {
278
- if (txtNode.textContent.startsWith(OPEN_TOKEN)) {
279
- offset = txtNode.textContent.indexOf(CLOSE_TOKEN) + 2;
280
- txtNode.splitText(offset);
281
- tokenNodes.push(txtNode);
282
- } else {
283
- offset = txtNode.textContent.indexOf(OPEN_TOKEN);
284
- txtNode.splitText(offset);
285
- }
286
- txtNode = txtNode.nextSibling;
287
- }
288
- tokenNodes.forEach((tNode) => {
289
- let prop = tNode.textContent.replace(OPEN_TOKEN, "").replace(CLOSE_TOKEN, "");
290
- fnCtx.sub(prop, (val) => {
291
- tNode.textContent = val;
292
- });
293
- });
294
- });
295
- };
296
- var tpl_processors_default = [slotProcessor, refProcessor, domSetProcessor, txtNodesProcessor];
297
-
298
- // core/BaseComponent.js
299
- var autoTagsCount = 0;
300
- var BaseComponent = class extends HTMLElement {
301
- initCallback() {
302
- }
303
- __initCallback() {
304
- var _a;
305
- if (this.__initialized) {
306
- return;
307
- }
308
- this.__initialized = true;
309
- (_a = this.initCallback) == null ? void 0 : _a.call(this);
310
- }
311
- render(template, shadow = this.renderShadow) {
312
- let fr;
313
- if (template || this.constructor["template"]) {
314
- if (this.constructor["template"] && !this.constructor["__tpl"]) {
315
- this.constructor["__tpl"] = document.createElement("template");
316
- this.constructor["__tpl"].innerHTML = this.constructor["template"];
317
- }
318
- while (this.firstChild) {
319
- this.firstChild.remove();
320
- }
321
- if ((template == null ? void 0 : template.constructor) === DocumentFragment) {
322
- fr = template;
323
- } else if ((template == null ? void 0 : template.constructor) === String) {
324
- let tpl = document.createElement("template");
325
- tpl.innerHTML = template;
326
- fr = tpl.content.cloneNode(true);
327
- } else if (this.constructor["__tpl"]) {
328
- fr = this.constructor["__tpl"].content.cloneNode(true);
329
- }
330
- for (let fn of this.tplProcessors) {
331
- fn(fr, this);
332
- }
333
- }
334
- if ((shadow || this.constructor["__shadowStylesUrl"]) && !this.shadowRoot) {
335
- this.attachShadow({
336
- mode: "open"
337
- });
338
- }
339
- let addFr = () => {
340
- fr && (shadow && this.shadowRoot.appendChild(fr) || this.appendChild(fr));
341
- this.__initCallback();
342
- };
343
- if (this.constructor["__shadowStylesUrl"]) {
344
- shadow = true;
345
- let styleLink = document.createElement("link");
346
- styleLink.rel = "stylesheet";
347
- styleLink.href = this.constructor["__shadowStylesUrl"];
348
- styleLink.onload = addFr;
349
- this.shadowRoot.prepend(styleLink);
350
- } else {
351
- addFr();
352
- }
353
- }
354
- addTemplateProcessor(processorFn) {
355
- this.tplProcessors.add(processorFn);
356
- }
357
- constructor() {
358
- super();
359
- this.init$ = Object.create(null);
360
- this.tplProcessors = /* @__PURE__ */ new Set();
361
- this.ref = Object.create(null);
362
- this.allSubs = /* @__PURE__ */ new Set();
363
- this.pauseRender = false;
364
- this.renderShadow = false;
365
- this.readyToDestroy = true;
366
- }
367
- get autoCtxName() {
368
- if (!this.__autoCtxName) {
369
- this.__autoCtxName = UID.generate();
370
- this.style.setProperty(DICT.CSS_CTX_PROP, `'${this.__autoCtxName}'`);
371
- }
372
- return this.__autoCtxName;
373
- }
374
- get cssCtxName() {
375
- return this.getCssData(DICT.CSS_CTX_PROP, true);
376
- }
377
- get ctxName() {
378
- var _a;
379
- return ((_a = this.getAttribute(DICT.CTX_NAME_ATTR)) == null ? void 0 : _a.trim()) || this.cssCtxName || this.autoCtxName;
380
- }
381
- get localCtx() {
382
- if (!this.__localCtx) {
383
- this.__localCtx = Data.registerLocalCtx({});
384
- }
385
- return this.__localCtx;
386
- }
387
- get nodeCtx() {
388
- return Data.getNamedCtx(this.ctxName, false) || Data.registerNamedCtx(this.ctxName, {});
389
- }
390
- static __parseProp(prop, fnCtx) {
391
- let ctx;
392
- let name;
393
- if (prop.startsWith(DICT.EXT_DATA_CTX_PRFX)) {
394
- ctx = fnCtx.nodeCtx;
395
- name = prop.replace(DICT.EXT_DATA_CTX_PRFX, "");
396
- } else if (prop.includes(DICT.NAMED_DATA_CTX_SPLTR)) {
397
- let pArr = prop.split(DICT.NAMED_DATA_CTX_SPLTR);
398
- ctx = Data.getNamedCtx(pArr[0]);
399
- name = pArr[1];
400
- } else {
401
- ctx = fnCtx.localCtx;
402
- name = prop;
403
- }
404
- return {
405
- ctx,
406
- name
407
- };
408
- }
409
- sub(prop, handler) {
410
- let parsed = BaseComponent.__parseProp(prop, this);
411
- this.allSubs.add(parsed.ctx.sub(parsed.name, handler));
412
- }
413
- notify(prop) {
414
- let parsed = BaseComponent.__parseProp(prop, this);
415
- parsed.ctx.notify(parsed.name);
416
- }
417
- has(prop) {
418
- let parsed = BaseComponent.__parseProp(prop, this);
419
- return parsed.ctx.has(parsed.name);
420
- }
421
- add(prop, val) {
422
- let parsed = BaseComponent.__parseProp(prop, this);
423
- parsed.ctx.add(parsed.name, val, false);
424
- }
425
- add$(obj) {
426
- for (let prop in obj) {
427
- this.add(prop, obj[prop]);
428
- }
429
- }
430
- get $() {
431
- if (!this.__stateProxy) {
432
- let o = Object.create(null);
433
- this.__stateProxy = new Proxy(o, {
434
- set: (obj, prop, val) => {
435
- let parsed = BaseComponent.__parseProp(prop, this);
436
- parsed.ctx.pub(parsed.name, val);
437
- return true;
438
- },
439
- get: (obj, prop) => {
440
- let parsed = BaseComponent.__parseProp(prop, this);
441
- return parsed.ctx.read(parsed.name);
442
- }
443
- });
444
- }
445
- return this.__stateProxy;
446
- }
447
- set$(kvObj) {
448
- for (let key in kvObj) {
449
- this.$[key] = kvObj[key];
450
- }
451
- }
452
- __initDataCtx() {
453
- let attrDesc = this.constructor["__attrDesc"];
454
- if (attrDesc) {
455
- for (let prop of Object.values(attrDesc)) {
456
- if (!Object.keys(this.init$).includes(prop)) {
457
- this.init$[prop] = "";
458
- }
459
- }
460
- }
461
- for (let prop in this.init$) {
462
- if (prop.startsWith(DICT.EXT_DATA_CTX_PRFX)) {
463
- this.nodeCtx.add(prop.replace(DICT.EXT_DATA_CTX_PRFX, ""), this.init$[prop]);
464
- } else if (prop.includes(DICT.NAMED_DATA_CTX_SPLTR)) {
465
- let propArr = prop.split(DICT.NAMED_DATA_CTX_SPLTR);
466
- let ctxName = propArr[0].trim();
467
- let propName = propArr[1].trim();
468
- if (ctxName && propName) {
469
- let namedCtx = Data.getNamedCtx(ctxName, false);
470
- if (!namedCtx) {
471
- namedCtx = Data.registerNamedCtx(ctxName, {});
472
- }
473
- namedCtx.add(propName, this.init$[prop]);
474
- }
475
- } else {
476
- this.localCtx.add(prop, this.init$[prop]);
477
- }
478
- }
479
- this.__dataCtxInitialized = true;
480
- }
481
- connectedCallback() {
482
- var _a;
483
- if (this.__disconnectTimeout) {
484
- window.clearTimeout(this.__disconnectTimeout);
485
- }
486
- if (!this.connectedOnce) {
487
- let ctxNameAttrVal = (_a = this.getAttribute(DICT.CTX_NAME_ATTR)) == null ? void 0 : _a.trim();
488
- if (ctxNameAttrVal) {
489
- this.style.setProperty(DICT.CSS_CTX_PROP, `'${ctxNameAttrVal}'`);
490
- }
491
- this.__initDataCtx();
492
- this.__initChildren = [...this.childNodes];
493
- for (let proc of tpl_processors_default) {
494
- this.addTemplateProcessor(proc);
495
- }
496
- if (!this.pauseRender) {
497
- this.render();
498
- }
499
- }
500
- this.connectedOnce = true;
501
- }
502
- destroyCallback() {
503
- }
504
- disconnectedCallback() {
505
- this.dropCssDataCache();
506
- if (!this.readyToDestroy) {
507
- return;
508
- }
509
- if (this.__disconnectTimeout) {
510
- window.clearTimeout(this.__disconnectTimeout);
511
- }
512
- this.__disconnectTimeout = window.setTimeout(() => {
513
- this.destroyCallback();
514
- for (let sub of this.allSubs) {
515
- sub.remove();
516
- this.allSubs.delete(sub);
517
- }
518
- for (let proc of this.tplProcessors) {
519
- this.tplProcessors.delete(proc);
520
- }
521
- }, 100);
522
- }
523
- static reg(tagName, isAlias = false) {
524
- if (!tagName) {
525
- autoTagsCount++;
526
- tagName = `${DICT.AUTO_TAG_PRFX}-${autoTagsCount}`;
527
- }
528
- this.__tag = tagName;
529
- if (window.customElements.get(tagName)) {
530
- console.warn(`${tagName} - is already in "customElements" registry`);
531
- return;
532
- }
533
- window.customElements.define(tagName, isAlias ? class extends this {
534
- } : this);
535
- }
536
- static get is() {
537
- if (!this.__tag) {
538
- this.reg();
539
- }
540
- return this.__tag;
541
- }
542
- static bindAttributes(desc) {
543
- this.observedAttributes = Object.keys(desc);
544
- this.__attrDesc = desc;
545
- }
546
- attributeChangedCallback(name, oldVal, newVal) {
547
- if (oldVal === newVal) {
548
- return;
549
- }
550
- let $prop = this.constructor["__attrDesc"][name];
551
- if ($prop) {
552
- if (this.__dataCtxInitialized) {
553
- this.$[$prop] = newVal;
554
- } else {
555
- this.init$[$prop] = newVal;
556
- }
557
- } else {
558
- this[name] = newVal;
559
- }
560
- }
561
- getCssData(propName, silentCheck = false) {
562
- if (!this.__cssDataCache) {
563
- this.__cssDataCache = Object.create(null);
564
- }
565
- if (!Object.keys(this.__cssDataCache).includes(propName)) {
566
- if (!this.__computedStyle) {
567
- this.__computedStyle = window.getComputedStyle(this);
568
- }
569
- let val = this.__computedStyle.getPropertyValue(propName).trim();
570
- if (val.startsWith(`'`) && val.endsWith(`'`)) {
571
- val = val.replace(/\'/g, '"');
572
- }
573
- try {
574
- this.__cssDataCache[propName] = JSON.parse(val);
575
- } catch (e) {
576
- !silentCheck && console.warn(`CSS Data error: ${propName}`);
577
- this.__cssDataCache[propName] = null;
578
- }
579
- }
580
- return this.__cssDataCache[propName];
581
- }
582
- bindCssData(propName, external = true) {
583
- let stateName = (external ? DICT.EXT_DATA_CTX_PRFX : "") + propName;
584
- this.add(stateName, this.getCssData(propName, true));
585
- return stateName;
586
- }
587
- dropCssDataCache() {
588
- this.__cssDataCache = null;
589
- this.__computedStyle = null;
590
- }
591
- defineAccessor(propName, handler, isAsync) {
592
- let localPropName = "__" + propName;
593
- this[localPropName] = this[propName];
594
- Object.defineProperty(this, propName, {
595
- set: (val) => {
596
- this[localPropName] = val;
597
- if (isAsync) {
598
- window.setTimeout(() => {
599
- handler == null ? void 0 : handler(val);
600
- });
601
- } else {
602
- handler == null ? void 0 : handler(val);
603
- }
604
- },
605
- get: () => {
606
- return this[localPropName];
607
- }
608
- });
609
- this[propName] = this[localPropName];
610
- }
611
- static set shadowStyles(cssTxt) {
612
- let styleBlob = new Blob([cssTxt], {
613
- type: "text/css"
614
- });
615
- this.__shadowStylesUrl = URL.createObjectURL(styleBlob);
616
- }
617
- };
618
- export {
619
- BaseComponent
620
- };