-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathmodel.js
1326 lines (1124 loc) · 46.7 KB
/
model.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2011-2017 Massachusetts Institute of Technology
// Chris Terman
// Model:
// Module := {aname: Aspect, properties: {pname: Property, ...}}
// Property := {type: ..., label: ..., value: ..., edit: ..., choices: ...}
// Aspect := [Component, ...]
// Component := [type [x, y, rotation, ...] {property: value, ... }]
jade_defs.model = function (jade) {
var AUTOSAVE_TRIGGER = 25; // number edits that triggers an autosave
var edit_counter = 0;
function set_autosave_trigger(n) {
AUTOSAVE_TRIGGER = n;
}
//////////////////////////////////////////////////////////////////////
//
// Modules
//
//////////////////////////////////////////////////////////////////////
var modules = {};
function get_modules() {
return modules;
}
function clear_modules() {
modules = {};
}
function load_json(json,mark_clean) {
try {
$.each(json,function(mname,mjson) {
// force module names to be a pathname, in /user by default
if (mname[0] != '/') mname = '/user/'+mname;
var m = find_module(mname,mjson,mark_clean);
});
}
catch (e) {
console.log(e.stack);
}
}
// return json for all non-shared modules + modified flag
function json_modules(dirty_only) {
var modified = false;
var save = {};
$.each(modules,function(mname,module) {
modified |= module.modified;
if (!module.shared) {
var json = module.json(dirty_only);
if (Object.keys(json).length > 0) save[mname] = json;
}
});
return {
modified: modified,
json: save
};
}
// update server with any changes to loaded modules
function save_modules(force) {
var result = json_modules();
if (force || result.modified) jade.save_to_server(result.json,clear_modified);
}
function clear_modified() {
jade.unsaved_changes(false);
$.each(modules,function(mname,module) {
if (!module.shared) module.clear_modified();
});
}
// return specified Module, newly created if necessary
function find_module(name,json,mark_clean) {
var module = modules[name];
if (module === undefined) {
module = new Module(name, json, mark_clean);
modules[name] = module;
} else if (json) module.load(json, mark_clean);
return module;
}
// remove module from list
function remove_module(name) {
if (name in modules) {
delete modules[name];
save_modules(true); // do this now since there's no place to keep modified indicator!
}
};
// apply function to matching modules. plist can be an
// array of patterns; each pattern should either be
// a RegExp or a string that can be converted to one.
function map_modules(plist,f) {
if (!(plist instanceof Array)) plist = [plist];
$.each(plist,function (pindex,p) {
if (!(p instanceof RegExp)) p = new RegExp(p);
$.each(modules,function(mname,module) {
if (p.test(mname)) f(module);
});
});
}
function Module(name, json, mark_clean) {
this.name = name;
this.aspects = {};
this.properties = { // every module has itertions and name properties
"name":{"edit":"yes","type":"name","value":"","label":"Name"}
};
this.modified = false;
this.properties_clean = false;
// list of callbacks when load is complete
this.loaded = false;
this.listeners = [];
if (json) this.load(json, mark_clean);
}
Module.prototype.get_name = function() {
return this.name;
};
Module.prototype.confidential = function() {
return this.property_value('confidential') == 'true';
};
Module.prototype.read_only = function() {
return this.confidential() || this.property_value('readonly') == 'true';
};
Module.prototype.add_listener = function(callback) {
this.listeners.push(callback);
};
Module.prototype.notify_listeners = function(msg) {
// pass along message to all our listeners
$.each(this.listeners,function (index,callback) { callback(msg); });
};
/*
Module.prototype.set_clean = function() {
this.properties_clean = true;
for (var a in this.aspects) this.aspects[a].clean = true;
};
*/
Module.prototype.set_modified = function() {
this.modified = true;
jade.unsaved_changes(true);
edit_counter += 1;
if (edit_counter % AUTOSAVE_TRIGGER == 0) {
save_modules();
}
};
Module.prototype.clear_modified = function() {
for (var a in this.aspects) this.aspects[a].clear_modified();
this.modified = false;
};
Module.prototype.property_value = function(pname) {
var p = this.properties[pname];
// editable properties have a value field, otherwise just return what we have
return p !== undefined ? (p.value === undefined ? p : p.value) : undefined;
};
Module.prototype.set_property = function(prop, v) {
if (v != this.properties[prop]) {
this.properties_clean = false;
this.properties[prop] = v;
this.set_modified();
}
};
Module.prototype.set_property_attribute = function(pname, attr, v) {
var prop = this.properties[pname];
if (prop === undefined) {
prop = {"edit":"yes","type":"string","value":"","label":pname};
this.properties[pname] = prop;
this.set_modified();
}
if (v != prop[attr]) {
prop[attr] = v;
this.set_modified();
this.properties_clean = false;
}
};
Module.prototype.remove_property = function(prop) {
if (prop in this.properties) {
delete this.properties[prop];
this.set_modified();
this.properties_clean = false;
}
};
// initialize module from JSON object
Module.prototype.load = function(json, mark_clean) {
// load aspects
for (var a in json) {
if (a == 'properties') {
// if properties are marked as clean, only load properties
// if they differ from the current ones, preserving cleanliness if we can!
if (!this.properties_clean || JSON.stringify(this.properties) != JSON.stringify(json[a])) {
$.extend(this.properties,json[a]);
this.properties_clean = mark_clean;
}
} else {
var current = this.aspects[a];
var ld = true;
if (current) {
// if current aspect is read-only, don't overwrite!
if (current.read_only()) ld = false;
// if current aspect is marked as clean, only load aspect
// if it differs from the current one, preserving cleanliness if we can!
if (current.clean && JSON.stringify(current.json()) == JSON.stringify(json[a])) ld = false;
}
if (ld) this.aspects[a] = new Aspect(a, this, json[a], mark_clean);
}
}
// a newly loaded module starts as unmodified
this.clear_modified();
this.loaded = true;
for (var i = this.listeners.length - 1; i >= 0; i -= 1) {
this.listeners[i]('load');
}
};
Module.prototype.has_aspect = function(name) {
if (name in this.aspects) return !this.aspects[name].empty();
return false;
};
// return specified aspect, newly created if necessary
Module.prototype.aspect = function(name) {
var aspect = this.aspects[name];
if (aspect === undefined) {
aspect = new Aspect(name, this);
this.aspects[name] = aspect;
}
return aspect;
};
// produce JSON representation of a module
Module.prototype.json = function(dirty_only) {
var aspects = {};
if (!dirty_only || !this.properties_clean)
aspects.properties = this.properties;
for (var aname in this.aspects) {
var a = this.aspects[aname];
if (dirty_only && a.clean) continue;
var json = a.json();
// weed out empty aspects
if (json.length > 0) aspects[aname] = json;
}
return aspects;
};
//////////////////////////////////////////////////////////////////////
//
// Aspects
//
//////////////////////////////////////////////////////////////////////
function Aspect(name, module, json, mark_clean) {
this.module = module;
this.name = name;
this.components = [];
this.modified = false;
// set clean after loading shared module info
// clear clean on any subsequent modification
this.clean = false;
this.connection_points = {}; // location string => list of cp's
// for undo/redo keep a list of actions and the changes that resulted.
// Each element of the list is a list of changes that happened concurrently,
// they will all be undone or redone together. Each change is a list:
// [component, 'action', params...]
this.actions = [];
this.current_action = -1; // index of current list of changes
this.change_list = undefined;
if (json) this.load(json, mark_clean);
}
// initialize aspect from JSON object
Aspect.prototype.load = function(json, mark_clean) {
this.components = []; // start with a clean slate
for (var i = 0; i < json.length; i += 1) {
var c = make_component(json[i]);
c.add(this);
}
this.clear_modified();
this.clean = mark_clean;
};
Aspect.prototype.set_modified = function() {
this.modified = true;
this.clean = false;
if (this.module)
this.module.set_modified();
};
Aspect.prototype.clear_modified = function() {
this.modified = false;
};
Aspect.prototype.json = function() {
var json = [];
for (var i = 0; i < this.components.length; i += 1) {
json.push(this.components[i].json());
}
return json;
};
Aspect.prototype.confidential = function() {
if (!this.module) return false;
// is this aspect read only?
if (this.module.property_value(this.name + '-confidential') == 'true') return true;
return this.module.confidential();
};
Aspect.prototype.read_only = function() {
if (!this.module) return false;
// is this aspect read only?
if (this.module.property_value(this.name + '-readonly') == 'true') return true;
return this.module.read_only();
};
Aspect.prototype.empty = function() {
return this.components.length === 0;
};
Aspect.prototype.start_action = function() {
this.change_list = []; // start recording changes
};
Aspect.prototype.end_action = function() {
if (this.change_list !== undefined && this.change_list.length > 0) {
this.clean_up_wires(true); // canonicalize diagram's wires
this.current_action += 1;
// truncate action list at current entry
if (this.actions.length > this.current_action) this.actions = this.actions.slice(0, this.current_action);
this.actions.push(this.change_list);
this.set_modified();
}
this.change_list = undefined; // stop recording changes
};
Aspect.prototype.add_change = function(change) {
if (this.change_list !== undefined) this.change_list.push(change);
};
Aspect.prototype.can_undo = function() {
return this.current_action >= 0;
};
Aspect.prototype.undo = function() {
if (this.current_action >= 0) {
var changes = this.actions[this.current_action];
this.current_action -= 1;
// undo changes in reverse order
for (var i = changes.length - 1; i >= 0; i -= 1) {
changes[i](this, 'undo');
}
this.clean_up_wires(false); // canonicalize diagram's wires
this.set_modified();
}
};
Aspect.prototype.can_redo = function() {
return this.current_action + 1 < this.actions.length;
};
Aspect.prototype.redo = function() {
if (this.current_action + 1 < this.actions.length) {
this.current_action += 1;
var changes = this.actions[this.current_action];
// redo changes in original order
for (var i = 0; i < changes.length; i += 1) {
changes[i](this, 'redo');
}
this.clean_up_wires(false); // canonicalize diagram's wires
this.set_modified();
}
};
Aspect.prototype.add_component = function(new_c) {
this.components.push(new_c);
};
Aspect.prototype.remove_component = function(c) {
var index = this.components.indexOf(c);
if (index != -1) {
this.components.splice(index, 1);
}
};
Aspect.prototype.map_over_components = function(f) {
for (var i = this.components.length - 1; i >= 0; i -= 1) {
if (f(this.components[i], i)) return;
}
};
Aspect.prototype.selections = function() {
for (var i = this.components.length - 1; i >= 0; i -= 1) {
if (this.components[i].selected) return true;
}
return false;
};
// returns component if there's exactly one selected, else undefined
Aspect.prototype.selected_component = function() {
var selected;
for (var i = this.components.length - 1; i >= 0; i -= 1) {
if (this.components[i].selected) {
if (selected === undefined) selected = this.components[i];
else return undefined;
}
}
return selected;
};
Aspect.prototype.find_connections = function(cp) {
return this.connection_points[cp.location];
};
// add connection point to list of connection points at that location
Aspect.prototype.add_connection_point = function(cp) {
var cplist = this.connection_points[cp.location];
if (cplist) cplist.push(cp);
else {
cplist = [cp];
this.connection_points[cp.location] = cplist;
}
// return list of conincident connection points
return cplist;
};
// remove connection point from the list points at the old location
Aspect.prototype.remove_connection_point = function(cp, old_location) {
// remove cp from list at old location
var cplist = this.connection_points[old_location];
if (cplist) {
var index = cplist.indexOf(cp);
if (index != -1) {
cplist.splice(index, 1);
// if no more connections at this location, remove
// entry from array to keep our search time short
if (cplist.length === 0) delete this.connection_points[old_location];
}
}
};
// connection point has changed location: remove, then add
Aspect.prototype.update_connection_point = function(cp, old_location) {
this.remove_connection_point(cp, old_location);
return this.add_connection_point(cp);
};
// add a wire to the diagram
Aspect.prototype.add_wire = function(x1, y1, x2, y2, rot) {
var new_wire = make_component(['wire', [x1, y1, rot, x2 - x1, y2 - y1]]);
new_wire.add(this);
return new_wire;
};
Aspect.prototype.split_wire = function(w, cp) {
// remove bisected wire
w.remove();
// add two new wires with connection point cp in the middle
this.add_wire(w.coords[0], w.coords[1], cp.x, cp.y, 0);
var far_end = w.far_end();
this.add_wire(far_end[0], far_end[1], cp.x, cp.y, 0);
};
// see if connection points of component c split any wires
Aspect.prototype.check_wires = function(c) {
for (var i = 0; i < this.components.length; i += 1) {
var cc = this.components[i];
if (cc != c) { // don't check a component against itself
// only wires will return non-null from a bisect call
var cp = cc.bisect(c);
if (cp) {
// cc is a wire bisected by connection point cp
this.split_wire(cc, cp);
}
}
}
};
// see if there are any existing connection points that bisect wire w
Aspect.prototype.check_connection_points = function(w) {
for (var locn in this.connection_points) {
var cplist = this.connection_points[locn];
if (cplist && w.bisect_cp(cplist[0])) {
this.split_wire(w, cplist[0]);
// stop here, new wires introduced by split will do their own checks
return;
}
}
};
// merge collinear wires sharing an end point.
Aspect.prototype.clean_up_wires = function() {
// merge colinear wires
for (var locn in this.connection_points) {
var cplist = this.connection_points[locn];
if (cplist && cplist.length == 2) {
// found a connection with just two connections, see if they're wires
var c1 = cplist[0].parent;
var c2 = cplist[1].parent;
if (c1.type() == 'wire' && c2.type() == 'wire') {
var e1 = c1.other_end(cplist[0]);
var e2 = c2.other_end(cplist[1]);
var e3 = cplist[0]; // point shared by the two wires
if (collinear(e1, e2, e3)) {
c1.remove();
c2.remove();
this.add_wire(e1.x, e1.y, e2.x, e2.y, 0);
}
}
}
}
// remove redundant wires
while (this.remove_redundant_wires());
};
// elminate wires between the same end points. Keep calling until it returns false.
Aspect.prototype.remove_redundant_wires = function() {
for (var locn in this.connection_points) {
var cplist = this.connection_points[locn];
for (var i = 0; i < cplist.length; i += 1) {
var cp1 = cplist[i];
var w1 = cp1.parent;
if (w1.type() == 'wire') {
var cp2 = w1.other_end(cp1);
for (var j = i + 1; j < cplist.length; j += 1) {
var w2 = cplist[j].parent;
if (w2.type() == 'wire' && w2.other_end(cp1).coincident(cp2.x, cp2.y)) {
// circumvent unnecessary wire removal search
Component.prototype.remove.call(w2);
// we've modified lists we're iterating over, so to avoid
// confusion, start over
return true;
}
}
}
}
}
return false;
};
Aspect.prototype.selections = function() {
var selections = false;
for (var i = this.components.length - 1; i >= 0; i -= 1) {
if (this.components[i].selected) selections = true;
}
return selections;
};
Aspect.prototype.compute_bbox = function(initial_bbox, selected, unselected) {
if (this.components.length == 0) return [-16,-16,16,16];
// compute bounding box for selection
var min_x = (initial_bbox === undefined) ? Infinity : initial_bbox[0];
var max_x = (initial_bbox === undefined) ? -Infinity : initial_bbox[2];
var min_y = (initial_bbox === undefined) ? Infinity : initial_bbox[1];
var max_y = (initial_bbox === undefined) ? -Infinity : initial_bbox[3];
for (var i = this.components.length - 1; i >= 0; i -= 1) {
var component = this.components[i];
if (selected && !component.selected) continue;
if (unselected && component.selected) continue;
if (component.type() == 'property') continue;
min_x = Math.min(component.bbox[0], min_x);
max_x = Math.max(component.bbox[2], max_x);
min_y = Math.min(component.bbox[1], min_y);
max_y = Math.max(component.bbox[3], max_y);
}
return [min_x, min_y, max_x, max_y];
};
Aspect.prototype.unselected_bbox = function(initial_bbox) {
return this.compute_bbox(initial_bbox, false, true);
};
Aspect.prototype.selected_bbox = function(initial_bbox) {
return this.compute_bbox(initial_bbox, true, false);
};
Aspect.prototype.selected_grid = function() {
var grid = 1;
for (var i = this.components.length - 1; i >= 0; i -= 1) {
var c = this.components[i];
if (c.selected) grid = Math.max(grid, c.required_grid);
}
return grid;
};
////////////////////////////////////////////////////////////////////////////////
//
// Rectangle helper functions
//
////////////////////////////////////////////////////////////////////////////////
// rect is an array of the form [left,top,right,bottom]
// ensure left < right, top < bottom
function canonicalize(r) {
var temp;
// canonicalize bounding box
if (r[0] > r[2]) {
temp = r[0];
r[0] = r[2];
r[2] = temp;
}
if (r[1] > r[3]) {
temp = r[1];
r[1] = r[3];
r[3] = temp;
}
}
function between(x, x1, x2) {
return x1 <= x && x <= x2;
}
// only works for manhattan rectangles
function intersect(r1, r2) {
// look for non-intersection, negate result
var result = !(r2[0] > r1[2] || r2[2] < r1[0] || r2[1] > r1[3] || r2[3] < r1[1]);
// if I try to return the above expression, javascript returns undefined!!!
return result;
}
function transform_x(rot, x, y) {
if (rot === 0 || rot == 6) return x;
else if (rot == 1 || rot == 5) return -y;
else if (rot == 2 || rot == 4) return -x;
else return y;
}
function transform_y(rot, x, y) {
if (rot == 1 || rot == 7) return x;
else if (rot == 2 || rot == 6) return -y;
else if (rot == 3 || rot == 5) return -x;
else return y;
}
// result of composing two rotations: orient[old*8 + new]
var rotate = [
0, 1, 2, 3, 4, 5, 6, 7, // NORTH (identity)
1, 2, 3, 0, 7, 4, 5, 6, // EAST (rot270) rotcw
2, 3, 0, 1, 6, 7, 4, 5, // SOUTH (rot180)
3, 0, 1, 2, 5, 6, 7, 4, // WEST (rot90) rotccw
4, 5, 6, 7, 0, 1, 2, 3, // RNORTH (negx) fliph
5, 6, 7, 4, 3, 0, 1, 2, // REAST (int-neg)
6, 7, 4, 5, 2, 3, 0, 1, // RSOUTH (negy) flipy
7, 4, 5, 6, 1, 2, 3, 0 // RWEST (int-pos)
];
//////////////////////////////////////////////////////////////////////
//
// Components
//
//////////////////////////////////////////////////////////////////////
var built_in_components = {};
function make_component(json) {
var c = built_in_components[json[0]];
if (c) return new c(json);
else {
// instance of a module -- force type to be
// a pathname, by default in '/user'
if (json[0][0] != '/')
json[0] = '/user/' + json[0];
return new Component(json);
}
}
// general-purpose component, drawn in a diagram using its icon
function Component(json) {
this.aspect = undefined;
this.module = undefined;
this.icon = undefined;
this.coords = [0, 0, 0];
this.properties = {};
this.selected = false;
this.bounding_box = [0, 0, 0, 0]; // in device coords [left,top,right,bottom]
this.bbox = this.bounding_box; // in absolute coords
this.connections = [];
if (json) this.load(json);
}
Component.prototype.required_grid = 8;
Component.prototype.type = function() {
// always ask module for name... simplifies renaming modules
return this.module.get_name();
};
Component.prototype.clone_properties = function(remove_default_values) {
// weed out empty properties or those that match default value
var props = {};
for (var p in this.properties) {
var v = this.properties[p];
if (v !== undefined && v !== '' && this.module.properties[p] &&
(!remove_default_values || v != this.module.properties[p].value)) props[p] = v;
}
return props;
};
Component.prototype.set_property = function(pname,pvalue) {
this.properties[pname] = pvalue;
if (this.aspect) this.aspect.set_modified();
};
Component.prototype.load = function(json) {
this.module = find_module(json[0]);
this.coords = json[1];
this.properties = json[2] || {};
// track down icon and set up bounding box and connections
var component = this; // for closure
this.module.add_listener(function(msg) {
if (msg == 'load' || msg == 'icon_changed')
component.compute_bbox();
});
// if module is already loaded, we can compute bbox now
if (this.module.loaded) this.compute_bbox();
};
Component.prototype.default_properties = function() {
// update properties from module's default values
for (var p in this.module.properties) {
if (!(p in this.properties)) this.properties[p] = this.module.properties[p].value || '';
}
};
Component.prototype.property_info = function (pname) {
return this.module.properties[pname] || {};
};
Component.prototype.compute_bbox = function() {
//console.log('compute bbox for '+this.module.get_name());
// update properties from module's default values
this.default_properties();
this.name = this.properties.name; // used when extracting netlists
if (this.name) this.name = this.name.toLowerCase();
if (this.module.has_aspect('icon')) {
this.icon = this.module.aspect('icon');
// clear out old connection points if any
var component = this; // for closures
if (this.aspect) {
$.each(this.connections,function (index,cp) {
component.aspect.remove_connection_point(cp, cp.location);
});
}
this.connections = [];
// look for terminals in the icon and add appropriate connection
// points for this instance
this.icon.map_over_components(function(c) {
var cp = c.terminal_coords();
if (cp) component.add_connection(cp[0], cp[1], cp[2]);
});
this.bounding_box = this.icon.compute_bbox();
this.update_coords();
}
};
// default: no terminal coords to provide!
Component.prototype.terminal_coords = function() {
return undefined;
};
Component.prototype.json = function() {
var p = this.clone_properties(true);
if (Object.keys(p).length > 0) return [this.type(), this.coords.slice(0), p];
else return [this.type(), this.coords.slice(0)];
};
Component.prototype.clone = function(x, y) {
var c = make_component(this.json());
c.name = undefined; // don't clone name
if (c.properties.name) c.properties.name.value = '';
c.coords[0] = x; // override x and y
c.coords[1] = y;
return c;
};
Component.prototype.can_view = function() {
return this.module && this.module.confidential && !this.module.confidential();
};
Component.prototype.has_aspect = function(name) {
if (this.module !== undefined) return this.module.has_aspect(name);
else return false;
};
Component.prototype.set_select = function(which) {
this.selected = which;
};
Component.prototype.add_connection = function(offset_x, offset_y, name) {
var cp = new ConnectionPoint(this, offset_x, offset_y, name);
this.connections.push(cp);
return cp;
};
Component.prototype.update_coords = function() {
var x = this.coords[0];
var y = this.coords[1];
// update bbox
var b = this.bounding_box;
this.bbox[0] = this.transform_x(b[0], b[1]) + x;
this.bbox[1] = this.transform_y(b[0], b[1]) + y;
this.bbox[2] = this.transform_x(b[2], b[3]) + x;
this.bbox[3] = this.transform_y(b[2], b[3]) + y;
canonicalize(this.bbox);
// update connections
for (var i = this.connections.length - 1; i >= 0; i -= 1) {
this.connections[i].update_location();
}
};
Component.prototype.inside = function(x, y, rect) {
if (rect === undefined) rect = this.bbox;
return between(x, rect[0], rect[2]) && between(y, rect[1], rect[3]);
};
// rotate component relative to specified center of rotation
Component.prototype.rotate = function(rotation, cx, cy) {
var old_x = this.coords[0];
var old_y = this.coords[1];
var old_rotation = this.coords[2];
// compute relative coords
var rx = old_x - cx;
var ry = old_y - cy;
// compute new position and rotation
var new_x = transform_x(rotation, rx, ry) + cx;
var new_y = transform_y(rotation, rx, ry) + cy;
var new_rotation = rotate[old_rotation * 8 + rotation];
this.coords[0] = new_x;
this.coords[1] = new_y;
this.coords[2] = new_rotation;
this.update_coords();
// create a record of the change
var component = this; // for closure
function component_rotate(diagram, action) {
if (action == 'undo') {
component.coords[0] = old_x;
component.coords[1] = old_y;
component.coords[2] = old_rotation;
}
else {
component.coords[0] = new_x;
component.coords[1] = new_y;
component.coords[2] = new_rotation;
}
component.update_coords();
}
this.aspect.add_change(component_rotate);
};
Component.prototype.move_begin = function() {
// remember where we started this move
this.move_x = this.coords[0];
this.move_y = this.coords[1];
this.move_rotation = this.coords[2];
};
Component.prototype.move = function(dx, dy) {
// update coordinates
this.coords[0] += dx;
this.coords[1] += dy;
this.update_coords();
};
Component.prototype.move_end = function() {
var dx = this.coords[0] - this.move_x;
var dy = this.coords[1] - this.move_y;
if (dx !== 0 || dy !== 0 || this.coords[2] != this.move_rotation) {
// create a record of the change
var component = this; // for closure
function component_move_end(diagram, action) {
if (action == 'undo') component.move(-dx, - dy);
else component.move(dx, dy);
component.aspect.check_wires(component);
}
this.aspect.add_change(component_move_end);
this.aspect.check_wires(this);
}
};
Component.prototype.add = function(aspect) {
this.aspect = aspect; // we now belong to a diagram!
aspect.add_component(this);
this.update_coords();
// create a record of the change
var component = this; // for closure
function component_add(diagram, action) {
if (action == 'undo') component.remove();
else component.add(diagram);
}
aspect.add_change(component_add);
};
Component.prototype.remove = function() {
// remove connection points from diagram
for (var i = this.connections.length - 1; i >= 0; i -= 1) {
var cp = this.connections[i];
this.aspect.remove_connection_point(cp, cp.location);
}
// remove component from diagram
this.aspect.remove_component(this);
// create a record of the change
var component = this; // for closure
function component_remove(diagram, action) {
if (action == 'undo') component.add(diagram);
else component.remove();
}
this.aspect.add_change(component_remove);
};
Component.prototype.transform_x = function(x, y) {
return transform_x(this.coords[2], x, y);
};
Component.prototype.transform_y = function(x, y) {
return transform_y(this.coords[2], x, y);
};
Component.prototype.moveTo = function(diagram, x, y) {
var nx = this.transform_x(x, y) + this.coords[0];
var ny = this.transform_y(x, y) + this.coords[1];
diagram.moveTo(nx, ny);
};
Component.prototype.lineTo = function(diagram, x, y) {
var nx = this.transform_x(x, y) + this.coords[0];
var ny = this.transform_y(x, y) + this.coords[1];
diagram.lineTo(nx, ny);
};
var colors_rgb = {
'red': 'rgb(255,64,64)',
'green': 'rgb(64,255,64)',
'blue': 'rgb(64,64,255)',
'cyan': 'rgb(64,255,255)',
'magenta': 'rgb(255,64,255)',
'yellow': 'rgb(255,255,64)',