-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
74 lines (60 loc) · 2.03 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>elm-ement</title>
<script src="webcomponents.js/webcomponents-lite.min.js"></script>
<style media="screen">
elm-ement {
display: block;
padding: 10px;
border: 1px dotted red;
}
</style>
</head>
<body>
<elm-ement greeting="Hello" id="elm-ement"></elm-ement>
<p>The contents of the dotted red box above are an <a href="http://elm-lang.org/" target="_blank">Elm</a> application inside a Custom Element: <code><elm-ement></code>.</p>
<button onclick="callIncrement()">Call the Custom Element's increment() method</button>
<button onclick="changeGreeting()">Change the `greeting` attribute</button>
<p>
<a href="https://github.com/ohanhi/elm-ement">Source code</a>
</p>
<script src="elm.js"></script>
<script>
var element = document.getElementById("elm-ement")
function callIncrement() {
element.increment();
}
var greetingsPool = ['Howdy', 'Avast ye', 'How d’ya do', 'Nice to meet you,'];
function changeGreeting() {
var currentGreeting = element.getAttribute('greeting');
var greeting = greetingsPool[Math.floor(Math.random() * greetingsPool.length)];
while (greeting == currentGreeting) {
greeting = greetingsPool[Math.floor(Math.random() * greetingsPool.length)];
}
element.setAttribute('greeting', greeting);
}
var ElmEmentProto = Object.create(HTMLElement.prototype);
ElmEmentProto.increment = function() {
console.log('Increment!');
this.hello.ports.increment.send(null);
};
ElmEmentProto.attachedCallback = function() {
console.log('attachedCallback()');
};
ElmEmentProto.createdCallback = function() {
var div = document.createElement('div');
this.appendChild(div);
this.hello = Elm.Hello.embed(div);
};
ElmEmentProto.attributeChangedCallback = function(attribute, _, value) {
this.hello.ports.greeting.send(value);
};
var ElmEment = document.registerElement(
'elm-ement',
{ prototype: ElmEmentProto }
);
</script>
</body>
</html>