-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_app.js
179 lines (161 loc) · 5 KB
/
graph_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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {GraphIo} from "./graph_io.js"
import { Render } from "./render.js"
import { Physics } from "./physics.js"
import { Mouse } from "./mouse.js"
import { Layout } from "./layout.js";
import { Mutate } from "./mutate.js";
import { defined } from "../libs/web-js-utils.js";
//singelton protected members
let graph = null
let gio = null
let render = null
let physics = null
let mouse = null
let layout = null
let mutate = null
let parent_div = null
function onVertexMenuAction(e){
console.log(`${e.detail.v.label} => ${e.detail.action}:${e.detail.type}`)
if(e.detail.action == "layout"){
e.detail.v.pinned = true
e.detail.v.svg.shape.classList.add("pinned")
layout.propagate_neighbors(graph,{
width:parent_div.offsetWidth,
height:parent_div.offsetHeight,
v:e.detail.v,
demo:0
})
.then(console.log("layout promise done"))
}else if(e.detail.action == "attract"){
let forces = e.detail.v.forces
if(e.detail.type == "start"){
forces.used = true
forces.attract_neighbors = true
}else if(e.detail.type == "end"){
forces.used = false
forces.attract_neighbors = false
}
}else if(e.detail.action == "group"){
mutate.group(graph,e.detail.v)
}else if(e.detail.action == "ungroup"){
mutate.ungroup(graph,e.detail.v)
}
}
function onGlobalMenuAction(e){
let demo = (e.detail.action == "demo")?1:0
console.log(`graph_app> onGlobalMenuAction(demo=${demo})`)
layout.centrals_first(graph,{
width:parent_div.offsetWidth,
height:parent_div.offsetHeight,
demo:demo
})
.then(console.log("layout promise done"))
}
function onMenuAction(e){
if(e.detail.menu == "Vertex"){
onVertexMenuAction(e)
}else if(e.detail.menu == "Global"){
onGlobalMenuAction(e)
}
}
function onDragEvents(event){
event.stopPropagation();
event.preventDefault();
if(event.type == "dragenter"){
event.dataTransfer.dropEffect = "copy";
}
if(event.type == "drop"){
if(event.dataTransfer.files.length == 1){
reload(event.dataTransfer.files[0])
.then(console.log("reload() done"))
}else{
console.warn("only one file drop allowed");
console.log(event.dataTransfer.files);
}
};
}
function onMqttMessage(e){
const topic = e.detail.topic
const data = e.detail.payload
//console.log(`graph_app> ${topic} => ${JSON.stringify(data)}`);
const action = topic.split('/')[1]
if(action == "reload"){
common_load(null, true, null, data)
}else if(action == "update"){
for(const vertex of data.vertices){
let v = graph.vertices[vertex.id]
v.viewBox.x = vertex.viewBox.x
v.viewBox.y = vertex.viewBox.y
v.viewBox.placed = true
}
}
}
async function common_load(file,reload,config=null,graph_input=null){
if(reload){
console.log(`graph_app> reloading`)
physics.pause()
render.pause()
}
if(graph_input){
console.log("graph_app> mqtt : graph_input")
gio.import_json(graph_input)
}else{
console.log(`graph_app> file : ${file.name}`)
await gio.import_file(file,config)
}
console.log(graph)
if(!reload){
//required svg before create_graph()
render.create_svg(parent_div,config)
}
//defines vertices boxes sizes
render.fitLabels()
if(!graph.layout_done){
//layout will define vertices boxes positions
await layout.centrals_first(graph,{width:parent_div.offsetWidth,height:parent_div.offsetHeight})
}
delete graph.layout_done
//create physical models of vertices boxes with their sizes and positions
physics.create_bodies(parent_div)
//creates svg elements of vertices boxes with their sizes and positions
render.create_graph()
mutate.all_groups(graph)
if(reload){
physics.resume()
render.resume()
console.log("reload() end")
}else{
mouse.init(parent_div)
}
}
async function reload(file){
await common_load(file,true)
return
}
class GraphApp{
constructor(){
graph = {} // shared data between all singleton classes
gio = new GraphIo(graph)
render = new Render()
render.init(graph)
physics = new Physics(graph)
mouse = new Mouse()
layout = new Layout()
mutate = new Mutate()
window.addEventListener( 'menu_action', onMenuAction, false );
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
document.addEventListener(eventName, onDragEvents, false)
});
window.addEventListener( 'mqtt_message', onMqttMessage, false);
}
async load(config,p_div){
parent_div = p_div
await common_load(config.default_graph_file,false,config)
return
}
run(){
physics.run()
render.update()
}
}
export {GraphApp};