Browse Source

Show progress percentage when installing docs

Closes #449.
Thibaut Courouble 9 years ago
parent
commit
8d7b2029b8

+ 2 - 0
assets/javascripts/lib/ajax.coffee

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

+ 2 - 1
assets/javascripts/models/doc.coffee

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

+ 8 - 1
assets/javascripts/views/content/offline_page.coffee

@@ -54,7 +54,7 @@ class app.views.OfflinePage extends app.View
       $.stopEvent(event)
       doc = @docByEl(link)
       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…"
     else if action = link.getAttribute('data-action-all')
       $.stopEvent(event)
@@ -79,6 +79,13 @@ class app.views.OfflinePage extends app.View
       el.lastElementChild.textContent = 'Error'
     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) ->
     if event.target.name is 'autoUpdate'
       app.settings.set 'manualUpdate', !event.target.checked