hyper-element 0.11.1 → 0.12.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/source/bundle.js DELETED
@@ -1,562 +0,0 @@
1
- 'use strict';
2
-
3
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
-
5
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
6
-
7
- var _templateObject = _taggedTemplateLiteral([''], ['']);
8
-
9
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10
-
11
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
12
-
13
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
14
-
15
- function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
16
-
17
- function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
18
-
19
- (function (factory) {
20
-
21
- if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
22
- // Node, CommonJS-like
23
- module.exports = factory(require('hyperhtml/cjs'));
24
- } else if (typeof define === 'function' && define.amd) {
25
- // AMD
26
- define(['hyperhtml'], factory);
27
- } else {
28
- window.hyperElement = factory(window.hyperHTML);
29
- }
30
- })(function (hyperHTML) {
31
-
32
- var manager = {},
33
- sharedAttrs = {},
34
- customTagMatch = /<\s*[a-z]+-[a-z][^>]*>/g,
35
- isCustomTag = /<+\w+[-]+\w/;
36
-
37
- function makeid() {
38
- var text = "";
39
- var possible = "bcdfghjklmnpqrstvwxyz";
40
-
41
- for (var i = 0; i < 15; i++) {
42
- text += possible.charAt(Math.floor(Math.random() * possible.length));
43
- }return text;
44
- }
45
-
46
- //=====================================================
47
- //=========================== re-render on store change
48
- //=====================================================
49
-
50
- function onNext(that, store) {
51
-
52
- var storeFn = "function" == typeof store ? store : function () {
53
- return store;
54
- };
55
-
56
- var render = this.render;
57
-
58
- var render2 = function render2() {
59
- for (var _len = arguments.length, data = Array(_len), _key = 0; _key < _len; _key++) {
60
- data[_key] = arguments[_key];
61
- }
62
-
63
- if (undefined === store) {
64
- that.store = undefined;
65
- render.apply(undefined, data);
66
- } else {
67
- that.store = storeFn();
68
- render.apply(undefined, [that.store].concat(data));
69
- }
70
- };
71
- this.render = render2;
72
-
73
- return render2;
74
- }
75
-
76
- //=====================================================
77
- //======================== Observer change to innerHTML
78
- //=====================================================
79
-
80
- function observer(ref) {
81
- var _this = this;
82
-
83
- var that = ref.this;
84
- var mutationObserver = new MutationObserver(function (mutations) {
85
- /*
86
- //if(!this.textContent){
87
- const mutation = mutations[mutations.length - 1]
88
- const addedNodes = mutation.addedNodes[0]
89
- console.log(this,addedNodes,ref.observe)
90
- //}
91
- */
92
- var textContent = _this.textContent;
93
- /*
94
- //if("" === textContent){
95
- // const mutation = mutations[mutations.length - 1]
96
- // const addedNodes = mutation.addedNodes[0]
97
- if(addedNodes)
98
- textContent = addedNodes.data
99
- // }
100
-
101
- console.log(textContent === this.wrapedConten,"TEXT_CONTENT:",textContent, "WRAPED_CONTENT:",this.wrappedContent)
102
- */
103
-
104
- if (!ref.observe) return;
105
-
106
- ref.innerHTML = _this.innerHTML;
107
- // that.wrappedContent = textContent
108
- if (that.attrs.template) {
109
- //this.attachAttrs(this.attributes)
110
- that.attrs = _this.attachAttrs(_this.attributes) || {};
111
- }
112
-
113
- //reset the element
114
- hyperHTML.bind(ref.shadow)(_templateObject); // HACK, dont know why this works?
115
-
116
- that.wrappedContent = textContent;
117
- _this.render();
118
- });
119
-
120
- mutationObserver.observe(this, {
121
- // Set to true if mutations to target's attributes are to be observed.
122
- //attributes: true,
123
-
124
- // Set to true if mutations to target's data are to be observed.
125
- // characterData: true, // re-render on content change
126
-
127
- // Set to true if additions and removals of the target node's child elements (including text nodes) are to be observed.
128
- childList: true,
129
-
130
- // Set to true if mutations to target and target's descendants are to be observed.
131
- subtree: true
132
-
133
- // Set to true if attributes is set to true and target's attribute value before the mutation needs to be recorded.
134
- //attributeOldValue: true,
135
-
136
- // Set to true if characterData is set to true and target's data before the mutation needs to be recorded.
137
- //characterDataOldValue: true
138
- });
139
- }
140
-
141
- function buildTemplate(innerHTML) {
142
-
143
- var re = /(\{[\w]+\})/g; // /\s*(\{[\w]+\})\s*/g
144
- var templateVals = innerHTML.split(re).reduce(function (vals, item) {
145
-
146
- if ("{" === item[0] && "}" === item.slice(-1)) {
147
- vals.keys.push(item.slice(1, -1));
148
- } else {
149
- vals.markup.push(item);
150
- }
151
-
152
- return vals;
153
- }, { markup: [], keys: [] });
154
-
155
- templateVals.id = ":" + templateVals.markup.join().trim();
156
-
157
- function fragment(data, render) {
158
-
159
- var output = [templateVals.markup].concat(_toConsumableArray(templateVals.keys.map(function (key) {
160
- return data[key];
161
- })));
162
- output.raw = { value: templateVals.markup };
163
- return output;
164
- }
165
-
166
- return function template(data) {
167
- if ("object" !== (typeof data === 'undefined' ? 'undefined' : _typeof(data))) {
168
- throw new Error("Templates must be passed an object to be populated with. You passed " + JSON.stringify(data) + " to " + templateVals.id);
169
- }
170
- return hyperHTML.wire(data, templateVals.id).apply(undefined, _toConsumableArray(fragment(data)));
171
- };
172
- } // END buildTemplate
173
-
174
- function parceAttribute(key, value) {
175
- if ("template" === key && "" === value) {
176
- return true;
177
- }
178
-
179
- if (+value + "" === value.trim()) {
180
- return +value; // to number
181
- }
182
-
183
- var lowerCaseValue = value.toLowerCase().trim();
184
-
185
- if ("true" === lowerCaseValue) {
186
- return true;
187
- } else if ("false" === lowerCaseValue) {
188
- return false;
189
- } // END boolean check
190
-
191
- //if("data-json"===key){
192
- if (lowerCaseValue[0] === "[" && lowerCaseValue.slice(-1) === "]" || lowerCaseValue[0] === "{" && lowerCaseValue.slice(-1) === "}") {
193
- return JSON.parse(value);
194
- }
195
-
196
- return value;
197
- } // END parceAttribute
198
-
199
-
200
- //=====================================================
201
- //======================================= All the magic
202
- //=====================================================
203
-
204
- function _createdCallback() {
205
- var _this2 = this;
206
-
207
- // an instance of the element is created
208
- this.identifier = Symbol(this.localName);
209
- var ref = manager[this.identifier] = { attrsToIgnore: {} };
210
- ref.innerHTML = this.innerHTML;
211
- var that = ref.this = { element: this };
212
- that.wrappedContent = this.textContent;
213
-
214
- observer.call(this, ref); // observer change to innerHTML
215
-
216
- Object.getOwnPropertyNames(this.__proto__).filter(function (name) {
217
- return !("constructor" === name || "setup" === name || "render" === name);
218
- }).forEach(function (name) {
219
- if (/^[A-Z]/.test(name)) {
220
- var result = void 0;
221
- var templatestrings = {};
222
- var wrapFragment = function wrapFragment(data) {
223
-
224
- if (undefined !== result && result.once) return result;
225
-
226
- result = _this2[name](data);
227
- if (!!result.template) {
228
- if ("string" === typeof result.template) {
229
- /* if(undefined === result.values){
230
- throw new Error("'values' was not defined for a 'template' in "+name)
231
- }*/
232
- if (!templatestrings[result.template]) {
233
- templatestrings[result.template] = buildTemplate(result.template);
234
- }
235
- result = { any: templatestrings[result.template](result.values || data) };
236
- } // END "string" === typeof result.template
237
- else if ("object" === _typeof(result.template) && "function" === typeof result.template.then) {
238
-
239
- result = Object.assign({}, result, { any: result.template.then(function (args) {
240
- var template = args.template,
241
- values = args.values;
242
-
243
- if (!template && "string" === typeof args) {
244
- template = args;
245
- values = {};
246
- }
247
-
248
- if (!templatestrings[template]) {
249
- templatestrings[template] = buildTemplate(template);
250
- }
251
- if (Array.isArray(values)) {
252
- result = { any: values.map(templatestrings[template]), once: result.once };
253
- } else {
254
- result = { any: templatestrings[template](values || data), once: result.once };
255
- }
256
- return result.any;
257
- })
258
- }); // END Object.assign
259
- } // END result.template is promise ?
260
- else {
261
- throw new Error("unknow template type:" + _typeof(result.template) + " | " + JSON.stringify(result.template));
262
- }
263
- } // END !!result.template
264
- return result;
265
- }; // END wrapFragment
266
- hyperHTML.define(name, wrapFragment);
267
- } else {
268
- that[name] = _this2[name].bind(that);
269
- }
270
- delete _this2[name];
271
- });
272
- function toString() {
273
- return "hyper-element: " + this.localName;
274
- }
275
- Object.defineProperty(that, "toString", { value: toString.bind(this), writable: false });
276
- // use shadow DOM, else fallback to render to element
277
- ref.shadow = this; //.attachShadow ? this.attachShadow({mode: 'closed'}) : this
278
-
279
- // Restrict access to hyperHTML
280
- var hyperHTMLbind = hyperHTML.bind(ref.shadow);
281
- ref.Html = function Html() {
282
- for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
283
- args[_key2] = arguments[_key2];
284
- }
285
-
286
- if (args.some(function (item) {
287
- return "function" === typeof item;
288
- }) && args[0].some(function (t) {
289
- return isCustomTag.test(t);
290
- })) {
291
-
292
- var inCustomTag = false;
293
- var localName = "";
294
- var lookup = [];
295
-
296
- args[0].forEach(function (item, index, items) {
297
-
298
- if (isCustomTag.test(item)) {
299
- inCustomTag = -1 === item.substring(item.match(isCustomTag).index).indexOf(">");
300
- localName = inCustomTag && item.substring(item.indexOf(item.match(isCustomTag))).split(" ")[0].substr(1);
301
- } // END if CustomTag start
302
- else if (0 <= item.indexOf(">")) {
303
- inCustomTag = false;
304
- localName = "";
305
- } // END if CustomTag end
306
-
307
- if (!inCustomTag) {
308
- return;
309
- }
310
- var val = args[index + 1];
311
-
312
- if ("function" === typeof val || "object" === (typeof val === 'undefined' ? 'undefined' : _typeof(val))) {
313
- var attrName = item.split(" ").pop().slice(0, -1);
314
- if ("on" === attrName.substring(0, 2)) {
315
- throw new Error('\'on\' is reserve for native elements. Change: "' + attrName + '" for "' + localName + '" to something else');
316
- }
317
- var id = makeid();
318
- sharedAttrs[id] = { attrName: attrName, val: val, localName: localName };
319
- args[index + 1] = ("function" === typeof val ? 'fn-' : 'ob-') + id;
320
- } // END if("function" === typeof val)
321
- }); // END forEach
322
- } // END if
323
-
324
- return hyperHTMLbind.apply(undefined, args);
325
- }; // END ref.Html
326
- ref.Html.wire = function wire() {
327
- return hyperHTML.wire.apply(hyperHTML, arguments);
328
- };
329
- ref.Html.lite = function lite() {
330
- return hyperHTML.apply(undefined, arguments);
331
- };
332
-
333
- if (this.attrs) {
334
- throw new Error("'attrs' is defined!!");
335
- }
336
- that.attrs = this.attachAttrs(this.attributes) || {};
337
- that.dataset = this.getDataset();
338
- var render = this.render;
339
- this.render = function () {
340
- for (var _len3 = arguments.length, data = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
341
- data[_key3] = arguments[_key3];
342
- }
343
-
344
- ref.observe = false;
345
- setTimeout(function () {
346
- ref.observe = true;
347
- }, 0);
348
-
349
- render.call.apply(render, [that, ref.Html].concat(data));
350
-
351
- //after render check if dataset has chacked
352
- Object.getOwnPropertyNames(that.dataset).filter(function (key) {
353
- return !_this2.dataset[key];
354
- }).forEach(function (key) {
355
-
356
- var value = that.dataset[key];
357
- _this2.addDataset(that.dataset, key.replace(/([A-Z])/g, function (g) {
358
- return '-' + g[0].toLowerCase();
359
- }));
360
- that.dataset[key] = value;
361
- });
362
- };
363
-
364
- if (this.setup) {
365
- ref.teardown = this.setup.call(that, onNext.bind(this, that));
366
- }
367
-
368
- this.render();
369
- }
370
-
371
- //=====================================================
372
- //==================================== Wrap the element
373
- //=====================================================
374
-
375
- var hyperElement = function (_HTMLElement) {
376
- _inherits(hyperElement, _HTMLElement);
377
-
378
- function hyperElement() {
379
- _classCallCheck(this, hyperElement);
380
-
381
- return _possibleConstructorReturn(this, (hyperElement.__proto__ || Object.getPrototypeOf(hyperElement)).apply(this, arguments));
382
- }
383
-
384
- _createClass(hyperElement, [{
385
- key: 'createdCallback',
386
-
387
-
388
- //++++++++++++++++++++++++++++++++++++++++++++++ Setup
389
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
390
- value: function createdCallback() {
391
- _createdCallback.call(this);
392
- }
393
-
394
- // Called when the element is inserted into a document, including into a shadow tree
395
-
396
- }, {
397
- key: 'connectedCallback',
398
- value: function connectedCallback() {
399
- _createdCallback.call(this);
400
- }
401
-
402
- //+++++++++++++++++++++++++++++++++++++++ attach Attrs
403
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
404
-
405
- }, {
406
- key: 'addDataset',
407
- value: function addDataset(dataset, dash_key) {
408
- var _this4 = this;
409
-
410
- var camel_key = dash_key.replace(/-([a-z])/g, function (g) {
411
- return g[1].toUpperCase();
412
- });
413
-
414
- Object.defineProperty(dataset, camel_key, {
415
- enumerable: true, // can be selected
416
- configurable: true, // can be delete
417
- get: function get() {
418
- return parceAttribute(camel_key, _this4.dataset[camel_key]);
419
- },
420
- set: function set(value) {
421
- manager[_this4.identifier].attrsToIgnore["data-" + dash_key] = true;
422
- if ("string" === typeof value) {
423
- _this4.dataset[camel_key] = value;
424
- } else {
425
- _this4.dataset[camel_key] = JSON.stringify(value);
426
- } // END else
427
- } // END set
428
-
429
- }); // END defineProperty
430
- } // END addDataset
431
-
432
- }, {
433
- key: 'getDataset',
434
- value: function getDataset() {
435
- var _this5 = this;
436
-
437
- var dataset = {};
438
- Object.keys(this.dataset).forEach(function (key) {
439
- return _this5.addDataset(dataset, key.replace(/([A-Z])/g, function (g) {
440
- return '-' + g[0].toLowerCase();
441
- }));
442
- }); // END forEach
443
- return dataset;
444
- } // END getDataset
445
-
446
- }, {
447
- key: 'attachAttrs',
448
- value: function attachAttrs(attributes) {
449
-
450
- var accumulator = {};
451
-
452
- for (var i = 0; i < attributes.length; i++) {
453
- var _attributes$i = attributes[i],
454
- value = _attributes$i.value,
455
- name = _attributes$i.name;
456
-
457
-
458
- if ("template" === name && !value) {
459
-
460
- var ref = manager[this.identifier];
461
- ref.Html.template = buildTemplate(ref.innerHTML);
462
- accumulator[name] = true;
463
- } else if ("fn-" === value.substr(0, 3) || "ob-" === value.substr(0, 3) && !!sharedAttrs[value.substr(3)] && sharedAttrs[value.substr(3)].localName === this.localName) {
464
- accumulator[name] = sharedAttrs[value.substr(3)].val;
465
- } else {
466
- if (+value + "" === (value + "").trim()) {
467
- accumulator[name] = +value;
468
- } else {
469
- accumulator[name] = value; //parceAttribute(name,value)
470
- }
471
- }
472
- }
473
- return accumulator;
474
- }
475
- /*
476
- attachedCallback(){
477
- console.log("an instance was inserted into the document")
478
- //an instance was inserted into the document
479
- }
480
- */
481
-
482
- //+++++++++++++++++++++++++++++++++++ element teardown
483
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
484
- /*
485
- detachedCallback(){
486
- this.disposer && this.disposer()
487
- this.disconnectedCallback()
488
- }
489
- */
490
-
491
- }, {
492
- key: 'attributeChangedCallback',
493
- value: function attributeChangedCallback(name, oldVal, newVal) {
494
- if (+newVal + "" === newVal.trim()) {
495
- newVal = +newVal; // to number
496
- }
497
- var ref = manager[this.identifier];
498
- var attrsToIgnore = ref.attrsToIgnore;
499
-
500
- var that = ref.this;
501
- if (0 <= name.indexOf("data-")) {
502
- // we have data
503
- var dataSetName = name.slice("data-".length);
504
- if (null === oldVal) {
505
- //if(undefined === that.dataset[dataSetName]){
506
- this.addDataset(that.dataset, dataSetName);
507
- } else if (null === newVal) {
508
- // Object.defineProperty(that.dataset, dataSetName, { }) // END defineProperty
509
- var camel_key = dataSetName.replace(/-([a-z])/g, function (g) {
510
- return g[1].toUpperCase();
511
- });
512
- delete that.dataset[camel_key];
513
- }
514
- }
515
- //newVal = parceAttribute(name,newVal)
516
-
517
- if (newVal === that.attrs[name]) {
518
- return;
519
- }
520
- if (null === newVal) {
521
- delete that.attrs[name];
522
- } else {
523
- that.attrs[name] = newVal;
524
- }
525
- if (!!attrsToIgnore[name]) {
526
- delete attrsToIgnore[name];
527
- return;
528
- } else {
529
- this.render();
530
- } // END else
531
- } // END attributeChangedCallback
532
-
533
- }, {
534
- key: 'disconnectedCallback',
535
- value: function disconnectedCallback() {
536
- var ref = manager[this.identifier];
537
- ref.teardown && ref.teardown();
538
- //ref.teardown = null
539
- //Called when the element is removed from a document
540
- } // END disconnectedCallback
541
-
542
- }, {
543
- key: 'innerShadow',
544
-
545
-
546
- //++++++++++++++++++++++++++++++++ get element content
547
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
548
-
549
- get: function get() {
550
- return manager[this.identifier].shadow.innerHTML;
551
- }
552
- }]);
553
-
554
- return hyperElement;
555
- }(HTMLElement);
556
-
557
- //=====================================================
558
- //================================================ Done
559
- //=====================================================
560
-
561
- return hyperElement;
562
- });