Skip to content

Commit f0eaebc

Browse files
committed
feat: add search feature, use divaservices-utils
1 parent ffc6742 commit f0eaebc

23 files changed

+361
-273
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@fortawesome/free-solid-svg-icons": "^5.10.2",
2222
"ajv": "^6.10.2",
2323
"bootstrap": "^4.3.1",
24+
"divaservices-utils": "https://github.com/pyphilia/divaservices-utils",
2425
"dotenv": "^8.1.0",
2526
"file-saver": "^2.0.2",
2627
"jointjs": "^3.0.4",

services.xml

-1
This file was deleted.

src/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ <h1 class="col-3">Name</h1>
6363

6464
<toolsbar :scale="scale" :paper="paper" :selected-elements="selectedElements"></toolsbar>
6565

66-
66+
<search-elements ref="searchElements"></search-elements>
6767
</div>
6868
</div>
6969
</div>

src/js/app.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import { mapActions, mapState } from "vuex";
44
import plugins from "./plugins";
55
import components from "./layout/components";
66
import store from "./store/store";
7+
import { Constants } from "divaservices-utils";
8+
const { Types } = Constants;
79
import { initWebservices } from "./constants/globals";
810
import { initPaperEvents } from "./events/paperEvents";
911
import { getElementByBoxId, getLinkBySourceTarget } from "./layout/utils";
1012
import { highlightSelection, unHighlight } from "./store/modules/highlight";
11-
import {
12-
Inputs,
13-
CATEGORY_SERVICE,
14-
CATEGORY_DATATEST
15-
} from "./constants/constants";
13+
import { CATEGORY_SERVICE, CATEGORY_DATATEST } from "./constants/constants";
1614
import { addElementByName, addLinkFromLink } from "./elements/addElement";
1715
import { deleteElementByBoxId, deleteLink } from "./elements/deleteElement";
1816
import { resizeElements } from "./elements/resizeElement";
@@ -191,8 +189,8 @@ export let app;
191189
"defaultParams"
192190
);
193191
for (const { boxId, defaultParams } of difference) {
194-
setSelectValueInElement(boxId, defaultParams[Inputs.SELECT.type]);
195-
setInputValueInElement(boxId, defaultParams[Inputs.NUMBER.type]);
192+
setSelectValueInElement(boxId, defaultParams[Types.SELECT.type]);
193+
setInputValueInElement(boxId, defaultParams[Types.NUMBER.type]);
196194
}
197195
}
198196
},

src/js/constants/constants.js

-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import { PORT_SELECTOR, OUT_PORT_CLASS, IN_PORT_CLASS } from "./selectors";
22

