Unified data cache into single utility class
This commit is contained in:
@@ -19,17 +19,14 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { get_collections } from "../api/api.js";
|
||||
import { discover_server_features } from "../api/sharing.js";
|
||||
|
||||
import { SERVER } from "../constants.js";
|
||||
import { Collection, CollectionType } from "../models/collection.js";
|
||||
import { collectionsCache } from "../utils/collections_cache.js";
|
||||
import { bytesToHumanReadable } from "../utils/misc.js";
|
||||
import { CreateEditCollectionScene } from "./CreateEditCollectionScene.js";
|
||||
import { DeleteCollectionScene } from "./DeleteCollectionScene.js";
|
||||
import { IncomingSharingScene } from "./IncomingSharingScene.js";
|
||||
import { LoadingScene } from "./LoadingScene.js";
|
||||
import { Scene, is_current_scene, pop_scene, push_scene } from "./scene_manager.js";
|
||||
import { Scene, push_scene } from "./scene_manager.js";
|
||||
import { ShareCollectionScene, maybe_enable_sharing_options } from "./ShareCollectionScene.js";
|
||||
import { UploadCollectionScene } from "./UploadCollectionScene.js";
|
||||
|
||||
@@ -51,8 +48,6 @@ export class CollectionsScene {
|
||||
/** @type {HTMLElement} */ let upload_btn = html_scene.querySelector("[data-name=upload]");
|
||||
/** @type {HTMLElement} */ let incomingshares_btn = html_scene.querySelector("[data-name=incomingshares]");
|
||||
|
||||
/** @type {?XMLHttpRequest} */ let collections_req = null;
|
||||
/** @type {?Array<Collection>} */ let child_collections = null;
|
||||
/** @type {Array<HTMLElement>} */ let nodes = [];
|
||||
|
||||
function onnew() {
|
||||
@@ -132,6 +127,13 @@ export class CollectionsScene {
|
||||
let heightOfNavBar = navBar.offsetHeight + "px";
|
||||
html_scene.style.marginTop = heightOfNavBar;
|
||||
html_scene.style.height = "calc(100vh - " + heightOfNavBar + ")";
|
||||
|
||||
// Clear old nodes
|
||||
nodes.forEach(function (node) {
|
||||
node.parentNode.removeChild(node);
|
||||
});
|
||||
nodes = [];
|
||||
|
||||
collections.forEach(function (/** @type {Collection} */ collection) {
|
||||
/** @type {HTMLElement} */ let node = /** @type {HTMLElement} */(template.cloneNode(true));
|
||||
node.classList.remove("hidden");
|
||||
@@ -192,43 +194,20 @@ export class CollectionsScene {
|
||||
});
|
||||
}
|
||||
|
||||
function update() {
|
||||
let loading_scene = new LoadingScene();
|
||||
push_scene(loading_scene);
|
||||
collections_req = get_collections(user, password, principal_collection, function (child_collections_, error) {
|
||||
if (!is_current_scene(loading_scene)) {
|
||||
return;
|
||||
}
|
||||
collections_req = null;
|
||||
if (error) {
|
||||
onerror(error);
|
||||
pop_scene();
|
||||
} else {
|
||||
child_collections = child_collections_;
|
||||
pop_scene();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.show = function () {
|
||||
html_scene.classList.remove("hidden");
|
||||
new_btn.onclick = onnew;
|
||||
upload_btn.onclick = onupload;
|
||||
incomingshares_btn.onclick = onincomingshares;
|
||||
if (child_collections === null) {
|
||||
update();
|
||||
discover_server_features(user, password, maybe_enable_sharing_options);
|
||||
} else {
|
||||
// from update loading scene
|
||||
show_collections(child_collections);
|
||||
}
|
||||
collectionsCache.getChildCollections(user, password, principal_collection, onerror, show_collections);
|
||||
collectionsCache.getServerFeatures(user, password, null, maybe_enable_sharing_options);
|
||||
};
|
||||
this.hide = function () {
|
||||
html_scene.classList.add("hidden");
|
||||
new_btn.onclick = null;
|
||||
upload_btn.onclick = null;
|
||||
incomingshares_btn.onclick = null;
|
||||
child_collections = null;
|
||||
// remove collection
|
||||
nodes.forEach(function (node) {
|
||||
node.parentNode.removeChild(node);
|
||||
@@ -236,11 +215,6 @@ export class CollectionsScene {
|
||||
nodes = [];
|
||||
};
|
||||
this.release = function () {
|
||||
if (collections_req !== null) {
|
||||
collections_req.abort();
|
||||
collections_req = null;
|
||||
}
|
||||
child_collections = null;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@
|
||||
import { create_collection, edit_collection } from "../api/api.js";
|
||||
import { COLOR_RE } from "../constants.js";
|
||||
import { Collection, CollectionType } from "../models/collection.js";
|
||||
import { collectionsCache } from "../utils/collections_cache.js";
|
||||
import { ErrorHandler } from "../utils/error.js";
|
||||
import { FormValidator, validate_color, validate_href } from "../utils/form_validator.js";
|
||||
import { cleanHREFinput, onCleanHREFinput, random_hex, random_uuid } from "../utils/misc.js";
|
||||
@@ -140,6 +141,7 @@ export class CreateEditCollectionScene {
|
||||
errorHandler.setError(error1);
|
||||
pop_scene();
|
||||
} else {
|
||||
collectionsCache.invalidate();
|
||||
pop_to_parent();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
import { delete_collection } from "../api/api.js";
|
||||
import { DELETE_CONFIRMATION_TEXT } from "../constants.js";
|
||||
import { Collection } from "../models/collection.js";
|
||||
import { collectionsCache } from "../utils/collections_cache.js";
|
||||
import { ErrorHandler } from "../utils/error.js";
|
||||
import { FormValidator, validate_equals } from "../utils/form_validator.js";
|
||||
import { LoadingScene } from "./LoadingScene.js";
|
||||
@@ -72,6 +73,7 @@ export class DeleteCollectionScene {
|
||||
errorHandler.setError(error1);
|
||||
pop_scene();
|
||||
} else {
|
||||
collectionsCache.invalidate();
|
||||
pop_to_parent();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -19,11 +19,11 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Share, reload_sharing_list, update_incoming_share } from "../api/sharing.js";
|
||||
import { update_incoming_share } from "../api/sharing.js";
|
||||
import { collectionsCache } from "../utils/collections_cache.js";
|
||||
import { ErrorHandler } from "../utils/error.js";
|
||||
import { displayPermissions } from "../utils/permissions.js";
|
||||
import { LoadingScene } from "./LoadingScene.js";
|
||||
import { Scene, is_current_scene, pop_scene, push_scene } from "./scene_manager.js";
|
||||
import { Scene, pop_scene } from "./scene_manager.js";
|
||||
|
||||
/**
|
||||
* @implements {Scene}
|
||||
@@ -45,14 +45,13 @@ export class IncomingSharingScene {
|
||||
let error_handler = new ErrorHandler(error_element);
|
||||
|
||||
/** @type {Array<HTMLElement>} */ let nodes = [];
|
||||
/** @type {?Array<Object>} */ let shares_cache = null;
|
||||
|
||||
function on_cancel() {
|
||||
pop_scene();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Share} share
|
||||
* @param {import("../api/sharing.js").Share} share
|
||||
* @param {HTMLElement} node
|
||||
*/
|
||||
function toggle_share(share, node) {
|
||||
@@ -79,12 +78,14 @@ export class IncomingSharingScene {
|
||||
enabled_cb.checked = old_enabled;
|
||||
shown_cb.checked = !old_hidden;
|
||||
shown_cb.disabled = !old_enabled;
|
||||
} else {
|
||||
collectionsCache.invalidate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Share[]} shares
|
||||
* @param {import("../api/sharing.js").Share[]} shares
|
||||
*/
|
||||
function render_shares(shares) {
|
||||
// clear old nodes
|
||||
@@ -138,34 +139,11 @@ export class IncomingSharingScene {
|
||||
});
|
||||
}
|
||||
|
||||
function update() {
|
||||
let loading_scene = new LoadingScene();
|
||||
push_scene(loading_scene);
|
||||
|
||||
error_handler.clearError();
|
||||
|
||||
reload_sharing_list(user, password, null, function (shares, error) {
|
||||
if (!is_current_scene(loading_scene)) {
|
||||
return;
|
||||
}
|
||||
if (error) {
|
||||
error_handler.setError(error);
|
||||
pop_scene();
|
||||
} else {
|
||||
shares_cache = shares;
|
||||
pop_scene();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.show = function () {
|
||||
html_scene.classList.remove("hidden");
|
||||
cancel_btn.onclick = on_cancel;
|
||||
if (shares_cache === null) {
|
||||
update();
|
||||
} else {
|
||||
render_shares(shares_cache);
|
||||
}
|
||||
error_handler.clearError();
|
||||
collectionsCache.getIncomingShares(user, password, error_handler.setError, render_shares);
|
||||
};
|
||||
|
||||
this.hide = function () {
|
||||
@@ -182,7 +160,6 @@ export class IncomingSharingScene {
|
||||
}
|
||||
});
|
||||
nodes = [];
|
||||
shares_cache = null;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
import { get_principal } from "../api/api.js";
|
||||
import { collectionsCache } from "../utils/collections_cache.js";
|
||||
import { ErrorHandler } from "../utils/error.js";
|
||||
import { FormValidator, validate_non_empty } from "../utils/form_validator.js";
|
||||
import { CollectionsScene } from "./CollectionsScene.js";
|
||||
@@ -58,6 +59,7 @@ export class LoginScene {
|
||||
|
||||
function onlogin() {
|
||||
try {
|
||||
collectionsCache.invalidate();
|
||||
read_form();
|
||||
let password = password_form.value;
|
||||
if (!validator.validate()) {
|
||||
@@ -99,6 +101,7 @@ export class LoginScene {
|
||||
|
||||
let onlogout = function () {
|
||||
try {
|
||||
collectionsCache.invalidate();
|
||||
user = "";
|
||||
pop_to_root();
|
||||
} catch (err) {
|
||||
@@ -118,6 +121,7 @@ export class LoginScene {
|
||||
function refresh() {
|
||||
// The easiest way to refresh is to push a LoadingScene onto the stack and then pop it
|
||||
// forcing the scene below it, the Collections Scene to refresh itself.
|
||||
collectionsCache.invalidate();
|
||||
push_scene(new LoadingScene());
|
||||
pop_scene();
|
||||
}
|
||||
|
||||
@@ -23,9 +23,9 @@ import {
|
||||
delete_share_by_map,
|
||||
delete_share_by_token,
|
||||
reload_sharing_list,
|
||||
server_features,
|
||||
} from "../api/sharing.js";
|
||||
import { Collection } from "../models/collection.js";
|
||||
import { collectionsCache } from "../utils/collections_cache.js";
|
||||
import { ErrorHandler } from "../utils/error.js";
|
||||
import { displayPermissions } from "../utils/permissions.js";
|
||||
import { CreateEditShareScene } from "./CreateEditShareScene.js";
|
||||
@@ -86,35 +86,38 @@ export class ShareCollectionScene {
|
||||
this.release();
|
||||
html_scene.classList.remove("hidden");
|
||||
cancel_btn.onclick = oncancel;
|
||||
if (server_features.sharing && server_features.sharing.PermittedCreateCollectionByToken) {
|
||||
if (share_by_token_btn) {
|
||||
share_by_token_btn.classList.remove("hidden");
|
||||
share_by_token_btn.onclick = onsharebytoken;
|
||||
|
||||
collectionsCache.getServerFeatures(user, password, errorHandler.setError, (features) => {
|
||||
if (features.sharing && features.sharing.PermittedCreateCollectionByToken) {
|
||||
if (share_by_token_btn) {
|
||||
share_by_token_btn.classList.remove("hidden");
|
||||
share_by_token_btn.onclick = onsharebytoken;
|
||||
}
|
||||
} else {
|
||||
if (share_by_token_btn) share_by_token_btn.classList.add("hidden");
|
||||
}
|
||||
} else {
|
||||
if (share_by_token_btn) share_by_token_btn.classList.add("hidden");
|
||||
}
|
||||
|
||||
if (server_features.sharing && server_features.sharing.FeatureEnabledCollectionByToken) {
|
||||
if (share_by_token_div) share_by_token_div.classList.remove("hidden");
|
||||
} else {
|
||||
if (share_by_token_div) share_by_token_div.classList.add("hidden");
|
||||
}
|
||||
|
||||
if (server_features.sharing && server_features.sharing.PermittedCreateCollectionByMap) {
|
||||
if (share_by_map_btn) {
|
||||
share_by_map_btn.classList.remove("hidden");
|
||||
share_by_map_btn.onclick = onsharebymap;
|
||||
if (features.sharing && features.sharing.FeatureEnabledCollectionByToken) {
|
||||
if (share_by_token_div) share_by_token_div.classList.remove("hidden");
|
||||
} else {
|
||||
if (share_by_token_div) share_by_token_div.classList.add("hidden");
|
||||
}
|
||||
} else {
|
||||
if (share_by_map_btn) share_by_map_btn.classList.add("hidden");
|
||||
}
|
||||
|
||||
if (server_features.sharing && server_features.sharing.FeatureEnabledCollectionByMap) {
|
||||
if (share_by_map_div) share_by_map_div.classList.remove("hidden");
|
||||
} else {
|
||||
if (share_by_map_div) share_by_map_div.classList.add("hidden");
|
||||
}
|
||||
if (features.sharing && features.sharing.PermittedCreateCollectionByMap) {
|
||||
if (share_by_map_btn) {
|
||||
share_by_map_btn.classList.remove("hidden");
|
||||
share_by_map_btn.onclick = onsharebymap;
|
||||
}
|
||||
} else {
|
||||
if (share_by_map_btn) share_by_map_btn.classList.add("hidden");
|
||||
}
|
||||
|
||||
if (features.sharing && features.sharing.FeatureEnabledCollectionByMap) {
|
||||
if (share_by_map_div) share_by_map_div.classList.remove("hidden");
|
||||
} else {
|
||||
if (share_by_map_div) share_by_map_div.classList.add("hidden");
|
||||
}
|
||||
});
|
||||
|
||||
title.textContent = collection.displayname || collection.href;
|
||||
update_share_list(user, password, collection, errorHandler);
|
||||
@@ -231,10 +234,13 @@ function add_share_rows(user, password, collection, shares, errorHandler) {
|
||||
});
|
||||
}
|
||||
|
||||
export function maybe_enable_sharing_options() {
|
||||
if (!server_features.sharing) return;
|
||||
let map_is_enabled = server_features.sharing.FeatureEnabledCollectionByMap || false;
|
||||
let token_is_enabled = server_features.sharing.FeatureEnabledCollectionByToken || false;
|
||||
/**
|
||||
* @param {import('../api/sharing.js').ServerFeatures} features
|
||||
*/
|
||||
export function maybe_enable_sharing_options(features) {
|
||||
if (!features || !features.sharing) return;
|
||||
let map_is_enabled = features.sharing.FeatureEnabledCollectionByMap || false;
|
||||
let token_is_enabled = features.sharing.FeatureEnabledCollectionByToken || false;
|
||||
if (map_is_enabled || token_is_enabled) {
|
||||
let share_options = document.querySelectorAll("[data-name=shareoption]");
|
||||
for (let i = 0; i < share_options.length; i++) {
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
import { upload_collection } from "../api/api.js";
|
||||
import { Collection } from "../models/collection.js";
|
||||
import { collectionsCache } from "../utils/collections_cache.js";
|
||||
import { ErrorHandler } from "../utils/error.js";
|
||||
import { FormValidator, validate_files, validate_href } from "../utils/form_validator.js";
|
||||
import { cleanHREFinput, onCleanHREFinput, random_uuid } from "../utils/misc.js";
|
||||
@@ -127,6 +128,9 @@ export class UploadCollectionScene {
|
||||
|
||||
function onclose() {
|
||||
try {
|
||||
if (results.length > 0) {
|
||||
collectionsCache.invalidate();
|
||||
}
|
||||
pop_scene();
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
Reference in New Issue
Block a user