-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsearcher.js
More file actions
146 lines (122 loc) · 3.45 KB
/
searcher.js
File metadata and controls
146 lines (122 loc) · 3.45 KB
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
/**
* Class for searching through notes
*/
export default class Searcher {
/**
* @constructor
*/
constructor() {
/**
* DOM tree of class
* @type {Object}
*/
this.DOM = {
input: document.getElementsByClassName('searcher__input')[0],
foldersContainer: document.getElementsByName('js-folders-container')[0],
notes: {
created: document.getElementsByName('js-notes-menu')[0],
found: document.getElementsByName('js-found-notes-menu')[0]
}
};
/**
* CSS classes for DOM
* @type {Object}
*/
this.CSS = {
hidden: 'searcher__hidden'
};
/**
* Default value in the search input form
* @type {String}
*/
this.defaultInputValue = 'Search';
/**
* Where to search
* @type {Array}
*/
this.dataset = [];
/**
* Keyword from search field saved after search mode was disabled
* @type {Strng}
*/
this.lastSearch = '';
this.DOM.input.addEventListener('focus', () => {
this.DOM.notes.created.classList.add(this.CSS.hidden);
this.DOM.notes.found.classList.remove(this.CSS.hidden);
this.DOM.foldersContainer.classList.add(this.CSS.hidden);
this.DOM.input.value = this.lastSearch;
});
this.DOM.input.addEventListener('blur', (event) => {
if (event.relatedTarget != this.DOM.notes.found) {
this.DOM.notes.found.classList.add(this.CSS.hidden);
this.DOM.notes.found.classList.add(this.CSS.hidden);
this.DOM.notes.created.classList.remove(this.CSS.hidden);
this.DOM.foldersContainer.classList.remove(this.CSS.hidden);
this.lastSearch = this.DOM.input.value;
this.DOM.input.value = this.defaultInputValue;
}
});
this.DOM.input.addEventListener('keyup', () => {
this.search(this.DOM.input.value);
});
}
/**
* Cleans search results
* @param {Object} data - data to push to the dataset
*/
reset() {
let found = this.DOM.notes.found,
parent = found.parentNode;
this.DOM.notes.found = parent.removeChild(found).cloneNode(false);
parent.appendChild(this.DOM.notes.found);
}
/**
* Push note data to array where search will be done
* @param {Object} data - data to push to the dataset
*/
pushData( data ) {
let existingDataIndex = this.dataset.length;
this.dataset.forEach((item, index) => {
if (item._id == data._id) {
existingDataIndex = index;
return;
}
});
this.dataset.splice(existingDataIndex, 1, data);
}
/**
* Remove note data from array where search will be done
* @param {Object} dataId - the id of data to remove from the dataset
*/
removeData( dataId ) {
let existingDataIndex = this.dataset.length;
this.dataset.forEach((item, index) => {
if (item._id == dataId) {
existingDataIndex = index;
return;
}
});
this.dataset.splice(existingDataIndex, 1);
}
/**
* Find data in the dataset array
* @param {String} title - key to find data
*/
search( title ) {
this.reset();
if (title == '') {
return;
}
let highlight = {
start: 0,
end: 0
};
this.dataset.forEach((element) => {
highlight.start = element.title.indexOf(title);
if (highlight.start > -1) {
highlight.end = highlight.start + title.length;
codex.notes.aside.addMenuItem(element, true, {highlight: highlight});
}
});
}
}