Debian Codegraph @

src/pyj/book_list/add.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5 from elementmaker import E
6 from gettext import gettext as _
7
8 from ajax import ajax_send_file
9 from book_list.library_data import force_refresh_on_next_load, loaded_books_query
10 from book_list.router import back
11 from book_list.theme import get_color
12 from book_list.top_bar import create_top_bar
13 from book_list.ui import query_as_href, show_panel
14 from dom import add_extra_css, build_rule, clear, ensure_id
15 from file_uploads import (
16 update_status_widget, upload_files_widget, upload_status_widget
17 )
18 from session import get_interface_data
19 from utils import safe_set_inner_html
20 from widgets import create_button, enable_escape_key
21
22 state = {
23 'in_progress': False,
24 'container_id': None,
25 'counter': 0,
26 'number': 0,
27 'fake_send': False,
28 'transfers': [],
29 }
30
31
32 add_extra_css(def ():
33 panel = '.cs-add-books'
34 sel = panel + ' '
35 s = ''
36 s += build_rule(panel,
37 display='flex',
38 flex_direction='column',
39 min_height='0',
40 flex='1 1 auto',
41 )
42 s += build_rule('.cs-upload-dropzone',
43 box_sizing='border-box',
44 min_height='10rem',
45 padding='1.1rem 1rem',
46 border='var(--calibre-border-style-subtle)',
47 border_radius='14px',
48 background_color='var(--calibre-color-primary-light)',
49 color='inherit',
50 transition='background-color 160ms ease, border-color 160ms ease, box-shadow 160ms ease',
51 )
52 s += build_rule(sel + '.cs-upload-dropzone',
53 min_height='min(72vh, 520px)',
54 padding='2rem 1.5rem',
55 margin='0.75rem 1rem 0.5rem 1rem',
56 border='2px dashed var(--calibre-color-border)',
57 border_radius='18px',
58 display='flex',
59 flex_direction='column',
60 align_items='center',
61 justify_content='center',
62 cursor='pointer',
63 box_shadow='0 1px 2px rgba(0,0,0,0.04)',
64 )
65 s += build_rule(sel + '.cs-upload-dropzone:hover',
66 background_color='var(--calibre-color-primary-light-2)',
67 border_color='var(--calibre-color-primary)',
68 box_shadow='0 4px 18px rgba(0,0,0,0.08)',
69 )
70 s += build_rule('.cs-upload-dropzone-msg',
71 display='block',
72 max_width='28rem',
73 margin='0',
74 text_align='center',
75 font_size='1.02rem',
76 line_height='1.55',
77 opacity='0.92',
78 )
79 s += build_rule(sel + '.cs-upload-dropzone-msg',
80 font_size='1.06rem',
81 line_height='1.6',
82 )
83 s += build_rule('.cs-upload-dropzone-msg a.blue-link',
84 font_weight='600',
85 text_decoration='underline',
86 text_underline_offset='3px',
87 )
88 s += build_rule('.cs-upload-job',
89 margin='0 0 0.65rem 0',
90 padding='1.1rem 1.2rem',
91 box_sizing='border-box',
92 border='var(--calibre-border-style-subtle)',
93 border_radius='14px',
94 background_color=get_color('window-background'),
95 box_shadow='0 2px 8px rgba(0,0,0,0.06)',
96 )
97 s += build_rule(sel + '.cs-upload-job',
98 margin_left='1rem',
99 margin_right='1rem',
100 )
101 s += build_rule('.cs-upload-job-title',
102 margin='0 0 0.75rem 0',
103 font_size='1.02rem',
104 font_weight='700',
105 line_height='1.35',
106 word_break='break-word',
107 )
108 s += build_rule('.cs-upload-progress',
109 width='100%',
110 height='0.5rem',
111 border_radius='999px',
112 overflow='hidden',
113 border='none',
114 background_color='rgba(0,0,0,0.08)',
115 accent_color='var(--calibre-color-primary)',
116 )
117 s += build_rule('.cs-upload-progress-label',
118 display='block',
119 margin_top='0.55rem',
120 font_size='0.88rem',
121 opacity='0.8',
122 )
123 s += build_rule(sel + '.cs-add-books-done-msg',
124 display='inline',
125 margin_right='0.35rem',
126 font_size='0.95rem',
127 opacity='0.88',
128 )
129 s += build_rule(sel + '.cs-add-books-dup-list',
130 margin_top='0.45rem',
131 )
132 s += build_rule(sel + '.cs-add-books-dup-hint',
133 padding='0.35rem 0',
134 line_height='1.5',
135 opacity='0.9',
136 )
137 s += build_rule(sel + '.cs-add-books-dup-detail',
138 margin_top='0.85rem',
139 padding_top='0.85rem',
140 border_top='var(--calibre-border-style-subtle)',
141 font_size='0.95rem',
142 line_height='1.5',
143 opacity='0.88',
144 )
145 s += build_rule(sel + '.cs-add-books-dup-detail ol',
146 margin='0.5rem 0 0 0',
147 padding_left='1.25rem',
148 )
149 s += build_rule(sel + '.cs-add-books-error',
150 padding='0.5rem 0 0 0',
151 font_size='0.95rem',
152 line_height='1.55',
153 color='inherit',
154 )
155 s += r'''
156 @media (max-width: 520px) {
157 .cs-add-books .cs-upload-dropzone { min-height: 56vh; padding: 1.35rem 1rem; margin-left: 0.65rem; margin-right: 0.65rem; }
158 .cs-add-books .cs-upload-job { margin-left: 0.65rem; margin-right: 0.65rem; }
159 }
160 '''
161 return s
162 )
163
164
165 def cancel_in_progress():
166 for xhr in state.transfers:
167 xhr.abort()
168 state.transfers = []
169 state.in_progress = False
170
171
172 def on_close():
173 cancel_in_progress()
174 back()
175
176
177 def get_job_container(container_id, job_id):
178 container = document.getElementById(container_id)
179 if not container:
180 return
181 return container.querySelector(f'[data-job="{job_id}"]')
182
183
184 def on_progress(container_id, job_id, loaded, total, xhr):
185 container = get_job_container(container_id, job_id)
186 if not container:
187 return
188 if total:
189 update_status_widget(container, loaded, total)
190
191
192 def list_added_book(container, data):
193 container.appendChild(E.h3(
194 E.b(data.title),
195 E.span(' ' + _('by') + ' '),
196 E.i(' & '.join(data.authors)),
197 class_='cs-upload-job-title',
198 ))
199 done_msg = _('Added successfully') + ' '
200 container.appendChild(E.span(done_msg, class_='cs-add-books-done-msg'))
201 q = {'book_id': data.book_id + ''}
202 a = create_button(_('Open'), action=query_as_href(q, 'book_details'))
203 if state.number > 1:
204 a.setAttribute('target', '_blank')
205 else:
206 a.addEventListener('click', def (e):
207 e.preventDefault()
208 show_panel('book_details', {'book_id': data.book_id + ''}, True)
209 )
210 container.appendChild(a)
211
212
213 def list_duplicate_book(container, container_id, job_id, data, file):
214 container.appendChild(E.div(
215 _('A book with the title "{0}" already exists in the library.').format(data.title),
216 class_='cs-add-books-dup-hint',
217 ))
218 b = create_button(_('Add anyway'), action=def ():
219 c = get_job_container(container_id, job_id)
220 clear(c)
221 w = upload_status_widget(file.name, job_id)
222 c.appendChild(w)
223 send_file(file, container_id, job_id, True)
224 )
225
226 def list_duplicates():
227 ans = E.ol(class_='cs-add-books-dup-ol')
228 for item in data.duplicates:
229 ans.appendChild(E.li(_('{0} by {1}').format(item.title, ' & '.join(item.authors))))
230 return ans
231
232 container.appendChild(b)
233 container.appendChild(E.div(
234 class_='cs-add-books-dup-detail',
235 _('The book you are trying to add is:'),
236 E.span(' ', E.b(data.title), ' ', _('by'), ' ', E.i(' & '.join(data.authors))),
237 _('. Books already in the library with that title are:'),
238 E.div(class_='cs-add-books-dup-list', list_duplicates())
239 ))
240
241
242 def write_access_error(msg, xhr):
243 html = msg + '<br>'
244 if xhr.status is 403:
245 un = get_interface_data().username
246 if un:
247 html += _('You are not allowed to make changes to the library') + '<hr>'
248 else:
249 html += _('You must be logged in to make changes to the library') + '<hr>'
250 return html + '<br>' + xhr.error_html
251
252
253 def on_complete(container_id, job_id, end_type, xhr, ev):
254 idx = state.transfers.indexOf(xhr)
255 if idx > -1:
256 state.transfers.splice(idx, 1)
257 container = get_job_container(container_id, job_id)
258 if not container:
259 return
260 clear(container)
261 if end_type is 'load':
262 data = JSON.parse(xhr.responseText)
263 if data.book_id:
264 list_added_book(container, data)
265 force_refresh_on_next_load()
266 else:
267 list_duplicate_book(container, container_id, job_id, data, this)
268 elif end_type is 'abort':
269 return
270 else:
271 html = write_access_error(_('Failed to upload the file: {}').format(this.name), xhr)
272 safe_set_inner_html(container, '<div class="cs-add-books-error">' + html + '</div>')
273
274
275 def fake_send(container_id, job_id):
276 container = get_job_container(container_id, job_id)
277 prev = parseInt(container.dataset.fake or '0')
278 container.dataset.fake = prev + 10
279 update_status_widget(container, container.dataset.fake, 100)
280 if parseInt(container.dataset.fake or '0') < 100:
281 setTimeout(fake_send.bind(None, container_id, job_id), 1000)
282
283
284 def send_file(file, container_id, job_id, add_duplicates):
285 lid = loaded_books_query().library_id
286 ad = 'y' if add_duplicates else 'n'
287 xhr = ajax_send_file(
288 f'cdb/add-book/{job_id}/{ad}/{encodeURIComponent(file.name)}/{lid}',
289 file, on_complete.bind(file, container_id, job_id), on_progress.bind(None, container_id, job_id))
290 state.transfers.push(xhr)
291 return xhr
292
293
294 def files_chosen(container_id, files):
295 container = document.getElementById(container_id)
296 if not container:
297 return
298 state.number = 0
299 for file in files:
300 state.counter += 1
301 state.number += 1
302 job_id = state.counter
303 w = upload_status_widget(file.name, job_id)
304 container.appendChild(w)
305 if state.fake_send:
306 setTimeout(fake_send.bind(None, container_id, job_id), 100)
307 else:
308 send_file(file, container_id, job_id)
309
310
311 def add_books_panel(container_id):
312 container = document.getElementById(container_id)
313 container.classList.add('cs-add-books')
314 create_top_bar(container, title=_('Add books'), action=on_close, icon='close')
315 cancel_in_progress()
316 state.in_progress = True
317 state.container_id = container_id
318 state.fake_send = False
319 enable_escape_key(container, on_close)
320 return upload_files_widget(container, files_chosen)
321
322
323 def develop(container):
324 add_books_panel(ensure_id(container))