diff options
Diffstat (limited to 'assets_src/js')
-rw-r--r-- | assets_src/js/list.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/assets_src/js/list.js b/assets_src/js/list.js index 0d865c7..844e453 100644 --- a/assets_src/js/list.js +++ b/assets_src/js/list.js | |||
@@ -4,6 +4,9 @@ import * as dom from './lib/dom'; | |||
4 | import * as ajax from './lib/ajax'; | 4 | import * as ajax from './lib/ajax'; |
5 | 5 | ||
6 | async function resultClicked(grilId) { | 6 | async function resultClicked(grilId) { |
7 | if (document.getElementById(grilId) !== null) { | ||
8 | return; | ||
9 | } | ||
7 | let response = null; | 10 | let response = null; |
8 | try { | 11 | try { |
9 | response = await ajax.post( | 12 | response = await ajax.post( |
@@ -86,7 +89,84 @@ async function listButtonHandler(e) { | |||
86 | } | 89 | } |
87 | } | 90 | } |
88 | 91 | ||
92 | function buildMergeModal(grils, merge) { | ||
93 | document.getElementById('gril-merge-modal-name').textContent = merge.name; | ||
94 | document.getElementById('gril-merge-modal-image').setAttribute('src', merge.image_url); | ||
95 | var targetDiv = document.getElementById('gril-merge-modal-targets'); | ||
96 | targetDiv.innerHTML = ''; | ||
97 | for (var i = 0; i < merge.targets.length; i++) { ((target) => { | ||
98 | var template = document.createElement('div'); | ||
99 | template.innerHTML = document.getElementById('gril-merge-modal-template').innerHTML; | ||
100 | template.getElementsByClassName('card-image')[0].setAttribute('src', target.image_url); | ||
101 | template.getElementsByClassName('card-title')[0].textContent = target.name; | ||
102 | var links = template.querySelectorAll('.card-action > a'); | ||
103 | links[0].setAttribute('href', '/gril/' + target.id); | ||
104 | links[1].setAttribute('href', ''); | ||
105 | links[1].addEventListener('click', (e) => { | ||
106 | e.preventDefault(); | ||
107 | ajax.get(`/api/import/anilist/assign?id=${merge.id}&target=${target.id}`) | ||
108 | .then(() => { | ||
109 | $('#gril-merge-modal').modal('close'); | ||
110 | resultClicked(target.id); | ||
111 | processFirstGril(grils); | ||
112 | }) | ||
113 | .catch((e) => { | ||
114 | alert(e); | ||
115 | }); | ||
116 | }, false); | ||
117 | while (template.firstChild) { | ||
118 | targetDiv.appendChild(template.firstChild); | ||
119 | } | ||
120 | })(merge.targets[i]); } | ||
121 | $('#gril-merge-modal').modal('open'); | ||
122 | } | ||
123 | |||
124 | async function processFirstGril(grils) { | ||
125 | if (grils.length === 0) { | ||
126 | Materialize.toast('Anilist Favoriten wurden erfolgreich importiert!', 5000); | ||
127 | return; | ||
128 | } | ||
129 | var gril = grils[0] | ||
130 | grils = grils.slice(1); | ||
131 | if (gril.mapped > 0) { | ||
132 | resultClicked(gril.mapped); | ||
133 | processFirstGril(grils); | ||
134 | return; | ||
135 | } | ||
136 | var merge; | ||
137 | try { | ||
138 | merge = JSON.parse(await ajax.get('/api/import/anilist/translate?id=' + gril.id)) | ||
139 | } catch (e) { | ||
140 | alert(e); | ||
141 | return; | ||
142 | } | ||
143 | switch (merge.targets.length) { | ||
144 | case 0: | ||
145 | alert('Gril existiert nicht! Das sollte nicht passieren...'); | ||
146 | break; | ||
147 | case 1: | ||
148 | resultClicked(merge.targets[0].id); | ||
149 | processFirstGril(grils); | ||
150 | break; | ||
151 | default: | ||
152 | buildMergeModal(grils, merge); | ||
153 | break; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | async function startAnilistImport() { | ||
158 | var favs = []; | ||
159 | try { | ||
160 | favs = JSON.parse(await ajax.get('/api/import/anilist/favourites')); | ||
161 | } catch (e) { | ||
162 | alert(e); | ||
163 | return; | ||
164 | } | ||
165 | processFirstGril(favs); | ||
166 | } | ||
167 | |||
89 | function main() { | 168 | function main() { |
169 | $('#gril-merge-modal').modal(); | ||
90 | let is = new search.InstantSearch( | 170 | let is = new search.InstantSearch( |
91 | document.getElementById('gril-add-search'), | 171 | document.getElementById('gril-add-search'), |
92 | document.getElementById('search-results')); | 172 | document.getElementById('search-results')); |
@@ -95,6 +175,10 @@ function main() { | |||
95 | 'click', | 175 | 'click', |
96 | listButtonHandler, | 176 | listButtonHandler, |
97 | true); | 177 | true); |
178 | document.getElementById('import-from-anilist').addEventListener( | ||
179 | 'click', | ||
180 | startAnilistImport, | ||
181 | false); | ||
98 | } | 182 | } |
99 | 183 | ||
100 | dom.ready(main); | 184 | dom.ready(main); |