forked from clientIO/joint
-
Notifications
You must be signed in to change notification settings - Fork 64
/
basic.js
1054 lines (731 loc) · 36.9 KB
/
basic.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
module('basic', {
setup: function() {
var $fixture = $('#qunit-fixture');
var $paper = $('<div/>');
$fixture.append($paper);
this.graph = new joint.dia.Graph;
this.paper = new joint.dia.Paper({
el: $paper,
gridSize: 10,
model: this.graph
});
},
teardown: function() {
delete this.graph;
delete this.paper;
}
});
test('construction', function() {
var myrect = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(myrect);
strictEqual(myrect.constructor, joint.shapes.basic.Rect, 'myrect.constructor === joint.shapes.basic.Rect');
var textEls = this.paper.svg.getElementsByTagName('text');
var rectEls = this.paper.svg.getElementsByTagName('rect');
equal(textEls.length, 1, 'there is exactly one <text> element in the paper');
equal(rectEls.length, 1, 'there is exactly one <rect> element in the paper');
equal(textEls[0].textContent, 'my rectangle', 'text element has a proper content');
});
asyncTest('async: resetCells', function() {
var r1 = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
var r2 = r1.clone();
var r3 = r1.clone();
this.paper.options.async = { batchSize: 1 };
this.paper.on('render:done', function() {
var textEls = this.paper.svg.getElementsByTagName('text');
var rectEls = this.paper.svg.getElementsByTagName('rect');
equal(textEls.length, 3, 'there is exactly 3 <text> elements in the paper');
equal(rectEls.length, 3, 'there is exactly 3 <rect> elements in the paper');
equal(textEls[0].textContent, 'my rectangle', 'text element has a proper content');
start();
}, this);
this.graph.resetCells([r1, r2, r3]);
});
asyncTest('async: addCells', function() {
var r1 = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
var r2 = r1.clone();
var r3 = r1.clone();
var r4 = r1.clone();
var r5 = r1.clone();
this.graph.addCells([r1, r2]);
this.paper.options.async = { batchSize: 1 };
this.paper.on('render:done', function() {
var textEls = this.paper.svg.getElementsByTagName('text');
var rectEls = this.paper.svg.getElementsByTagName('rect');
equal(textEls.length, 5, 'there is exactly 5 <text> elements in the paper');
equal(rectEls.length, 5, 'there is exactly 5 <rect> elements in the paper');
equal(textEls[0].textContent, 'my rectangle', 'text element has a proper content');
start();
}, this);
this.graph.addCells([r3, r4, r5]);
});
test('getBBox()', function() {
var myrect = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(myrect);
var view = this.paper.findViewByModel(myrect);
var bbox = view.getBBox();
equal(bbox.x, 20, 'bbox.x is correct');
equal(bbox.y, 30, 'bbox.y is correct');
equal(bbox.width, 120, 'bbox.width is correct');
equal(bbox.height, 80, 'bbox.height is correct');
myrect.attr('text', { ref: 'rect', 'ref-y': 100 });
bbox = view.getBBox({ useModelGeometry: false });
ok(bbox.height > 80, 'Translating text outside the rect: bbox.width grew.');
equal(bbox.x, 20, 'bbox.x is correct');
equal(bbox.y, 30, 'bbox.y is correct');
equal(bbox.width, 120, 'bbox.width is correct');
bbox = view.getBBox({ useModelGeometry: true });
equal(bbox.x, 20, 'Using model geometry: bbox.x is correct');
equal(bbox.y, 30, 'bbox.y is correct');
equal(bbox.width, 120, 'bbox.width is correct');
equal(bbox.height, 80, 'bbox.height is correct');
});
test('z index', function() {
var r1 = new joint.shapes.basic.Rect;
var r2 = new joint.shapes.basic.Rect;
var r3 = new joint.shapes.basic.Rect;
this.graph.addCell(r1);
this.graph.addCell(r2);
this.graph.addCell(r3);
ok(r1.get('z') < r2.get('z'), 'z index of the first added cell is lower than that of the second one');
ok(r2.get('z') < r3.get('z'), 'z index of the second added cell is lower than that of the third one');
// Test removing/adding new cells to cover https://github.com/clientIO/JointJS_plus/issues/21.
r1.remove();
var r4 = new joint.shapes.basic.Rect;
this.graph.addCell(r4);
ok(r2.get('z') < r3.get('z'), 'z index of the second added cell is lower than that of the third one');
ok(r3.get('z') < r4.get('z'), 'z index of the third added cell is lower than that of the fourth, newly added, one');
});
test('position()', function() {
var r1 = new joint.shapes.basic.Rect({
position: { x: 100, y: 100 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' }}
});
this.graph.addCell(r1);
var pos = r1.position();
checkBbox(this.paper, r1, pos.x, pos.y, 120, 80, 'getter "position()" returns the elements position.');
r1.position(200,200);
checkBbox(this.paper, r1, 200, 200, 120, 80, 'setter "position(a,b)" should move element to the given position.');
// parentRelative option
var r2 = new joint.shapes.basic.Rect({
position: { x: 10, y: 10 },
size: { width: 30, height: 30 }
});
throws(function() {
r2.position(100,100, { parentRelative: true });
}, 'getter throws an error if "parentRelative" option passed and the element is not part of any collection.');
throws(function() {
r2.position({ parentRelative: true });
}, 'getter throws an error if "parentRelative" option passed and the element is not part of any collection.');
this.graph.addCell(r2);
deepEqual(r2.position({ parentRelative: true }), r2.position(), 'getter with "parentRelative" option works in same way as getter without this option for an unembed element.');
r1.embed(r2);
r2.position(10,10, { parentRelative: true });
checkBbox(this.paper, r2, 210, 210, 30, 30, 'setter "position(a,b)" with "parentRelative" option should move element to the position relative to its parent.');
pos = r2.position({ parentRelative: true });
deepEqual(pos.toString(), "10@10", 'getter with "parentRelative" option returns position relative to the element parent.');
});
test('translate()', function() {
var myrect = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(myrect);
myrect.translate(50);
checkBbox(this.paper, myrect, 70, 30, 120, 80, 'translate(50) should translate by 50px in x direction only');
myrect.translate(0, 20);
checkBbox(this.paper, myrect, 70, 50, 120, 80, 'translate(0, 20) should translate by 20px in y direction only');
myrect.translate(10, 10);
checkBbox(this.paper, myrect, 80, 60, 120, 80, 'translate(10, 10) should translate by 10px in both x and y directions');
myrect.translate(-10, -10);
checkBbox(this.paper, myrect, 70, 50, 120, 80, 'translate(-10, -10) should translate back by 10px in both x and y directions');
});
test('resize()', function() {
var myrect = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(myrect);
myrect.resize(120, 80);
checkBbox(this.paper, myrect, 20, 30, 120, 80, 'resize([same width], [same height]) should not change bbox');
myrect.resize(240, 160);
checkBbox(this.paper, myrect, 20, 30, 240, 160, 'resize([2*width], [2*height]) should scale twice preserving top-left corner as it was');
myrect.resize(120, 80);
checkBbox(this.paper, myrect, 20, 30, 120, 80, 'resize([orig width], [orig height]) should scale back to the original size and position');
});
test('rotate()', function() {
var myrect = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(myrect);
myrect.rotate(90);
checkBbox(this.paper, myrect, 40, 10, 80, 120, 'rotate(90) should rotate the object by 90 degrees around its center');
myrect.rotate(-90);
checkBbox(this.paper, myrect, 20, 30, 120, 80, 'rotate(-90) should rotate the object back to its original angle');
// Rotation around an origin.
myrect.rotate(180, false, { x: 140, y: 70 });
checkBbox(this.paper, myrect, 140, 30, 120, 80, 'rotate(180, 140, 70) should rotate the object around the middle of its right edge');
myrect.rotate(180, true, { x: 140, y: 70 });
checkBbox(this.paper, myrect, 140, 30, 120, 80, 'rotate(180, 140, 70) with absolute flag should not rotate the object as it is already rotated');
});
test('object reconstruction after several transformations', function() {
var r1 = new joint.shapes.basic.Rect({
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(r1);
r1.resize(150, 60);
r1.rotate(45);
r1.translate(20, 30);
r1.rotate(15);
r1.resize(20, 30);
r1.rotate(45, true);
r1.rotate(-15);
var r2 = r1.clone();
this.graph.addCell(r2);
var r1View = this.paper.findViewByModel(r1);
var r2View = this.paper.findViewByModel(r2);
var r1Bbox = g.rect(r1View.getBBox()).round();
var r2Bbox = g.rect(r2View.getBBox()).round();
deepEqual(
{ x: r1Bbox.x, y: r1Bbox.y, width: r1Bbox.width, height: r1Bbox.height },
{ x: r2Bbox.x, y: r2Bbox.y, width: r2Bbox.width, height: r2Bbox.height },
'bounding box of the clone of an element that has been severely transformed is the same as of the original element'
);
});
test('attr()', function() {
var el = new joint.shapes.basic.Generic({
position: { x: 20, y: 30 },
markup: '<rect class="big"/><rect class="small"/>',
attrs: {
'.big': { width: 100, height: 50, fill: 'gray' },
'.small': { width: 10, height: 10, fill: 'red' }
}
});
this.graph.addCell(el);
var elView = this.paper.findViewByModel(el);
equal(elView.$('.big').attr('opacity'), undefined, 'No opacity is set on the element');
el.attr({ '.big': { opacity: .5 } });
equal(elView.$('.big').attr('opacity'), .5, '.5 opacity was correctly set by attr()');
});
test('removeAttr()', function() {
var el = new joint.shapes.basic.Generic({
position: { x: 20, y: 30 },
markup: '<rect class="big"/><rect class="small"/>',
attrs: {
'.big': { width: 100, height: 50, fill: 'gray', stroke: 'pink' },
'.small': { width: 10, height: 10, fill: 'red' }
}
});
this.graph.addCell(el);
var elView = this.paper.findViewByModel(el);
equal(elView.$('.big').attr('stroke'), 'pink', 'A stroke is set on the element');
el.removeAttr('.big/stroke');
equal(elView.$('.big').attr('stroke'), undefined, 'The stroke was correctly unset from the element by removeAttr()');
var link = new joint.dia.Link({
source: { x: 100, y: 100 },
target: { x: 200, y: 200 },
attrs: {
'.connection': { width: 100, height: 50, fill: 'gray', 'stroke-width': 2 }
}
});
this.graph.addCell(link);
var linkView = this.paper.findViewByModel(link);
equal(linkView.$('.connection').attr('stroke-width'), '2', 'A stroke is set on the link');
link.removeAttr('.connection/stroke-width');
equal(linkView.$('.connection').attr('stroke-width'), undefined, 'The stroke was correctly unset from the link by removeAttr()');
});
test('prop()', function() {
var el = new joint.shapes.basic.Rect({
flat: 5,
object: { nested: { value: 'foo' }, nested2: { value: 'bar' } },
array: [[5], [{ value: ['bar'] }]],
a: { b: { c: 1 }}
});
equal(el.prop('flat'), 5, 'flat value returned in getter');
equal(el.prop('object/nested/value'), 'foo', 'nested object value returned in getter');
deepEqual(el.prop('array/0'), [5], 'nested array returned in getter');
equal(el.prop('array/0/0'), 5, 'value in nested array returned in getter');
deepEqual(el.prop('array/1/0/value'), ['bar'], 'object in nested array returned in getter');
equal(el.prop('array/1/0/value/0'), 'bar', 'value in nested object in nested array returned in getter');
el.prop('array/1/0/value/0', 'baz');
equal(el.prop('array/1/0/value/0'), 'baz', 'value in nested object in nested array set correctly');
ok(_.isArray(el.prop('array/1/0/value')), 'type of the nested array was preserved');
ok(_.isObject(el.prop('array/1/0')), 'type of the nested object was preserved');
ok(_.isArray(el.prop('array/1')), 'type of the nested array was preserved');
ok(_.isArray(el.prop('array')), 'type of the top level array was preserved');
el.prop('array/1/0/value', { s: 'baz' });
deepEqual(el.prop('array/1/0/value'), { s: 'baz' }, 'value in nested object in nested array set correctly');
ok(_.isObject(el.prop('array/1/0/value')), 'type of the object was changed');
el.prop('array/2', 10);
ok(_.isArray(el.prop('array')), 'type of the top level array was preserved after adding new item');
equal(el.prop('array/2'), '10', 'value of the newly added array item is correct');
el.prop({ array: [['foo']] });
ok(_.isArray(el.prop('array')), 'type of the top level array was preserved after changing an item');
equal(el.prop('array/0/0'), 'foo', 'value of the newly added array item is correct');
ok(_.isArray(el.prop('array/0')), 'type of the nested array is correct');
var called = false;
el.once('change:array', function(cell, changed, opt) {
ok(opt.flag, 'options object was correctly passed in path syntax of prop');
called = true;
});
el.prop('array/0', 'something', { flag: true });
ok(called, 'on change callback with options passed was called');
called = false;
el.once('change:array', function(cell, changed, opt) {
ok(opt.flag, 'options object was correctly passed in object syntax of prop');
called = true;
});
el.prop({ array: ['something else'] }, { flag: true });
ok(called, 'on change callback with options passed was called');
el.prop('object/nested', 'baz');
deepEqual(el.prop('object/nested2'), { value: 'bar' }, 'value in untouched nested object was preserved');
equal(el.prop('object/nested'), 'baz', 'value in nested object was changed');
el.prop('a/b', { d: 2 }, { rewrite: true });
deepEqual(el.prop('a/b'), { d: 2 }, 'rewrite mode doesn\'t merge values');
});
test('removeProp()', function() {
expect(4);
var el = new joint.dia.Cell({
flat: 6,
nested: { a: 4, b: 5 }
});
el.removeProp('NonExisting');
deepEqual(el.attributes, {
id: el.id,
flat: 6,
nested: { a: 4, b: 5 }
}, 'Removing a non-existing property won\'t affect the model\'s attributes.');
el.removeProp('flat');
ok(!el.has('flat'), 'A flat property was unset from the model.');
el.removeProp('nested/a');
deepEqual(el.get('nested'), { b: 5 }, 'A nested property was unset from the model.');
el.on('change', function(cell, opt) {
ok(opt.OPT_PRESENT, 'Options are propagated to the underlying model method.');
});
el.removeProp('nested/b', { OPT_PRESENT: true });
});
test('toBack(), toFront()', function() {
var r1 = new joint.shapes.basic.Rect;
var r2 = new joint.shapes.basic.Rect;
this.graph.addCell(r1);
this.graph.addCell(r2);
var r1View = this.paper.findViewByModel(r1);
var r2View = this.paper.findViewByModel(r2);
notEqual(r2View.$el.prevAll(r1View.$el).length, 0, 'r1 element is before r2 element in the DOM');
r1.toFront();
equal(r2View.$el.prevAll(r1View.$el).length, 0, 'r1 element moved after r2 element in the DOM after toFront()');
r1.toBack();
notEqual(r2View.$el.prevAll(r1View.$el).length, 0, 'r1 element moved back before r2 element in the DOM after toBack()');
});
test('toBack(), toFront() with { deep: true } option', function() {
var a1 = new joint.shapes.basic.Rect;
var a2 = new joint.shapes.basic.Rect;
var a3 = new joint.shapes.basic.Rect;
var a4 = new joint.shapes.basic.Rect;
a1.embed(a2).embed(a3.embed(a4));
var b1 = new joint.shapes.basic.Rect;
var b2 = new joint.shapes.basic.Rect;
this.graph.addCells([b1, a1, a2, a3, a4, b2]);
var a1View = this.paper.findViewByModel(a1);
var a2View = this.paper.findViewByModel(a2);
var a3View = this.paper.findViewByModel(a3);
var a4View = this.paper.findViewByModel(a4);
var b1View = this.paper.findViewByModel(b1);
var b2View = this.paper.findViewByModel(b2);
equal(b2View.$el.nextAll('.basic.Rect').length, 0, 'element b2 after a1 element in the DOM');
equal(b1View.$el.prevAll('.basic.Rect').length, 0, 'element b1 before a1 element in the DOM');
a1.toFront({ deep: true });
equal(_.unique(a1View.$el.prevAll('.basic.Rect').toArray().concat([b1View.el, b2View.el])).length, 2, 'a1 element moved after b1,b2 element in the DOM after toFront()');
ok(a4View.$el.prev('.basic.Rect')[0] == a3View.el || a4View.$el.prev('.basic.Rect')[0] == a2View.el, 'and a4 element moved after a3 or a2 element');
ok(a2View.$el.prev('.basic.Rect')[0] == a1View.el || a3View.$el.prev('.basic.Rect')[0] == a1View.el, 'and a2 or a3 element moved just after a1 element');
a1.toBack({ deep: true });
equal(a1View.$el.prevAll('.basic.Rect').length, 0, 'a1 element moved back before a2,a3,a4,b1,b2 elements in the DOM after toBack()');
ok(a4View.$el.prev('.basic.Rect')[0] == a3View.el || a4View.$el.prev('.basic.Rect')[0] == a2View.el, 'and a4 element moved after a3 or a2 element');
ok(a2View.$el.prev('.basic.Rect')[0] == a1View.el || a3View.$el.prev('.basic.Rect')[0] == a1View.el, 'and a2 or a3 element moved just after a1 element');
});
test('clone()', function() {
var r1 = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(r1);
var r2 = r1.clone();
this.graph.addCell(r2);
var textEls = this.paper.svg.getElementsByTagName('text');
var rectEls = this.paper.svg.getElementsByTagName('rect');
equal(textEls.length, 2, 'there are exactly two <text> elements in the paper');
equal(rectEls.length, 2, 'there are exactly two <rect> elements in the paper');
equal(textEls[0].textContent, 'my rectangle', 'text element has a proper content');
equal(textEls[1].textContent, 'my rectangle', 'text element of the cloned element has a proper content');
checkBbox(this.paper, r2, 20, 30, 120, 80, 'cloned element is at the exact same position as the original element');
// Check correct offset of the element when translate() is called before appending the element to the paper.
// This is critical as in this situation, render() is called after translate() and should therefore
// reset the transformation attribute of the element.
var r3 = r1.clone();
r3.translate(50);
this.graph.addCell(r3);
checkBbox(this.paper, r3, 70, 30, 120, 80, 'cloned element is offset by 50px to the right of the original element if translate() was called before appending it to the paper');
// Deep clone.
r1.embed(r2);
var l = new joint.dia.Link({ source: { id: r1.id }, target: { id: r2.id } });
this.graph.addCell(l);
var clones = r1.clone({ deep: true });
equal(clones.length, 3, 'deep clone returned three clones for a parent element with one child connected with a link');
equal(clones[0].id, clones[1].get('parent'), 'clone of the embedded element gets a parent attribute set to the clone of the parent element');
});
test('embed(), unembed()', function() {
var r1 = new joint.shapes.basic.Rect({
position: { x: 20, y: 30 },
size: { width: 120, height: 80 },
attrs: { text: { text: 'my rectangle' } }
});
this.graph.addCell(r1);
var r2 = r1.clone();
this.graph.addCell(r2);
r1.embed(r2);
r1.translate(50);
checkBbox(this.paper, r1, 70, 30, 120, 80, 'translate(50) should translate the parent element by 50px');
checkBbox(this.paper, r2, 70, 30, 120, 80, 'embedded element should translate the same as the parent element');
equal(r2.get('parent'), r1.id, 'embedded element gains the parent attribute pointing to its parent cell');
r1.unembed(r2);
r1.translate(-50);
checkBbox(this.paper, r1, 20, 30, 120, 80, 'translate(-50) should translate the parent element by -50px');
checkBbox(this.paper, r2, 70, 30, 120, 80, 'unembedded element should stay at the same position when its old parent got translated');
equal(r2.get('parent'), undefined, 'embedded element gets its parent attribute pointing to its parent cell removed');
r1.embed(r2);
r2.remove();
deepEqual(r1.get('embeds'), [], 'embedded element got removed from the embeds array of its parent when the embedded element remove() was called.');
});
test('isEmbeddedIn()', function() {
var r1 = new joint.shapes.basic.Rect;
var r2 = r1.clone();
var r3 = r1.clone();
r1.embed(r2);
r2.embed(r3);
this.graph.addCells([r1,r2,r3]);
ok(!r1.isEmbeddedIn(r1), 'We have 3 elements. r3 is embedded in r2, r2 is embedded in r1. | r1 is not child of r1. ');
ok(r2.isEmbeddedIn(r1), 'r2 is descendent of r1');
ok(r3.isEmbeddedIn(r1), 'r3 is descendent of r1');
ok(r3.isEmbeddedIn(r1, { deep: false }), 'r3 is not direct child of r1 (option { deep: false })');
ok(!r1.isEmbeddedIn(r3), 'r1 is not descendent of r3');
});
test('findMagnet()', function() {
var r1 = new joint.shapes.basic.Rect({
attrs: { text: { text: 'my\nrectangle' } }
});
this.graph.addCell(r1);
var r1View = this.paper.findViewByModel(r1);
var magnet = r1View.findMagnet('tspan');
equal(magnet, r1View.el, 'should return the root element of the view if there is no subelement with magnet attribute set to true');
r1.attr({ text: { magnet: true } });
magnet = r1View.findMagnet('tspan');
equal(magnet, r1View.$('text')[0], 'should return the text element that has the magnet attribute set to true even though we passed the child <tspan> in the selector');
});
test('getSelector()', function() {
var r1 = new joint.shapes.basic.Rect;
this.graph.addCell(r1);
var r1View = this.paper.findViewByModel(r1);
var selector = r1View.getSelector(r1View.$('text')[0]);
equal(r1View.$(selector)[0], r1View.$('text')[0], 'applying the selector returned from getSelector() should point to the selected element');
});
test('ref-x, ref-y, ref', function() {
var el = new joint.shapes.basic.Generic({
markup: '<rect class="big"/><rect class="small"/><rect class="smaller"/>',
attrs: {
'.big': { width: 100, height: 50, fill: 'gray' },
'.small': { width: 10, height: 10, 'ref-x': 20, 'ref-y': 10, fill: 'red' },
'.smaller': { width: 5, height: 5, 'ref-x': 20, 'ref-y': 10, ref: '.small', fill: 'black' }
}
});
this.graph.addCell(el);
var elView = this.paper.findViewByModel(el);
// Range [1, x]
var smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
deepEqual(
{ x: smallRectBbox.x, y: smallRectBbox.y, width: smallRectBbox.width, height: smallRectBbox.height },
{ x: 20, y: 10, width: 10, height: 10 },
'ref-x: 20, ref-y: 10 attributes should offset the element by 20px in x axis and 10px in y axis'
);
// Range [0, 1]
el.attr({ '.small': { 'ref-x': .5, 'ref-y': .5 } });
smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
deepEqual(
{ x: smallRectBbox.x, y: smallRectBbox.y, width: smallRectBbox.width, height: smallRectBbox.height },
{ x: 50, y: 25, width: 10, height: 10 },
'ref-x: .5, ref-y: .5 attributes should position the element in the center, i.e. at [50, 25] coordinate'
);
// Range [-x, 0]
el.attr({ '.small': { 'ref-x': -10, 'ref-y': -15 } });
smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
deepEqual(
{ x: smallRectBbox.x, y: smallRectBbox.y, width: smallRectBbox.width, height: smallRectBbox.height },
{ x: -10, y: -15, width: 10, height: 10 },
'ref-x: -10, ref-y: -15 attributes should offset the element from the left by 10px and from the top by 15px'
);
var smallerRectBbox = V(elView.$('.smaller')[0]).bbox(false, elView.el);
deepEqual(
{ x: smallerRectBbox.x, y: smallerRectBbox.y, width: smallerRectBbox.width, height: smallerRectBbox.height },
{ x: smallRectBbox.x + 20, y: smallRectBbox.y + 10, width: 5, height: 5 },
'ref-x: 20, ref-y: 10 and ref set to .small should offset the element by 20px in x axis and 10px in y axis with respect to the x-y coordinate of the .small element'
);
});
test('ref-dx, ref-dy, ref', function() {
var el = new joint.shapes.basic.Generic({
markup: '<rect class="big"/><rect class="small"/><rect class="smaller"/>',
attrs: {
'.big': { width: 100, height: 50, fill: 'gray' },
'.small': { width: 10, height: 10, 'ref-dx': 20, 'ref-dy': 10, fill: 'red' },
'.smaller': { width: 5, height: 5, 'ref-dx': 10, 'ref-dy': 10, ref: '.small', fill: 'black' }
}
});
this.graph.addCell(el);
var elView = this.paper.findViewByModel(el);
var smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
deepEqual(
{ x: smallRectBbox.x, y: smallRectBbox.y, width: smallRectBbox.width, height: smallRectBbox.height },
{ x: 120, y: 60, width: 10, height: 10 },
'ref-dx: 20, ref-dy: 10 attributes should offset the element by 20px in x axis and 10px in y axis with respect to the right-bottom coordinate of the ref element'
);
var smallerRectBbox = V(elView.$('.smaller')[0]).bbox(false, elView.el);
deepEqual(
{ x: smallerRectBbox.x, y: smallerRectBbox.y, width: smallerRectBbox.width, height: smallerRectBbox.height },
{ x: smallRectBbox.x + smallRectBbox.width + 10, y: smallRectBbox.y + smallRectBbox.height + 10, width: 5, height: 5 },
'ref-dx: 10, ref-dy: 10 with ref set to .small should offset the element by 10px in x axis and 10px in y axis with respect to the right-bottom coordinate of the .small element'
);
});
test('ref-width, ref-height', function() {
var el = new joint.shapes.basic.Generic({
markup: '<rect class="big"/><rect class="small"/><rect class="smaller"/>',
attrs: {
'.big': { width: 100, height: 50, fill: 'gray' },
'.small': { 'ref-width': .5, 'ref-height': .4, ref: '.big', fill: 'red' },
'.smaller': { 'ref-width': 10, 'ref-height': -10, ref: '.small', fill: 'black' }
}
});
this.graph.addCell(el);
var elView = this.paper.findViewByModel(el);
var smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
// Range [0, 1]
deepEqual(
{ width: smallRectBbox.width, height: smallRectBbox.height },
{ width: 50, height: 20 },
'ref-width: .5, ref-height: .4 attributes should set the element size to 50x20.'
);
var smallerRectBbox = V(elView.$('.smaller')[0]).bbox(false, elView.el);
// Range [-x, 0] && [1, x]
deepEqual(
{ width: smallerRectBbox.width, height: smallerRectBbox.height },
{ width: 60, height: 10 },
'ref-width: 10, ref-height: -10 attributes referenced to the previous element should set the element size to 60x10.'
);
// Margin value 1
el.attr({ '.small': { 'ref-width': 1, 'ref-height': 1 } });
smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
deepEqual(
{ width: smallRectBbox.width, height: smallRectBbox.height },
{ width: 100, height: 50 },
'ref-width: 1, ref-height: 1 attributes element should set the exact referenced element size 100x50.'
);
el.attr({ '.small': { 'ref-width': 0 } });
smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
ok(smallRectBbox.width === 0, 'ref-width: 0 attribute element should set its width to 0.');
});
test('x-alignment, y-alignment', function() {
var el = new joint.shapes.basic.Generic({
markup: '<rect class="big"/><rect class="small"/>',
attrs: {
'.big': { width: 100, height: 50, fill: 'gray' },
'.small': { width: 20, height: 20, 'ref-x': .5, 'ref-y': .5, 'y-alignment': 'middle', 'x-alignment': 'middle', fill: 'red' }
}
});
this.graph.addCell(el);
var elView = this.paper.findViewByModel(el);
var smallRectBbox = V(elView.$('.small')[0]).bbox(false, elView.el);
deepEqual(
{ x: smallRectBbox.x, y: smallRectBbox.y, width: smallRectBbox.width, height: smallRectBbox.height },
{ x: 40, y: 15, width: 20, height: 20 },
'ref-x: .5, ref-y: .5, x-alignment: middle, y-alignment: middle aligns the element center with the root center'
);
});
test('gradient', function() {
var el = new joint.shapes.basic.Rect;
this.graph.addCell(el);
var elView = this.paper.findViewByModel(el);
var defs = this.paper.svg.querySelector('defs');
var defsChildrenCount = $(defs).children().length;
equal(defsChildrenCount, 0, 'there is no element in the <defs> by default.');
el.attr('rect/fill', {
type: 'linearGradient',
stops: [
{ offset: '0%', color: 'red' },
{ offset: '20%', color: 'blue' }
]
});
// PhantomJS fails to lookup linearGradient with `querySelectorAll()` (also with jQuery).
// Therefore, we use the following trick to check whether the element is in DOM.
defsChildrenCount = $(defs).children().length;
equal(defsChildrenCount, 1, 'one element got created in <defs>.');
var linearGradient = $(defs).children()[0];
equal(linearGradient.tagName.toLowerCase(), 'lineargradient', 'one <linearGradient> element got created in <defs>.');
equal('url(#' + linearGradient.id + ')', elView.$('rect').attr('fill'), 'fill attribute pointing to the newly created gradient with url()');
el.attr('rect/stroke', {
type: 'linearGradient',
stops: [
{ offset: '0%', color: 'red' },
{ offset: '20%', color: 'blue' }
]
});
defsChildrenCount = $(defs).children().length;
equal(defsChildrenCount, 1, 'one element is in <defs>.');
linearGradient = $(defs).children()[0];
equal(linearGradient.tagName.toLowerCase(), 'lineargradient', 'still only one <linearGradient> element is in <defs>.');
equal('url(#' + linearGradient.id + ')', elView.$('rect').attr('stroke'), 'stroke attribute pointing to the correct gradient with url()');
});
test('filter', function() {
var el = new joint.shapes.basic.Rect;
var el2 = new joint.shapes.basic.Rect;
this.graph.addCells([el, el2]);
var elView = this.paper.findViewByModel(el);
var el2View = this.paper.findViewByModel(el2);
var defs = this.paper.svg.querySelector('defs');
var defsChildrenCount = $(defs).children().length;
equal(defsChildrenCount, 0, 'there is no element in the <defs> by default.');
el.attr('rect/filter', { name: 'dropShadow', args: { dx: 2, dy: 2, blur: 3 } });
// PhantomJS fails to lookup linearGradient with `querySelectorAll()` (also with jQuery).
// Therefore, we use the following trick to check whether the element is in DOM.
defsChildrenCount = $(defs).children().length;
equal(defsChildrenCount, 1, 'one element got created in <defs>.');
var filter = $(defs).children()[0];
equal(filter.tagName.toLowerCase(), 'filter', 'one <filter> element got created in <defs>.');
equal('url(#' + filter.id + ')', elView.$('rect').attr('filter'), 'filter attribute pointing to the newly created filter with url()');
el2.attr('rect/filter', { name: 'dropShadow', args: { dx: 2, dy: 2, blur: 3 } });
defsChildrenCount = $(defs).children().length;
equal(defsChildrenCount, 1, 'one element still in <defs>.');
filter = $(defs).children()[0];
equal(filter.tagName.toLowerCase(), 'filter', 'still only one <filter> element is in <defs>.');
equal('url(#' + filter.id + ')', el2View.$('rect').attr('filter'), 'filter attribute pointing to the correct gradient with url()');
el.attr('rect/filter', { name: 'blur', args: { x: 5 } });
defsChildrenCount = $(defs).children().length;
equal(defsChildrenCount, 2, 'now two elements are in <defs>.');
var filter0 = $(defs).children()[0];
var filter1 = $(defs).children()[1];
deepEqual([filter0.tagName.toLowerCase(), filter1.tagName.toLowerCase()], ['filter', 'filter'], 'both elements in <defs> are <filter> elements.');
notEqual(filter0.id, filter1.id, 'both <filter> elements have different IDs');
equal('url(#' + filter0.id + ')', el2View.$('rect').attr('filter'), 'filter attribute pointing to the correct gradient with url()');
equal('url(#' + filter1.id + ')', elView.$('rect').attr('filter'), 'filter attribute pointing to the correct gradient with url()');
});
asyncTest('transition: sanity', 5, function() {
var p0 = true, p1 = true, p2 = true;
var el = new joint.shapes.basic.Rect({
property: 1
});
this.graph.addCell(el);
el.transition('property', 3, {
valueFunction: function(a, b) {
equal(a, 2, 'The method passes the current value to a valueFunction as start.');
equal(b, 3, 'The method passes the requested value to a valueFunction as end.');
return function(t) {
if (t < .1 && p0) {
ok(true, 'Transition starts.');
p0 = false;
};
if (t > .1 && t < .9 && p1) {
ok(true, 'Transition runs.');
p1 = false;
}
if (t > .9 && p2) {
ok(true, 'Transition ends.');
p2 = false;
start();
}
return 0;
}
}
});
el.set('property', 2);
});
asyncTest('transition: primitive value', function() {
var el = new joint.shapes.basic.Rect({
timer: -1
});
this.graph.addCell(el);
var timerArray = [];
el.transition('timer', 100, {
delay: 100,
duration: 100,
valueFunction: function(a, b) { return function(t) { return t; }}
});
el.on('change:timer', function(cell, changed) { timerArray.push(changed); });
setTimeout(function() {
var timerMedian = timerArray[Math.floor(timerArray.length / 2)];
equal(el.get('timer'), 1, 'The transition sets the primitive property.');
deepEqual(timerArray.sort(), timerArray, 'The transition changes the primitive property gradually, ');
ok(0 < timerMedian && timerMedian < 1, 'The transition median value is between min and max.');
start();
}, 300);
});
asyncTest('transition: nested value', function() {
var el = new joint.shapes.basic.Rect({
nested: {
timer: -1,
other: 'nochange'
}
});