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" ] ;
2020-08-11 11:32:39 +00:00
exports . after = [ "startup" ] ;
2019-02-03 12:51:15 +00:00
exports . synchronous = true ;
2019-03-01 22:06:14 +00:00
var ENABLED _TITLE = "$:/config/BrowserStorage/Enabled" ,
2023-01-17 22:12:18 +00:00
SAVE _FILTER _TITLE = "$:/config/BrowserStorage/SaveFilter" ;
var BrowserStorageUtil = require ( "$:/plugins/tiddlywiki/browser-storage/util.js" ) . BrowserStorageUtil ;
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 ;
2023-01-17 22:12:18 +00:00
// If not exists, add ENABLED tiddler with default value "yes"
if ( ! $tw . wiki . getTiddler ( ENABLED _TITLE ) ) {
$tw . wiki . addTiddler ( { title : ENABLED _TITLE , text : "yes" } ) ;
}
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"
} ) ;
2023-01-17 22:12:18 +00:00
// Add browserStorage object to $tw
$tw . browserStorage = new BrowserStorageUtil ( $tw . wiki , {
enabledTitle : ENABLED _TITLE ,
prefix : prefix ,
logger : logger
} ) ;
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 ) {
$tw . wiki . addTiddler ( { title : ENABLED _TITLE , text : "no" } ) ;
2023-01-17 22:12:18 +00:00
$tw . browserStorage . clearLocalStorage ( ) ;
2019-03-01 22:06:14 +00:00
} ) ;
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-03-03 11:30:14 +00:00
// Save the tiddler
2023-01-17 22:12:18 +00:00
$tw . browserStorage . saveTiddlerToLocalStorage ( title ) ;
2019-02-03 12:51:15 +00:00
} ) ;
} ) ;
} ;
} ) ( ) ;