diff options
author | rtz12 <koenig@fagott.pw> | 2016-11-15 17:29:02 (UTC) |
---|---|---|
committer | rtz12 <koenig@fagott.pw> | 2016-11-15 17:29:02 (UTC) |
commit | 0ad174d7329e02b3f644614de0758789bb3677d4 (patch) | |
tree | e49a659c0b89bdec07cde778850b7fe002ac27ea /assets_src | |
parent | e02ad854383265db216ed92b10c4f12408999d6a (diff) |
Verrückter Anilist Import
Bestes Feature 2k16.
Hashtag Make Grilist Great Again.
Diffstat (limited to 'assets_src')
-rw-r--r-- | assets_src/css/list.css | 9 | ||||
-rw-r--r-- | assets_src/js/list.js | 84 |
2 files changed, 93 insertions, 0 deletions
diff --git a/assets_src/css/list.css b/assets_src/css/list.css index a3e6fcf..89f1476 100644 --- a/assets_src/css/list.css +++ b/assets_src/css/list.css | |||
@@ -29,3 +29,12 @@ | |||
29 | width: 64px; | 29 | width: 64px; |
30 | height: 64px; | 30 | height: 64px; |
31 | } | 31 | } |
32 | |||
33 | /* Merge Modal */ | ||
34 | #gril-merge-modal-template { | ||
35 | display: none; | ||
36 | } | ||
37 | |||
38 | #gril-merge-modal-name { | ||
39 | text-align: center; | ||
40 | } | ||
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); |