3-
export const Inputs = {
4-
SELECT: { tag: "select", type: "select" },
5-
NUMBER: { tag: "input", type: "number" },
6-
FILE: { type: "file" },
7-
FOLDER: { type: "folder" }
8-
};
9-
Object.freeze(Inputs);
10-
113
export const BOX_HIGHLIGHTERS = [
124
{
135
highlighter: {

src/js/constants/globals.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import xml2js from "xml2js";
22
import { INPUTS_DATA_XML_FILEPATH } from "../../config";
3-
import webservicesDecorator from "./webservicesDecorator";
3+
import { Decorators } from "divaservices-utils";
44
import { dataTestDecorator } from "./dataTestDecorator";
55
import { getServicesAPI } from "../api/requests";
66

@@ -22,7 +22,7 @@ const createXml2jsPromise = xml => {
2222
const _initWebservices = async () => {
2323
const xml = await getServicesAPI();
2424
const data = await createXml2jsPromise(xml);
25-
webservices = webservicesDecorator(data);
25+
webservices = Decorators.webservicesDecorator(data);
2626
};
2727

2828
const _initDataInputs = async () => {

src/js/constants/webservicesDecorator.js

-142
This file was deleted.

src/js/elements/addElement.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import * as joint from "jointjs";
88
import { app } from "../app";
99
import { getWebserviceByName } from "../constants/globals";
10+
import { Constants } from "divaservices-utils";
11+
const { Types } = Constants;
1012
import {
1113
THEME,
1214
BOX_TITLE_HTML_TAG,
1315
ICON_COL,
1416
TITLE_COL,
15-
Inputs,
1617
CATEGORY_SERVICE,
1718
PORT_MARKUP,
1819
PORT_LABEL_MARKUP
@@ -116,7 +117,7 @@ export const buildDefaultParameters = params => {
116117
const defaultParams = { select: {}, number: {} };
117118
for (const p of params) {
118119
let { type, name, defaultValue, values } = p;
119-
if (type == Inputs.SELECT.type) {
120+
if (type == Types.SELECT.type) {
120121
defaultValue = values[defaultValue];
121122
}
122123
defaultParams[type][name] = { value: defaultValue, defaultValue, values };

src/js/events/paperEvents.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as $ from "jquery";
22
import * as joint from "jointjs";
33
import Vue from "vue";
4-
import { THEME, Inputs } from "../constants/constants";
4+
import { Constants } from "divaservices-utils";
5+
const { Types } = Constants;
6+
import { THEME } from "../constants/constants";
57
import {
68
INTERFACE_ROOT,
79
PORT_SELECTOR,
@@ -185,7 +187,7 @@ export const elementOnChangePosition = (
185187
{ multitranslate }
186188
) => {
187189
changePosition = true;
188-
const allSelect = $(`${INTERFACE_ROOT} .select ${Inputs.SELECT.tag}`);
190+
const allSelect = $(`${INTERFACE_ROOT} .select ${Types.SELECT.tag}`);
189191
allSelect.select2("close");
190192
app.$removeResizer();
191193

src/js/layout/components/Search.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Initialize toolsbar
3+
*/
4+
import Vue from "vue";
5+
import { app } from "../../app";
6+
import { buildSearchRegex } from "../../utils/utils";
7+
import { centerBoxInPaperByBoxId } from "../utils";
8+
9+
const Search = Vue.component("SearchElements", {
10+
props: ["selectedElements", "paper", "scale"],
11+
data() {
12+
return {
13+
open: false,
14+
query: "",
15+
candidates: [],
16+
currentId: 0
17+
};
18+
},
19+
methods: {
20+
openSearch() {
21+
console.log("open");
22+
this.open = true;
23+
},
24+
searchForElements() {
25+
this.query = document.getElementById("search-elements").value;
26+
27+
// @TODO: check on searchStr
28+
const regex = buildSearchRegex(this.query);
29+
30+
// apply search regex
31+
this.candidates = app.elements
32+
.map(el => {
33+
return { value: el.type.toLowerCase().search(regex), ...el };
34+
})
35+
// filter out non matching items, and sort results
36+
.filter(({ value }) => value != -1)
37+
.sort((a, b) => a.value - b.value);
38+
39+
this.currentId = 0;
40+
41+
if (this.candidates.length) {
42+
centerBoxInPaperByBoxId(this.candidates[this.currentId].boxId);
43+
}
44+
},
45+
nextCandidate() {
46+
if (++this.currentId >= this.candidates.length) {
47+
this.currentId = 0;
48+
} else if (this.currentId < 0) {
49+
this.currentId = this.candidates.length - 1;
50+
}
51+
centerBoxInPaperByBoxId(this.candidates[this.currentId].boxId);
52+
}
53+
},
54+
// @TODO search done twice because of a
55+
template: `
56+
<div v-if="open" id="search-component">
57+
<input id="search-elements" type="text" placeholder="Search for an element..." />
58+
{{candidates.length ? currentId+1 : 0}} / {{candidates.length}}
59+
<button type="button" @click="searchForElements()">Search</button>
60+
<button type="button" @click="nextCandidate()" :disabled="!candidates.length">Next</button>
61+
</div>
62+
`
63+
});
64+
65+
export default Search;

src/js/layout/components/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import LeftSidebar from "./LeftSidebar";
44
import ContextMenus from "./ContextMenu";
55
import Minimap from "./Minimap";
66
import Log from "./Log";
7+
import Search from "./Search";
78

89
const components = {
910
LeftSidebar,
1011
Minimap,
1112
Toolsbar,
1213
FileMenu,
1314
ContextMenus,
14-
Log
15+
Log,
16+
Search
1517
};
1618

1719
export default components;

src/js/layout/components/leftSidebar.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "../../constants/selectors";
1515
import { mapActions } from "vuex";
1616
import { toggleSplit } from "../split";
17+
import { buildSearchRegex } from "../../utils/utils";
1718

1819
const alphabeticalOrder = (a, b) => {
1920
const x = a.name.toLowerCase();
@@ -59,24 +60,18 @@ const LeftSidebar = Vue.component("LeftSidebar", {
5960

6061
const searchQuery = this.search.toLowerCase();
6162

62-
// build regex to take into account capital letters
63-
let regex = "";
64-
for (const letter of searchQuery) {
65-
regex += `[${letter}${letter.toUpperCase()}]`;
66-
}
67-
const re = new RegExp(regex);
63+
let regex = buildSearchRegex(searchQuery);
6864

6965
// apply search regex
70-
const searchResult = [];
71-
for (const item of this.services) {
72-
const value = item.name.toLowerCase().search(re);
73-
searchResult.push({ value, ...item });
74-
}
75-
76-
// filter out non matching items, and sort results
77-
return searchResult
78-
.filter(({ value }) => value != -1)
79-
.sort((a, b) => a.value - b.value);
66+
return (
67+
this.services
68+
.map(s => {
69+
return { value: s.name.toLowerCase().search(regex), ...s };
70+
})
71+
// filter out non matching items, and sort results
72+
.filter(({ value }) => value != -1)
73+
.sort((a, b) => a.value - b.value)
74+
);
8075
} else {
8176
return this.services;
8277
}

0 commit comments

Comments
 (0)