-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
61 lines (55 loc) · 2.04 KB
/
index.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
import WebMachine from './modules/machine'
class App {
constructor () {
this.ws = new WebSocket(`ws://${document.location.host}/ws`)
this.ws.onopen = function () { console.log('Open Websocket') }
this.ws.onclose = function () { console.log('Websocket Closed') }
this.ws.onmessage = evt => this.onMessageReceived(evt)
document.getElementById('layoutButton').addEventListener('click', evt => this.onLayoutClicked(evt))
document.getElementById('saveButton').addEventListener('click', evt => this.webMachine.saveLayout())
}
onMessageReceived (evt) {
try {
const msg = JSON.parse(evt.data)
const _this = this
switch (msg.method) {
case 'update_machine':
this.webMachine = new WebMachine(msg.arg, getURLParameter('layout'), getURLParameter('details'), msg.style)
// console.log("Style " + msg.style)
this.webMachine.cy.on('tap', 'edge', function (evt) {
if (_this.webMachine.cy.autolock()) {
_this.ws.send(JSON.stringify({
method: 'trigger',
arg: evt.target.data('trigger')
}))
}
})
// console.log(this.webMachine.modelClasses)
break
case 'state_changed':
if (this.webMachine !== undefined) {
// console.log(msg)
this.webMachine.selectTransition(msg.arg.model, msg.arg.transition)
this.webMachine.selectState(msg.arg.model, msg.arg.state)
}
break
}
} catch (err) {
console.log('ERROR: ', err)
}
}
onLayoutClicked (evt) {
console.log(this)
let elem = evt.target
elem.classList.toggle('unlocked')
this.webMachine.cy.autolock(!elem.classList.contains('unlocked'))
}
}
// http://stackoverflow.com/a/11582513/1617563
function getURLParameter (name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null
}
function init () {
window.app = new App()
}
window.onload = init