2012-07-10 22:18:44 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/savers/download.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: saver
|
|
|
|
|
|
|
|
Handles saving changes via HTML5's download APIs
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Select the appropriate saver module and set it up
|
|
|
|
*/
|
2012-07-12 15:58:49 +00:00
|
|
|
var DownloadSaver = function(wiki) {
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
2014-11-10 19:54:19 +00:00
|
|
|
DownloadSaver.prototype.save = function(text,method,callback,options) {
|
|
|
|
options = options || {};
|
2013-09-19 09:50:14 +00:00
|
|
|
// Get the current filename
|
2014-11-10 19:54:19 +00:00
|
|
|
var filename = options.variables.filename;
|
|
|
|
if(!filename) {
|
|
|
|
var p = document.location.pathname.lastIndexOf("/");
|
|
|
|
if(p !== -1) {
|
|
|
|
filename = document.location.pathname.substr(p+1);
|
|
|
|
} else {
|
|
|
|
filename = "tiddlywiki.html";
|
|
|
|
}
|
2013-09-19 09:50:14 +00:00
|
|
|
}
|
2012-07-12 12:13:48 +00:00
|
|
|
// Set up the link
|
|
|
|
var link = document.createElement("a");
|
|
|
|
link.setAttribute("target","_blank");
|
2014-01-03 10:54:00 +00:00
|
|
|
if(Blob !== undefined) {
|
2013-09-17 19:15:38 +00:00
|
|
|
var blob = new Blob([text], {type: "text/html"});
|
2013-10-01 21:35:47 +00:00
|
|
|
link.setAttribute("href", URL.createObjectURL(blob));
|
|
|
|
} else {
|
|
|
|
link.setAttribute("href","data:text/html," + encodeURIComponent(text));
|
|
|
|
}
|
2013-09-19 09:50:14 +00:00
|
|
|
link.setAttribute("download",filename);
|
2013-05-23 10:26:47 +00:00
|
|
|
document.body.appendChild(link);
|
2012-07-12 12:13:48 +00:00
|
|
|
link.click();
|
2013-05-23 10:26:47 +00:00
|
|
|
document.body.removeChild(link);
|
2014-08-27 08:59:01 +00:00
|
|
|
// Callback that we succeeded
|
|
|
|
callback(null);
|
2012-07-12 15:58:49 +00:00
|
|
|
return true;
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Information about this saver
|
|
|
|
*/
|
|
|
|
DownloadSaver.prototype.info = {
|
|
|
|
name: "download",
|
2014-02-04 21:21:01 +00:00
|
|
|
priority: 100,
|
|
|
|
capabilities: ["save", "download"]
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Static method that returns true if this saver is capable of working
|
|
|
|
*/
|
2012-07-13 12:03:38 +00:00
|
|
|
exports.canSave = function(wiki) {
|
2012-11-16 19:31:32 +00:00
|
|
|
return document.createElement("a").download !== undefined;
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create an instance of this saver
|
|
|
|
*/
|
2012-07-13 12:03:38 +00:00
|
|
|
exports.create = function(wiki) {
|
|
|
|
return new DownloadSaver(wiki);
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|