hyper-element 0.10.0 → 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,554 +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
-
168
- return hyperHTML.wire(data, templateVals.id).apply(undefined, _toConsumableArray(fragment(data)));
169
- };
170
- } // END buildTemplate
171
-
172
- function parceAttribute(key, value) {
173
- if ("template" === key && "" === value) {
174
- return true;
175
- }
176
-
177
- if (+value + "" === value.trim()) {
178
- return +value; // to number
179
- }
180
-
181
- var lowerCaseValue = value.toLowerCase().trim();
182
-
183
- if ("true" === lowerCaseValue) {
184
- return true;
185
- } else if ("false" === lowerCaseValue) {
186
- return false;
187
- } // END boolean check
188
-
189
- //if("data-json"===key){
190
- if (lowerCaseValue[0] === "[" && lowerCaseValue.slice(-1) === "]" || lowerCaseValue[0] === "{" && lowerCaseValue.slice(-1) === "}") {
191
- return JSON.parse(value);
192
- }
193
-
194
- return value;
195
- } // END parceAttribute
196
-
197
-
198
- //=====================================================
199
- //======================================= All the magic
200
- //=====================================================
201
-
202
- function _createdCallback() {
203
- var _this2 = this;
204
-
205
- // an instance of the element is created
206
- this.identifier = Symbol(this.localName);
207
- var ref = manager[this.identifier] = { attrsToIgnore: {} };
208
- ref.innerHTML = this.innerHTML;
209
- var that = ref.this = { element: this };
210
- that.wrappedContent = this.textContent;
211
-
212
- observer.call(this, ref); // observer change to innerHTML
213
-
214
- Object.getOwnPropertyNames(this.__proto__).filter(function (name) {
215
- return !("constructor" === name || "setup" === name || "render" === name);
216
- }).forEach(function (name) {
217
- if (/^[A-Z]/.test(name)) {
218
- var result = void 0;
219
- var templatestrings = {};
220
- var wrapFragment = function wrapFragment(data) {
221
-
222
- if (undefined !== result && result.once) return result;
223
-
224
- result = _this2[name](data);
225
- if (!!result.template) {
226
- if ("string" === typeof result.template) {
227
- /* if(undefined === result.values){
228
- throw new Error("'values' was not defined for a 'template' in "+name)
229
- }*/
230
- if (!templatestrings[result.template]) {
231
- templatestrings[result.template] = buildTemplate(result.template);
232
- }
233
- result = { any: templatestrings[result.template](result.values || data) };
234
- } // END "string" === typeof result.template
235
- else if ("object" === _typeof(result.template) && "function" === typeof result.template.then) {
236
-
237
- result = Object.assign({}, result, { any: result.template.then(function (_ref) {
238
- var template = _ref.template,
239
- values = _ref.values;
240
-
241
-
242
- if (!templatestrings[template]) {
243
- templatestrings[template] = buildTemplate(template);
244
- }
245
- if (Array.isArray(values)) {
246
- result = { any: values.map(templatestrings[template]) };
247
- } else {
248
- //TODO: test if values is an object
249
- result = { any: templatestrings[template](values || data) };
250
- }
251
- return result.any;
252
- })
253
- }); // END Object.assign
254
- } // END result.template is promise ?
255
- else {
256
- throw new Error("unknow template type:" + _typeof(result.template) + " | " + JSON.stringify(result.template));
257
- }
258
- } // END !!result.template
259
- return result;
260
- }; // END wrapFragment
261
- hyperHTML.define(name, wrapFragment);
262
- } else {
263
- that[name] = _this2[name].bind(that);
264
- }
265
- delete _this2[name];
266
- });
267
- function toString() {
268
- return "hyper-element: " + this.localName;
269
- }
270
- Object.defineProperty(that, "toString", { value: toString.bind(this), writable: false });
271
- // use shadow DOM, else fallback to render to element
272
- ref.shadow = this; //.attachShadow ? this.attachShadow({mode: 'closed'}) : this
273
-
274
- // Restrict access to hyperHTML
275
- var hyperHTMLbind = hyperHTML.bind(ref.shadow);
276
- ref.Html = function Html() {
277
- for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
278
- args[_key2] = arguments[_key2];
279
- }
280
-
281
- if (args.some(function (item) {
282
- return "function" === typeof item;
283
- }) && args[0].some(function (t) {
284
- return isCustomTag.test(t);
285
- })) {
286
-
287
- var inCustomTag = false;
288
- var localName = "";
289
- var lookup = [];
290
-
291
- args[0].forEach(function (item, index, items) {
292
-
293
- if (isCustomTag.test(item)) {
294
- inCustomTag = -1 === item.substring(item.match(isCustomTag).index).indexOf(">");
295
- localName = inCustomTag && item.substring(item.indexOf(item.match(isCustomTag))).split(" ")[0].substr(1);
296
- } // END if CustomTag start
297
- else if (0 <= item.indexOf(">")) {
298
- inCustomTag = false;
299
- localName = "";
300
- } // END if CustomTag end
301
-
302
- if (!inCustomTag) {
303
- return;
304
- }
305
- var val = args[index + 1];
306
-
307
- if ("function" === typeof val) {
308
- var attrName = item.split(" ").pop().slice(0, -1);
309
- if ("on" === attrName.substring(0, 2)) {
310
- throw new Error('\'on\' is reserve for native elements. Change: "' + attrName + '" for "' + localName + '" to something else');
311
- }
312
- var id = makeid();
313
- sharedAttrs[id] = { attrName: attrName, val: val, localName: localName };
314
- args[index + 1] = 'fn-' + id;
315
- } // END if("function" === typeof val)
316
- }); // END forEach
317
- } // END if
318
-
319
- return hyperHTMLbind.apply(undefined, args);
320
- }; // END ref.Html
321
- ref.Html.wire = function wire() {
322
- return hyperHTML.wire.apply(hyperHTML, arguments);
323
- };
324
- ref.Html.lite = function lite() {
325
- return hyperHTML.apply(undefined, arguments);
326
- };
327
-
328
- if (this.attrs) {
329
- throw new Error("'attrs' is defined!!");
330
- }
331
- that.attrs = this.attachAttrs(this.attributes) || {};
332
- that.dataset = this.getDataset();
333
- var render = this.render;
334
- this.render = function () {
335
- for (var _len3 = arguments.length, data = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
336
- data[_key3] = arguments[_key3];
337
- }
338
-
339
- ref.observe = false;
340
- setTimeout(function () {
341
- ref.observe = true;
342
- }, 0);
343
-
344
- render.call.apply(render, [that, ref.Html].concat(data));
345
-
346
- //after render check if dataset has chacked
347
- Object.getOwnPropertyNames(that.dataset).filter(function (key) {
348
- return !_this2.dataset[key];
349
- }).forEach(function (key) {
350
-
351
- var value = that.dataset[key];
352
- _this2.addDataset(that.dataset, key.replace(/([A-Z])/g, function (g) {
353
- return '-' + g[0].toLowerCase();
354
- }));
355
- that.dataset[key] = value;
356
- });
357
- };
358
-
359
- if (this.setup) {
360
- ref.teardown = this.setup.call(that, onNext.bind(this, that));
361
- }
362
-
363
- this.render();
364
- }
365
-
366
- //=====================================================
367
- //==================================== Wrap the element
368
- //=====================================================
369
-
370
- var hyperElement = function (_HTMLElement) {
371
- _inherits(hyperElement, _HTMLElement);
372
-
373
- function hyperElement() {
374
- _classCallCheck(this, hyperElement);
375
-
376
- return _possibleConstructorReturn(this, (hyperElement.__proto__ || Object.getPrototypeOf(hyperElement)).apply(this, arguments));
377
- }
378
-
379
- _createClass(hyperElement, [{
380
- key: 'createdCallback',
381
-
382
-
383
- //++++++++++++++++++++++++++++++++++++++++++++++ Setup
384
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
385
- value: function createdCallback() {
386
- _createdCallback.call(this);
387
- }
388
-
389
- // Called when the element is inserted into a document, including into a shadow tree
390
-
391
- }, {
392
- key: 'connectedCallback',
393
- value: function connectedCallback() {
394
- _createdCallback.call(this);
395
- }
396
-
397
- //+++++++++++++++++++++++++++++++++++++++ attach Attrs
398
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
399
-
400
- }, {
401
- key: 'addDataset',
402
- value: function addDataset(dataset, dash_key) {
403
- var _this4 = this;
404
-
405
- var camel_key = dash_key.replace(/-([a-z])/g, function (g) {
406
- return g[1].toUpperCase();
407
- });
408
-
409
- Object.defineProperty(dataset, camel_key, {
410
- enumerable: true, // can be selected
411
- configurable: true, // can be delete
412
- get: function get() {
413
- return parceAttribute(camel_key, _this4.dataset[camel_key]);
414
- },
415
- set: function set(value) {
416
- manager[_this4.identifier].attrsToIgnore["data-" + dash_key] = true;
417
- if ("string" === typeof value) {
418
- _this4.dataset[camel_key] = value;
419
- } else {
420
- _this4.dataset[camel_key] = JSON.stringify(value);
421
- } // END else
422
- } // END set
423
-
424
- }); // END defineProperty
425
- } // END addDataset
426
-
427
- }, {
428
- key: 'getDataset',
429
- value: function getDataset() {
430
- var _this5 = this;
431
-
432
- var dataset = {};
433
- Object.keys(this.dataset).forEach(function (key) {
434
- return _this5.addDataset(dataset, key.replace(/([A-Z])/g, function (g) {
435
- return '-' + g[0].toLowerCase();
436
- }));
437
- }); // END forEach
438
- return dataset;
439
- } // END getDataset
440
-
441
- }, {
442
- key: 'attachAttrs',
443
- value: function attachAttrs(attributes) {
444
-
445
- var accumulator = {};
446
-
447
- for (var i = 0; i < attributes.length; i++) {
448
- var _attributes$i = attributes[i],
449
- value = _attributes$i.value,
450
- name = _attributes$i.name;
451
-
452
-
453
- if ("template" === name && !value) {
454
-
455
- var ref = manager[this.identifier];
456
- ref.Html.template = buildTemplate(ref.innerHTML);
457
- accumulator[name] = true;
458
- } else if ("fn-" === value.substr(0, 3) && !!sharedAttrs[value.substr(3)] && sharedAttrs[value.substr(3)].localName === this.localName) {
459
- accumulator[name] = sharedAttrs[value.substr(3)].val;
460
- } else {
461
- if (+value + "" === (value + "").trim()) {
462
- accumulator[name] = +value;
463
- } else {
464
- accumulator[name] = value; //parceAttribute(name,value)
465
- }
466
- }
467
- }
468
- return accumulator;
469
- }
470
- /*
471
- attachedCallback(){
472
- console.log("an instance was inserted into the document")
473
- //an instance was inserted into the document
474
- }
475
- */
476
-
477
- //+++++++++++++++++++++++++++++++++++ element teardown
478
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
479
- /*
480
- detachedCallback(){
481
- this.disposer && this.disposer()
482
- this.disconnectedCallback()
483
- }
484
- */
485
-
486
- }, {
487
- key: 'attributeChangedCallback',
488
- value: function attributeChangedCallback(name, oldVal, newVal) {
489
- var ref = manager[this.identifier];
490
- var attrsToIgnore = ref.attrsToIgnore;
491
-
492
- var that = ref.this;
493
- if (0 <= name.indexOf("data-")) {
494
- // we have data
495
- var dataSetName = name.slice("data-".length);
496
- if (null === oldVal) {
497
- //if(undefined === that.dataset[dataSetName]){
498
- this.addDataset(that.dataset, dataSetName);
499
- } else if (null === newVal) {
500
- // Object.defineProperty(that.dataset, dataSetName, { }) // END defineProperty
501
- var camel_key = dataSetName.replace(/-([a-z])/g, function (g) {
502
- return g[1].toUpperCase();
503
- });
504
- delete that.dataset[camel_key];
505
- }
506
- }
507
- //newVal = parceAttribute(name,newVal)
508
-
509
- if (newVal === that.attrs[name]) {
510
- return;
511
- }
512
- if (null === newVal) {
513
- delete that.attrs[name];
514
- } else {
515
- that.attrs[name] = newVal;
516
- }
517
- if (!!attrsToIgnore[name]) {
518
- delete attrsToIgnore[name];
519
- return;
520
- } else {
521
- this.render();
522
- } // END else
523
- } // END attributeChangedCallback
524
-
525
- }, {
526
- key: 'disconnectedCallback',
527
- value: function disconnectedCallback() {
528
- var ref = manager[this.identifier];
529
- ref.teardown && ref.teardown();
530
- //ref.teardown = null
531
- //Called when the element is removed from a document
532
- } // END disconnectedCallback
533
-
534
- }, {
535
- key: 'innerShadow',
536
-
537
-
538
- //++++++++++++++++++++++++++++++++ get element content
539
- //++++++++++++++++++++++++++++++++++++++++++++++++++++
540
-
541
- get: function get() {
542
- return manager[this.identifier].shadow.innerHTML;
543
- }
544
- }]);
545
-
546
- return hyperElement;
547
- }(HTMLElement);
548
-
549
- //=====================================================
550
- //================================================ Done
551
- //=====================================================
552
-
553
- return hyperElement;
554
- });