-
Notifications
You must be signed in to change notification settings - Fork 0
/
jfive-board.html
136 lines (122 loc) · 4.27 KB
/
jfive-board.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
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
133
134
135
136
<script>
class JFiveBoard extends HTMLElement {
constructor() {
super();
this.boardId;
this.port;
this.ioPlugin;
this.repl;
this.debug;
this.timeout;
this.Board;
this.five = require('johnny-five');
this.require = require;
}
static get observedAttributes() {
return [
'board-id',
'port',
'repl',
'debug',
'timeout',
'io-plugin'
];
}
attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
case 'board-id': {
this.boardId = newValue;
break;
}
case 'port': {
this.port = newValue;
break;
}
case 'repl': {
this.repl = newValue === '' || newValue === 'true' ? true : newValue === 'false' ? false : this.repl;
break;
}
case 'debug': {
this.debug = newValue === '' || newValue === 'true' ? true : newValue === 'false' ? false : this.debug;
break;
}
case 'timeout': {
this.timeout = parseInt(newValue, 10);
break;
}
case 'io-plugin': {
this.ioPlugin = newValue;
break;
}
}
}
connectedCallback() {
// initialize the board
this.Board = new this.five.Board({
id: this.boardId,
port: this.port,
repl: this.repl,
debug: this.debug,
timeout: this.timeout,
io: this.ioPlugin ? new this.require(this.ioPlugin) : undefined
});
// initialize all event handlers, pass the events up to the host element and no further
this.Board.on('connect', (event) => {
this.dispatchEvent(new CustomEvent('connect', {
detail: event,
bubbles: false
}));
});
this.Board.on('ready', (event) => {
this.dispatchEvent(new CustomEvent('ready', {
detail: event,
bubbles: false
}));
});
this.Board.on('exit', (event) => {
this.dispatchEvent(new CustomEvent('exit', {
detail: event,
bubbles: false
}));
});
this.Board.on('info', (event) => {
this.dispatchEvent(new CustomEvent('info', {
detail: event,
bubbles: false
}));
});
this.Board.on('warn', (event) => {
this.dispatchEvent(new CustomEvent('warn', {
detail: event,
bubbles: false
}));
});
this.Board.on('fail', (event) => {
this.dispatchEvent(new CustomEvent('fail', {
detail: event,
bubbles: false
}));
});
this.Board.on('message', (event) => {
this.dispatchEvent(new CustomEvent('message', {
detail: event,
bubbles: false
}));
});
// initialize all children
const _jfiveInitChildren = (element) => {
const children = Array.from(element.children);
children.forEach((child) => {
if (child.boardReadyCallback) {
child.boardReadyCallback();
// Give the child this initChildren function so that it will initialize all of its children
child._jfiveInitChildren = _jfiveInitChildren;
child._jfiveInitChildren(child);
}
});
};
_jfiveInitChildren(this);
}
}
window.customElements.define('jfive-board', JFiveBoard);
</script>