Skip to content

Commit 7fd9381

Browse files
committed
refactor: paper and graph classes
1 parent 0f38a2b commit 7fd9381

36 files changed

+1033
-1048
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"prettier:write": "prettier --write \"src/**/*.js\" \"src/**/*.scss\" \"test/*.js\"",
1313
"pre-commit": "yarn prettier:check && yarn eslint && yarn test",
1414
"pre-push": "yarn prettier:check && yarn eslint && yarn test",
15-
"test": "exit 0",
15+
"test": "jest test/Tool*",
1616
"webpack": "webpack",
1717
"webpack:p": "webpack -p"
1818
},

src/index.html

+27-8
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414
<!-- tooltip -->
1515
<div id="tooltip"></div>
1616

17-
<!-- Modal for settings -->
18-
<div id="layoutSettings"></div>
1917

2018

2119
<div id="app" class="container-fluid no-gutters h-100">
20+
21+
<!-- Modal for settings -->
22+
<layout-settings
23+
ref="layoutSettings"
24+
>
25+
</layout-settings>
26+
2227

2328
<collections ref="collections"></collections>
2429

@@ -30,7 +35,7 @@
3035

3136
<context-menus
3237
ref="contextmenu"
33-
:paper="paper"
38+
:copy="copy"
3439
:current-elements="currentElements"
3540
:copied-elements="copiedElements"
3641
:selected-elements="selectedElements">
@@ -41,20 +46,34 @@
4146
:scale="scale"
4247
:moved-elements="movedElements"
4348
:translation="translation"
44-
:paper="paper"
45-
:graph="graph">
49+
>
4650
</minimap>
4751

48-
<log ref="log"></log>
52+
<log
53+
ref="log"
54+
>
55+
56+
</log>
4957

5058
<!-- content -->
5159
<div id="paper-html-elements" class="h-100"></div>
5260

5361
<div id="message"></div>
5462

55-
<toolsbar :scale="scale" :graph="graph" :paper="paper" :selected-elements="selectedElements"></toolsbar>
63+
<toolsbar
64+
:scale="scale"
65+
:selected-elements="selectedElements"
66+
:save-workflow="saveWorkflow"
67+
:undo="undo"
68+
:redo="redo"
69+
:can-undo="canUndo"
70+
:can-redo="canRedo">
71+
</toolsbar>
5672

57-
<search-elements ref="searchElements"></search-elements>
73+
<search-elements
74+
ref="searchElements"
75+
>
76+
</search-elements>
5877
</div>
5978
</div>
6079
</div>

