2014-08-14 10:43:07 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/saver-handler.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: global
|
|
|
|
|
|
|
|
The saver handler tracks changes to the store and handles saving the entire wiki via saver modules.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Instantiate the saver handler with the following options:
|
|
|
|
wiki: wiki to be synced
|
2014-08-20 09:02:44 +00:00
|
|
|
dirtyTracking: true if dirty tracking should be performed
|
2014-08-14 10:43:07 +00:00
|
|
|
*/
|
|
|
|
function SaverHandler(options) {
|
2014-08-29 08:58:30 +00:00
|
|
|
var self = this;
|
|
|
|
this.wiki = options.wiki;
|
|
|
|
this.dirtyTracking = options.dirtyTracking;
|
|
|
|
this.pendingAutoSave = false;
|
|
|
|
// Make a logger
|
|
|
|
this.logger = new $tw.utils.Logger("saver-handler");
|
|
|
|
// Initialise our savers
|
|
|
|
if($tw.browser) {
|
|
|
|
this.initSavers();
|
|
|
|
}
|
|
|
|
// Only do dirty tracking if required
|
|
|
|
if($tw.browser && this.dirtyTracking) {
|
|
|
|
// Compile the dirty tiddler filter
|
|
|
|
this.filterFn = this.wiki.compileFilter(this.wiki.getTiddlerText(this.titleSyncFilter));
|
2014-09-01 19:39:08 +00:00
|
|
|
// Count of changes that have not yet been saved
|
|
|
|
this.numChanges = 0;
|
2014-08-29 08:58:30 +00:00
|
|
|
// Listen out for changes to tiddlers
|
|
|
|
this.wiki.addEventListener("change",function(changes) {
|
2014-09-01 19:39:08 +00:00
|
|
|
// Filter the changes so that we only count changes to tiddlers that we care about
|
2017-02-04 17:24:25 +00:00
|
|
|
var filteredChanges = self.filterFn.call(self.wiki,function(iterator) {
|
2014-08-29 08:58:30 +00:00
|
|
|
$tw.utils.each(changes,function(change,title) {
|
|
|
|
var tiddler = self.wiki.getTiddler(title);
|
2017-02-04 17:24:25 +00:00
|
|
|
iterator(tiddler,title);
|
2014-08-29 08:58:30 +00:00
|
|
|
});
|
|
|
|
});
|
2014-09-01 19:39:08 +00:00
|
|
|
// Adjust the number of changes
|
|
|
|
self.numChanges += filteredChanges.length;
|
2014-08-29 08:58:30 +00:00
|
|
|
self.updateDirtyStatus();
|
|
|
|
// Do any autosave if one is pending and there's no more change events
|
|
|
|
if(self.pendingAutoSave && self.wiki.getSizeOfTiddlerEventQueue() === 0) {
|
|
|
|
// Check if we're dirty
|
2014-09-01 19:39:08 +00:00
|
|
|
if(self.numChanges > 0) {
|
2014-08-29 08:58:30 +00:00
|
|
|
self.saveWiki({
|
|
|
|
method: "autosave",
|
|
|
|
downloadType: "text/plain"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
self.pendingAutoSave = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Listen for the autosave event
|
2014-09-02 10:16:06 +00:00
|
|
|
$tw.rootWidget.addEventListener("tm-auto-save-wiki",function(event) {
|
2014-08-29 08:58:30 +00:00
|
|
|
// Do the autosave unless there are outstanding tiddler change events
|
|
|
|
if(self.wiki.getSizeOfTiddlerEventQueue() === 0) {
|
|
|
|
// Check if we're dirty
|
2014-09-01 19:39:08 +00:00
|
|
|
if(self.numChanges > 0) {
|
2014-08-29 08:58:30 +00:00
|
|
|
self.saveWiki({
|
|
|
|
method: "autosave",
|
|
|
|
downloadType: "text/plain"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise put ourselves in the "pending autosave" state and wait for the change event before we do the autosave
|
|
|
|
self.pendingAutoSave = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Set up our beforeunload handler
|
2015-05-03 15:23:35 +00:00
|
|
|
$tw.addUnloadTask(function(event) {
|
2014-08-30 19:44:26 +00:00
|
|
|
var confirmationMessage;
|
2014-08-29 08:58:30 +00:00
|
|
|
if(self.isDirty()) {
|
|
|
|
confirmationMessage = $tw.language.getString("UnsavedChangesWarning");
|
|
|
|
event.returnValue = confirmationMessage; // Gecko
|
|
|
|
}
|
|
|
|
return confirmationMessage;
|
2015-05-03 15:23:35 +00:00
|
|
|
});
|
2014-08-29 08:58:30 +00:00
|
|
|
}
|
|
|
|
// Install the save action handlers
|
|
|
|
if($tw.browser) {
|
|
|
|
$tw.rootWidget.addEventListener("tm-save-wiki",function(event) {
|
|
|
|
self.saveWiki({
|
|
|
|
template: event.param,
|
2014-11-10 19:54:19 +00:00
|
|
|
downloadType: "text/plain",
|
|
|
|
variables: event.paramObject
|
2014-08-29 08:58:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
$tw.rootWidget.addEventListener("tm-download-file",function(event) {
|
|
|
|
self.saveWiki({
|
|
|
|
method: "download",
|
|
|
|
template: event.param,
|
2014-11-10 19:54:19 +00:00
|
|
|
downloadType: "text/plain",
|
|
|
|
variables: event.paramObject
|
2014-08-29 08:58:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-08-14 10:43:07 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 09:04:54 +00:00
|
|
|
SaverHandler.prototype.titleSyncFilter = "$:/config/SaverFilter";
|
2014-08-14 10:43:07 +00:00
|
|
|
SaverHandler.prototype.titleAutoSave = "$:/config/AutoSave";
|
|
|
|
SaverHandler.prototype.titleSavedNotification = "$:/language/Notifications/Save/Done";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Select the appropriate saver modules and set them up
|
|
|
|
*/
|
|
|
|
SaverHandler.prototype.initSavers = function(moduleType) {
|
2014-08-29 08:58:30 +00:00
|
|
|
moduleType = moduleType || "saver";
|
|
|
|
// Instantiate the available savers
|
|
|
|
this.savers = [];
|
|
|
|
var self = this;
|
|
|
|
$tw.modules.forEachModuleOfType(moduleType,function(title,module) {
|
|
|
|
if(module.canSave(self)) {
|
|
|
|
self.savers.push(module.create(self.wiki));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Sort the savers into priority order
|
|
|
|
this.savers.sort(function(a,b) {
|
|
|
|
if(a.info.priority < b.info.priority) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if(a.info.priority > b.info.priority) {
|
|
|
|
return +1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-08-14 10:43:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Save the wiki contents. Options are:
|
2014-08-29 08:58:30 +00:00
|
|
|
method: "save", "autosave" or "download"
|
|
|
|
template: the tiddler containing the template to save
|
|
|
|
downloadType: the content type for the saved file
|
2014-08-14 10:43:07 +00:00
|
|
|
*/
|
|
|
|
SaverHandler.prototype.saveWiki = function(options) {
|
2014-08-29 08:58:30 +00:00
|
|
|
options = options || {};
|
|
|
|
var self = this,
|
|
|
|
method = options.method || "save",
|
2014-11-10 19:54:19 +00:00
|
|
|
variables = options.variables || {},
|
2014-08-29 08:58:30 +00:00
|
|
|
template = options.template || "$:/core/save/all",
|
|
|
|
downloadType = options.downloadType || "text/plain",
|
2014-11-10 19:54:19 +00:00
|
|
|
text = this.wiki.renderTiddler(downloadType,template,options),
|
2014-08-29 08:58:30 +00:00
|
|
|
callback = function(err) {
|
|
|
|
if(err) {
|
2016-05-17 20:58:47 +00:00
|
|
|
alert($tw.language.getString("Error/WhileSaving") + ":\n\n" + err);
|
2014-08-29 08:58:30 +00:00
|
|
|
} else {
|
|
|
|
// Clear the task queue if we're saving (rather than downloading)
|
|
|
|
if(method !== "download") {
|
2014-09-01 19:39:08 +00:00
|
|
|
self.numChanges = 0;
|
2014-08-29 08:58:30 +00:00
|
|
|
self.updateDirtyStatus();
|
|
|
|
}
|
|
|
|
$tw.notifier.display(self.titleSavedNotification);
|
|
|
|
if(options.callback) {
|
|
|
|
options.callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Ignore autosave if disabled
|
|
|
|
if(method === "autosave" && this.wiki.getTiddlerText(this.titleAutoSave,"yes") !== "yes") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Call the highest priority saver that supports this method
|
|
|
|
for(var t=this.savers.length-1; t>=0; t--) {
|
|
|
|
var saver = this.savers[t];
|
2014-11-10 19:54:19 +00:00
|
|
|
if(saver.info.capabilities.indexOf(method) !== -1 && saver.save(text,method,callback,{variables: {filename: variables.filename}})) {
|
2014-08-29 08:58:30 +00:00
|
|
|
this.logger.log("Saving wiki with method",method,"through saver",saver.info.name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-08-14 10:43:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Checks whether the wiki is dirty (ie the window shouldn't be closed)
|
|
|
|
*/
|
|
|
|
SaverHandler.prototype.isDirty = function() {
|
2014-09-01 19:39:08 +00:00
|
|
|
return this.numChanges > 0;
|
2014-08-14 10:43:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2014-08-28 17:21:08 +00:00
|
|
|
Update the document body with the class "tc-dirty" if the wiki has unsaved/unsynced changes
|
2014-08-14 10:43:07 +00:00
|
|
|
*/
|
|
|
|
SaverHandler.prototype.updateDirtyStatus = function() {
|
2014-08-29 08:58:30 +00:00
|
|
|
if($tw.browser) {
|
|
|
|
$tw.utils.toggleClass(document.body,"tc-dirty",this.isDirty());
|
|
|
|
}
|
2014-08-14 10:43:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.SaverHandler = SaverHandler;
|
|
|
|
|
|
|
|
})();
|