-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_element.js
2745 lines (2199 loc) Β· 58.4 KB
/
custom_element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let has_premature_undried_elements,
originalCreateElement,
creating_element,
observed_attributes = Symbol('observed_attributes'),
weak_properties = new WeakMap(),
browser_store = {},
ENSURED_CSS_FILES,
Element,
has_v1 = typeof customElements == 'object',
render_counter = 0;
const CURRENT_RENDER = Symbol('current_render');
let custom_stylesheet_handler;
if (Blast.isBrowser) {
Hawkejs.browser_store = browser_store;
ENSURED_CSS_FILES = new Map();
}
/**
* Base element constructor
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.1.0
* @version 2.2.17
*/
function ElementConstructor() {
if (this[Hawkejs.CONSTRUCTED]) {
throw new Error('Element ' + this.nodeName + ' is being constructed twice');
}
let ElClass = this.constructor;
if (ElClass._has_default_attributes === true) {
let name,
conf;
for (name in ElClass._attributes) {
conf = ElClass._attributes[name];
if (conf.default != null && !this.hasAttribute(name)) {
this.setAttributeSilent(name, conf.default);
}
}
}
if (this.attributes.length) {
let attr,
i;
// Trigger an initial attribute change
for (i = 0; i < this.attributes.length; i++) {
attr = this.attributes.item(i);
this.attributeChangedCallback(attr.name, null, attr.value, true);
}
}
// If the custom element has an inline template,
// execute it immediately
if (ElClass.compiled_template && ElClass.compiled_template.render_immediate !== false) {
// If this has already been rendered or the render should be delayed, do nothing
if (this[Hawkejs.DELAY_SYNC_RENDER] || this.hasAttribute('he-rendered')) {
// Do nothing
} else {
this._renderTemplateSynchronously();
}
}
this[Hawkejs.CONSTRUCTED] = true;
}
Hawkejs.CustomElementConstructor = ElementConstructor;
if (Blast.isNode) {
Element = Fn.inherits('Hawkejs.HTMLElement', 'Hawkejs.Element', function Element() {
// Do not call the HTMLElement super, that'll be done somewhere else
});
} else {
// IE & Edge fix
if (typeof HTMLElement === 'object') {
let Element = function HTMLElement(){};
Element.prototype = HTMLElement.prototype;
HTMLElement = Element;
}
Element = Blast.Bound.Function.inherits('HTMLElement', 'Hawkejs.Element', function Element() {
// Do nothing
});
if (typeof customElements != 'undefined') {
let originalCreateElement = document.createElement;
Hawkejs.originalCreateElement = originalCreateElement;
document.createElement = function createElement(name) {
const element = Hawkejs._createUnconstructedElement.apply(null, arguments);
// Call the custom-element constructor too
if (Hawkejs.Hawkejs.isCustomElement(element)) {
element[Hawkejs.CREATED_MANUALLY] = true;
element.constructor.call(element);
ElementConstructor.call(element);
}
return element;
};
}
}
/**
* Create an element on which we can call the constructor later
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 2.1.0
* @version 2.2.9
*
* @param {String} name
*
* @return {HTMLElement}
*/
Hawkejs._createUnconstructedElement = function _createUnconstructedElement(name) {
let element;
creating_element = true;
if (Hawkejs.originalCreateElement) {
element = Hawkejs.originalCreateElement.apply(document, arguments);
} else if (Blast.isBrowser || !Hawkejs.Hawkejs.isCustomElement(name)) {
element = Hawkejs.Hawkejs.createElement(name);
} else {
// Get the constructor
let constructor = Hawkejs.Hawkejs.elements[name];
// Create the element without invoking the constructor
element = Object.create(constructor.prototype);
// Call the HTMLElement constructor
// (The custom constructor will be called later!)
Hawkejs.HTMLElement.call(element);
}
creating_element = false;
return element;
};
/**
* Create a private variable for the given instance
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.1.1
* @version 2.3.19
*
* @param {Element} instance
*
* @return {Object}
*/
const getPrivate = instance => {
let map = weak_properties.get(instance);
if (!map) {
map = {
// Attribute configurations
attributes : {},
// Attribute values for use in property getters
prop_values : {},
// Attribute string values
values : {},
// New values that are being set
new_values : {},
// Cached element getters
cached_elements : new Map(),
};
weak_properties.set(instance, map);
}
return map;
};
/**
* Is the given name allowed as a property?
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 2.1.3
* @version 2.1.3
*
* @param {String} name
*/
function assertPropertyName(name) {
switch (name) {
case 'children':
case 'childNodes':
case 'dataset':
throw new Error('The property name "' + name + '" is already in use');
}
}
/**
* Register the element in the browser
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.1.0
* @version 2.2.17
*/
Element.constitute(function registerElement() {
if (this.is_abstract_class) {
return;
}
let name = this.name,
tag_name;
if (this.prototype.hasOwnProperty('tagName')) {
tag_name = this.prototype.tagName;
}
if (name.endsWith('Element')) {
name = Bound.String.before(name, 'Element');
}
// If the name is just "Element", ignore it
if (!name && !tag_name) {
return;
}
const that = this;
let upper_tag,
options;
if (!tag_name) {
// Add underscores
tag_name = Bound.String.underscore(name);
// Turn underscores into dashes
tag_name = Bound.String.dasherize(tag_name);
// See if a prefix *needs* to be added
let prefix = this.custom_element_prefix;
// If the prefix equals the name, don't use it
// (This is to prevent alchemy-widgets-alchemy-widgets situations)
if (prefix == tag_name) {
prefix = false;
}
// Make sure the name contains a dash
if (prefix || tag_name.indexOf('-') < 1) {
// If no custom prefix was found, see if a default one exists
if (!prefix) {
prefix = this.default_element_prefix;
}
// Still nothing? Look in the namespace
if (!prefix) {
let ns;
if (this.namespace) {
ns = Bound.Function.getNamespace(this.namespace);
}
if (ns && ns.default_element_prefix) {
prefix = ns.default_element_prefix;
}
}
if (!prefix) {
prefix = 'he';
}
tag_name = prefix + '-' + tag_name;
}
}
// Make sure the tag name is uppercased
tag_name = tag_name.toUpperCase();
// This is for elements with a "parent" element, so it would be like
// <button is="my-button">, though this is rarely used
if (this.prototype.parent_element) {
upper_tag = this.prototype.parent_element.toUpperCase();
} else {
upper_tag = tag_name;
}
let lower_name = tag_name.toLowerCase();
this.setProperty('nodeName', tag_name);
this.setProperty('tagName', upper_tag);
this.setProperty('_l_node_name', lower_name);
this.setProperty('_l_tag_name', upper_tag.toLowerCase());
if (Blast.isNode) {
// No need to mess with any `define` stuff on the server
} else if (typeof customElements == 'undefined') {
options = {
prototype: Object.create(this.prototype, {createdCallback: {value: this}})
};
// If a parent element is set, the 'is=""' attribute needs to be used
if (this.prototype.parent_element) {
options.extends = this.prototype.parent_element;
}
// Register the element in the browser
Blast.afterOnce('requiring', function registerElement() {
that.ElementConstructor = document.registerElement(name, options);
});
} else {
options = {};
// If a parent element is set, the 'is=""' attribute needs to be used
if (this.prototype.parent_element) {
options.extends = this.prototype.parent_element;
}
// Create a wrapper function that'll be used to actually create the element
// using the v1 custom-element structure
let ClassSyntaxFixer = function ClassSyntaxFixer() {
var instance = Reflect.construct(HTMLElement, [], ClassSyntaxFixer);
if (creating_element) {
// Do nothing, constructor will be called later
} else if (typeof hawkejs == 'undefined' || (Blast.isBrowser && document.readyState != 'complete')) {
Blast.setImmediate(function callWithHawkejsOnNextTick() {
if (document.readyState == 'loading') {
return Blast.setImmediate(callWithHawkejsOnNextTick);
}
ElementConstructor.call(instance);
});
} else {
ElementConstructor.call(instance);
}
return instance;
}
// Needed to make onAttributeChanged work
Fn.setStaticProperty(ClassSyntaxFixer, function observedAttributes() {
return that.observedAttributes;
});
// Make sure to completely replace the wrapper's prototype
// with our own
ClassSyntaxFixer.prototype = that.prototype;
// Register the element as soon as all the constitutors have finished
that.afterConstituted(function defineElement() {
customElements.define(lower_name, ClassSyntaxFixer, options);
});
}
// Register with hawkejs
Hawkejs.Hawkejs.elements[lower_name] = this;
Hawkejs.Hawkejs.elements[upper_tag] = this;
});
/**
* Set the tagName of this element
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 2.2.17
* @version 2.2.17
*
* @param {String} tag_name
*/
Element.setStatic(function setTagName(tag_name) {
this.setProperty('tagName', tag_name.toUpperCase());
});
/**
* Do something when the scene is ready
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.3.2
* @version 1.3.2
*
* @param {Function}
*/
Element.setStatic(function sceneReady(fnc) {
if (Blast.hasBeenSeen('hawkejs_registered')) {
return Blast.nextTick(fnc);
}
Blast.afterOnce('hawkejs_registered', function waitForScene() {
fnc();
});
}, false);
/**
* Revive this object
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.1.0
* @version 2.2.22
*/
Element.setStatic(function unDry(obj, force) {
var that = this,
element,
temp,
key;
if (Blast.isBrowser) {
let tagName = obj.tagName || obj.tag_name;
if (obj.hawkejs_id) {
element = document.querySelector('[data-hid="' + obj.hawkejs_id + '"]');
}
if (!element) {
if (obj.outerHTML) {
element = Blast.parseHTML(obj.outerHTML);
if (typeof element == 'string') {
return element;
}
} else if (tagName) {
element = document.createElement(tagName);
Hawkejs.setAttributes(element, obj.attributes);
if (obj.className) {
element.className = obj.className;
}
if (obj.id) {
element.id = obj.id;
}
if (obj.cssText) {
element.style.cssText = obj.cssText;
}
if (obj.innerHTML) {
element.innerHTML = obj.innerHTML;
}
if (obj.dataset) {
let key;
for (key in obj.dataset) {
element.dataset[key] = obj.dataset[key];
}
}
if (obj.assigned_data) {
element.assigned_data = obj.assigned_data;
}
if (obj.renderer) {
element.hawkejs_renderer = obj.renderer;
}
// Make it empty, or else it'll set it again later
obj = {};
}
}
// No id was given, or it wasn't found (yet)
// So create a temporary element
if (!element) {
if (!obj.hawkejs_id || Hawkejs.canQueryBody()) {
force = true;
}
// If force is enabled, a new element is made
if (force) {
if (tagName || has_v1) {
element = document.createElement(tagName || this.prototype.nodeName);
} else if (this.ElementConstructor) {
element = new this.ElementConstructor;
} else {
throw new Error('Custom element ' + this.name + ' has no ElementConstructor');
}
} else {
has_premature_undried_elements = true;
// Return an object the Scene instance will replace
// once the DOM is ready
temp = {
__delay_undry: function delayUndry() {
if (temp.revived == null) {
temp.revived = that.unDry(obj, true);
}
return temp.revived;
},
revived: null,
obj: obj
};
return temp;
}
}
} else {
element = new this;
}
if (obj.assigned_data) {
element.assigned_data = obj.assigned_data;
}
if (obj.variables) {
element[Hawkejs.VARIABLES] = obj.variables;
}
// Delay this so the object is fully undried
Element.sceneReady(function() {
if (typeof element.undried == 'function') {
element.undried();
}
});
return element;
});
/**
* Make sure contents are rendered before doing this parent getter/method
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 2.2.3
* @version 2.2.3
*/
Element.setStatic(function renderBeforeGet(name) {
let descriptor = Blast.Collection.Object.getPropertyDescriptor(this.prototype, name);
function getParent() {
if (this[Hawkejs.DELAY_SYNC_RENDER]) {
this._renderTemplateSynchronously();
}
return getParent.super.apply(this, arguments);
}
if (descriptor.value) {
this.setMethod(name, getParent);
} else {
this.setProperty(name, getParent);
}
});
/**
* Fix premature undried elements
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 2.0.0
* @version 2.0.0
*/
Element.setStatic(function fixPrematureUndriedElements(scene, renderer) {
if (!has_premature_undried_elements) {
return;
}
// This shouldn't take too long, unless lots of debug data is set
Obj.walk(renderer.variables, function eachEntry(value, key, parent) {
if (value && typeof value == 'object' && value.__delay_undry) {
parent[key] = value.__delay_undry();
}
});
});
/**
* Set the custom-element stylesheet handler.
* Can only be called on the root custom element class.
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 2.3.18
* @version 2.3.18
*/
Element.setStatic(function setStylesheetHandler(fnc) {
custom_stylesheet_handler = fnc;
}, false);
/**
* Set the role of this element
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 2.1.3
* @version 2.1.3
*
* @param {String} value
*/
Element.setStatic(function setRole(value) {
this.setAttribute('role', {default: value});
});
/**
* Add an attribute setter/getter
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.2.8
* @version 2.1.3
*/
Element.setStatic(function setAttribute(name, getter, setter, config) {
if (typeof name == 'function') {
config = setter;
setter = getter;
getter = name;
name = getter.name;
}
if (!name) {
throw new Error('No name was given for attribute');
}
assertPropertyName(name);
if (getter && typeof getter == 'object') {
config = getter;
getter = null;
setter = null;
}
if (setter && typeof setter == 'object') {
config = setter;
setter = null;
}
if (!config) {
config = {};
}
this.constitute(function setAttribute() {
this._setAttribute(name, getter, setter, config);
});
});
/**
* Add an attribute setter/getter
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.1.1
* @version 2.3.12
*/
Element.setStatic(function _setAttribute(name, getter, setter, config) {
// Store the custom getters and setters
config.getter = getter;
config.setter = setter;
// Create an object on the constructor
// so we know which attributes are configured
if (!this._attributes) {
this._attributes = {};
}
let different_prop_type = false,
attribute_name = name,
property_name = name,
type = config.type || 'string',
is_boolean = false,
is_number = false,
is_token_list = false;
if (name.indexOf('_') > -1) {
attribute_name = name.replace(/_/g, '-');
}
if (name.indexOf('-') > -1) {
property_name = name.replace(/-/g, '_');
}
if (config.boolean) {
type = 'boolean';
} else if (config.number) {
type = 'number';
}
config.type = type;
if (config.type !== 'string') {
different_prop_type = true;
is_boolean = type === 'boolean';
is_number = type === 'number';
is_token_list = type === 'token_list';
}
config.is_boolean = is_boolean;
config.is_number = is_number;
config.is_token_list = is_token_list;
if (setter || different_prop_type) {
this.addObservedAttribute(attribute_name);
}
// Store the attribute configuration
this._attributes[attribute_name] = config;
if (config.default != null) {
this._has_default_attributes = true;
}
let names = [attribute_name];
if (attribute_name != property_name) {
names.push(property_name);
}
// Set the instance property getter/setter
this.setProperty(names, function attrGetter() {
var val;
if (this.constructor.prototype == this) {
return;
}
if (different_prop_type) {
let map = getPrivate(this);
val = map.prop_values[attribute_name];
if (is_token_list && !val) {
val = new Hawkejs.LinkedTokenList(this, name);
map.prop_values[attribute_name] = val;
}
} else {
val = this.getAttribute(attribute_name);
}
// Custom getters can modify the attribute string
if (getter) {
val = getter.call(this, val);
}
if (is_boolean) {
return !!val;
}
return val;
}, function attrSetter(value) {
if (this.constructor.prototype == this) {
throw new Error('Unable to assign to setter outside of object instance');
}
let newval;
if (setter) {
newval = setter.call(this, value);
// If there was no return value, use the original value
if (typeof newval == 'undefined') {
newval = value;
}
} else {
newval = value;
}
if (is_boolean) {
newval = !!newval;
if (newval) {
this.setAttributeSilent(attribute_name, newval);
} else {
this.removeAttributeSilent(attribute_name);
}
} else {
this.setAttributeSilent(attribute_name, newval);
}
return newval;
});
});
/**
* Monitor a value for changes
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.1.0
* @version 1.1.0
*/
Element.setStatic(function monitor(name, callback) {
if (!this._monitors) this._monitors = {};
if (!this._monitors[name]) this._monitors[name] = [];
this._monitors[name].push(callback);
});
/**
* Add a property that is stored in assigned_data
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 1.2.2
* @version 2.1.3
*/
Element.setStatic(function setAssignedProperty(name, getter, setter) {
if (typeof name == 'function') {
setter = getter;
getter = name;
name = getter.name;
}
assertPropertyName(name);
let listener_method = 'on' + Bound.String.camelize(name) + 'Assignment';
this.setProperty(name, function assignedGetter() {
var val;
if (this.constructor.prototype == this) {
return;
}
val = this.assigned_data[name];
if (getter) {
val = getter.call(this, val);
}
return val;
}, function assignedSetter(value) {
var oldval,
newval,
replace;
if (this.constructor.prototype == this) {
throw new Error('Unable to assign to setter outside of object instance');
}
if (setter) {
newval = setter.call(this, value);
if (typeof newval == 'undefined') {
newval = value;
}
} else {
newval = value;
}
oldval = this.assigned_data[name];
if (Blast.isNode && oldval == null && this.hawkejs_renderer) {
this.hawkejs_renderer.registerElementInstance(this);
}
this.assigned_data[name] = newval;
if (typeof this[listener_method] == 'function') {
replace = this[listener_method](newval, oldval);
if (typeof replace !== 'undefined' && !(replace && replace.then)) {
newval = replace;
this.assigned_data[name] = replace;
}
}
return newval;
});
});
/**
* Add a getter with a query selector
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 2.0.0
* @version 2.3.19
*
* @param {String|Object} name
* @param {String} query
*/
Element.setStatic(function addElementGetter(name, query) {
if (arguments.length == 1 && Obj.isObject(name)) {
let key;
for (key in name) {
this.addElementGetter(key, name[key]);
}
return;
}
this.setProperty(name, function performQuery() {
let private_map = getPrivate(this),
result = private_map.cached_elements.get(name);
if (result == null) {
result = this.querySelector(query);
private_map.cached_elements.set(name, result);
}
return result;
});
});
/**
* Add a getter with a query selector for multiple elements
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 2.0.0
* @version 2.3.19
*/
Element.setStatic(function addElementsGetter(name, query) {
if (arguments.length == 1 && Obj.isObject(name)) {
let key;
for (key in name) {
this.addElementsGetter(key, name[key]);
}
return;
}
this.setProperty(name, function performQuery() {
let private_map = getPrivate(this),
result = private_map.cached_elements.get(name);
if (result == null) {
result = this.querySelectorAll(query);
private_map.cached_elements.set(name, result);
}
return result;
});
});
/**
* Add a custom template renderer to the given element
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 2.0.0
* @version 2.0.0
*
* @param {Function} element_class
*/
function addTemplateRenderer(element_class) {
// Add the method to force rendering the template
Hawkejs.setCachedMethod(element_class, Hawkejs.RENDER_CONTENT, renderCustomTemplate);
}
/**
* The custom template render function
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 2.0.0
* @version 2.3.17
*
* @param {Boolean} re_render Is this a render done from outside a normal render?
*/
function renderCustomTemplate(re_render) {
let template = this.inner_template,
has_prepare_logic = typeof this.prepareRenderVariables == 'function';
// Get the private map
let private_map = getPrivate(this);
// And clear the element cache
private_map.cached_elements.clear();
if (!template) {
return this.prepareRenderVariables();
}
let that = this,
render_vars,
renderer = this.ensureHawkejsRenderer();
if (this.constructor.use_new_renderer_scope || renderer?.dialog_open) {
renderer = renderer.createSubRenderer();
renderer.scope_id = renderer.getId();
}
let slot_data = _extractSlotData.call(this, true, re_render);