-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamx-gmodel.js
executable file
·577 lines (469 loc) · 14 KB
/
streamx-gmodel.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
573
574
575
576
577
var PlotView = function(plot){
var view = {
zoom : 1,
x : 0,
y : 0,
width : plot.width,
height : plot.height
}
var observer = {};
this.absoluteToPlotCoords = function(x,y){
return {
x : x - offset.left,
y : y - offset.top
}
}
this.justify = function(point){
var ds = view.zoom;
var dx = (point.x + view.x) * ds - (view.width*ds - view.width)/2;
var dy = (point.y + view.y) * ds - (view.height*ds - view.height)/2;
return {
x : dx,
y : dy
}
}
this.normalize = function(point){
var ds = view.zoom;
var dx = (point.x + (view.width*ds - view.width)/2)/ds - view.x;
var dy = (point.y + (view.height*ds - view.height)/2)/ds - view.y;
return {
x : dx,
y : dy
}
}
this.addObserver = function(name, callback, context){
observer[name] = {
fn : callback,
context : context
}
}
this.removeObserver = function(name){
if(typeof observer[name] != 'undefined'){
delete observer[name];
}
}
// TODO - memento has the same property set as view !!!
this.memento = function(memento){
if(memento){
view = $.extend({},memento);
}else{
memento = $.extend({},view);
}
return memento;
}
this.refresh = function(){
for(var name in observer){
observer[name].fn.call(
observer[name].context
)
}
}
this.x = function(x){
if(typeof x != 'undefined'){
view.x = x;
this.refresh();
return this;
}
return view.x;
}
this.y = function(y){
if(typeof y != 'undefined'){
view.y = y;
this.refresh();
return this;
}
return view.y;
}
this.zoom = function(zoom){
if(typeof zoom != 'undefined'){
view.zoom = zoom;
this.refresh();
return this;
}
return view.zoom;
}
this.width = function(width){
if(typeof width != 'undefined'){
throw 'Not supported yet!';
}
return view.width;
}
this.height = function(height){
if(typeof height != 'undefined'){
throw 'Not supported yet!';
}
return view.height;
}
}
var PlotHelper = {
getObjectAbsolutePath : function(graphic){
return Raphael.transformPath(
graphic.attr('path'),
graphic.transform()
)
},
getObjectAbsolutePathBox : function(graphic){
return Raphael.pathBBox(
Raphael.transformPath(
graphic.attr('path'),
graphic.transform()
)
)
},
getObjectsByType : function(plot,type){
var element = plot.bottom, res = [];
while (element) {
var self = element.data('self');
if(typeof self == 'object' && typeof self.config == 'object' && self.config.type == type){
res.push(self);
}
element = element.next;
}
return res;
}
}
var Intersector = function(){
this.filter = function(element, elementArray){
var intersectArray = [];
for(var index in elementArray){
if(this.check(element,elementArray[index])){
intersectArray.push(elementArray[index]);
}
}
return intersectArray;
}
this.check = function(elementA, elementB){
var aPosition = PlotHelper.getObjectAbsolutePathBox(elementA.graphic);
var bPath = PlotHelper.getObjectAbsolutePath(elementB.graphic);
return Raphael.isPointInsidePath(bPath,aPosition.x,aPosition.y);
}
}
var Enclosure = function(){
this.check = function(parent,child){
var p = PlotHelper.getObjectAbsolutePathBox(parent.graphic);
var c = PlotHelper.getObjectAbsolutePathBox(child.graphic);
var cr = [
[c.x,c.y],[c.x2,c.y],[c.x,c.y2],[c.x2,c.y2]
]
for(var j in cr){
if( p.x < cr[j][0] && p.y < cr[j][1] &&
p.x2 > cr[j][0] && p.y2 > cr[j][1])
{
return true;
}
}
return false;
}
this.checkArray = function(parent,childArray){
var res = [];
for(var index in childArray){
if(this.check(parent,childArray[index])){
res.push(childArray[index]);
}
}
return res;
}
}
var PlotState = function(plot){
var smartSelectQueue = [];
this.smartSelect = function(element){
if(smartSelectQueue.length == 0){
smartSelectQueue.push(element);
// update context(element.config.id)
log.info(element.config.id)
return;
}
if(smartSelectQueue[0] !== element){
smartSelectQueue.unshift(element);
if(smartSelectQueue.length > 2){
smartSelectQueue[2].hint('');
}
smartSelectQueue[0].hint('end');
smartSelectQueue[1].hint('begin');
log.info(
smartSelectQueue[1].config.id + '\t' + smartSelectQueue[0].config.id
)
// update context(smartSelectQueue[1].config.id, smartSelectQueue[0].config.id)
return;
}
}
this.select = function(elements){
// TODO
log.info(elements)
}
}
var PlotServices = function(plot,screen){
this.plot = plot;
this.view = new PlotView(plot);;
this.state = new PlotState(plot);
this.screen = null;
this.intersect = new Intersector();
this.enclosure = new Enclosure();
}
var Edge = function(connection,callback,plotService){
var halo = plotService.plot.path(Shapes.circle(0,0,PlotStyle.size.edge)).attr(PlotStyle['edge']);
halo.data('self',this);
this.refresh = function(){
var j = plotService.view.justify(this.position);
halo.transform('t' + j.x + ',' + j.y + " s" + plotService.view.zoom());
}
var onstart = function(x,y,eventDOM){
callback.selected(connection.config.id);
this.position = plotService.view.justify(this.self.position);
this.min = {
x : PlotStyle.size.edge*plotService.view.zoom(),
y : PlotStyle.size.edge*plotService.view.zoom()
}
this.max = {
x : plotService.view.width() - this.min.x,
y : plotService.view.height() - this.min.y
}
}
var onmove = function(dx,dy,x,y,eventDOM){
var point = {
x : Math.min(Math.max(this.position.x + dx, this.min.x), this.max.x),
y : Math.min(Math.max(this.position.y + dy, this.min.y), this.max.y)
}
this.self.position = plotService.view.normalize(point)
this.self.refresh()
connection.refresh(this.self);
}
var onend = function(eventDOM){
if(this.position.x != this.self.position.x || this.position.y != this.self.position.y){
callback.moved(connection.config.id);
}
log.info(plotService.intersect.filter(this.self,PlotHelper.getObjectsByType(plotService.plot,'Element')))
delete this.position, this.min, this.max;
}
var context = {
self : this
}
halo.drag(onmove,onstart,onend,context,context,context);
// Public properties
this.graphic = halo;
this.position = {
x : 0,
y : 0
}
}
var Connection = function(config,callback,plotService){
var line = plotService.plot.path('').attr(PlotStyle['connection']).attr(PlotStyle[config.style]);
line.data('self',this);
var set = plotService.plot.set();
set.push(line);
// Register connection to its Elements
config.fromElement.connection[config.id] = this;
config.toElement.connection[config.id] = this;
this.refresh = function(notifier){
var sizeElement = PlotStyle.size.element*plotService.view.zoom();
var sizeEdge = PlotStyle.size.edge*plotService.view.zoom();
var fromObject = this[notifier === this.fromEdge ? 'fromEdge' : 'from'];
var toObject = this[notifier === this.toEdge ? 'toEdge' : 'to'];
var fromPoint = plotService.view.justify(fromObject.position);
var toPoint = plotService.view.justify(toObject.position);
var fromOffset = (notifier === this.fromEdge ? sizeEdge : sizeElement) / 2
var toOffset = (notifier === this.toEdge ? sizeEdge : sizeElement) / 2
var path = Shapes.line(fromPoint.x,fromPoint.y, toPoint.x, toPoint.y);
var pathLength = Raphael.getTotalLength(path);
var lineIsVisible = pathLength > sizeElement*1.2;
var newPath = lineIsVisible ?
Raphael.getSubpath(path,fromOffset+sizeEdge,pathLength - (toOffset + sizeEdge)) :
path;
// Update Line position
set.attr({path : newPath})
// Update Edges position
var fromEdgePoint = Raphael.getPointAtLength(path, lineIsVisible ? fromOffset : 0);
this.fromEdge.position = plotService.view.normalize(fromEdgePoint);
this.fromEdge.refresh();
var toEdgePoint = Raphael.getPointAtLength(path,lineIsVisible ? pathLength - toOffset : pathLength);
this.toEdge.position = plotService.view.normalize(toEdgePoint);
this.toEdge.refresh();
}
// Public properties
this.config = config;
this.graphic = line;
this.from = config.fromElement;
this.to = config.toElement;
this.fromEdge = new Edge(this,callback,plotService);
this.toEdge = new Edge(this,callback,plotService);
this.refresh();
}
var Element = function(config,callback,plotService){
var image = plotService.plot.image(config.icon,0,0,0,0);
var halo = plotService.plot.path(Shapes.circle(0,0,0)).attr(PlotStyle['element-halo']);
var state = plotService.plot.path(Shapes.circle(0,0,0)).attr(PlotStyle['element-state']);
var text = plotService.plot.text(0,0,config.description).attr(PlotStyle['element-text']);
var hint = plotService.plot.text(0,0,'.').attr(PlotStyle['element-hint']);
halo.data('self',this);
var set = plotService.plot.set();
set.push(image,text,hint,state,halo);
var resize = function(size){
var imageSize = size / Math.sqrt(2);
image.attr({
x : -imageSize/2,
y : -imageSize/2,
width : imageSize,
height : imageSize
})
halo.attr({
path : Shapes.circle(0,0,size/2)
})
state.attr({
path : Shapes.circle(0,0,size/2)
})
text.attr({
y : size/2
})
hint.attr({
y : -size/2
})
}
var refresh = function(){
resize(PlotStyle.size.element)
var textOffset = PlotStyle.size.element/2 * plotService.view.zoom();
text.attr({y : textOffset})
hint.attr({y : -textOffset})
var j = plotService.view.justify(this.position);
set.transform('t' + j.x + ',' + j.y + " s" + plotService.view.zoom());
// refresh its connections
for(var id in this.connection){
this.connection[id].refresh();
}
}
var setHint = function(newHint){
hint.attr({text : newHint})
}
var select = function(selected){
selected ? state.show() : state.hide();
}
var onstart = function(x,y,eventDOM){
callback.selected(config.id);
this.position = plotService.view.justify(this.self.position);
this.min = {
x : PlotStyle.size.element*plotService.view.zoom()/2,
y : PlotStyle.size.element*plotService.view.zoom()/2
}
this.max = {
x : plotService.view.width() - this.min.x,
y : plotService.view.height() - this.min.y
}
}
var onmove = function(dx,dy,x,y,eventDOM){
var point = {
x : Math.min(Math.max(this.position.x + dx, this.min.x), this.max.x),
y : Math.min(Math.max(this.position.y + dy, this.min.y), this.max.y)
}
this.self.position = plotService.view.normalize(point)
this.self.refresh()
}
var onend = function(eventDOM){
if(this.position.x != this.self.position.x || this.position.y != this.self.position.y){
callback.moved(config.id);
}
delete this.position, this.min, this.max;
plotService.state.smartSelect(this.self);
}
var context = {
self : this
}
halo.drag(onmove,onstart,onend,context,context,context);
// Re-render when VIEW got changed
plotService.view.addObserver(config.id, refresh, this);
// Public properties
this.config = config;
this.graphic = halo;
this.connection = {}
this.position = {
x : config.x,
y : config.y
}
this.select = select;
this.hint = setHint;
this.refresh = refresh;
// Render element on screen
this.refresh();
}
var Base = function(plotService,selector){
var base = plotService.plot.path(Shapes.rect(
0,0,plotService.view.width(),plotService.view.height())).attr(PlotStyle['base']).toBack();
// Public properties
this.graphic = base;
}
var Selector = function(plotService,screenService){
var rect = plotService.plot.path(Shapes.rect(0,0,0,0)).attr(PlotStyle['selector']).hide();
var onstart = function(x,y,eventDOM){
// TODO -
var relative = screenService.getRelativeCoords(plotService.plot.canvas,x,y);
this.x = relative.x;
this.y = relative.y;
}
var onend = function(eventDOM){
if(this.self.attr('path') == ''){
return;
}
var selected = rect.getBBox();
var elements = PlotHelper.getObjectsByType(plotService.plot,'Element');
plotService.state.select(
plotService.enclosure.checkArray({ graphic : rect },elements)
);
delete this.x;
delete this.y;
this.self.attr({path : ''})
this.self.hide();
}
var onmove = function(dx,dy,x,y,eventDOM){
var path = Shapes.rect(
this.x + (dx < 0 ? dx : 0),
this.y + (dy < 0 ? dy : 0),
Math.abs(dx),
Math.abs(dy)
)
this.self.attr({path : path}).show();
}
var context = {
self : rect
}
rect.drag(onmove,onstart,onend,context,context,context);
this.mousedown = function(e){
rect.events[0].f.call(rect,e)
}
}
var Scroller = function(plotService,screenService){
var dot = plotService.plot.path(Shapes.circle(0,0,PlotStyle.size.edge)).attr(PlotStyle.edge).hide();
var onstart = function(x,y,eventDOM){
this.x = plotService.view.x();
this.y = plotService.view.y();
this.newPoint = screenService.getRelativeCoords(plotService.plot.canvas,x,y);
dot.show();
this.self.position = this.newPoint;
this.self.refresh();
}
var onend = function(eventDOM){
delete this.x;
delete this.y;
}
var onmove = function(dx,dy,x,y,eventDOM){
plotService.view.x(this.x + dx/plotService.view.zoom())
plotService.view.y(this.y + dy/plotService.view.zoom())
this.self.position = {
x : this.newPoint.x + dx,
y : this.newPoint.y + dy
}
this.self.refresh();
}
var context = {
self : this
}
dot.drag(onmove,onstart,onend,context,context,context);
this.mousedown = function(e){
dot.events[0].f.call(dot,e)
}
this.refresh = function(){
dot.transform('t' + this.position.x + ',' + this.position.y);
}
this.graphic = dot;
this.position = {
x : 0,
y : 0
}
}