Skip to content

Commit 6b19a31

Browse files
committed
refactor: refactor structure, watch functions in plugins
1 parent d244b07 commit 6b19a31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+567
-625
lines changed

src/index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@
6767
:undo="undo"
6868
:redo="redo"
6969
:can-undo="canUndo"
70-
:can-redo="canRedo">
70+
:can-redo="canRedo"
71+
:resize-commit="resizeElementByBoxId"
72+
>
7173
</toolsbar>
7274

7375
<search-elements

src/js/api/requests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { Decorators, API } from "divaservices-utils";
77
import { WEBSERVICES_XML_FILEPATH } from "../../config";
8-
import { PRODUCTION_MODE } from "../constants/constants";
8+
import { PRODUCTION_MODE } from "../utils/constants";
99

1010
export const getServices = async () => {
1111
if (process.env.NODE_ENV === PRODUCTION_MODE) {

src/js/app.js

+7-77
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
import Vue from "vue";
22
import { mapActions, mapState } from "vuex";
3-
import plugins from "./plugins";
43

54
import Graph from "./classes/Graph";
65
import Paper from "./classes/Paper";
76

8-
import components from "./layout/components";
7+
import components from "./components";
98
import store from "./store/store";
109
import { DivaServices } from "divaservices-utils";
11-
import { initWebservices } from "./constants/globals";
12-
import { CATEGORY_SERVICE, CATEGORY_DATATEST } from "./constants/constants";
13-
import { addElementFromName, addLinkFromLink } from "./elements/addElement";
14-
import { deleteElementByBoxId } from "./elements/deleteElement";
15-
import { findDifferenceBy } from "./utils/utils";
10+
import { initWebservices } from "./utils/globals";
1611
import {
1712
selectedElements,
1813
copiedElements,
1914
deletedElements,
2015
currentElements,
2116
currentDataElements
2217
} from "./store/modules/utils";
23-
import { addDataBox } from "./elements/addDataElement";
24-
import { initSplit } from "./layout/split";
25-
import { initKeyboardEvents } from "./events/keyboardEvents";
18+
import { initSplit } from "./utils/split";
2619
import { initTour } from "./utils/walkthrough";
2720
import { openWorkflow } from "./workflows/openWorkflow";
2821
import UndoRedoHistory from "./store/plugins/UndoRedoHistory";
@@ -31,16 +24,15 @@ import {
3124
MESSAGE_SAVE_SUCCESS,
3225
MESSAGE_COPY_SUCCESS,
3326
MESSAGE_COPY_ERROR
34-
} from "./constants/messages";
27+
} from "./utils/messages";
3528
import { saveWorkflow } from "./workflows/saveWorkflow";
29+
import { initKeyboardEvents } from "./events/keyboardEvents";
3630

3731
export let app;
3832

3933
(async () => {
4034
await initWebservices();
4135

42-
plugins.forEach(x => Vue.use(x));
43-
4436
app = new Vue({
4537
el: "#app",
4638
store,
@@ -77,15 +69,15 @@ export let app;
7769
return deletedElements(this.elementsData);
7870
},
7971
movedElements() {
80-
return this.elements.map(({ boxId, position }) => {
72+
return this.currentElements.map(({ boxId, position }) => {
8173
return { boxId, position };
8274
});
8375
},
8476
dataElements() {
8577
return this.dataTest;
8678
},
8779
logElements() {
88-
return this.elements.map(({ boxId, type, defaultParams }) => ({
80+
return this.currentElements.map(({ boxId, type, defaultParams }) => ({
8981
boxId,
9082
type,
9183
defaultParams
@@ -167,68 +159,6 @@ export let app;
167159
])
168160
},
169161
watch: {
170-
/**
171-
* watches current elements
172-
* add new elements, remove deleted elements
173-
*/
174-
currentElements: {
175-
handler(newValue) {
176-
// if boxId element does not exist in the graph, we add it
177-
for (const el of Graph.getNewElements(newValue)) {
178-
const { type, category } = el;
179-
switch (category) {
180-
case CATEGORY_SERVICE:
181-
addElementFromName(type, el);
182-
break;
183-
case CATEGORY_DATATEST:
184-
addDataBox(el);
185-
break;
186-
default:
187-
console.log("ERROR ADDING EL");
188-
}
189-
}
190-
}
191-
},
192-
193-
/**
194-
* watches current data elements
195-
* current state: debug
196-
*/
197-
currentDataElements: {
198-
deep: true,
199-
handler(newValue, oldValue) {
200-
const difference = findDifferenceBy(newValue, oldValue, "boxId");
201-
console.log("TCL: handler -> difference", difference);
202-
for (const el of difference) {
203-
console.log(el);
204-
// const { boxId, defaultParams } = el;
205-
// @TODO: suppose one image
206-
// updateImgPreview(this.id, data);
207-
}
208-
}
209-
},
210-
211-
/**
212-
* watch links
213-
* on direct operation, nothing changes (mechanic operation)
214-
* on start, add parsed links
215-
* apply changes on undo-redo
216-
*/
217-
links(newValue) {
218-
for (const l of Graph.getNewLinks(newValue)) {
219-
addLinkFromLink(l);
220-
}
221-
},
222-
/**
223-
* watches deleted elements
224-
*/
225-
deletedElements(newValue) {
226-
// if element is not in elements but exist in graph, delete it
227-
// @TODO: optimize ?
228-
for (const element of Graph.getElementsInGraph(newValue)) {
229-
deleteElementByBoxId(element.boxId);
230-
}
231-
},
232162
/**
233163
* watches paper scale
234164
*/

src/js/classes/AreaSelection.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Area Selection plugin
3+
* allows selecting multiple elements from a rectangle selection
4+
*/
5+
6+
import { AREA_SELECTION_ELEMENT } from "../utils/selectors";
7+
8+
class AreaSelection {
9+
constructor(commit) {
10+
this.areaSelectionElement = document.getElementById(AREA_SELECTION_ELEMENT);
11+
this.active = false;
12+
13+
this.x1 = 0;
14+
this.y1 = 0;
15+
this.x2 = 0;
16+
this.y2 = 0;
17+
18+
this.commit = commit;
19+
}
20+
21+
get isActive() {
22+
return this.active;
23+
}
24+
25+
/**
26+
* draw rectangle selection
27+
*
28+
* @param {number} x1
29+
* @param {number} x2
30+
* @param {number} y1
31+
* @param {number} y2
32+
*/
33+
reCalc(x1, x2, y1, y2) {
34+
const x3 = Math.min(x1, x2);
35+
const x4 = Math.max(x1, x2);
36+
const y3 = Math.min(y1, y2);
37+
const y4 = Math.max(y1, y2);
38+
this.areaSelectionElement.style.left = x3 + "px";
39+
this.areaSelectionElement.style.top = y3 + "px";
40+
this.areaSelectionElement.style.width = x4 - x3 + "px";
41+
this.areaSelectionElement.style.height = y4 - y3 + "px";
42+
}
43+
44+
/**
45+
* find elements in given area, defined with x, y, width and height
46+
*
47+
* @param {number} x
48+
* @param {number} y
49+
* @param {number} width
50+
* @param {number} height
51+
*/
52+
findViewsInAreaCustom(elements, { x, y, width, height }) {
53+
return elements.filter(el => {
54+
return !(
55+
x > el.position.x + el.size.width ||
56+
x + width < el.position.x ||
57+
y > el.position.y + el.size.height ||
58+
y + height < el.position.y
59+
);
60+
});
61+
}
62+
63+
/**
64+
* begin selection operation
65+
*/
66+
init(event) {
67+
this.active = true;
68+
this.x1 = event.clientX;
69+
this.y1 = event.clientY;
70+
this.x2 = event.clientX;
71+
this.y2 = event.clientY;
72+
this.reCalc(this.x1, this.x2, this.y1, this.y2);
73+
this.areaSelectionElement.hidden = 0;
74+
}
75+
76+
/**
77+
* end selection operation
78+
*/
79+
end(
80+
{ x: paperOffsetX, y: paperOffsetY },
81+
{ tx: paperTranslateX, ty: paperTranslateY },
82+
scale,
83+
elements
84+
) {
85+
this.active = false;
86+
87+
const {
88+
x,
89+
y,
90+
width,
91+
height
92+
} = this.areaSelectionElement.getBoundingClientRect();
93+
94+
const pointX = (x - paperOffsetX - paperTranslateX) / scale;
95+
const pointY = (y - paperOffsetY - paperTranslateY) / scale;
96+
97+
const selectedElements = this.findViewsInAreaCustom(elements, {
98+
x: pointX,
99+
y: pointY,
100+
width: width / scale,
101+
height: height / scale
102+
});
103+
104+
this.areaSelectionElement.hidden = 1;
105+
106+
if (selectedElements.length) {
107+
this.commit({ elements: selectedElements });
108+
}
109+
}
110+
111+
/**
112+
* translate mouse coordinate to paper coordinate to draw selection
113+
*/
114+
compute() {
115+
if (this.active) {
116+
this.x2 = event.clientX;
117+
this.y2 = event.clientY;
118+
this.reCalc(this.x1, this.x2, this.y1, this.y2);
119+
}
120+
}
121+
}
122+
123+
export default AreaSelection;

0 commit comments

Comments
 (0)