From c7971d3da3bbef4bbc81d4462ee93590adc248f8 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Fri, 7 Nov 2014 14:53:37 +0000 Subject: [PATCH] Update new tiddler message handling to accept title and fields Now you can create a new tiddler with both a template and additional specified fields. --- core/modules/widgets/navigator.js | 42 ++++++++++--------- .../WidgetMessage_ tm-new-tiddler.tid | 29 +++++++++++++ 2 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 editions/prerelease/tiddlers/WidgetMessage_ tm-new-tiddler.tid diff --git a/core/modules/widgets/navigator.js b/core/modules/widgets/navigator.js index f4a164c62..b3484cf5e 100755 --- a/core/modules/widgets/navigator.js +++ b/core/modules/widgets/navigator.js @@ -373,23 +373,26 @@ NavigatorWidget.prototype.handleCancelTiddlerEvent = function(event) { NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) { // Get the story details var storyList = this.getStoryList(), - templateTiddler, title, draftTitle, existingTiddler, mergedTags; - // Work out the title of the target tiddler - if(typeof event.param === "object") { - // If we got a hashmap use it as the template - templateTiddler = event.param; - if(templateTiddler.title) { - // Use the provided title - title = templateTiddler.title - } else { - // Generate a new unique title - title = this.wiki.generateNewTitle($tw.language.getString("DefaultNewTiddlerTitle")); - } - } else { - // If we got a string, use it as the template and generate a new title + templateTiddler, additionalFields, title, draftTitle, existingTiddler, mergedTags; + // Get the template tiddler (if any) + if(typeof event.param === "string") { + // Get the template tiddler templateTiddler = this.wiki.getTiddler(event.param); + // Generate a new title title = this.wiki.generateNewTitle(event.param || $tw.language.getString("DefaultNewTiddlerTitle")); } + // Get the specified additional fields + if(typeof event.paramObject === "object") { + additionalFields = event.paramObject; + } + if(typeof event.param === "object") { // Backwards compatibility with 5.1.3 + additionalFields = event.param; + } + if(additionalFields.title) { + title = additionalFields.title; + } + // Generate a title if we don't have one + title = title || this.wiki.generateNewTitle($tw.language.getString("DefaultNewTiddlerTitle")); // Find any existing draft for this tiddler draftTitle = this.wiki.findDraft(title); // Pull in any existing tiddler @@ -400,15 +403,13 @@ NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) { existingTiddler = this.wiki.getTiddler(title); } // Merge the tags - if(existingTiddler && existingTiddler.fields.tags && templateTiddler && templateTiddler.tags) { + if(existingTiddler && existingTiddler.fields.tags && additionalFields && additionalFields.tags) { // Merge tags - mergedTags = $tw.utils.pushTop($tw.utils.parseStringArray(templateTiddler.tags),existingTiddler.fields.tags); + mergedTags = $tw.utils.pushTop($tw.utils.parseStringArray(additionalFields.tags),existingTiddler.fields.tags); } else if(existingTiddler && existingTiddler.fields.tags) { mergedTags = existingTiddler.fields.tags; - } else if(templateTiddler && templateTiddler.tags) { - mergedTags = templateTiddler.tags; - } else if(templateTiddler && templateTiddler.fields && templateTiddler.fields.tags) { - mergedTags = templateTiddler.fields.tags; + } else if(additionalFields && additionalFields.tags) { + mergedTags = additionalFields.tags; } // Save the draft tiddler var draftTiddler = new $tw.Tiddler({ @@ -417,6 +418,7 @@ NavigatorWidget.prototype.handleNewTiddlerEvent = function(event) { }, templateTiddler, existingTiddler, + additionalFields, this.wiki.getCreationFields(), { title: draftTitle, diff --git a/editions/prerelease/tiddlers/WidgetMessage_ tm-new-tiddler.tid b/editions/prerelease/tiddlers/WidgetMessage_ tm-new-tiddler.tid new file mode 100644 index 000000000..167acec49 --- /dev/null +++ b/editions/prerelease/tiddlers/WidgetMessage_ tm-new-tiddler.tid @@ -0,0 +1,29 @@ +caption: tm-new-tiddler +created: 20140226194405353 +modified: 20141107132122081 +tags: Messages navigator-message +title: WidgetMessage: tm-new-tiddler +type: text/vnd.tiddlywiki + +The new tiddler message creates a new draft tiddler and adds it to the current story. It requires the following properties on the `event` object: + +|!Name |!Description | +|param |The optional title of a tiddler to use as a template for the new tiddler | +|paramObject |Optional hashmap of additional tiddler fields | +|navigateFromTitle |Title of the tiddler from which the navigation to the new tiddler was initiated | + +The title for the draft tiddler is chosen according to these rules: + +* If a hashmap was used and a title field was specified, use that title +* If a template tiddler was used, use the title of the template tiddler, making it unique with a numeric suffix +* Otherwise, generate a new title based on the default new tiddler title with a numeric suffix to make it unique + +The new tiddler message is usually generated with the ButtonWidget or ActionSendMessageWidget and is handled by the NavigatorWidget. + +! Example + +To make a button that creates new tiddlers tagged "task", create a tiddler called "TaskTemplate" with that tag, and then make your button like this: + +``` +<$button message="tm-new-tiddler" param="TaskTemplate">New Task +```