1
1
import Vue from "vue" ;
2
- import * as joint from "jointjs" ;
3
2
import { mapActions , mapState } from "vuex" ;
4
3
import plugins from "./plugins" ;
4
+
5
+ import Graph from "./classes/Graph" ;
6
+ import Paper from "./classes/Paper" ;
7
+
5
8
import components from "./layout/components" ;
6
9
import store from "./store/store" ;
7
10
import { DivaServices } from "divaservices-utils" ;
8
11
import { initWebservices } from "./constants/globals" ;
9
- import { initPaperEvents } from "./events/paperEvents" ;
10
- import { getElementByBoxId , getLinkBySourceTarget } from "./layout/utils" ;
11
12
import { highlightSelection , unHighlight } from "./store/modules/highlight" ;
12
13
import { CATEGORY_SERVICE , CATEGORY_DATATEST } from "./constants/constants" ;
13
14
import { addElementFromName , addLinkFromLink } from "./elements/addElement" ;
14
15
import { deleteElementByBoxId , deleteLink } from "./elements/deleteElement" ;
15
16
import { resizeElements } from "./elements/resizeElement" ;
16
17
import {
17
18
setInputValueInElement ,
18
- setSelectValueInElement
19
+ setSelectValueInElement ,
20
+ closeSelects
19
21
} from "./layout/inputs" ;
20
22
import {
21
23
equalObjects ,
22
24
findDifferenceBy ,
23
- getNewElements ,
24
- getDeletedElements ,
25
- getNewLinks ,
26
- getElementsInGraph
25
+ getDeletedElements
27
26
} from "./utils/utils" ;
28
27
import {
29
28
selectedElements ,
@@ -34,11 +33,18 @@ import {
34
33
} from "./store/modules/utils" ;
35
34
import { moveElements } from "./elements/moveElement" ;
36
35
import { addDataBox } from "./elements/addDataElement" ;
37
- import { initPaper } from "./layout/initPaper" ;
38
36
import { initSplit } from "./layout/split" ;
39
37
import { initKeyboardEvents } from "./events/keyboardEvents" ;
40
38
import { initTour } from "./utils/walkthrough" ;
41
- import { readWorkflow } from "./workflows/readWorkflow" ;
39
+ import { openWorkflow } from "./workflows/openWorkflow" ;
40
+ import UndoRedoHistory from "./store/plugins/UndoRedoHistory" ;
41
+ import { fireAlert } from "./utils/alerts" ;
42
+ import {
43
+ MESSAGE_SAVE_SUCCESS ,
44
+ MESSAGE_COPY_SUCCESS ,
45
+ MESSAGE_COPY_ERROR
46
+ } from "./constants/messages" ;
47
+ import { saveWorkflow } from "./workflows/saveWorkflow" ;
42
48
43
49
export let app ;
44
50
@@ -50,14 +56,18 @@ export let app;
50
56
app = new Vue ( {
51
57
el : "#app" ,
52
58
store,
59
+ components,
53
60
data : {
54
- graph : new joint . dia . Graph ( ) ,
55
- paper : null ,
56
- translation : { tx : 0 , ty : 0 } ,
61
+ paper : Paper , // in order to watch translation
57
62
workflowId : undefined
58
63
} ,
59
- components,
60
64
computed : {
65
+ translation ( ) {
66
+ return Paper . translation ;
67
+ } ,
68
+ scale ( ) {
69
+ return Paper . scale ;
70
+ } ,
61
71
// this computed method is necessary since we shouldn't
62
72
// listen to the store directly
63
73
elementsData ( ) {
@@ -93,28 +103,65 @@ export let app;
93
103
return { boxId, size } ;
94
104
} ) ;
95
105
} ,
96
- scale ( ) {
97
- return this . $zoom . scale ;
98
- } ,
99
106
dataElements ( ) {
100
107
return this . dataTest ;
101
108
} ,
102
109
...mapState ( "Interface" , [ "elements" , "links" ] ) ,
103
110
...mapState ( "Keyboard" , [ "ctrl" , "space" ] )
104
111
} ,
105
112
methods : {
113
+ /**
114
+ * Utility function to clear interactions on the paper
115
+ */
116
+ clearInteractions ( ) {
117
+ // close select manually
118
+ closeSelects ( ) ;
119
+
120
+ // remove resizer
121
+ this . $removeResizer ( ) ;
122
+ } ,
123
+ copy ( ) {
124
+ if ( selectedElements . length ) {
125
+ this . $copySelectedElements ( ) ;
126
+ fireAlert ( "success" , MESSAGE_COPY_SUCCESS ) ;
127
+ } else {
128
+ fireAlert ( "danger" , MESSAGE_COPY_ERROR ) ;
129
+ }
130
+ } ,
131
+ saveWorkflow ( installation = true ) {
132
+ // @TODO receive response and fire correct alert
133
+ saveWorkflow (
134
+ {
135
+ elements : this . currentElements ,
136
+ links : this . links ,
137
+ workflowId : this . workflowId
138
+ } ,
139
+ installation
140
+ ) ; // WARNING: promise
141
+
142
+ fireAlert ( "success" , MESSAGE_SAVE_SUCCESS ) ;
143
+ } ,
144
+ canUndo ( ) {
145
+ return UndoRedoHistory . canUndo ( ) ;
146
+ } ,
147
+ canRedo ( ) {
148
+ return UndoRedoHistory . canRedo ( ) ;
149
+ } ,
150
+ redo ( ) {
151
+ UndoRedoHistory . redo ( ) ;
152
+ } ,
153
+ undo ( ) {
154
+ UndoRedoHistory . undo ( ) ;
155
+ } ,
156
+
106
157
addElementToSelection ( cellView ) {
107
158
this . $addElementToSelection ( cellView ) ;
108
159
} ,
109
- translate ( newX , newY ) {
110
- this . paper . translate ( newX * this . scale , newY * this . scale ) ;
111
- this . translation = this . paper . translate ( ) ;
112
- } ,
113
160
addLinkFromApp ( payload ) {
114
- this . $addLink ( { ...payload , graph : this . graph } ) ;
161
+ this . $addLink ( { ...payload } ) ;
115
162
} ,
116
163
deleteLinkFromApp ( payload ) {
117
- this . $deleteLink ( { ...payload , graph : this . graph } ) ;
164
+ this . $deleteLink ( { ...payload } ) ;
118
165
} ,
119
166
deleteElementByCellView ( cellView ) {
120
167
const boxId = cellView . model . attributes . boxId ;
@@ -123,16 +170,6 @@ export let app;
123
170
} ) ;
124
171
} ,
125
172
126
- /**
127
- * zoom call
128
- */
129
- zoomInFromApp ( ) {
130
- this . $zoomIn ( this . paper ) ;
131
- } ,
132
- zoomOutFromApp ( ) {
133
- this . $zoomOut ( this . paper ) ;
134
- } ,
135
-
136
173
resizeElementByBoxId ( boxId , size ) {
137
174
const element = this . elements . find ( el => el . boxId === boxId ) ;
138
175
this . $resizeElement ( { element, size } ) ;
@@ -166,7 +203,7 @@ export let app;
166
203
currentElements : {
167
204
handler ( newValue , oldValue ) {
168
205
// if boxId element does not exist in the graph, we add it
169
- for ( const el of getNewElements ( this . graph , newValue ) ) {
206
+ for ( const el of Graph . getNewElements ( newValue ) ) {
170
207
const { type, category } = el ;
171
208
switch ( category ) {
172
209
case CATEGORY_SERVICE :
@@ -183,7 +220,7 @@ export let app;
183
220
// remove element removed from arr
184
221
// cannot use arr.includes because states are deep cloned
185
222
for ( const el of getDeletedElements ( oldValue , newValue ) ) {
186
- deleteElementByBoxId ( this . graph , el . boxId ) ;
223
+ deleteElementByBoxId ( el . boxId ) ;
187
224
}
188
225
}
189
226
} ,
@@ -197,10 +234,12 @@ export let app;
197
234
handler ( newValue , oldValue ) {
198
235
const difference = findDifferenceBy ( newValue , oldValue , "boxId" ) ;
199
236
console . log ( "TCL: handler -> difference" , difference ) ;
200
- // for (const { boxId, defaultParams } of difference) {
201
- // setSelectValueInElement(...);
202
- // setInputValueInElement(...);
203
- // }
237
+ for ( const el of difference ) {
238
+ console . log ( el ) ;
239
+ // const { boxId, defaultParams } = el;
240
+ // @TODO : suppose one image
241
+ // updateImgPreview(this.id, data);
242
+ }
204
243
}
205
244
} ,
206
245
/**
@@ -253,7 +292,7 @@ export let app;
253
292
* apply changes on undo-redo
254
293
*/
255
294
links ( newValue , oldValue ) {
256
- for ( const l of getNewLinks ( newValue ) ) {
295
+ for ( const l of Graph . getNewLinks ( newValue ) ) {
257
296
addLinkFromLink ( l ) ;
258
297
}
259
298
@@ -262,7 +301,7 @@ export let app;
262
301
for ( const el of oldValue . filter (
263
302
el => newValue . filter ( v => equalObjects ( v , el ) ) . length === 0
264
303
) ) {
265
- const link = getLinkBySourceTarget ( el . source , el . target ) ;
304
+ const link = Graph . getLinkBySourceTarget ( el . source , el . target ) ;
266
305
deleteLink ( link ) ;
267
306
}
268
307
} ,
@@ -272,8 +311,8 @@ export let app;
272
311
deletedElements ( newValue ) {
273
312
// if element is not in elements but exist in graph, delete it
274
313
// @TODO : optimize ?
275
- for ( const element of getElementsInGraph ( this . graph , newValue ) ) {
276
- deleteElementByBoxId ( this . graph , element . boxId ) ;
314
+ for ( const element of Graph . getElementsInGraph ( newValue ) ) {
315
+ deleteElementByBoxId ( element . boxId ) ;
277
316
}
278
317
} ,
279
318
/**
@@ -282,18 +321,18 @@ export let app;
282
321
selectedElements ( newValue , oldValue ) {
283
322
// highlight current selection
284
323
for ( const { boxId } of newValue ) {
285
- const cellView = getElementByBoxId ( this . graph , boxId ) . findView (
286
- this . paper
324
+ const cellView = Paper . findViewInPaper (
325
+ Graph . getElementByBoxId ( boxId )
287
326
) ;
288
327
highlightSelection ( cellView ) ;
289
328
}
290
329
// remove unselected element if not deleted
291
330
for ( const { boxId } of oldValue . filter ( el => ! newValue . includes ( el ) ) ) {
292
- const el = getElementByBoxId ( this . graph , boxId ) ;
331
+ const el = Graph . getElementByBoxId ( boxId ) ;
293
332
294
333
// on delete, these might be still be selected
295
334
if ( el ) {
296
- const cellView = el . findView ( this . paper ) ;
335
+ const cellView = Paper . findViewInPaper ( el ) ;
297
336
unHighlight ( cellView ) ;
298
337
}
299
338
}
@@ -302,26 +341,22 @@ export let app;
302
341
* watches paper scale
303
342
*/
304
343
scale ( nextScale , currentScale ) {
305
- this . $changePaperScale ( this . paper , nextScale , currentScale ) ;
344
+ Paper . changeScale ( nextScale , currentScale ) ;
306
345
}
307
346
} ,
308
347
mounted ( ) {
309
- this . paper = initPaper ( this . graph ) ;
348
+ Paper . initPaper ( this , Graph . graph ) ;
310
349
311
- this . $nextTick ( ( ) => {
312
- // init events
313
- initPaperEvents ( ) ;
314
- initKeyboardEvents ( ) ; // WARNiNG promise
350
+ initKeyboardEvents ( ) ; // WARNiNG promise
315
351
316
- // retrieve workflow id
317
- const id = DivaServices . getUrlParameters ( ) . id ;
318
- if ( ! isNaN ( id ) ) {
319
- this . workflowId = id ;
320
- readWorkflow ( id ) ;
321
- } else {
322
- throw "Error with id " + id ;
323
- }
324
- } ) ;
352
+ // retrieve workflow id
353
+ const id = DivaServices . getUrlParameters ( ) . id ;
354
+ if ( ! isNaN ( id ) ) {
355
+ this . workflowId = id ;
356
+ openWorkflow ( id ) ;
357
+ } else {
358
+ throw "Error with id " + id ;
359
+ }
325
360
326
361
// initialize rezisable split
327
362
initSplit ( ) ;
0 commit comments