Showing the changes to assets/javascripts (4 of 4 files). Show all
Unified · Split
| @@ -103,6 +103,26 @@ class app.DB · 102 unmodified lines | ||
| 103 | 103 | return |
| 104 | 104 | return |
| 105 | 105 | |
| 106 | + versions: (docs, fn) -> | |
| 107 | + @db (db) -> | |
| 108 | + result = {} | |
| 109 | + | |
| 110 | + unless db | |
| 111 | + result[doc.slug] = false for doc in docs | |
| 112 | + fn(result) | |
| 113 | + return | |
| 114 | + | |
| 115 | + txn = db.transaction ['docs'], 'readonly' | |
| 116 | + txn.oncomplete = -> fn(result) | |
| 117 | + store = txn.objectStore('docs') | |
| 118 | + | |
| 119 | + docs.forEach (doc) -> | |
| 120 | + req = store.get(doc.slug) | |
| 121 | + req.onsuccess = -> result[doc.slug] = req.result | |
| 122 | + req.onerror = -> result[doc.slug] = false | |
| 123 | + return | |
| 124 | + return | |
| 125 | + | |
| 106 | 126 | load: (entry, onSuccess, onError) -> |
| 107 | 127 | if @useIndexedDB and @_db isnt false |
| 108 | 128 | onError = @loadWithXHR.bind(@, entry, onSuccess, onError) |
| @@ -34,3 +34,9 @@ class app.collections.Docs extends app.Collection · 33 unmodified lines | ||
| 34 | 34 | clearCache: -> |
| 35 | 35 | doc.clearCache() for doc in @models |
| 36 | 36 | return |
| 37 | + | |
| 38 | + getDownloadStatuses: (callback) -> | |
| 39 | + app.db.versions @models, (statuses) -> | |
| 40 | + for key, value of statuses | |
| 41 | + statuses[key] = downloaded: !!value, version: value | |
| 42 | + callback(statuses) | |
| @@ -1,16 +1,13 @@ | ||
| 1 | −app.templates.offlinePage = -> | |
| 1 | +app.templates.offlinePage = (docs) -> | |
| 2 | 2 | """ <h1 class="_lined-heading">Offline</h1> |
| 3 | − <table class="_docs"> | |
| 4 | − #{app.templates.render 'offlineDoc', app.docs.all()} | |
| 5 | − </table> """ | |
| 3 | + <table class="_docs">#{docs}</table> """ | |
| 6 | 4 | |
| 7 | −app.templates.offlineDoc = (doc) -> | |
| 8 | − """<tr data-slug="#{doc.slug}"></tr>""" | |
| 9 | − | |
| 10 | −app.templates.offlineDocContent = (doc, status) -> | |
| 11 | − html = """<th class="_icon-#{doc.slug}">#{doc.name}</th>""" | |
| 5 | +app.templates.offlineDoc = (doc, status) -> | |
| 6 | + html = """<tr data-slug="#{doc.slug}">""" | |
| 7 | + html += """<th class="_icon-#{doc.slug}">#{doc.name}</th>""" | |
| 12 | 8 | html += if status.downloaded |
| 13 | 9 | """<td><a data-del>Delete</a></td>""" |
| 14 | 10 | else |
| 15 | 11 | """<td><a data-dl>Download</a></td>""" |
| 12 | + html += """</tr>""" | |
| 16 | 13 | html |
| @@ -4,32 +4,26 @@ class app.views.OfflinePage extends app.View · 3 unmodified lines | ||
| 4 | 4 | @events: |
| 5 | 5 | click: 'onClick' |
| 6 | 6 | |
| 7 | − @elements: | |
| 8 | − list: '_._docs' | |
| 9 | − | |
| 10 | 7 | deactivate: -> |
| 11 | 8 | if super |
| 12 | 9 | @empty() |
| 13 | 10 | return |
| 14 | 11 | |
| 15 | 12 | render: -> |
| 16 | − @html @tmpl('offlinePage') | |
| 17 | − @refreshElements() | |
| 18 | − app.docs.each(@renderDoc) | |
| 13 | + app.docs.getDownloadStatuses (statuses) => | |
| 14 | + html = '' | |
| 15 | + html += @renderDoc(doc, statuses[doc.slug]) for doc in app.docs.all() | |
| 16 | + @html @tmpl('offlinePage', html) | |
| 17 | + return | |
| 19 | 18 | return |
| 20 | 19 | |
| 21 | − renderDoc: (doc) => | |
| 22 | − doc.getDownloadStatus (status) => | |
| 23 | − html = app.templates.render('offlineDocContent', doc, status) | |
| 24 | − el = @docEl(doc) | |
| 25 | − el.className = '' | |
| 26 | − el.innerHTML = html | |
| 27 | − return | |
| 20 | + renderDoc: (doc, status) -> | |
| 21 | + app.templates.render('offlineDoc', doc, status) | |
| 28 | 22 | |
| 29 | 23 | getTitle: -> |
| 30 | 24 | 'Offline' |
| 31 | 25 | |
| 32 | − getDoc: (el) -> | |
| 26 | + docByEl: (el) -> | |
| 33 | 27 | el = el.parentNode until slug = el.getAttribute('data-slug') |
| 34 | 28 | app.docs.findBy('slug', slug) |
| 35 | 29 | |
| @@ -48,16 +42,18 @@ class app.views.OfflinePage extends app.View · 12 unmodified lines | ||
| 48 | 42 | |
| 49 | 43 | if action |
| 50 | 44 | $.stopEvent(event) |
| 51 | − doc = @getDoc(event.target) | |
| 45 | + doc = @docByEl(event.target) | |
| 52 | 46 | doc[action](@onDownloadSuccess.bind(@, doc), @onDownloadError.bind(@, doc)) |
| 53 | 47 | @docEl(doc).classList.add("#{action}ing") |
| 54 | 48 | return |
| 55 | 49 | |
| 56 | 50 | onDownloadSuccess: (doc) -> |
| 57 | − @renderDoc(doc) | |
| 51 | + doc.getDownloadStatus (status) => | |
| 52 | + @docEl(doc).outerHTML = @renderDoc(doc, status) | |
| 58 | 53 | return |
| 59 | 54 | |
| 60 | 55 | onDownloadError: (doc) -> |
| 61 | 56 | el = @docEl(doc) |
| 62 | 57 | el.className = '' |
| 63 | 58 | el.classList.add('error') |
| 59 | + return | |