-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-timeline.js
574 lines (478 loc) · 13 KB
/
data-timeline.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
ns="http://www.w3.org/2000/svg";
w=1000;
h=600;
function transitionable(){
this.getDisplayed=function(){return this.__transition__||this;};
this.propagate=function(p,v){
if(this[p]!=undefined){
if(this[p]!=v){
if(this.updates==undefined){
this.updates={};
}
//console.info("updating: "+p+" <- "+v);
this.updates[p]=v;
}
}else{
this[p]=v;
}
};
this.get=function(x){
var o=this.__transition__;
if (o) {
o=o[x];
if (o) {
return o;
}else{
return this[x];
}
}else{
return this[x];
}
};
var trans;
this.update=function(newTrans,tween,callback){
copyProps=function(){
fields = this.drawingProps;
for(var x in fields){
p = fields[x];
if(o[p]==undefined)o[p]=this[p];
}
}.bind(this)//end copyProps
copyMethods =function(){
fields=this.drawingMethods;
for(var x in fields){
p = fields[x];
if(o[p]==undefined)o[p]=this[p].bind(o);
}
}.bind(this)//end copyMethods
registerTweens=function(){
if(o){
//if(this.constructor==node)console.info([o,u,newTrans]);
var func = tween!=undefined?tween():undefined;
for(var x in u){
p=u[x];
if(o[x]==undefined){
//if(x=="y")console.info("initializing __transition__."+x+": "+o[x]);
o[x]=this.get(x);
}
if(newTrans||this.tween[x]!=undefined){
//console.info("tweening "+x+": "+o[x]+" -> "+p);
this.tween[x]=d3.interpolateNumber(o[x],p)
}else{
//if(x=="y")console.info("setting __transition__."+x+": "+o[x]+" -> "+p)
o[x]=p;
}
}
if(newTrans){
trans.tween("update",function(){
return function(t){
this.__runTween__(t);
if(func)func();
}
})
}
}else{
if(this.constructor==node){
//console.info("no transition is present")
//console.info(this);
}
}
}.bind(this)//end tween
updateData=function(){
for(var x in u){
//console.info("updating "+x+": "+this[x]+" -> "+u[x])
this[x]=u[x];
}
this.updates=undefined;
}.bind(this)//end update
var o,u=this.updates,p,fields;
//if(u==undefined){
// return function(){};
//}
if(newTrans){
//if we have a transition already going on
if ((o = this.__transition__)) {
trans=d3.select(this).transition().duration(inTime);
}else{
trans=d3.select(this).transition().duration(inTime);
var o=this.__transition__;
//copy over properties and methods requiered for drawing;
copyProps();
copyMethods();
this.tween={};
this.__runTween__=function(t){
for(var x in this){
o[x]=this[x](t);
}
}.bind(this.tween);
}
}else{
o = this.__transition__;
};
registerTweens();
//add drawing properties to __transition__ object
if(trans)trans.each("end",function(){this.tween=undefined;trans=undefined;if(callback)callback();})
return updateData;
}
}
function node(d){
for(var x in d){
this[x]=d[x];
}
this.prereqs=[];
this.dependencies=[];
this.links=[];
this.label=d.fulltext.replace("Ticket:","");
this.initProps=function(){
var req;
if(this.printouts["Start date"].length){
this.startTime=this.printouts["Start date"]*1000;
}else{
var today = new Date().getTime();
today-=today%(24*60*60*1000);
this.startTime=today;
}
if(!this.printouts["Duration"].length){
if((req=this.printouts["Date required"]).length){
this.duration=(req*1000-this.startTime);
}else{
//if none exists fake it based on string length -> three days per character
this.duration=this.label.length*3600*24*1000*3;
}
}else{
this.duration=this.printouts["Duration"]*24*60*60*1000;
}
//a.duration*=Math.random();
this.asap=this.printouts["Asap"]*1 || false;
}
}
node.prototype = new transitionable();
node.prototype.constructor = node
node.isValidItem=function(){//the crux of the matter when switching between datasets seem to be here
return (this.printouts["Start date"].length&&this.printouts["Date required"].length||this.printouts["Duration"].length);
}
node.prototype.isValidItem=node.isValidItem;
function link(s,t){
this.source=s;
this.target=t;
this.name=s.label+" -> "+t.label
}
link.prototype = new transitionable();
link.prototype.constructor = link
function smWikiClient(mainQuery,printouts,api,linkTypes){
function fetchData() {
var qs=mainQuery.concat(printouts);
var out=smwAsk(qs);
return out;
}
function getArticle(title){
var qs=mainQuery.concat("[[".concat(title.concat("]]".concat(printouts))));
return smwAsk(qs);
}
function getPageDiv(page, oldid) {
var ask = page + '?action=render' + (oldid > 0 ? '&oldid=' + oldid : "");
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", ask, false );
xmlHttp.send( null );
var out = document.createElement("div");
out.innerHTML=xmlHttp.responseText
return out;
}
function smwAsk(query) {
var ask = api + '?action=ask&query=' + query + '|limit=5000&format=json';
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", ask, false );
xmlHttp.send( null );
return JSON.parse(xmlHttp.responseText);
}
var itemMap=[];
function prepareData(data){//parse data and check for any updates in response
var ordered=[];
var initProps=[];
var startProps=[];
var newProps=[];
if(!data){
data=parseData(fetchData());
}else{
data=parseData(data);
}
//make sure all temporal dependencies are enforced and updates in properties are registered
orderData();
data.earliest=getEarliest(data.nodes);
data.latest=getLatest(data.nodes);
console.info(data);
data.invalid.forEach(function(d,i){
if(d.links.length)d.links.forEach(function(d,i){
console.info(d.name)
})
})
return data;
function mostRecent(d,p){
if(d.updates){
if(d.updates[p]){
return d.updates[p].end
}
}
return d[p];
};
function getEarliest(arr){return d3.min(arr,function(d){
return mostRecent(d,"startTime");
})
;};
function getLatest(arr){return d3.max(arr,function(d){
return mostRecent(d,"endTime");
});
};
function parseData(data){
var nodes=[];
var links=[];
var items=[];
var newMap=[];
var item;
var updates=[];
var invalid=[];
function registerItem(a){
var n = new node(a);
itemMap[a.fulltext]=n;
newMap[a.fulltext]=n;
if(n.isValidItem())n.initProps();
return n;
}
function getComponents(a,t){
return a.printouts[t];
}
function consolidateLinks(a,b){//a<-b
// console.info("merging item links: "+a.label+" <- "+b.fulltext.replace("Ticket:",""))
var c;
for(var x in linkTypes){
var comps=getComponents(b,linkTypes[x].key);
for(var j=0;j<comps.length;j++){
c=itemMap[comps[j].fulltext];
if(c!=undefined){
if(!isLinkedTo(a,c)){
if(linkTypes[x].direction=='forward'){
linkItems(a,c);
}else{
if(linkTypes[x].direction=='backward')linkItems(c,a);
}
}else{
links.push(getLink(a,c))
}
}
}
}
}
function validateLinks(a){
function removeLinkage(l,i){
function removeElm(arr,d){
arr.splice(arr.indexOf(d),1)
}
removeElm(l.source.prereqs,l.target);
removeElm(l.target.dependencies,l.source);
if(l.source==a){
removeElm(l.target,l);
}else{
removeElm(l.source,l)
}
a.links.splice(i);
}
var n=a.links.length;
for(var i=0;i<n;i++){
if(links.indexOf(a.links[i])==-1){
removeLinkage(a.links[i],i);
}
}
}
function isLinkedTo(a,b){
for(var l in a.prereqs){
if(a.prereqs[l]==b)return true;
}
for(var l in a.dependencies){
if(a.dependencies[l]==b)return true;
}
return false;
}
function getLink(a,b){
for(var l in a.links){
if(a.links[l].source==b||a.links[l].target==b)return a.links[l];
}
}
function linkItems(a,b){
var l=new link(a,b);
links.push(l);
a.prereqs.push(b);
a.links.push(l);
b.dependencies.push(a);
b.links.push(l);
}
function isUnregisteredItem(a){
return itemMap[a.fulltext]==undefined;
}
function isNewItem(a){
return newMap[a.fulltext]!=undefined;
}
function linkNew(arr){
for(var i=0;i<arr.length;i++){
item=arr[i];
nodes.push(item);
for(var x in linkTypes){
var comps=getComponents(item,linkTypes[x].key);
if(comps!=undefined){
for(var j=0;j<comps.length;j++){
//push new links
if(!isUnregisteredItem(comps[j])&&isNewItem(comps[j])){
//console.info("usable link: "+comps[j].fulltext)
//console.info(itemMap[comps[j].fulltext]);
if(linkTypes[x].direction.indexOf('forward')==0){
linkItems(itemMap[comps[j].fulltext],item);
}else{
if(linkTypes[x].direction.indexOf('backward')==0)linkItems(item,itemMap[comps[j].fulltext]);
}
}else{
console.info("unusable link: "+comps[j].fulltext)
}
}
}
}
}
}
var otherItem;
//start by registering everything
for(var x in data.query.results){
item=data.query.results[x];
if(node.isValidItem.bind(item)()){
otherItem=itemMap[item.fulltext];
if(!otherItem){
items.push(registerItem(item));
}else{
initProps(item);
updates.push({old:otherItem,new:item});
}
}else{
otherItem=itemMap[item.fulltext];
if(!otherItem){
invalid.push(registerItem(item));
}else{
initProps(item);
updates.push({old:otherItem,new:item});
}
}
}
var pair,n,o;
//push old nodes which will be updated further along the stream
for(var i=0;i<updates.length;i++){
o=updates[i].old;
n=updates[i].new;
consolidateLinks(o,n);
nodes.push(updates[i].old);
}
for(var i=0;i<updates.length;i++){
validateLinks(updates[i].old);
}
linkNew(items);
return {nodes:nodes,links:links,updates:updates,invalid:invalid};
}//end parseData
function orderData(){
function copyProps(d,i,arr){
var props=["startTime","endTime","duration","asap"];
if(!arr[i])arr[i]={};
for(var x in props){
var p=props[x];
arr[i][p]=d[p];
}
}
function registerInitProps(d){
if(d.endTime){
copyProps(d,d.index,initProps);
}
copyProps(d,d.index,newProps);
}
function updateItem(a,b){
function hasChangedDuration(){
var i=a.index;
return initProps[i]&&newProps[i].duration!=initProps[i].duration;
}
//overwrite startProps with updated data
copyProps(b,a.index,startProps);
}//end updateItem
function placeAfterReqs(d){
function checkOrder(d,o){
newProps[d.index].startTime=Math.max(newProps[o.index].startTime+newProps[o.index].duration,newProps[d.index].startTime);
}
//if(d.asap)return;//pretty sure this the opposite of what we want but it does the trick for now
var reqs=d.prereqs;
var startTime,origStart=d.startTime,o;
for(var j=0;j<reqs.length;j++){
o=reqs[j];
if(o.isValidItem()){
if(!ordered[o.index]){
placeAfterReqs(o);
}
checkOrder(d,o);
}
}
ordered[d.index]=true;
}//end placeAfterReqs
function propagateNewProp(d,p){
var i=d.index;
function propHasChanged(){
return initProps[i][p]!=newProps[i][p];
}
function isOldItem(){
return initProps[i]!=undefined;
}
if(isOldItem()){
if(propHasChanged())d.propagate(p,newProps[i][p]);
}else{
d[p]=newProps[i][p];
}
}
//begin
var n=data.nodes.length;
for(var i=0;i<n;i++){
d=data.nodes[i];
//console.info(new Date(d.duration));
d.index=i;
ordered[i]=false;
registerInitProps(d);
}
//push new props onto old ones and register any changes in duration
n=data.updates.length;
for (var i=0;i<n;i++) {
d=data.updates[i].old;
copyProps(data.updates[i].new,d.index,newProps);
}
n=data.nodes.length;
//ensure order is maintained and register and changes in startTime
for(var i=0;i<n;i++){
//console.info(new Date(data.nodes[i].duration));
placeAfterReqs(data.nodes[i]);
}
//calculate new end times after having shuffled things around
var origEnd;
for (var i=0;i<n;i++) {
d=data.nodes[i];
p=newProps[d.index]
p.endTime=p.startTime+p.duration;
propagateNewProp(d,"endTime");
propagateNewProp(d,"duration");
//console.info(new Date(d.duration));
propagateNewProp(d,"startTime");
}
}//end orderData
};
function listenForChanges(){
var client = new Faye.Client("http://access.fungalgenomics.ca:8000/faye");
client.subscribe("/updates/articleSave",function(m){
handleMessage(m)
})
function handleMessage(m){
console.info(m)
timeline.add(prepareData(getArticle(m)));
};
}
var out = {};
out.prepareData=prepareData;
out.listenForChanges=listenForChanges;
return out;
}