diff options
Diffstat (limited to 'assets_src/js')
-rw-r--r-- | assets_src/js/lib/dom.js | 32 | ||||
-rw-r--r-- | assets_src/js/list.js | 29 |
2 files changed, 61 insertions, 0 deletions
diff --git a/assets_src/js/lib/dom.js b/assets_src/js/lib/dom.js index 33845af..bfbeea7 100644 --- a/assets_src/js/lib/dom.js +++ b/assets_src/js/lib/dom.js | |||
@@ -5,10 +5,42 @@ export function ready(fn) { | |||
5 | } | 5 | } |
6 | 6 | ||
7 | export function closest(el, fn) { | 7 | export function closest(el, fn) { |
8 | if (!el) { | ||
9 | return null; | ||
10 | } | ||
8 | while (el) { | 11 | while (el) { |
9 | if (fn(el)) { | 12 | if (fn(el)) { |
10 | return el; | 13 | return el; |
11 | } | 14 | } |
12 | el = el.parentNode | 15 | el = el.parentNode |
13 | } | 16 | } |
17 | return null; | ||
18 | } | ||
19 | |||
20 | export function next(el, fn) { | ||
21 | if (!el) { | ||
22 | return null; | ||
23 | } | ||
24 | el = el.nextSibling; | ||
25 | while (el) { | ||
26 | if (fn(el)) { | ||
27 | return el; | ||
28 | } | ||
29 | el = el.nextSibling; | ||
30 | } | ||
31 | return null; | ||
32 | } | ||
33 | |||
34 | export function previous(el, fn) { | ||
35 | if (!el) { | ||
36 | return null; | ||
37 | } | ||
38 | el = el.previousSibling; | ||
39 | while (el) { | ||
40 | if (fn(el)) { | ||
41 | return el; | ||
42 | } | ||
43 | el = el.previousSibling; | ||
44 | } | ||
45 | return null; | ||
14 | } | 46 | } |
diff --git a/assets_src/js/list.js b/assets_src/js/list.js index 413f517..8246950 100644 --- a/assets_src/js/list.js +++ b/assets_src/js/list.js | |||
@@ -19,11 +19,40 @@ async function resultClicked(grilId) { | |||
19 | document.getElementById('gril-list').innerHTML += response; | 19 | document.getElementById('gril-list').innerHTML += response; |
20 | } | 20 | } |
21 | 21 | ||
22 | function listButtonHandler(e) { | ||
23 | let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li'; | ||
24 | let item = dom.closest(e.target, liFilter); | ||
25 | let pItem = dom.previous(item, liFilter), | ||
26 | nItem = dom.next(dom.next(item, liFilter), liFilter); | ||
27 | switch (e.target.classList[0]) { | ||
28 | case 'gril-sort-up': | ||
29 | //TODO: backend | ||
30 | item.parentNode.insertBefore(item, pItem); | ||
31 | break; | ||
32 | case 'gril-sort-down': | ||
33 | //TODO: backend | ||
34 | if (nItem) { | ||
35 | item.parentNode.insertBefore(item, nItem); | ||
36 | } else { | ||
37 | item.parentNode.appendChild(item); | ||
38 | } | ||
39 | break; | ||
40 | case 'gril-delete': | ||
41 | //TODO: backend | ||
42 | item.parentNode.removeChild(item); | ||
43 | break; | ||
44 | } | ||
45 | } | ||
46 | |||
22 | function main() { | 47 | function main() { |
23 | let is = new search.InstantSearch( | 48 | let is = new search.InstantSearch( |
24 | document.getElementById('gril-add-search'), | 49 | document.getElementById('gril-add-search'), |
25 | document.getElementById('search-results')); | 50 | document.getElementById('search-results')); |
26 | is.resultClicked = resultClicked; | 51 | is.resultClicked = resultClicked; |
52 | document.getElementById('gril-list').addEventListener( | ||
53 | 'click', | ||
54 | listButtonHandler, | ||
55 | true); | ||
27 | } | 56 | } |
28 | 57 | ||
29 | dom.ready(main); | 58 | dom.ready(main); |