-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
98 lines (78 loc) · 2.77 KB
/
app.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
class MyParagraph extends HTMLElement {
constructor() {
super();
this.$host = this.$template('#my-paragraph');
}
async connectedCallback() {
const $host = await this.$host;
$host.slot().text($host.attr('msg'));
$host.find('p').css('background-color', '#FE9868');
$host.find('p > b').text('Updated with jQuery');
const b = $host.find('p').find('b');
b.css('background-color', 'green');
$host.find('button[is=fancy-button]')
.css('background-color', 'green')
.text('Fancy Template button').click(() => {
alert('Hello from template');
});
}
}
class FancyButton extends HTMLButtonElement {
constructor() {
super();
$(this).css('background-color', 'yellow');
}
}
class MyComponent extends HTMLElement {
constructor() {
super();
this.$host = this.$templateSources('./my-component.css', './my-component.html');
}
async connectedCallback() {
const $host = await this.$host;
//...
}
}
class MyOtherComponent extends HTMLElement {
constructor() {
super();
this.$host = this.$templateSources('./my-component.css', $('<div>This is my other component template kept in a separate file</div>'));
}
async connectedCallback() {
const $host = await this.$host;
//...
}
}
customElements.define('my-component', MyComponent);
customElements.define('my-other-component', MyOtherComponent);
customElements.define('my-paragraph', MyParagraph);
customElements.define('fancy-button', FancyButton, { extends: 'button' });
$wc()
.template('<p>')
.onCreate(function () { this.hello = "Hello World"; })
.connectedCallback(function ($host) { $host.find('p').text(this.hello); })
.attributeChangedCallback(($host, changed) => {
if(changed.name === 'color'){
$host.find('p').css('color', changed.newValue);
}
}, ['color'])
.extend({
changeMessage: async function (newMessage) {
const $host = await this.$host;
$host.find('p').text(`"${this.hello}" changed with "${newMessage}"`);
}
})
.define('fluent-component');
$wc(HTMLButtonElement).define('fluent-button', 'button');
$(document).ready(async () => {
$('button[is=fancy-button]').click(() => {
$('fluent-component').changeMessage("Hello universe!");
});
$('button[is=fluent-button]').click(() => {
const randomColor = Math.floor(Math.random()*16777215).toString(16);
$('fluent-component').attr('color', '#'+randomColor);
});
const $host = await $('fluent-component').$host();
$host.find('p').css('outline', '1px solid red');
console.log($('fluent-component').$shr().getSelection());
});