/
custom-element.js
132 lines (105 loc) · 3.98 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
import {Metadata, Origin, ResourceType} from 'aurelia-metadata';
import {BehaviorInstance} from './behavior-instance';
import {configureBehavior} from './behaviors';
import {ContentSelector} from './content-selector';
import {ViewEngine} from './view-engine';
import {ViewStrategy} from './view-strategy';
import {hyphenate} from './util';
var defaultInstruction = { suppressBind:false },
contentSelectorFactoryOptions = { suppressBind:true },
hasShadowDOM = !!HTMLElement.prototype.createShadowRoot,
valuePropertyName = 'value';
export class UseShadowDOM {}
export class SkipContentProcessing {}
export class CustomElement extends ResourceType {
constructor(tagName){
this.name = tagName;
this.properties = [];
this.attributes = {};
}
static convention(name){
if(name.endsWith('CustomElement')){
return new CustomElement(hyphenate(name.substring(0, name.length-13)));
}
}
analyze(container, target){
var meta = Metadata.on(target);
configureBehavior(container, this, target, valuePropertyName);
this.targetShadowDOM = meta.has(UseShadowDOM);
this.skipContentProcessing = meta.has(SkipContentProcessing);
this.usesShadowDOM = this.targetShadowDOM && hasShadowDOM;
}
register(registry, name){
registry.registerElement(name || this.name, this);
}
load(container, target, viewStrategy, transientView){
var options;
viewStrategy = viewStrategy || this.viewStrategy || ViewStrategy.getDefault(target);
options = {
targetShadowDOM:this.targetShadowDOM,
beforeCompile:target.beforeCompile
};
if(!viewStrategy.moduleId){
viewStrategy.moduleId = Origin.get(target).moduleId;
}
return viewStrategy.loadViewFactory(container.get(ViewEngine), options).then(viewFactory => {
if(!transientView){
this.viewFactory = viewFactory;
}
return viewFactory;
});
}
compile(compiler, resources, node, instruction){
if(!this.usesShadowDOM && !this.skipContentProcessing && node.hasChildNodes()){
var fragment = document.createDocumentFragment(),
currentChild = node.firstChild,
nextSibling;
while (currentChild) {
nextSibling = currentChild.nextSibling;
fragment.appendChild(currentChild);
currentChild = nextSibling;
}
instruction.contentFactory = compiler.compile(fragment, resources);
}
instruction.suppressBind = true;
return node;
}
create(container, instruction=defaultInstruction, element=null){
var executionContext = instruction.executionContext || container.get(this.target),
behaviorInstance = new BehaviorInstance(this, executionContext, instruction),
viewFactory = instruction.viewFactory || this.viewFactory,
host;
if(viewFactory){
behaviorInstance.view = viewFactory.create(container, behaviorInstance.executionContext, instruction);
}
if(element){
element.primaryBehavior = behaviorInstance;
if(!(this.apiName in element)){
element[this.apiName] = behaviorInstance.executionContext;
}
if(behaviorInstance.view){
if(this.usesShadowDOM) {
host = element.createShadowRoot();
}else{
host = element;
if(instruction.contentFactory){
var contentView = instruction.contentFactory.create(container, null, contentSelectorFactoryOptions);
ContentSelector.applySelectors(
contentView,
behaviorInstance.view.contentSelectors,
(contentSelector, group) => contentSelector.add(group)
);
behaviorInstance.contentView = contentView;
}
}
if(this.childExpression){
behaviorInstance.view.addBinding(this.childExpression.createBinding(host, behaviorInstance.executionContext));
}
behaviorInstance.view.appendNodesTo(host);
}
}else if(behaviorInstance.view){
behaviorInstance.view.owner = behaviorInstance;
}
return behaviorInstance;
}
}