From 0956ae10a0c4a23e9a8a158ebcc54df4a67b71e6 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 27 Nov 2013 20:51:08 +0000 Subject: [PATCH] Add support for downloading files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were re-using the `tw-save-wiki` message both for saving the current wiki and downloading a new wiki. Now we’ll use the separate `tw-download-file` message for downloading. Fixes #236 --- core/modules/savers/andtidwiki.js | 6 +++++- core/modules/savers/download.js | 2 +- core/modules/savers/manualdownload.js | 2 +- core/modules/savers/msdownload.js | 2 +- core/modules/savers/tiddlyfox.js | 6 +++++- core/modules/savers/twedit.js | 6 +++++- core/modules/savers/upload.js | 6 +++++- core/modules/startup.js | 7 +++++++ core/modules/wiki.js | 6 ++++-- editions/tw5.com/tiddlers/Download.tid | 4 ++-- 10 files changed, 36 insertions(+), 11 deletions(-) diff --git a/core/modules/savers/andtidwiki.js b/core/modules/savers/andtidwiki.js index e165a0b1b..8d77c7f7d 100644 --- a/core/modules/savers/andtidwiki.js +++ b/core/modules/savers/andtidwiki.js @@ -15,7 +15,11 @@ Handles saving changes via the AndTidWiki Android app var AndTidWiki = function(wiki) { }; -AndTidWiki.prototype.save = function(text,callback) { +AndTidWiki.prototype.save = function(text,method,callback) { + // Bail out unless this is a save (rather than a download) + if(method !== "save") { + return false; + } // Get the pathname of this document var pathname = decodeURIComponent(document.location.toString()); // Strip the file:// diff --git a/core/modules/savers/download.js b/core/modules/savers/download.js index 7e10e188d..f44e7c4db 100644 --- a/core/modules/savers/download.js +++ b/core/modules/savers/download.js @@ -18,7 +18,7 @@ Select the appropriate saver module and set it up var DownloadSaver = function(wiki) { }; -DownloadSaver.prototype.save = function(text) { +DownloadSaver.prototype.save = function(text,method,callback) { // Get the current filename var filename = "tiddlywiki.html", p = document.location.pathname.lastIndexOf("/"); diff --git a/core/modules/savers/manualdownload.js b/core/modules/savers/manualdownload.js index a4b5471ae..9fde5f2f6 100644 --- a/core/modules/savers/manualdownload.js +++ b/core/modules/savers/manualdownload.js @@ -21,7 +21,7 @@ Select the appropriate saver module and set it up var ManualDownloadSaver = function(wiki) { }; -ManualDownloadSaver.prototype.save = function(text) { +ManualDownloadSaver.prototype.save = function(text,method,callback) { $tw.modal.display(downloadInstructionsTitle,{ downloadLink: "data:text/html," + encodeURIComponent(text) }); diff --git a/core/modules/savers/msdownload.js b/core/modules/savers/msdownload.js index c0e71b788..a3aa756c1 100644 --- a/core/modules/savers/msdownload.js +++ b/core/modules/savers/msdownload.js @@ -18,7 +18,7 @@ Select the appropriate saver module and set it up var MsDownloadSaver = function(wiki) { }; -MsDownloadSaver.prototype.save = function(text) { +MsDownloadSaver.prototype.save = function(text,method,callback) { // Get the current filename var filename = "tiddlywiki.html", p = document.location.pathname.lastIndexOf("/"); diff --git a/core/modules/savers/tiddlyfox.js b/core/modules/savers/tiddlyfox.js index 7ae1448da..d78f4f2cf 100644 --- a/core/modules/savers/tiddlyfox.js +++ b/core/modules/savers/tiddlyfox.js @@ -15,7 +15,11 @@ Handles saving changes via the TiddlyFox file extension var TiddlyFoxSaver = function(wiki) { }; -TiddlyFoxSaver.prototype.save = function(text,callback) { +TiddlyFoxSaver.prototype.save = function(text,method,callback) { + // Bail out unless this is a save (rather than a download) + if(method !== "save") { + return false; + } var messageBox = document.getElementById("tiddlyfox-message-box"); if(messageBox) { // Get the pathname of this document diff --git a/core/modules/savers/twedit.js b/core/modules/savers/twedit.js index e6a500ae3..f2e9b016d 100644 --- a/core/modules/savers/twedit.js +++ b/core/modules/savers/twedit.js @@ -15,7 +15,11 @@ Handles saving changes via the TWEdit iOS app var TWEditSaver = function(wiki) { }; -TWEditSaver.prototype.save = function(text,callback) { +TWEditSaver.prototype.save = function(text,method,callback) { + // Bail out unless this is a save (rather than a download) + if(method !== "save") { + return false; + } // Bail if we're not running under TWEdit if(typeof DeviceInfo !== "object") { return false; diff --git a/core/modules/savers/upload.js b/core/modules/savers/upload.js index 5609cc4d4..ef506cb84 100644 --- a/core/modules/savers/upload.js +++ b/core/modules/savers/upload.js @@ -21,7 +21,11 @@ var UploadSaver = function(wiki) { this.wiki = wiki; }; -UploadSaver.prototype.save = function(text,callback) { +UploadSaver.prototype.save = function(text,method,callback) { + // Bail out unless this is a save (rather than a download) + if(method !== "save") { + return false; + } // Get the various parameters we need var backupDir = ".", username = this.wiki.getTextReference("$:/UploadName"), diff --git a/core/modules/startup.js b/core/modules/startup.js index a9bed67fc..1cd96e21e 100755 --- a/core/modules/startup.js +++ b/core/modules/startup.js @@ -89,6 +89,13 @@ exports.startup = function() { downloadType: "text/plain" }); }); + $tw.rootWidget.addEventListener("tw-download-file",function(event) { + $tw.wiki.saveWiki({ + method: "download", + template: event.param, + downloadType: "text/plain" + }); + }); // Install the crypto event handlers $tw.rootWidget.addEventListener("tw-set-password",function(event) { $tw.passwordPrompt.createPrompt({ diff --git a/core/modules/wiki.js b/core/modules/wiki.js index 6f303e9a7..1c62c12fe 100755 --- a/core/modules/wiki.js +++ b/core/modules/wiki.js @@ -866,15 +866,17 @@ exports.callSaver = function(method /*, args */ ) { /* Save the wiki contents. Options are: + method: "save" or "download" template: the tiddler containing the template to save downloadType: the content type for the saved file */ exports.saveWiki = function(options) { options = options || {}; - var template = options.template || "$:/core/save/all", + var method = options.method || "save", + template = options.template || "$:/core/save/all", downloadType = options.downloadType || "text/plain"; var text = this.renderTiddler(downloadType,template); - this.callSaver("save",text,function(err) { + this.callSaver("save",text,method,function(err) { if(err) { alert("Error while saving:\n\n" + err); } else { diff --git a/editions/tw5.com/tiddlers/Download.tid b/editions/tw5.com/tiddlers/Download.tid index e19121c66..8ce557c43 100644 --- a/editions/tw5.com/tiddlers/Download.tid +++ b/editions/tw5.com/tiddlers/Download.tid @@ -7,8 +7,8 @@ title: Download ! Download Single File Editions -|<$button message="tw-save-wiki" param="$:/editions/tw5.com/save-empty" class="btn-big-green">Save Empty {{$:/core/images/save-button}}|Get started with an empty wiki | -|<$button message="tw-save-wiki" param="$:/core/save/all" class="btn-big-green">Save Full {{$:/core/images/save-button}}|Download a full copy of this site, including all the documentation | +|<$button message="tw-download-file" param="$:/editions/tw5.com/save-empty" class="btn-big-green">Download Empty {{$:/core/images/save-button}}|Get started with an empty wiki | +|<$button message="tw-download-file" param="$:/core/save/all" class="btn-big-green">Download Full {{$:/core/images/save-button}}|Download a full copy of this site, including all the documentation | ! Download Node.js Edition