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 | import theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js'; |
26 | import ToolbarItemsContainer from './ToolbarItemsContainer.js'; |
27 | import ButtonHTMLElementClickEL from './ButtonHTMLElementClickEL.js'; |
28 | import ButtonHTMLElementTouchEL from './ButtonHTMLElementTouchEL.js'; |
29 | import ButtonsHTMLElementTouchEL from './ButtonsHTMLElementTouchEL.js'; |
30 | import ButtonsHTMLElementWheelEL from './ButtonsHTMLElementWheelEL.js'; |
31 | import theConfig from '../../data/Config.js'; |
32 | import { ZERO } from '../../main/Constants.js'; |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | class BaseToolbar { |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | #toolbarHTMLElement; |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | #buttonsHTMLElement; |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | #timerId; |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | #buttonsHTMLElementWheelEL; |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | #buttonsHTMLElementTouchEL; |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | #buttonHTMLElementClickEL; |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | #buttonHTMLElementTouchEL; |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | #isShow; |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | #toolbarItemsContainer; |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | static get #MOUSE_EVENT_MAX_DELAY ( ) { return 100; } |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | #lastMouseEventTimestamp; |
119 | |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | #addButton ( toolbarItem, index ) { |
127 | const buttonHTMLElement = theHTMLElementsFactory.create ( |
128 | 'div', |
129 | { |
130 | className : 'TravelNotes-BaseToolbar-ButtonHTMLElement', |
131 | textContent : toolbarItem.textContent, |
132 | title : toolbarItem.title, |
133 | dataset : { ItemId : index } |
134 | }, |
135 | this.#buttonsHTMLElement |
136 | ); |
137 | buttonHTMLElement.addEventListener ( 'click', this.#buttonHTMLElementClickEL, false ); |
138 | buttonHTMLElement.addEventListener ( 'touchstart', this.#buttonHTMLElementTouchEL, false ); |
139 | buttonHTMLElement.addEventListener ( 'touchend', this.#buttonHTMLElementTouchEL, false ); |
140 | } |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | #show ( ) { |
147 | |
148 | |
149 | this.#buttonsHTMLElement = theHTMLElementsFactory.create ( |
150 | 'div', |
151 | { |
152 | className : 'TravelNotes-BaseToolbar-ButtonsHTMLElement' |
153 | }, |
154 | this.#toolbarHTMLElement |
155 | ); |
156 | |
157 | |
158 | this.#toolbarItemsContainer.toolbarItemsArray = [ ]; |
159 | this.addToolbarItems ( ); |
160 | |
161 | |
162 | this.#toolbarItemsContainer.toolbarItemsArray.forEach ( |
163 | ( toolbarItem, index ) => { |
164 | this.#addButton ( toolbarItem, index ); |
165 | } |
166 | ); |
167 | |
168 | |
169 | this.#buttonsHTMLElement.addEventListener ( 'wheel', this.#buttonsHTMLElementWheelEL, { passive : true } ); |
170 | |
171 | |
172 | this.#buttonsHTMLElement.addEventListener ( 'touchstart', this.#buttonsHTMLElementTouchEL, false ); |
173 | this.#buttonsHTMLElement.addEventListener ( 'touchmove', this.#buttonsHTMLElementTouchEL, false ); |
174 | this.#buttonsHTMLElement.addEventListener ( 'touchend', this.#buttonsHTMLElementTouchEL, false ); |
175 | this.#isShow = true; |
176 | } |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | #toolbarHTMLElementClickEL ( mouseEvent ) { |
185 | |
186 | |
187 | |
188 | |
189 | if ( BaseToolbar.#MOUSE_EVENT_MAX_DELAY > mouseEvent.timeStamp - this.#lastMouseEventTimestamp ) { |
190 | return; |
191 | } |
192 | |
193 | this.#toolbarHTMLElementMouseEnterEL ( mouseEvent ); |
194 | } |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | #toolbarHTMLElementMouseEnterEL ( mouseEvent ) { |
203 | |
204 | |
205 | this.#lastMouseEventTimestamp = mouseEvent.timeStamp; |
206 | |
207 | if ( this.#isShow ) { |
208 | if ( this.#timerId ) { |
209 | clearTimeout ( this.#timerId ); |
210 | this.#timerId = null; |
211 | return; |
212 | } |
213 | |
214 | |
215 | this.hide ( ); |
216 | } |
217 | else { |
218 | this.#show ( ); |
219 | } |
220 | } |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | #toolbarHTMLElementMouseLeaveEL ( ) { |
228 | if ( this.#isShow ) { |
229 | this.#timerId = setTimeout ( ( ) => this.hide ( ), theConfig.toolbars.timeOut ); |
230 | } |
231 | } |
232 | |
233 | |
234 | |
235 | |
236 | |
237 | constructor ( ) { |
238 | Object.freeze ( this ); |
239 | } |
240 | |
241 | |
242 | |
243 | |
244 | |
245 | hide ( ) { |
246 | |
247 | |
248 | if ( this.#timerId ) { |
249 | clearTimeout ( this.#timerId ); |
250 | this.#timerId = null; |
251 | } |
252 | |
253 | |
254 | while ( this.#buttonsHTMLElement.firstChild ) { |
255 | const buttonHTMLElement = this.#buttonsHTMLElement.firstChild; |
256 | buttonHTMLElement.removeEventListener ( 'click', this.#buttonHTMLElementClickEL, false ); |
257 | buttonHTMLElement.removeEventListener ( 'touchstart', this.#buttonHTMLElementTouchEL, false ); |
258 | buttonHTMLElement.removeEventListener ( 'touchend', this.#buttonHTMLElementTouchEL, false ); |
259 | this.#buttonsHTMLElement.removeChild ( buttonHTMLElement ); |
260 | } |
261 | |
262 | |
263 | this.#buttonsHTMLElement.removeEventListener ( 'wheel', this.#buttonsHTMLElementWheelEL, { passive : true } ); |
264 | this.#buttonsHTMLElement.removeEventListener ( 'touchstart', this.#buttonsHTMLElementTouchEL, false ); |
265 | this.#buttonsHTMLElement.removeEventListener ( 'touchmove', this.#buttonsHTMLElementTouchEL, false ); |
266 | this.#buttonsHTMLElement.removeEventListener ( 'touchend', this.#buttonsHTMLElementTouchEL, false ); |
267 | this.#toolbarHTMLElement.removeChild ( this.#buttonsHTMLElement ); |
268 | this.#buttonsHTMLElement = null; |
269 | |
270 | this.#isShow = false; |
271 | } |
272 | |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | createUI ( headerText, position ) { |
280 | |
281 | |
282 | if ( this.#toolbarHTMLElement ) { |
283 | return false; |
284 | } |
285 | |
286 | |
287 | this.#timerId = null; |
288 | this.#isShow = false; |
289 | this.#lastMouseEventTimestamp = ZERO; |
290 | this.#toolbarItemsContainer = new ToolbarItemsContainer ( ); |
291 | |
292 | |
293 | this.#buttonsHTMLElementWheelEL = new ButtonsHTMLElementWheelEL ( ); |
294 | this.#buttonsHTMLElementTouchEL = new ButtonsHTMLElementTouchEL ( ); |
295 | this.#buttonHTMLElementClickEL = new ButtonHTMLElementClickEL ( this.#toolbarItemsContainer ); |
296 | this.#buttonHTMLElementTouchEL = new ButtonHTMLElementTouchEL ( this, this.#toolbarItemsContainer ); |
297 | |
298 | |
299 | this.#toolbarHTMLElement = |
300 | theHTMLElementsFactory.create ( |
301 | 'div', |
302 | { |
303 | className : 'TravelNotes-BaseToolbar-ToolbarHTMLElement ' + position |
304 | }, |
305 | document.body |
306 | ); |
307 | this.#toolbarHTMLElement.addEventListener ( |
308 | 'click', |
309 | mouseEvent => this.#toolbarHTMLElementClickEL ( mouseEvent ), |
310 | false |
311 | ); |
312 | this.#toolbarHTMLElement.addEventListener ( |
313 | 'mouseenter', |
314 | mouseEvent => this.#toolbarHTMLElementMouseEnterEL ( mouseEvent ), |
315 | false |
316 | ); |
317 | this.#toolbarHTMLElement.addEventListener ( |
318 | 'mouseleave', |
319 | mouseEvent => this.#toolbarHTMLElementMouseLeaveEL ( mouseEvent ), |
320 | false |
321 | ); |
322 | |
323 | |
324 | theHTMLElementsFactory.create ( |
325 | 'div', |
326 | { |
327 | className : 'TravelNotes-BaseToolbar-HeaderTextHTMLElement', |
328 | textContent : headerText |
329 | }, |
330 | theHTMLElementsFactory.create ( |
331 | 'div', |
332 | { |
333 | className : 'TravelNotes-BaseToolbar-HeaderHTMLElement' |
334 | }, |
335 | this.#toolbarHTMLElement |
336 | ) |
337 | ); |
338 | } |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | addToolbarItem ( toolbarItem ) { |
346 | this.#toolbarItemsContainer.toolbarItemsArray.push ( toolbarItem ); |
347 | } |
348 | |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | addCssClass ( cssClass ) { |
355 | this.#toolbarHTMLElement.classList.add ( cssClass ); |
356 | } |
357 | } |
358 | |
359 | export default BaseToolbar; |
360 | |
361 | |
362 | |