-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_of_knowledge.js
More file actions
366 lines (347 loc) · 12.5 KB
/
tree_of_knowledge.js
File metadata and controls
366 lines (347 loc) · 12.5 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// var Graphs = new Meteor.Collection("graphList"); //available graphs
//Client and server Globals:
Nodes = new Meteor.Collection("all_Nodes");
Links = new Meteor.Collection("all_Links");
//Collection to store JSON strings as backups:
Backup = new Meteor.Collection("backups");
//factor by which each zoom step rescales the graph
ZoomStep=1.5; VisNNodes=[20,50];
var db = new treeData();
var graph; var svg;
var currGraph;
notify = function(text){ //notification messages
// console.log("notify: ",text);
$('#notifications').text(text);
// $('#notifications').css({"animation-name":"notify","animation-duration":"6s"})
$('#notifications').removeClass('notify');
setTimeout(function() {
$('#notifications').addClass('notify');
},1);
}
// Server-side code:============================
if (Meteor.isServer){
Meteor.startup(function(){
// var allCollections = function () { //return all collections
// var Future = Npm.require('fibers/future'),
// future = new Future(),
// db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
// db.collectionNames(
// function(error, results) {
// if (error) throw new Meteor.Error(500, "failed");
// future.return(results);
// }
// );
// return future.wait();
// };
// var collList=allCollections();
// console.log("Collections:", collList);
//direct DB manipulations:
// db.loadJSON(JSON.parse(Assets.getText("miserables.json")));
// db.clear();
// Nodes.insert({x: 0.0, y: 0.0});
// console.log("updated #",
// Nodes.update({}, {$set: {graph:"test0"}}, {multi:true}),
// Links.update({}, {$set: {graph:"test0"}}, {multi:true}));
// Nodes.update({importance: {$in:["",null,10]}},
// {$set: {importance:10}}, {multi:true}));
// Links.update({strength: {$in:["",null,10]}},
// {$set: {strength:5}}, {multi:true});
// Links.update({type: {$in:["connection"]}},
// {$set: {type:"related"}}, {multi:true});
// Nodes.update({$and: [{x: NaN}, {graph: "ClassMech"}]},
// {$set: {x:1000}}, {multi:true}),
// Nodes.update({$and: [{y: NaN}, {graph: "ClassMech"}]},
// {$set: {y:1000}}, {multi:true}),
// Nodes.find({graph:"ClassMech"}).fetch() )
// Nodes.update({graph:"test1"},{$set:{zoomLvl:0}},{multi:true})
// return;
// Graphs.remove({});
// console.log(
// Nodes.remove({graph:{$exists:false}}),
// Links.remove({graph:{$exists:false}}))
// console.log('all',
// Nodes.find().fetch(),
// Links.find().fetch())
// console.log(
// Nodes.remove({graph:'MetaMath'}),
// Links.remove({graph:'MetaMath'}))
// Nodes.update({graph:'MetaMath'},{$set:{x:2345,y:2345}},{multi:true})
var mxZm=Math.max(...Nodes.find({graph:'MetaMath'}).map(nd=>nd.zoomLvl));
Nodes.update({graph:'MetaMath', zoomLvl:mxZm},{$set:{x:2346, y:2346}},{multi:true})
// console.log(
// Nodes.remove({graph:'test1'}),
// Links.remove({graph:'test1'}))
Nodes.update({graph:'test'},{$set:{x:2345,y:2345}},{multi:true})
// Links.find({}).forEach(lk => Links.update(lk._id,
// {$set: {strength:parseFloat(lk.strength)}}));
console.log(Nodes.update({x:NaN},
{$set: {x:1000}}, {multi:true}),
Nodes.update({y:NaN},
{$set: {y:1000}}, {multi:true}))
db.publish();
Meteor.publish("srvBckup", function () {
return Backup.find();
});
})
Meteor.methods({
listGraphs: function(){ //list all available graphs
//console.log("graphs!!!",Nodes.rawCollection().distinct("graph"));
//scan the Nodes collection for unique "graph" values:
var graphs = _.uniq(Nodes
.find({}, {fields: {graph: true}, sort:{graph:1}})
.map(x => x.graph), true);
return graphs;
},
renameGraph: function(oldName, newName){
Nodes.update({graph:oldName},{$set:{graph:newName}},{multi:true});
Links.update({graph:oldName},{$set:{graph:newName}},{multi:true});
},
deleteGraph: function(name){
Links.remove({graph: name});
Nodes.remove({graph: name});
},
backupGraph: function(name, note, srv){
var bck={
nodes: //JSON.stringify(
Nodes.find({graph:name}).fetch(),
links: //JSON.stringify(
Links.find({graph:name}).fetch(),
date: new Date(),
graph: name,
note: note,
nodeCount: Nodes.find({graph:name}).count(),
linkCount: Links.find({graph:name}).count(),
graphNumber: (Backup.find().count()+1)
}
if(srv) return Backup.insert(bck); //server backup
else return bck; //client backup
},
restoreGraph: function(bckCnt, srv){
if(srv) var grObj=Backup.find({graphNumber:bckCnt}).fetch(); //server restore
else var grObj = [bckCnt]; //client restore
// console.log(bckCnt, grObj, grObj.length);
if(grObj.length==0){return false;}
var grList=Meteor.call("listGraphs");
var name=grObj[0].graph;
//Create new name for the graph:
while(grList.indexOf(name) >-1){
name+="~";
}
//Insert all nodes and links into their Collections:
//note: must update link source/target with new node IDs
var newId={};
grObj[0].nodes.forEach(function(nd){
nd.graph=name;
var oldID=nd._id;
delete nd._id;
newId[oldID]=Nodes.insert(nd);
})
grObj[0].links.forEach(function(lk){
lk.graph=name;
lk.source=newId[lk.source];
lk.target=newId[lk.target];
delete lk._id;
Links.insert(lk);
})
return name;
}
})
}
//Client-side code:============================
if (Meteor.isClient) {
//Once the SVG is rendered:
Template.graph.onRendered(function(){
//Dropdown for available graphs:
Meteor.call("listGraphs", function(err, list){
$.each(list, function (i, item) {
$('#availGraphs').append($('<option>', {
value: item,
text : item
}));
});
currGraph=list[0]; //First graph to show
$('#availGraphs').val(currGraph);
//Create canvas:
Session.set('lastUpdate', new Date() );
var width = $(window).innerWidth()-35,//$("body").prop("clientWidth"),
height = 500;//$(window).height(); //SVG size
console.log('width: ', width);
svg = d3.select("#graphSVG")
.attr("width", width)
.attr("height", height); //Set SVG attributes
$(".canvas").width(width);
// showGraph("test0");
graph = new ToK(svg, db);
showGraph(currGraph)
});
});
Template.graph.helpers({
lastUpdate(){return Session.get('lastUpdate');}
});
Template.graph.events({
'change #availGraphs': function(e){
if(graph.gui.editPopup){
notify("finish editing first");
return;}
var newValue = $('#availGraphs').val();
var oldValue = Session.get("currGraph");
if (newValue != oldValue) { // value changed, let's do something
showGraph(newValue);
}
},
'click #new': function(e){
if(graph.gui.editPopup){
notify("finish editing first");
return;}
var name=prompt("New graph name (no spaces)", "test1");
var newFl=true; //check if name already exists:
$("#availGraphs option").each(function(d){
newFl=newFl && (name!=$(this).val());
});
if(name){
if(newFl){
$('#availGraphs').append($('<option>', {
value: name,
text: name,
selected: 'selected'
}));
showGraph(name);
notify("created new graph");
}
else {
$('#availGraphs').val(name);
showGraph(name);}
}
},
'click #rename': function(e){
if(graph.gui.editPopup){
notify("finish editing first");
return;}
var newName = prompt("Enter new graph name:",Session.get("currGraph"));
Meteor.call("listGraphs", function(err, list){
if(list.indexOf(newName)>-1){notify("name already exists");
return;}
Meteor.call("renameGraph",Session.get("currGraph"),newName,
function(){
notify("Graph renamed, refresh the page to finish");
Session.set("currGraph",newName);
});
});
},
'click #delete': function(e){
var result = confirm("Delete the entire current graph?");
if (result) {
Meteor.call('deleteGraph',Session.get("currGraph"))
notify("graph deleted - switch to another graph")
}
},
'click #srvBckup': function(e){
e.preventDefault();
var note=prompt("Backup note:");
if(!note){notify("backup cancelled"); return}
Meteor.call("backupGraph",Session.get("currGraph"),note,true);
},
'click #srvRestore': function(e){
e.preventDefault();
if(graph.gui.editPopup){
notify("finish editing first");
return;}
Meteor.subscribe("srvBckup",function(){ //backup DB
//show the backup collection and and
//ask user for graphNumber in Backup collection:
console.log("Backups collection:",Backup.find().fetch())
var list=Backup.find({},{fields: {
graph: true, date:true, note:true, graphNumber:true,
nodeCount:true, _id:false //, linkCount:true
}}).fetch();
var restID=prompt(JSON.stringify(list,null,2)+
"\n enter graphNumber of graph to restore:");
if(!restID) return;
// "Backup ID of graph to restore (check console for list)");
//copy backup into Nodes and Links collections and show:
Meteor.call("restoreGraph",Number(restID),true,function(err,name){
if(!name){ alert("Invalid backup number"); return;}
$('#availGraphs').append($('<option>', {
value: name,
text: name,
selected: 'selected'
}));
showGraph(name);
notify("restored graph "+name);
})
})
},
'click #cltBckup': function(e){
e.preventDefault();
var note=prompt("Backup note (included in filename):");
if(!note){notify("backup cancelled"); return}
Meteor.call("backupGraph",Session.get("currGraph"),note,false,
function(err, bckObj){
var date = new Date();
var file = new File([JSON.stringify(bckObj,null,2)],
date.getFullYear().toString()+(date.getMonth()+1)+date.getDate()+
'-'+date.getHours()+';'+date.getMinutes()+';'+date.getSeconds()+
'-'+bckObj.graph+'-'+bckObj.note,
{type: "application/json"});//"text/plain;charset=utf-8"});
saveAs(file);
});
},
'change #cltRestore': function(event){
// var tmppath = URL.createObjectURL(event.target.files[0]);
// console.log(tmppath, event.target.files);
// $("img").fadeIn("fast").attr('src',tmppath);
if(graph.gui.editPopup){
notify("finish editing first");
return;}
var r = new FileReader();
//register callback on file load:
r.onload = function(e){
var bckObj = JSON.parse(e.target.result);
// console.log(bckObj);
Meteor.call("restoreGraph",bckObj,false,function(err,name){
if(!name){ alert("Invalid file chosen"); return;}
$('#availGraphs').append($('<option>', {
value: name,
text: name,
selected: 'selected'
}));
showGraph(name);
notify("restored graph "+name);
})
}
//trigger file load:
r.readAsText(event.target.files[0]);
},
'change #loadMetamath': function(event){
//trigger file load:
parseMetamath(event.target.files[0]);
},
'click #resetCrds': function(e) {
e.preventDefault();
// Get current graph name from session
const currentGraph = Session.get("currGraph");
// Call the server method
Meteor.call("resetNodeCoords", currentGraph, function(error) {
if (error) {
notify("Error resetting coordinates: " + error.reason);
} else {
notify("Node coordinates reset");
graph.redraw(); // Refresh the visualization
}
});
}
});
function showGraph(name){
Session.set("currGraph",name);
if(graph.gui.contentPopup){
Blaze.remove(graph.gui.contentPopup);
graph.gui.contentPopup=null;
}
Meteor.call('maxZoomLvl',name,function(err,res){
Session.set("currZmLvl", res); //Set zoom level
console.log("Max zoom level here:", res);
graph.redraw();//subscribe to and show the "currGraph"
});
$('#pgTitle').text("Graph "+name);
}
};