src/js/api/requests.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const getWorkflowById = async (id, asXml = false) => {
3636
<Step> <No>0</No> <Name>FilePickermimetypes</Name> <Service> <Key>47</Key> </Service> <Inputs> <Parameter> <Name>regex</Name> <Value>12345</Value> </Parameter> </Inputs> </Step> <Step> <No>1</No> <Name>OcropusBinarization</Name> <Service> <Key>6</Key> </Service> <Inputs> <Parameter> <Name>enableSkew</Name> <Value>true</Value> </Parameter> </Inputs> </Step>
3737
</Steps>
3838
</Workflow>`;
39+
console.log(Decorators);
3940
return asXml ? xml : await Decorators.workflowDecorator(xml, webservices);
4041
}
4142
};

src/js/app.js

+97-62
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import Vue from "vue";
2-
import * as joint from "jointjs";
32
import { mapActions, mapState } from "vuex";
43
import plugins from "./plugins";
4+
5+
import Graph from "./classes/Graph";
6+
import Paper from "./classes/Paper";
7+
58
import components from "./layout/components";
69
import store from "./store/store";
710
import { DivaServices } from "divaservices-utils";
811
import { initWebservices } from "./constants/globals";
9-
import { initPaperEvents } from "./events/paperEvents";
10-
import { getElementByBoxId, getLinkBySourceTarget } from "./layout/utils";
1112
import { highlightSelection, unHighlight } from "./store/modules/highlight";
1213
import { CATEGORY_SERVICE, CATEGORY_DATATEST } from "./constants/constants";
1314
import { addElementFromName, addLinkFromLink } from "./elements/addElement";
1415
import { deleteElementByBoxId, deleteLink } from "./elements/deleteElement";
1516
import { resizeElements } from "./elements/resizeElement";
1617
import {
1718
setInputValueInElement,
18-
setSelectValueInElement
19+
setSelectValueInElement,
20+
closeSelects
1921
} from "./layout/inputs";
2022
import {
2123
equalObjects,
2224
findDifferenceBy,
23-
getNewElements,
24-
getDeletedElements,
25-
getNewLinks,
26-
getElementsInGraph
25+
getDeletedElements
2726
} from "./utils/utils";
2827
import {
2928
selectedElements,
@@ -34,11 +33,18 @@ import {
3433
} from "./store/modules/utils";
3534
import { moveElements } from "./elements/moveElement";
3635
import { addDataBox } from "./elements/addDataElement";
37-
import { initPaper } from "./layout/initPaper";
3836
import { initSplit } from "./layout/split";
3937
import { initKeyboardEvents } from "./events/keyboardEvents";
4038
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";
4248

4349
export let app;
4450

@@ -50,14 +56,18 @@ export let app;
5056
app = new Vue({
5157
el: "#app",
5258
store,
59+
components,
5360
data: {
54-
graph: new joint.dia.Graph(),
55-
paper: null,
56-
translation: { tx: 0, ty: 0 },
61+
paper: Paper, // in order to watch translation
5762
workflowId: undefined
5863
},
59-
components,
6064
computed: {
65+
translation() {
66+
return Paper.translation;
67+
},
68+
scale() {
69+
return Paper.scale;
70+
},
6171
// this computed method is necessary since we shouldn't
6272
// listen to the store directly
6373
elementsData() {
@@ -93,28 +103,65 @@ export let app;
93103
return { boxId, size };
94104
});
95105
},
96-
scale() {
97-
return this.$zoom.scale;
98-
},
99106
dataElements() {
100107
return this.dataTest;
101108
},
102109
...mapState("Interface", ["elements", "links"]),
103110
...mapState("Keyboard", ["ctrl", "space"])
104111
},
105112
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+
106157
addElementToSelection(cellView) {
107158
this.$addElementToSelection(cellView);
108159
},
109-
translate(newX, newY) {
110-
this.paper.translate(newX * this.scale, newY * this.scale);
111-
this.translation = this.paper.translate();
112-
},
113160
addLinkFromApp(payload) {
114-
this.$addLink({ ...payload, graph: this.graph });
161+
this.$addLink({ ...payload });
115162
},
116163
deleteLinkFromApp(payload) {
117-
this.$deleteLink({ ...payload, graph: this.graph });
164+
this.$deleteLink({ ...payload });
118165
},
119166
deleteElementByCellView(cellView) {
120167
const boxId = cellView.model.attributes.boxId;
@@ -123,16 +170,6 @@ export let app;
123170
});
124171
},
125172

126-
/**
127-
* zoom call
128-
*/
129-
zoomInFromApp() {
130-
this.$zoomIn(this.paper);
131-
},
132-
zoomOutFromApp() {
133-
this.$zoomOut(this.paper);
134-
},
135-
136173
resizeElementByBoxId(boxId, size) {
137174
const element = this.elements.find(el => el.boxId === boxId);
138175
this.$resizeElement({ element, size });
@@ -166,7 +203,7 @@ export let app;
166203
currentElements: {
167204
handler(newValue, oldValue) {
168205
// 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)) {
170207
const { type, category } = el;
171208
switch (category) {
172209
case CATEGORY_SERVICE:
@@ -183,7 +220,7 @@ export let app;
183220
// remove element removed from arr
184221
// cannot use arr.includes because states are deep cloned
185222
for (const el of getDeletedElements(oldValue, newValue)) {
186-
deleteElementByBoxId(this.graph, el.boxId);
223+
deleteElementByBoxId(el.boxId);
187224
}
188225
}
189226
},
@@ -197,10 +234,12 @@ export let app;
197234
handler(newValue, oldValue) {
198235
const difference = findDifferenceBy(newValue, oldValue, "boxId");
199236
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+
}
204243
}
205244
},
206245
/**
@@ -253,7 +292,7 @@ export let app;
253292
* apply changes on undo-redo
254293
*/
255294
links(newValue, oldValue) {
256-
for (const l of getNewLinks(newValue)) {
295+
for (const l of Graph.getNewLinks(newValue)) {
257296
addLinkFromLink(l);
258297
}
259298

@@ -262,7 +301,7 @@ export let app;
262301
for (const el of oldValue.filter(
263302
el => newValue.filter(v => equalObjects(v, el)).length === 0
264303
)) {
265-
const link = getLinkBySourceTarget(el.source, el.target);
304+
const link = Graph.getLinkBySourceTarget(el.source, el.target);
266305
deleteLink(link);
267306
}
268307
},
@@ -272,8 +311,8 @@ export let app;
272311
deletedElements(newValue) {
273312
// if element is not in elements but exist in graph, delete it
274313
// @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);
277316
}
278317
},
279318
/**
@@ -282,18 +321,18 @@ export let app;
282321
selectedElements(newValue, oldValue) {
283322
// highlight current selection
284323
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)
287326
);
288327
highlightSelection(cellView);
289328
}
290329
// remove unselected element if not deleted
291330
for (const { boxId } of oldValue.filter(el => !newValue.includes(el))) {
292-
const el = getElementByBoxId(this.graph, boxId);
331+
const el = Graph.getElementByBoxId(boxId);
293332

294333
// on delete, these might be still be selected
295334
if (el) {
296-
const cellView = el.findView(this.paper);
335+
const cellView = Paper.findViewInPaper(el);
297336
unHighlight(cellView);
298337
}
299338
}
@@ -302,26 +341,22 @@ export let app;
302341
* watches paper scale
303342
*/
304343
scale(nextScale, currentScale) {
305-
this.$changePaperScale(this.paper, nextScale, currentScale);
344+
Paper.changeScale(nextScale, currentScale);
306345
}
307346
},
308347
mounted() {
309-
this.paper = initPaper(this.graph);
348+
Paper.initPaper(this, Graph.graph);
310349

311-
this.$nextTick(() => {
312-
// init events
313-
initPaperEvents();
314-
initKeyboardEvents(); // WARNiNG promise
350+
initKeyboardEvents(); // WARNiNG promise
315351

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+
}
325360

326361
// initialize rezisable split
327362
initSplit();

0 commit comments

Comments
 (0)