Show progress percentage when installing docs

Closes #449.
pull/417/merge
Thibaut Courouble 9 years ago
parent 3cd05ae6ae
commit 8d7b2029b8

@ -29,6 +29,7 @@ ajax.defaults =
# data # data
# error # error
# headers # headers
# progress
# success # success
# url # url
@ -54,6 +55,7 @@ applyCallbacks = (xhr, options) ->
return unless options.async return unless options.async
xhr.timer = setTimeout onTimeout.bind(undefined, xhr, options), options.timeout * 1000 xhr.timer = setTimeout onTimeout.bind(undefined, xhr, options), options.timeout * 1000
xhr.onprogress = options.progress if options.progress
xhr.onreadystatechange = -> xhr.onreadystatechange = ->
if xhr.readyState is 4 if xhr.readyState is 4
clearTimeout(xhr.timer) clearTimeout(xhr.timer)

@ -94,7 +94,7 @@ class app.models.Doc extends app.Model
app.localStorage.set @slug, [@mtime, data] app.localStorage.set @slug, [@mtime, data]
return return
install: (onSuccess, onError) -> install: (onSuccess, onError, onProgress) ->
return if @installing return if @installing
@installing = true @installing = true
@ -112,6 +112,7 @@ class app.models.Doc extends app.Model
url: @dbUrl() url: @dbUrl()
success: success success: success
error: error error: error
progress: onProgress
timeout: 3600 timeout: 3600
return return

@ -54,7 +54,7 @@ class app.views.OfflinePage extends app.View
$.stopEvent(event) $.stopEvent(event)
doc = @docByEl(link) doc = @docByEl(link)
action = 'install' if action is 'update' action = 'install' if action is 'update'
doc[action](@onInstallSuccess.bind(@, doc), @onInstallError.bind(@, doc)) doc[action](@onInstallSuccess.bind(@, doc), @onInstallError.bind(@, doc), @onInstallProgress.bind(@, doc))
link.parentNode.innerHTML = "#{link.textContent.replace(/e$/, '')}ing…" link.parentNode.innerHTML = "#{link.textContent.replace(/e$/, '')}ing…"
else if action = link.getAttribute('data-action-all') else if action = link.getAttribute('data-action-all')
$.stopEvent(event) $.stopEvent(event)
@ -79,6 +79,13 @@ class app.views.OfflinePage extends app.View
el.lastElementChild.textContent = 'Error' el.lastElementChild.textContent = 'Error'
return return
onInstallProgress: (doc, event) ->
return unless @activated and event.lengthComputable
if el = @docEl(doc)
percentage = Math.round event.loaded * 100 / event.total
el.lastElementChild.textContent = el.lastElementChild.textContent.replace(/(\s.+)?$/, " (#{percentage}%)")
return
onChange: (event) -> onChange: (event) ->
if event.target.name is 'autoUpdate' if event.target.name is 'autoUpdate'
app.settings.set 'manualUpdate', !event.target.checked app.settings.set 'manualUpdate', !event.target.checked

Loading…
Cancel
Save