-
Notifications
You must be signed in to change notification settings - Fork 8
/
lenses-freeform.html
1565 lines (1124 loc) · 49.2 KB
/
lenses-freeform.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../lens-data-table/lens-data-table.html">
<link rel="import" href="../lenses-component-list/lenses-component-list.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../core-overlay/core-overlay.html">
<polymer-element name="lenses-freeform" attributes="structure" on-keydown="{{keydownHandler}}" on-keyup="{{keyupHandler}}" on-mouseout="mouseoutHandler">
<template>
<link rel="stylesheet" href="lenses-freeform.css">
<div class="settings-slideout slideout right">
<span class="slideout-handle">settings</span>
<div class="slideout-inner">
<core-icon-button id="showDataBtn" class="active" icon="view-module" on-click="{{showDataBtn}}" alt="show component data">
<span>show component input/output</span>
</core-icon-button>
<!--
<core-icon-button id="saveBtn" icon="save" on-click="{{showSaveDialog}}" alt="save">
</core-icon-button>
-->
</div>
</div>
<div class="zoom-slideout slideout left">
<span class="slideout-handle">zoom</span>
<div class="slideout-inner">
<div id="zoomOptions">
<core-icon-button id="zoomInBtn" class="" icon="add" on-click="{{zoomIn}}">
</core-icon-button>
<br/>
<input is="core-input" id="zoomRange" type="range" value="{{_zoom}}" min="{{_zoomMin}}" max="{{_zoomMax}}" step="{{_zoomStep}}" style="width:25px" list=zoomsettings>
</input>
<datalist id="zoomsettings">
<option>1</option>
<option>0.8</option>
<option>0.6</option>
</datalist>
<br/>
<core-icon-button id="zoomOutBtn" icon="remove" on-click="{{zoomOut}}">
</core-icon-button>
</div>
</div>
</div>
<div class="component-slideout slideout right">
<span class="slideout-handle">components</span>
<div class="slideout-inner">
<label class="label">Drag components into canvas:</label>
<lenses-component-list id="component_list"></lenses-component-list>
</div>
</div>
<div class="output-slideout slideout right">
<span class="slideout-handle">output components</span>
<div class="slideout-inner">
<label class="label">Select output component:</label>
<template if="{{_elements.length===0}}">
<div>No component in canvas!</div>
</template>
<core-selector class="output-selector" selected="{{_outputComponents}}" valueattr="label" multi="true">
<template repeat="{{el, index in _elements}}">
<div class="col" label="{{el.id}}">{{el.id}}</div>
</template>
</core-selector>
</div>
</div>
<div id="input_preview" class="data-preview {{{visible: _showDataTable && _selElement.input && _selElement.input.length>0 } | tokenList }}">
<template if="{{_showDataTable && _selElement.input && _selElement.input.length>0}}">
<label>INPUT</label>
<lens-data-table id="input_table" input="{{_selElement.input}}" class="table" minimal="true"></lens-data-table>
</template>
</div>
<div id="container" on-mousedown="{{clickInContainer}}" on-drop="{{droppedInContainer}}" on-dragover="{{draggedOverContainer}}" on-mouseup="{{dragEndSVG}}" on-mousemove="{{dragSVG}}">
<!-- <svg id="svg" style="z-index:1000;"></svg>
--> <svg id="svg"></svg>
<!-- Components and connector dots -->
<!--<template if="{{_positioned}}">-->
<template repeat="{{el, index in _elements}}">
<div class="wrapper {{el.name}}" id="wrapper-{{el.id}}" component-id="{{el.id}}" on-click="{{wrapperClicked}}"
style="top: {{el.top}}px; left: {{el.left}}px; transform: {{el.transform}}; width: {{el.width}}px; height: {{el.height}}px"
on-click="{{wrapperClicked}}" on-dragStart="{{dragStartWrapper}}" on-drag="{{dragWrapper}}" on-dragend="{{dragEndWrapper}}" draggable="true"><!-- on-mouseover="{{dragOverWrapper}}" on-drop="{{droppedOnInArrow}}" needed? -->
<label>{{el.id}}</label>
<div class="deleteEl" component_id="{{el.element.id}}" on-click="{{_deleteElementEvent}}">X</div>
<div class="connectors-in" >
<!-- Input -->
<ul>
<template repeat="{{ins in el.element.publish | _checkForInputAttr}}">
<li class="{{el.element.id}}-{{ins}} arrow in-arrow" on-dragover="{{dragOverInArrow}}"
on-drop="{{droppedOnInArrow}}" on-dragenter="{{dragEnterInArrow}}" on-dragleave="{{dragLeaveInArrow}}" on-click="{{arrowClicked}}">
<div class="inputMarker">
<div class="delete" on-click="{{deleteConnection}}">x</div>
<span class="drop-zone">{{ins}}</span>
</div>
</li>
</template>
</ul>
<!-- Output -->
</div>
<!-- for components with settings (all non-resizables?) -->
<template if="{{!el.resizable}}">
<core-icon-button icon="settings" class="setting-btn {{ {open: el.settingOpen } | tokenList}}"
collapse-id="collapse-{{el.id}}" component-id="{{el.id}}"
on-click="{{toggleSetting}}"></core-icon-button>
</template>
<!-- for resizable components-->
<template if="{{el.resizable}}">
<content select="#{{el.id}}"></content>
</template>
<div class="connectors-out" >
<template if="{{el.element.publish | _checkForOutputAttr}}">
<div id="{{index}}" class="{{el.element.id}} arrow out-arrow"
on-drag="{{dragOutArrow}}" on-dragstart="{{dragOutArrowStart}}" on-dragend="{{dragOutArrowEnd}}"
on-click="{{arrowClicked}}" draggable="true">
<div class="outputMarker"></div>
</div>
</template>
</div>
<template if="{{el.resizable}}">
<div class="resize" draggable="true" on-dragStart="{{dragStartResize}}" on-drag="{{dragResize}}" on-dragend="{{dragEndResize}}"></div>
</template>
</div>
<template if="{{!el.resizable}}">
<core-collapse class="settings-collapse" id="collapse-{{el.id}}" opened="{{el.settingOpen}}"
style="top: {{el.top+35}}px; left: {{el.left}}px; transform: {{el.transform}}; width: {{el.width}}px; height: {{el.height}}px">
<content select="#{{el.id}}"></content>
</core-collapse>
</template>
</template>
</div>
<div id="output_preview"
class="data-preview {{{visible: _showDataTable && _selElement.output && _selElement.output.length>0 } | tokenList }}">
<template if="{{_showDataTable && _selElement.output && _selElement.output.length>0}}">
<label>OUTPUT</label>
<lens-data-table id="output_table" input="{{_selElement.output}}" class="table" minimal="true"></lens-data-table>
</template>
</div>
<core-overlay autoCloseDisabled id="deleteDialog" class="dialog size-position">
<p>Are you sure you want to remove the selected item?</p>
<div horizontal end-justified layout>
<core-icon-button icon="clear" dismissive on-click="{{_noDelete}}" ></core-icon-button>
<core-icon-button icon="check" affirmative on-click="{{_yesDelete}}" ></core-icon-button>
</div>
</core-overlay>
<!-- Title and Author fields + save action in dialog-->
<core-overlay class="dialog" id="savedetails">
<h1><paper-input label="Title of Lens" value="{{lensTitle}}"></paper-input></h1>
<h3><paper-input label="Your Name" value="{{lensAuthor}}"></paper-input></h3>
<core-icon-button class="save" affirmative icon="check" ng-click="saveLens()" on-click="{{_saveLens}}" ></core-icon-button>
<core-icon-button class="" dismissive icon="clear"></core-icon-button>
</core-overlay>
</template>
<script>
Polymer({
_elements: [],
_connections: [],
_selElement: null,
_selWrappers: [],
_highlightedConnection: null,
/**
* components which are saved for creating view
*/
_outputComponents: [],
_snapX: 10,
_snapY: 10,
_showDataTable: true,
_zoom: 1,
_zoomMax: 1,
_zoomMin: 0.6,
_zoomStep: 0.2,
/**
* An attribute to assign lens component structure. Needed for framework (e.g. Angular) integration
* @type {Object}
*/
structure: null,
// if dragging in container, drag area allows selection of multiple elements
_makeDragArea: false,
// svg rect of drag area
_dragArea: null,
// state to determine whether we are selecting or de-selecting elements during multi-drag
_selectingElByDrag: false,
// which keys are down
_hotKeys: {
'18' : false, // 18 = option
'16' : false, // 16 = shift
'17' : false, // 17 = control
'91' : false // 91 = command
},
observe: {
structure: 'scaffold'
},
// if delete = true when deleteDialog is closed, it will call _deleteCallback()
_deleteCallback: null,
domReady: function() {
this.addEventListener('core-resize', function() {
this._drawConnections();
});
this.$.zoomRange.value = this._zoom;
},
ready: function() {
this.focus();
this.tabIndex = 0;
// for testing:
// window.ff = this;
},
addNewElToLens: function(newElName, left, top){
var newEl = document.createElement(newElName);
this.$.component_list.checkAndImport(newEl, function() {
newEl.style.width = '100%';
newEl.style.height = '100%';
this.appendChild(newEl);
this._processNewElement(newEl, top/this._zoom, left/this._zoom);
}.bind(this));
},
_processNewElement: function(newEl, top, left) {
newEl.id = this._generateUniqueId(newEl.tagName.toLowerCase());
var elItem = { element: newEl,
id: newEl.id,
name: newEl.tagName.toLowerCase(),
top: top,
left: left,
//trying to use transform instead of top/left
//transform: 'translate(' + top + 'px, ' + left + 'px)'
};
var info = this.$.component_list.getComponentInfo(newEl.tagName.toLowerCase());
if(info.resizable || info.category==='visualize') {
elItem.resizable = true;
elItem.width = 250;
elItem.height = 200;
}
this._elements.push(elItem);
},
// when delete button is clicked
_deleteElementEvent: function(e) {
var self = this;
var compId = e.target.getAttribute('component_id');
self._deleteElementById(compId);
},
_deleteElementById: function(compId) {
var self = this;
var compId = compId;
self.$.deleteDialog.toggle();
// set the delete callback
self._deleteCallback = function() {
var ebmededEl = self.querySelector('#'+compId);
var elInArray = self.findElById(compId);
//remove all connections
self.removeAllConnectionsFromElement(compId)
// remove element from elements array (which removes it from the shadowDOM)
var filteredEls = self._elements.filter(function(el) {
return el.element.id!==compId;
});
self._elements = filteredEls;
// remove elements from lens-connector
self.removeChild(embeddedEl);
}
},
removeAllConnectionsFromElement: function(elId) {
var goodConnections = this._connections.filter(function (conn, index) {
//filter if element is source or target of a connection
return !(conn.source.id===elId || conn.target.id === elId)
});
this._connections = goodConnections;
this._drawConnections();
},
// if option key was pressed, clone the element when drag is done
_cloneElement: function(e, detail, selection) {
// figure out the name of the element...
var componentId = selection.getAttribute('component-id');
var idSplit = componentId.split('-');
var elNameArray = [];
var elName = '';
for (var i = 0; i < idSplit.length; i++) {
if (isNaN(Number(idSplit[i])) ) {
elNameArray.push(idSplit[i])
}
}
elName = elNameArray.join('-');
this.addNewElToLens(elName, e.x, e.y);
var dx = e.x - (e.toElement.getAttribute('start-x') || 0),
dy = e.y - (e.toElement.getAttribute('start-y')|| 0);
console.log(dx, dy);
// if other wrappers were selected, also clone those
for (var i = 0; i < this._selWrappers.length; i++) {
var target = this._selWrappers[i];
if (target === e.toElement) {
return;
} else {
var orig = target.getBoundingClientRect();
// find each element's distance from the e.toElement
var x = (orig.left + dx)/this._zoom;
var y = (orig.top + dy)/this._zoom;
this.addNewElToLens(elName, x, y);
}
}
},
/********************** Event Handlers Start ********************************/
draggedOverContainer: function(e) {
// Needed to enable droppedInContainer
e.preventDefault();
},
droppedInContainer: function(e, detail, selection){
var self = this;
//dont know why this method is being called instead of resizedragend?!
if(e.path && e.path.length>0 && e.path[0].classList.contains('resize')>0) {
self.dragResizeEnd(e, detail, selection);
}
var dataTransfer = e.dataTransfer.getData('text/plain');
//data added by lenses-component-list //TODO make better
if(dataTransfer && dataTransfer.length>0) {
var dataSeg = dataTransfer.split(',');
if(dataSeg.length==1) {
self.addNewElToLens(dataSeg[0], Math.round(e.offsetX/10)*10 * self._zoom, Math.round(e.offsetY/10)*10 * self._zoom);
return;
}
}
},
clickInContainer: function(e) {
var className = e.toElement.className;
if ( className.baseVal && className.baseVal.indexOf('cnxn') > -1 ) {
console.log('clicked on connection');
this.clickedOnConnection(e);
} else {
this._clearHighlightedConnections();
}
// start drag
if (e.target.id === 'container') {
this.dragStartSVG(e);
}
},
clickedOnConnection: function(e) {
var bezier = this.shadowRoot.querySelector('#'+e.toElement.id);
console.log(bezier);
if (String(bezier.classList).indexOf('highlight') > -1 ) {
// console.log('delete');
this._deleteBezier(e, bezier);
} else {
// if not highlighted, then highlight
this._clearHighlightedConnections();
bezier.classList.add('highlighted');
this._highlightedConnection = bezier;
}
},
_clearHighlightedConnections: function() {
if (this._highlightedConnection) {
this._highlightedConnection.classList.remove('highlighted');
this._highlightedConnection = null;
}
},
// clear all _selWrappers and _selElement
_clearSelElement: function() {
if(this._selWrappers) {
for (var i in this._selWrappers) {
this._selWrappers[i].classList.remove('selected-element');
}
}
this._selWrappers = [];
this._selElement = null;
},
dragStartResize: function(e, details, selection) {
e.stopPropagation();
},
dragResize: function(e, details, selection) {
var self = this;
var wrapper = selection.parentNode;
//var styles = window.getComputedStyle(wrapper);
// var wrapperTop = parseInt(styles.top.replace('px',''));
// var wrapperLeft = parseInt(styles.left.replace('px',''));
// var w = e.x - wrapperLeft - 65,
// h = e.y - wrapperTop - 172;
var wrapperTop = parseInt(wrapper.getBoundingClientRect().top),
wrapperLeft = parseInt(wrapper.getBoundingClientRect().left);
var w = e.x/this._zoom - wrapperLeft,
h = e.y/this._zoom - wrapperTop;
// wrapper.style.width = w +'px';
// wrapper.style.height = h +'px';
var elementInsideId = wrapper.getAttribute('component-id');//wrapper.id.replace('wrapper-','');
var elementInside = this.querySelector('#'+elementInsideId);
var element = this.findElById(elementInsideId);
element.width = w;
element.height = h;
self._drawConnections();
e.stopPropagation();
e.preventDefault();
},
dragResizeEnd: function(e, details, selection) {
var wrapper = null;
if(e.path && e.path.length>0 && e.path[0].classList.contains('resize')>0) {
wrapper = e.path[0].parentNode;
}
var elementInsideId = wrapper.id.replace('wrapper-','');
var elementInside = this.querySelector('#'+elementInsideId);
if(typeof elementInside.resize ==='function') {
elementInside.resize();
}
},
// _dragClone: function(e, details, selection) {
// // TO Do
// console.log(e.target);
// },
dragStartWrapper: function(e, details, selection) {
var target = e.target;
// make this the _selElement and _selWrapper
this.wrapperClicked(e, details, selection);
// set start-x and start-y for all _selWrappers
for (var i = 0; i < this._selWrappers.length; i++) {
var wrapper = this._selWrappers[i];
this._setWrapperDragStartingPoint(wrapper, e.x, e.y);
}
if (this._hotKeys['18']) {
// start dragging a clone
this._draggingClone = true;
return;
}
var dragImgEl = document.querySelector('#wrapper-drag-img');
if(!dragImgEl) {
dragImgEl = document.createElement('span');
dragImgEl.setAttribute('style','opacity: 0;' );
dragImgEl.id = 'wrapper-drag-img';
dragImgEl.textContent = 'a';
document.body.appendChild(dragImgEl);
}
//keep dragImage outside the canvas
e.dataTransfer.setDragImage(dragImgEl,-2000,-2000);
},
// set starting position of a drag for any target, not based on an event. Called by dragStartWrapper
_setWrapperDragStartingPoint: function(target, x, y) {
target.setAttribute('start-x', x);
target.setAttribute('start-y', y);
},
dragWrapper: function(e, details, selection) {
if (this._hotKeys['18']) {
// dragging a clone
return;
}
if(e.path[0].classList && e.path[0].classList.length>0 &&
(e.path[0].classList.contains('resize') || e.path[0].classList.contains('arrow'))) {
return false;
}
var target = e.target;
for (var i = 0; i < this._selWrappers.length; i++) {
var wrapper = this._selWrappers[i];
var dx = e.x/this._zoom - (wrapper.getAttribute('start-x')/this._zoom || 0),
dy = e.y/this._zoom - (wrapper.getAttribute('start-y')/this._zoom || 0);
this._updateWrapperLocation(e, wrapper, dx, dy);
}
this._drawConnections();
},
_updateWrapperLocation: function(e, target, dx, dy) {
// keep the dragged position in the data-x/data-y attributes, add snap residue from previous drag
var x = (parseFloat(target.getAttribute('data-x')) || 0) + dx + (parseFloat(target.getAttribute('residue-x')) || 0);
var y = (parseFloat(target.getAttribute('data-y')) || 0) + dy + (parseFloat(target.getAttribute('residue-y')) || 0);
var sx = Math.round(x/this._snapX)*this._snapX;
var sy = Math.round(y/this._snapY)*this._snapY;
target.setAttribute('residue-x', x - sx);
target.setAttribute('residue-y', y - sy);
// translate the element.
// last dragMove call passes e.x=0 and e.y=0 which causes neg x,y values. this if avoids that
if(e.x!==0 || e.y!==0)
{
var componentId = target.getAttribute('component-id');
this.findElById(componentId).transform = 'translate(' + sx + 'px, ' + sy + 'px)';
// update the posiion attributes
target.setAttribute('data-x', sx);
target.setAttribute('data-y', sy);
target.setAttribute('start-x', e.x);
target.setAttribute('start-y', e.y);
}
},
dragEndWrapper: function(e, details, selection) {
// console.log('done dragging!');
if (this._draggingClone) {
this._draggingClone = false;
this._cloneElement(e, details, selection);
return;
}
//if resize is being dragged don't do anything (stopPropagation is not doing anything)
var target = e.target;
target.setAttribute('residue-x', 0 );
target.setAttribute('residue-y', 0);
},
wrapperClicked: function(e, detail, selection) {
var wrapperEl = selection;
var elementId = wrapperEl.id;
elementId = elementId.substring(8, elementId.length);
var el = this.findElById(elementId).element
if (this._selElement != el) {
console.log('change');
console.log(this._selElement );
console.log(el);
// unhighlight other selected element if this was not one of them
if ( wrapperEl.classList.contains('selected-element') ) {
console.log('already selected');
} else {
this._clearSelElement();
console.log('not already selected');
}
this._selElement = el;
}
if (this._selWrappers.indexOf(wrapperEl) === -1) {
wrapperEl.classList.add('selected-element');
this._selWrappers.push(wrapperEl);
}
},
// SVG drag events, also called on mousedown
dragStartSVG: function(e, details, selection) {
console.log('hi');
// clear this._selWrappers [TO DO: unless Shift is clicked]
this._selWrappers = [];
this._makeDragArea = true;
this._selectingElByDrag = false;
this._dragArea = this._createDragArea(e, details, selection);
this.$.svg.insertBefore(this._dragArea, this.$.svg.firstChild);
},
// called when makeDragArea is true
dragSVG: function(e, details, selection) {
if (this._makeDragArea) {
this._updateDragArea(e, this._dragArea);
}
},
// also called on mouseup
dragEndSVG: function(e, details, selection) {
if (!this._dragArea) {
return;
}
// remember the old sel el
var oldSelElement = this._selElement;
// dimension of the rectangle:
var dragRect = this._dragArea.getBoundingClientRect();
// find any elements that intersect with the drag area, sort out which are _selElement and _selWrappers
for (var i in this._elements) {
var el = this._elements[i];
var wrapperEl = this.shadowRoot.querySelector('#wrapper-' + el.id);
// get element dimensions
var elRect = wrapperEl.getBoundingClientRect();
// compare and if there is overlap, add class
var overlap = this._boundingRectOverlap(elRect, dragRect);
if (overlap) {
// select elements that overlap
if (this._selWrappers.indexOf(wrapperEl) < 0) {
this._selWrappers.push(wrapperEl);
}
wrapperEl.classList.add('selected-element');
// remember selected element if it overlaps
if (el.element === oldSelElement) {
this._selElement = el.element;
}
}
else {
// otherwise remove selection
if ( wrapperEl.classList.contains('selected-element') ){
wrapperEl.classList.remove('selected-element');
} if (this._selElement === el.element) {
this._selElement = null;
}
}
}
this._clearDragArea();
},
// helper method
_boundingRectOverlap: function(rect1, rect2) {
var overlap = !(rect1.right < rect2.left ||
rect1.left > rect2.right ||
rect1.bottom < rect2.top ||
rect1.top > rect2.bottom)
return overlap;
},
// handle selecting elements by their wrapper during drag
dragOverWrapper: function(e, details, selection) {
// if (this._makeDragArea) {
// if (!this._selectingElByDrag) {
// this._selectingElByDrag = true;
// }
// console.log('drag over: ' + e.target.id);
// }
},
// create a rectangle to show drag area
_createDragArea: function(e) {
var x = e.x / this._zoom, y = e.y / this._zoom;
var svgns = "http://www.w3.org/2000/svg";
var rect = document.createElementNS(svgns, 'rect');
rect.setAttributeNS(null, 'opacity', 0.2);
rect.setAttributeNS(null, 'x', x);
rect.setAttributeNS(null, 'y', y);
rect.setAttributeNS(null, 'width', '0');
rect.setAttributeNS(null, 'height', '0');
rect.setAttributeNS(null, 'fill', '#D1D1D1');
rect.startX = x;
rect.startY = y;
return rect;
},
// update drag rectangle
_updateDragArea: function(e, rect) {
var svgns = "http://www.w3.org/2000/svg";
var startX = rect.startX;
var startY = rect.startY;
var deltaX = e.x/this._zoom - startX;
var deltaY = e.y/this._zoom - startY;
if (deltaX < 0) {
rect.setAttributeNS(null, 'x', startX + deltaX);
deltaX = Math.abs(deltaX);
}
if (deltaY < 0) {
rect.setAttributeNS(null, 'y', startY + deltaY);
deltaY = Math.abs(deltaY);
}
rect.setAttributeNS(null, 'width', deltaX);
rect.setAttributeNS(null, 'height', deltaY);
},
_clearDragArea: function() {
if (this._dragArea) {
this.$.svg.removeChild(this._dragArea);
}
this._makeDragArea = false;
this._dragArea = null;
//this.$.svg.innerHTML = '';
},
dragOutArrowStart: function(e, details, selection) {
this.draggedEl = selection;
//to stop wrapper drag to be called
e.stopPropagation();
if(!this.svgTop || !this.svgLeft) {
var svgRect = this.$.svg.getBoundingClientRect();
this.svgTop = svgRect.top;
this.svgLeft = svgRect.left;
}
this.startPointX = e.x/this._zoom - this.svgLeft;
this.startPointY = e.y/this._zoom - this.svgTop;
// create temp bezier
this._addBezierPath( this.startPointX , this.startPointY , this.startPointX , this.startPointY , 70, 'temp_path');
//this._addBezierPath( event.x0 , event.y0 , event.x0 , event.y0 , 70, 'temp_path');
},
dragOutArrow: function(e, details, selection) {
var tempPath = this.shadowRoot.querySelector('#temp_path');
// the last dragMove call passes e.x=0 and e.y=0 which causes neg x,y values. this if avoids that
if(e.x!==0 || e.y!==0)
{
var newPath = this._createBezierPath( this.startPointX, this.startPointY , e.x/this._zoom - this.svgLeft , e.y/this._zoom - this.svgTop , 70);
tempPath.setAttribute('d',newPath);
}
},
dragOutArrowEnd: function(e, details, selection) {
var tempPath = this.shadowRoot.querySelector('#temp_path');
if(tempPath) {
tempPath.parentNode.removeChild(tempPath);
}
},
dragOverInArrow: function(e, details, selection) {
//important! needed to allow drop
e.preventDefault();
},
dragEnterInArrow: function(e, details, selection) {
e.target.classList.add('drag-over');
},
dragLeaveInArrow: function(e, details, selection) {
e.target.classList.remove('drag-over');
},
droppedOnInArrow: function(e, details, selection) {
var self = this;
e.target.classList.remove('drag-over');
var className = selection.classList[0],
targetElementId = className.substring(0, className.lastIndexOf('-') ),
targetField = className.substring(className.lastIndexOf('-') + 1, className.length ),
source = self.findElById(self.draggedEl.classList[0]),
target = self.findElById(targetElementId);
this._connectAttributes(source, 'output', target, targetField);
self._drawConnections();
e.stopPropagation();
},
arrowClicked: function(e, details, selection) {
//e.stopPropagation();
var wrapper = null;
for(var i = 0; i< e.path.length && !wrapper; i++) {
if(e.path[i].classList.contains('wrapper')) {
wrapper = e.path[i];
}
}
var sourceTarget = e.path[0].classList.contains('inputMarker') ? 'target' : 'source';
var elementInsideId = wrapper.getAttribute('component-id');
//var elementInside = this.findElById(elementInsideId);
var connection = this._connections.filter(function(conn) {
if(conn[sourceTarget].id===elementInsideId) {
return true;
}
});
if(connection.length>0) {
var marker = e.path[0];
var bezierCssPath = '#container #svg #'+connection[0].source.id+'-'+connection[0].target.id;
var bezier = this.shadowRoot.querySelector(bezierCssPath);
if(connection[0].highlighted) {
connection[0].highlighted = false;
marker.classList.remove('highlighted');
bezier.classList.remove('highlighted');
}
else {
connection[0].highlighted = true;
marker.classList.add('highlighted');
bezier.classList.add('highlighted');
}
}
},
deleteConnection: function(e, details, selection) {
var self = this;
self.$.deleteDialog.toggle();
// set the delete callback
self._deleteCallback = function() {
if (e) {
e.stopPropagation();
}
var elementInsideId;
// figure out the source
var src;
if (self._highlightedConnection) {
var splitID = self._highlightedConnection.id.split('-');
src = splitID[0]+'-'+splitID[1]+'-'+splitID[2];
console.log(src);
elementInsideId = splitID[3] + '-' + splitID[4] + '-' + splitID[5];
}
else {
var wrapper = null;
for(var i = 0; i< e.path.length && !wrapper; i++) {
if(e.path[i].classList.contains('wrapper')) {
wrapper = e.path[i];
}
}
elementInsideId = wrapper.getAttribute('component-id');
}
if (!e){
e = undefined;
}
self.deleteConnectionByID(e, elementInsideId)
}
},