1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-24 17:10:29 +00:00

Extend the navigator widget to handle import events

This commit is contained in:
Jeremy Ruston 2013-10-25 21:15:32 +01:00
parent 8c2fb7b8d0
commit c2efdfdbc4

View File

@ -24,7 +24,8 @@ var NavigatorWidget = function(parseTreeNode,options) {
{type: "tw-cancel-tiddler", handler: "handleCancelTiddlerEvent"},
{type: "tw-close-tiddler", handler: "handleCloseTiddlerEvent"},
{type: "tw-close-all-tiddlers", handler: "handleCloseAllTiddlersEvent"},
{type: "tw-new-tiddler", handler: "handleNewTiddlerEvent"}
{type: "tw-new-tiddler", handler: "handleNewTiddlerEvent"},
{type: "tw-import-tiddlers", handler: "handleImportTiddlersEvent"},
]);
};
@ -305,6 +306,42 @@ NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) {
return false;
};
// Import JSON tiddlers
NavigatorWidget.prototype.handleImportTiddlersEvent = function(event) {
var self = this;
// Get the story and history details
this.getStoryList();
var history = this.wiki.getTiddlerData(this.historyTitle,[]);
// Get the tiddlers
var tiddlers = [];
try {
tiddlers = JSON.parse(event.param);
} catch(e) {
}
// Process each tiddler
$tw.utils.each(tiddlers,function(tiddlerFields) {
// Generate a unique title for the tiddler
var title = self.wiki.generateNewTitle(tiddlerFields.title);
// Add it to the store
self.wiki.addTiddler(new $tw.Tiddler(
self.wiki.getCreationFields(),
tiddlerFields,
self.wiki.getModificationFields(),
{title: title}
));
// Add it to the story
if(self.storyList.indexOf(title) === -1) {
self.storyList.unshift(title);
}
// And to history
history.push({title: title});
});
// Save the updated story and history
this.saveStoryList();
this.wiki.setTiddlerData(this.historyTitle,history);
return false;
};
exports.navigator = NavigatorWidget;
})();