2019-02-03 12:51:15 +00:00
/ * \
title : $ : / p l u g i n s / t i d d l y w i k i / b r o w s e r - s t o r a g e / s t a r t u p . j s
type : application / javascript
module - type : startup
Startup initialisation
\ * /
( function ( ) {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict" ;
// Export name and synchronous status
exports . name = "browser-storage" ;
exports . platforms = [ "browser" ] ;
exports . after = [ "load-modules" ] ;
exports . synchronous = true ;
2019-03-01 22:06:14 +00:00
var ENABLED _TITLE = "$:/config/BrowserStorage/Enabled" ,
SAVE _FILTER _TITLE = "$:/config/BrowserStorage/SaveFilter" ,
2019-02-07 14:46:23 +00:00
QUOTA _EXCEEDED _ALERT _TITLE = "$:/config/BrowserStorage/QuotaExceededAlert" ,
DEFAULT _QUOTA _EXCEEDED _ALERT _PREFIX = "Quota exceeded attempting to store `" ,
DEFAULT _QUOTA _EXCEEDED _ALERT _SUFFIX = "` in browser local storage" ;
2019-02-07 14:35:25 +00:00
2019-02-03 12:51:15 +00:00
exports . startup = function ( ) {
2019-02-07 14:35:25 +00:00
var self = this ;
2019-02-03 12:51:15 +00:00
// Compute our prefix for local storage keys
2019-02-08 15:19:20 +00:00
var prefix = "tw5#" + window . location . pathname + "#" ;
2019-02-03 17:29:32 +00:00
// Make a logger
var logger = new $tw . utils . Logger ( "browser-storage" , {
colour : "cyan"
} ) ;
2019-02-07 14:35:25 +00:00
// Function to compile the filter
var filterFn ,
compileFilter = function ( ) {
filterFn = $tw . wiki . compileFilter ( $tw . wiki . getTiddlerText ( SAVE _FILTER _TITLE ) ) ;
}
compileFilter ( ) ;
2019-03-01 22:06:14 +00:00
// Listen for tm-clear-browser-storage messages
$tw . rootWidget . addEventListener ( "tm-clear-browser-storage" , function ( event ) {
window . localStorage . clear ( ) ;
$tw . wiki . addTiddler ( { title : ENABLED _TITLE , text : "no" } ) ;
} ) ;
2019-02-03 12:51:15 +00:00
// Track tiddler changes
$tw . wiki . addEventListener ( "change" , function ( changes ) {
2019-03-01 22:06:14 +00:00
// Bail if browser storage is disabled
if ( $tw . wiki . getTiddlerText ( ENABLED _TITLE ) === "no" ) {
return ;
}
2019-02-07 14:35:25 +00:00
// Recompile the filter if it has changed
if ( changes [ SAVE _FILTER _TITLE ] ) {
compileFilter ( ) ;
}
// Filter the changes
var filteredChanges = filterFn . call ( $tw . wiki , function ( iterator ) {
$tw . utils . each ( changes , function ( change , title ) {
var tiddler = $tw . wiki . getTiddler ( title ) ;
iterator ( tiddler , title ) ;
} ) ;
} ) ;
$tw . utils . each ( filteredChanges , function ( title ) {
2019-03-01 22:06:14 +00:00
// Don't try to save changes to our enabled status
// (If it were enabled in the file but disabled in local storage then we might not realise that distributing a copy of the file would have local storage enabled for other users)
if ( title === ENABLED _TITLE ) {
return ;
}
2019-02-03 17:29:32 +00:00
// Get the tiddler
2019-02-03 12:51:15 +00:00
var tiddler = $tw . wiki . getTiddler ( title ) ;
if ( tiddler ) {
2019-02-03 17:29:32 +00:00
// Get the JSON of the tiddler
2019-02-03 12:51:15 +00:00
var json = JSON . stringify ( tiddler . getFieldStrings ( ) ) ;
2019-02-03 17:29:32 +00:00
// Try to save it to local storage
try {
window . localStorage . setItem ( prefix + title , json ) ;
} catch ( e ) {
if ( e . name === "QuotaExceededError" ) {
// Complain if we failed
2019-02-07 14:46:23 +00:00
var msg = $tw . wiki . getTiddlerText ( QUOTA _EXCEEDED _ALERT _TITLE , DEFAULT _QUOTA _EXCEEDED _ALERT _PREFIX + title + DEFAULT _QUOTA _EXCEEDED _ALERT _SUFFIX ) ;
logger . alert ( msg ) ;
2019-02-03 17:29:32 +00:00
// No point in keeping old values around for this tiddler
window . localStorage . removeItem ( prefix + title ) ;
} else {
throw e ;
}
}
2019-02-03 12:51:15 +00:00
console . log ( "browser-storage: Saving" , title ) ;
} else {
window . localStorage . removeItem ( prefix + title ) ;
console . log ( "browser-storage: Deleting" , title ) ;
}
} ) ;
} ) ;
} ;
} ) ( ) ;