mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-30 05:19:57 +00:00
Update tw.utils.Logger using tw.utils.Logger
This commit is contained in:
parent
eb38b0238a
commit
08577b7fdb
@ -25,23 +25,20 @@ var ENABLED_TITLE = "$:/config/BrowserStorage/Enabled",
|
|||||||
var BrowserStorageUtil = require("$:/plugins/tiddlywiki/browser-storage/util.js").BrowserStorageUtil;
|
var BrowserStorageUtil = require("$:/plugins/tiddlywiki/browser-storage/util.js").BrowserStorageUtil;
|
||||||
|
|
||||||
exports.startup = function() {
|
exports.startup = function() {
|
||||||
var self = this;
|
|
||||||
|
|
||||||
// If not exists, add ENABLED tiddler with default value "yes"
|
// If not exists, add ENABLED tiddler with default value "yes"
|
||||||
if(!$tw.wiki.getTiddler(ENABLED_TITLE)) {
|
if(!$tw.wiki.getTiddler(ENABLED_TITLE)) {
|
||||||
$tw.wiki.addTiddler({title: ENABLED_TITLE, text: "yes"});
|
$tw.wiki.addTiddler({title: ENABLED_TITLE, text: "yes"});
|
||||||
}
|
}
|
||||||
// Compute our prefix for local storage keys
|
// Compute our prefix for local storage keys
|
||||||
var prefix = "tw5#" + window.location.pathname + "#";
|
var prefix = "tw5#" + window.location.pathname + "#";
|
||||||
// Make a logger
|
|
||||||
var logger = new $tw.utils.Logger("browser-storage",{
|
|
||||||
colour: "cyan"
|
|
||||||
});
|
|
||||||
// Add browserStorage object to $tw
|
// Add browserStorage object to $tw
|
||||||
$tw.browserStorage = new BrowserStorageUtil($tw.wiki,{
|
$tw.browserStorage = new BrowserStorageUtil($tw.wiki,{
|
||||||
enabledTitle: ENABLED_TITLE,
|
enabledTitle: ENABLED_TITLE,
|
||||||
prefix: prefix,
|
prefix: prefix
|
||||||
logger: logger
|
});
|
||||||
|
// Add a logger
|
||||||
|
$tw.browserStorage.logger = new $tw.utils.Logger("browser-storage",{
|
||||||
|
colour: "green"
|
||||||
});
|
});
|
||||||
// Function to compile the filter
|
// Function to compile the filter
|
||||||
var filterFn,
|
var filterFn,
|
||||||
@ -60,19 +57,26 @@ exports.startup = function() {
|
|||||||
},
|
},
|
||||||
requestPersistence = function() {
|
requestPersistence = function() {
|
||||||
setPersistedState("requested");
|
setPersistedState("requested");
|
||||||
|
$tw.browserStorage.logger.info("Show user request for browser PERSISTED storage");
|
||||||
navigator.storage.persist().then(function(persisted) {
|
navigator.storage.persist().then(function(persisted) {
|
||||||
console.log("Request for persisted storage " + (persisted ? "granted" : "denied"));
|
if(persisted) {
|
||||||
setPersistedState(persisted ? "granted" : "denied");
|
$tw.browserStorage.logger.info("User GRANTED access to browser PERSISTED storage");
|
||||||
|
setPersistedState("granted");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$tw.browserStorage.logger.info("User DENIED access to persisted storage. Storage MAY BE CLEARED by the UA under storage pressure.");
|
||||||
|
setPersistedState("denied");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
persistPermissionRequested = false,
|
persistPermissionRequested = false,
|
||||||
requestPersistenceOnFirstSave = function() {
|
requestPersistenceOnFirstSave = function() {
|
||||||
$tw.hooks.addHook("th-saving-tiddler", function(tiddler) {
|
$tw.hooks.addHook("th-saving-tiddler", function(tiddler) {
|
||||||
if (!persistPermissionRequested) {
|
if(!persistPermissionRequested) {
|
||||||
var filteredChanges = filterFn.call($tw.wiki, function(iterator) {
|
var filteredChanges = filterFn.call($tw.wiki, function(iterator) {
|
||||||
iterator(tiddler,tiddler.getFieldString("title"));
|
iterator(tiddler,tiddler.getFieldString("title"));
|
||||||
});
|
});
|
||||||
if (filteredChanges.length > 0) {
|
if(filteredChanges.length > 0) {
|
||||||
// The tiddler will be saved to local storage, so request persistence
|
// The tiddler will be saved to local storage, so request persistence
|
||||||
requestPersistence();
|
requestPersistence();
|
||||||
persistPermissionRequested = true;
|
persistPermissionRequested = true;
|
||||||
@ -84,9 +88,9 @@ exports.startup = function() {
|
|||||||
// Request the browser to never evict the localstorage. Some browsers such as firefox
|
// Request the browser to never evict the localstorage. Some browsers such as firefox
|
||||||
// will prompt the user. To make the decision easier for the user only prompt them
|
// will prompt the user. To make the decision easier for the user only prompt them
|
||||||
// when they click the save button on a tiddler which will be stored to localstorage.
|
// when they click the save button on a tiddler which will be stored to localstorage.
|
||||||
if (navigator.storage && navigator.storage.persist) {
|
if(navigator.storage && navigator.storage.persist) {
|
||||||
navigator.storage.persisted().then(function(isPersisted) {
|
navigator.storage.persisted().then(function(isPersisted) {
|
||||||
if (!isPersisted) {
|
if(!isPersisted) {
|
||||||
setPersistedState("not requested yet");
|
setPersistedState("not requested yet");
|
||||||
requestPersistenceOnFirstSave();
|
requestPersistenceOnFirstSave();
|
||||||
} else {
|
} else {
|
||||||
|
@ -45,7 +45,7 @@ BrowserStorageUtil.prototype.addCachedTiddlers = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
BrowserStorageUtil.prototype.removeTiddlerFromLocalStorage = function(title) {
|
BrowserStorageUtil.prototype.removeTiddlerFromLocalStorage = function(title) {
|
||||||
console.log("browser-storage: Removing", title);
|
this.logger.log("Removing", title);
|
||||||
window.localStorage.removeItem(this.options.prefix + title);
|
window.localStorage.removeItem(this.options.prefix + title);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -53,9 +53,9 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
|
|||||||
// Get the tiddler
|
// Get the tiddler
|
||||||
var tiddler = $tw.wiki.getTiddler(title);
|
var tiddler = $tw.wiki.getTiddler(title);
|
||||||
if(tiddler) {
|
if(tiddler) {
|
||||||
if (this.wiki.tiddlerExists(title)) {
|
if(this.wiki.tiddlerExists(title)) {
|
||||||
// This is not a shadow tiddler
|
// This is not a shadow tiddler
|
||||||
console.log("browser-storage: Saving",title);
|
this.logger.log("Saving",title);
|
||||||
// Get the JSON of the tiddler
|
// Get the JSON of the tiddler
|
||||||
var json = JSON.stringify(tiddler.getFieldStrings());
|
var json = JSON.stringify(tiddler.getFieldStrings());
|
||||||
// Try to save it to local storage
|
// Try to save it to local storage
|
||||||
@ -65,13 +65,11 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
|
|||||||
if(e.name === "QuotaExceededError") {
|
if(e.name === "QuotaExceededError") {
|
||||||
// Complain if we failed
|
// Complain if we failed
|
||||||
var msg = $tw.wiki.getTiddlerText(this.QUOTA_EXCEEDED_ALERT_TITLE,this.DEFAULT_QUOTA_EXCEEDED_ALERT_PREFIX + title + this.DEFAULT_QUOTA_EXCEEDED_ALERT_SUFFIX);
|
var msg = $tw.wiki.getTiddlerText(this.QUOTA_EXCEEDED_ALERT_TITLE,this.DEFAULT_QUOTA_EXCEEDED_ALERT_PREFIX + title + this.DEFAULT_QUOTA_EXCEEDED_ALERT_SUFFIX);
|
||||||
if(this.options.logger) {
|
this.logger.alert(msg);
|
||||||
this.options.logger.alert(msg);
|
|
||||||
}
|
|
||||||
// No point in keeping old values around for this tiddler
|
// No point in keeping old values around for this tiddler
|
||||||
window.localStorage.removeItem(this.options.prefix + title);
|
window.localStorage.removeItem(this.options.prefix + title);
|
||||||
} else {
|
} else {
|
||||||
console.log("Browser-storage error:",e);
|
this.logger.log("Error:",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -83,18 +81,17 @@ BrowserStorageUtil.prototype.saveTiddlerToLocalStorage = function(title) {
|
|||||||
// In local storage, use the special value of empty string to mark the tiddler as deleted
|
// In local storage, use the special value of empty string to mark the tiddler as deleted
|
||||||
// On future page loads, if the tiddler is already gone from startup then the blank entry
|
// On future page loads, if the tiddler is already gone from startup then the blank entry
|
||||||
// will be removed from localstorage. Otherwise, the tiddler will be deleted.
|
// will be removed from localstorage. Otherwise, the tiddler will be deleted.
|
||||||
console.log("browser-storage: Blanking",title);
|
this.logger.log("Blanking",title);
|
||||||
try {
|
try {
|
||||||
window.localStorage.setItem(this.options.prefix + title, "");
|
window.localStorage.setItem(this.options.prefix + title, "");
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log("Browser-storage error:",e);
|
this.logger.error("Error:",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
BrowserStorageUtil.prototype.clearLocalStorage = function() {
|
BrowserStorageUtil.prototype.clearLocalStorage = function() {
|
||||||
var url = window.location.pathname,
|
var url = window.location.pathname;
|
||||||
log = [];
|
|
||||||
// Step through each browser storage item
|
// Step through each browser storage item
|
||||||
if(window.localStorage) {
|
if(window.localStorage) {
|
||||||
for(var index=window.localStorage.length - 1; index>=0; index--) {
|
for(var index=window.localStorage.length - 1; index>=0; index--) {
|
||||||
|
Loading…
Reference in New Issue
Block a user