2012-05-06 12:23:29 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/macros/download.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
2012-05-19 17:23:14 +00:00
|
|
|
Download macro
|
|
|
|
|
2012-05-06 12:23:29 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: "download",
|
|
|
|
params: {
|
|
|
|
title: {byName: "default", type: "text"},
|
|
|
|
type: {byName: true, type: "text"},
|
|
|
|
"filename": {byName: true, type: "text"},
|
|
|
|
"label": {byName: true, type: "text"}
|
|
|
|
},
|
|
|
|
events: ["click"]
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.handleEvent = function(event) {
|
2012-05-19 14:13:17 +00:00
|
|
|
if(event.type === "click") {
|
|
|
|
var text = this.wiki.renderTiddler(this.downloadType,this.downloadTitle),
|
|
|
|
link = document.createElement("a");
|
|
|
|
link.setAttribute("download",this.downloadFilename);
|
|
|
|
link.setAttribute("href","data:" + this.downloadType + "," + encodeURIComponent(text));
|
|
|
|
link.click();
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2012-05-06 12:23:29 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.executeMacro = function() {
|
2012-06-07 11:28:03 +00:00
|
|
|
this.downloadTitle = this.params.title || "$:/core/templates/tiddlywiki5.template.html";
|
2012-05-06 12:23:29 +00:00
|
|
|
this.downloadType = this.params.type || "text/plain";
|
|
|
|
this.downloadFilename = this.params.filename || this.downloadTitle;
|
|
|
|
var attributes = {},
|
|
|
|
content = [];
|
|
|
|
if(this.hasParameter("label")) {
|
|
|
|
content.push($tw.Tree.Text(this.params.label));
|
|
|
|
} else {
|
|
|
|
content.push($tw.Tree.Text("Download \"" + this.downloadFilename + "\""));
|
|
|
|
}
|
|
|
|
return [$tw.Tree.Element("button",attributes,content)];
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|