forked from Freeboard/freeboard
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
110 lines (91 loc) · 3.11 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"use strict";
import {DOM} from '../../base/dom.js'
import {command} from '../../base/command.js'
import {storage} from '../../base/storage.js'
import {Pipes} from '../../base/navigation.js'
import {project} from '../project/main.js'
/* Page that handles a dashboard insided an iFrame. */
class Freeboard {
/* Construct the object, is executed on load of the window. */
constructor (){
this.name = 'freeboard' // Same name of the class, but in lower case
this.inited = false // If the page is in initiated state
this._inited = false // If page has inited for the first time
this.ipipes = {} // Store references to iframe's methods
this.buffer = '' // Stora incoming chunks
this.tree // Store dashboard on load and interval only,
// freeboard has no on change event to keep on sync...
let $ = this.$ = {} // Store DOM objects here.
$.iframe = new DOM('iframe')
$.section = new DOM(DOM.get(`section#${this.name}`))
.append([$.iframe])
}
/* On display, initiates the page. */
init (){
// Run once
if (!this._inited){
this.$.iframe.onevent('load', this, () => {
// Alias some functions
this.ipipes.serialize = this.$.iframe.$.contentWindow.freeboard.serialize
this.ipipes.load = this.$.iframe.$.contentWindow.freeboard.loadDashboard
this.ipipes.loaded = true
this.ipipes.load(this.tree)
// Init a watcher to kinda regularly store changes to project
// Freeboard has no on change event
this.interval = setInterval(() => {
if (this.inited)
this.save()
}, 2000)
})
this.$.iframe.src = 'static/page/freeboard/index.html',
this._inited = true
}
if (this.inited)
return
// Do stuff to initiate the page's user interface
this.inited = true
}
/* On hidden, deinitiate the page. */
deinit(){
if (!this.inited)
return
// Do stuff to deinitiate the page's user interface
this.inited = false
}
/*
* On load a project, load the blocks' scope of the project.
* @param {Object} obj - page's scope of the project.
* @param {Object} tabUID - Identifier of the tab/window.
*/
load (obj, tabUID){
let inited = this.inited
// Deinit if loaded with another project.
if (this.inited)
this.deinit()
// Update page's scope of the project
this.tree = obj.tree
// If page visible, init page's scope of the project.
if (inited)
this.init()
}
/**
* Create this page's scope to be included in the project file.
* @return {Object} This page's scope object.
*/
empty (){
return {
tree:{} // Store freeboard inside ``tree'' object.
}
}
/**
* Store current dashboard in the project file on tab unload and interval.
* Is buggy because freeboard has no on change event.
*/
save (){
project.current.freeboard.tree = this.ipipes.serialize()
this.tree = project.current.freeboard.tree
project.write()
}
}
// Init and export freeboard object
export let freeboard = new Freeboard()