diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index dd422f8ed..2fb3b70f3 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -70,33 +70,76 @@ NavigatorWidget.prototype.refresh = function(changedTiddlers) { }; NavigatorWidget.prototype.getStoryList = function() { - this.storyList = this.wiki.getTiddlerList(this.storyTitle); + return this.storyTitle ? this.wiki.getTiddlerList(this.storyTitle) : null; }; -NavigatorWidget.prototype.saveStoryList = function() { +NavigatorWidget.prototype.saveStoryList = function(storyList) { var storyTiddler = this.wiki.getTiddler(this.storyTitle); - this.wiki.addTiddler(new $tw.Tiddler({ - title: this.storyTitle - },storyTiddler,{list: this.storyList})); + this.wiki.addTiddler(new $tw.Tiddler( + {title: this.storyTitle}, + storyTiddler, + {list: storyList} + )); }; -NavigatorWidget.prototype.findTitleInStory = function(title,defaultIndex) { - for(var t=0; t=0; t--) { - // Replace the first story instance of the original tiddler name with the draft title - if(this.storyList[t] === title) { - if(!gotOne) { - this.storyList[t] = draftTiddler.fields.title; - gotOne = true; - } else { - this.storyList.splice(t,1); - } - } else if(this.storyList[t] === draftTiddler.fields.title) { - // Remove any existing references to the draft - this.storyList.splice(t,1); - } - } - this.addToHistory(draftTiddler.fields.title,event.navigateFromClientRect); - this.saveStoryList(); + draftTiddler = this.makeDraftTiddler(title), + draftTitle = draftTiddler.fields.title, + storyList = this.getStoryList(); + this.removeTitleFromStory(storyList,draftTitle); + this.replaceFirstTitleInStory(storyList,title,draftTitle); + this.addToHistory(draftTitle,event.navigateFromClientRect); + this.saveStoryList(storyList); return false; }; @@ -179,32 +194,30 @@ NavigatorWidget.prototype.handleEditTiddlerEvent = function(event) { NavigatorWidget.prototype.handleDeleteTiddlerEvent = function(event) { // Get the tiddler we're deleting var title = event.param || event.tiddlerTitle, - tiddler = this.wiki.getTiddler(title); + tiddler = this.wiki.getTiddler(title), + storyList = this.getStoryList(); // Check if the tiddler we're deleting is in draft mode if(tiddler.hasField("draft.title")) { // Delete the original tiddler - this.wiki.deleteTiddler(tiddler.fields["draft.of"]); + var originalTitle = tiddler.fields["draft.of"]; + this.wiki.deleteTiddler(originalTitle); + this.removeTitleFromStory(storyList,originalTitle); } // Delete this tiddler this.wiki.deleteTiddler(title); // Remove the closed tiddler from the story - this.getStoryList(); - // Look for tiddler with this title to close - var slot = this.findTitleInStory(title,-1); - if(slot !== -1) { - this.storyList.splice(slot,1); - this.saveStoryList(); - } + this.removeTitleFromStory(storyList,title); + this.saveStoryList(storyList); return false; }; /* Create/reuse the draft tiddler for a given title */ -NavigatorWidget.prototype.getDraftTiddler = function(targetTitle) { +NavigatorWidget.prototype.makeDraftTiddler = function(targetTitle) { // See if there is already a draft tiddler for this tiddler var drafts = []; - this.wiki.forEachTiddler(function(title,tiddler) { + this.wiki.forEachTiddler({includeSystem: true},function(title,tiddler) { if(tiddler.fields["draft.title"] && tiddler.fields["draft.of"] === targetTitle) { drafts.push(tiddler); } @@ -244,75 +257,58 @@ NavigatorWidget.prototype.generateDraftTitle = function(title) { // Take a tiddler out of edit mode, saving the changes NavigatorWidget.prototype.handleSaveTiddlerEvent = function(event) { - this.getStoryList(); var title = event.param || event.tiddlerTitle, + tiddler = this.wiki.getTiddler(title), + storyList = this.getStoryList(), storyTiddlerModified = false; // We have to special case saving the story tiddler itself - for(var t=0; t