-
Notifications
You must be signed in to change notification settings - Fork 2
/
juicy-iframe.html
75 lines (71 loc) · 2.54 KB
/
juicy-iframe.html
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
<!--
juicy-iframe custom element to stamp HTML given as string or from given file into an iframe
by Juicy
MIT license
https://github.com/Juicy/juicy-iframe
-->
<script>
(function() {
var JuicyIframePrototype = Object.create((HTMLTemplateElement || HTMLElement).prototype);
/**
* Import given file, or source as iframe.
* @public only for debugging.
* @return {imported-template} self
*/
JuicyIframePrototype.loadTemplate_ = function() {
var src, srcdoc, iframe;
var val = this.getAttribute('content');
if (val === null) {
return val;
}
this.clear();
iframe = this.iframe = document.createElement('iframe');
iframe.name = "a";
iframe.model={a:"b"};
if (val && (val.indexOf('/') === 0 || val.indexOf('./') === 0)) {
iframe.src = val;
} else {
iframe.srcdoc = val;// encodeURI(val);
}
this.attributeChangedCallback("model", undefined, this.model || this.getAttribute("model"));
this.parentNode.insertBefore(iframe, this.nextSibling);
return this;
};
/**
* Remove stamped content.
*/
JuicyIframePrototype.clear = function() {
return this.iframe && this.iframe.parentNode.removeChild(this.iframe);
};
JuicyIframePrototype.isAttached = false;
JuicyIframePrototype.attachedCallback = function() {
this.isAttached = true;
this.loadTemplate_();
};
JuicyIframePrototype.detachedCallback = function() {
this.isAttached = false;
this.clear();
};
JuicyIframePrototype.attributeChangedCallback = function(name, oldVal, newVal) {
if(this.isAttached){
switch(name){
case "model":
if(typeof newVal === "string"){
this.model = newVal ? JSON.parse(newVal) : null;
} else {
this.model = newVal;
}
this.iframe.model = this.model;
break;
case "content":
this.loadTemplate_();
break;
}
}
};
document.registerElement('juicy-iframe', {
prototype: JuicyIframePrototype,
extends: "template"
});
})();
</script>