diff --git a/assets/javascripts/lib/ajax.coffee b/assets/javascripts/lib/ajax.coffee index 6433de0d..33ab2d9b 100644 --- a/assets/javascripts/lib/ajax.coffee +++ b/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) diff --git a/assets/javascripts/models/doc.coffee b/assets/javascripts/models/doc.coffee index 44a6ff21..b61db7cf 100644 --- a/assets/javascripts/models/doc.coffee +++ b/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 diff --git a/assets/javascripts/views/content/offline_page.coffee b/assets/javascripts/views/content/offline_page.coffee index f93fe54e..bfba16a6 100644 --- a/assets/javascripts/views/content/offline_page.coffee +++ b/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