-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube-pagelet-content.js
128 lines (114 loc) · 3.07 KB
/
cube-pagelet-content.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
import {PolymerElement, html} from '@polymer/polymer/polymer-element.js';
/**
An element to bind html content to a container
Example:
<cube-pagelet-content html="this is some <strong>bold</strong> content"></cube-pagelet-content>
@demo demo/cube-pagelet-content.html
*/
class CubePageletContent extends PolymerElement {
static get is() {return 'cube-pagelet-content';}
static get template()
{
return html`<style>
:host {
display: block;
}
</style>
<slot></slot>`;
}
static get properties()
{
return {
html: {type: String, observer: '_htmlChanged'},
local: {type: Boolean, value: false},
_trackedNodes: {type: Array, value: function () {return [];}},
_eventName: {type: String, value: 'cube-pagelet-content-ready', readOnly: true}
}
}
_htmlChanged()
{
if(this.html !== null)
{
let
idx,
tpl = document.createElement('template'),
doc = tpl.content.ownerDocument,
frag = doc.createRange().createContextualFragment(this.html || '');
// REMOVE IMPORTS
let
imports = frag.querySelectorAll('link[rel="import"]'),
importsLength = imports.length,
loadedImports = 0;
for(idx in imports)
{
if(imports.hasOwnProperty(idx))
{
frag.removeChild(imports[idx]);
}
}
// REMOVE OLD CONTENT
for(idx in this._trackedNodes)
{
if(this._trackedNodes.hasOwnProperty(idx))
{
this._trackedNodes[idx][0].removeChild(this._trackedNodes[idx][1]);
}
}
this._trackedNodes = [];
// REMOVE REMAINING (UNTRACKED/SLOTTED) CONTENT
for(idx in this.children)
{
if(this.children.hasOwnProperty(idx))
{
this.removeChild(this.children[idx]);
}
}
// DETERMINE TARGET NODE
tpl.content.appendChild(frag);
let target = this;
while(
(target.tagName === 'CUBE-PAGELET' || target.tagName === 'CUBE-PAGELET-CONTENT')
&& target.local)
{
if(target.parentNode)
{
target = target.parentNode;
}
if(target.host)
{
target = target.host;
}
}
// APPEND NEW CONTENT
let stampedNodes = this._stampTemplate(tpl).childNodes;
while(stampedNodes.length > 0)
{
this._trackedNodes.push([target, target.appendChild(stampedNodes[0])]);
}
// IMPORTS
if(importsLength > 0)
{
for(idx in imports)
{
if(imports.hasOwnProperty(idx))
{
this.importHref(
imports[idx].getAttribute('href'), function () {
loadedImports++;
if(loadedImports >= importsLength)
{
this.dispatchEvent(new Event(this._eventName));
}
}
);
}
}
}
else
{
this.dispatchEvent(new Event(this._eventName));
}
}
}
}
customElements.define(CubePageletContent.is, CubePageletContent);