Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions assets/javascripts/templates/pages/offline_tmpl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
app.templates.offlinePage = (docs) => `\
app.templates.offlinePage = (docs, hasPersistence, isPersistent) => `\
<h1 class="_lined-heading">Offline Documentation</h1>

<div class="_docs-tools">
Expand All @@ -23,7 +23,9 @@ app.templates.offlinePage = (docs) => `\
${docs}
</table>
</div>
<p class="_note"><strong>Note:</strong> your browser may delete DevDocs's offline data if your computer is running low on disk space and you haven't used the app in a while. Load this page before going offline to make sure the data is still there.
<div id="_offline-persistence-note">
${offlinePersistenceNote(hasPersistence, isPersistent)}
</div>
<h2 class="_block-heading">Questions & Answers</h2>
<dl>
<dt>How does this work?
Expand All @@ -42,6 +44,33 @@ app.templates.offlinePage = (docs) => `\
</dl>\
`;

app.templates.persistenceError = function (exception) {
const reason = exception
? `<code class="_label">${exception.name}: ${exception.message}</code>`
: "Bookmark this site and try again.";

return `<p class="_note _note-red"><strong>Persistent storage was denied by your browser.</strong> ${reason}`;
};

var offlinePersistenceNote = function (hasPersistence, isPersistent) {
if (isPersistent) {
return "";
}

let html =
"<p class=\"_note\"><strong>Note:</strong> your browser may delete DevDocs's offline data if your computer is running low on disk space and you haven't used the app in a while.";

if (hasPersistence) {
html +=
' <button type="button" class="_btn-link _bold" data-enable-persistence>Enable persistent storage</button>.';
} else {
html +=
" Load this page before going offline to make sure the data is still there.";
}

return html;
};

var canICloseTheTab = function () {
if (app.ServiceWorker.isEnabled()) {
return ' Yes! Even offline, you can open a new tab, go to <a href="//devdocs.io">devdocs.io</a>, and everything will work as if you were online (provided you installed all the documentations you want to use beforehand). ';
Expand Down
54 changes: 48 additions & 6 deletions assets/javascripts/views/content/offline_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ app.views.OfflinePage = class OfflinePage extends app.View {
if (statuses === false) {
this.html(this.tmpl("offlineError", app.db.reason, app.db.error));
} else {
let html = "";
for (var doc of app.docs.all()) {
html += this.renderDoc(doc, statuses[doc.slug]);
}
this.html(this.tmpl("offlinePage", html));
this.refreshLinks();
this.checkPersistence((hasPersistence, isPersistent) => {
if (!this.activated) {
return;
}
let html = "";
for (var doc of app.docs.all()) {
html += this.renderDoc(doc, statuses[doc.slug]);
}
this.html(
this.tmpl("offlinePage", html, hasPersistence, isPersistent)
);
this.refreshLinks();
});
}
});
}
Expand Down Expand Up @@ -93,6 +100,8 @@ app.views.OfflinePage = class OfflinePage extends app.View {
for (el of Array.from(this.findAll(`[data-action='${action}']`))) {
$.click(el);
}
} else if (el.hasAttribute("data-enable-persistence")) {
this.requestPersistence();
}
}

Expand Down Expand Up @@ -142,4 +151,37 @@ app.views.OfflinePage = class OfflinePage extends app.View {
app.settings.set("manualUpdate", !event.target.checked);
}
}

checkPersistence(callback) {
if (navigator.storage && navigator.storage.persisted) {
navigator.storage
.persisted()
.then((persisted) => callback(true, persisted))
.catch(() => callback(false, false));
} else {
callback(false, false);
}
}

requestPersistence() {
navigator.storage
.persist()
.then((success) => this.onPersistenceRequestCompleted(success))
.catch((exception) =>
this.onPersistenceRequestCompleted(false, exception)
);
}

onPersistenceRequestCompleted(success, exception) {
if (!this.activated) {
return;
}
const note = this.find("#_offline-persistence-note");
if (!note) {
return;
}
// Granting persistence retires the note, which is what a fresh render of
// the page would produce; the disappearing button is the confirmation.
note.innerHTML = success ? "" : this.tmpl("persistenceError", exception);
}
};
1 change: 1 addition & 0 deletions assets/stylesheets/components/_content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@
._bold { font-weight: var(--boldFontWeight); }
._note { @extend %note; }
._note-green { @extend %note-green; }
._note-red { @extend %note-red; }
._label { @extend %label; }
._code { @extend %code; }
._highlight, ._highlight > td { background: var(--highlightBackground) !important; }
Expand Down