2013-10-25 20:16:03 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/browse.js
|
2013-10-25 20:16:03 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-25 20:16:03 +00:00
|
|
|
|
|
|
|
Browse widget for browsing for files to import
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-25 20:16:03 +00:00
|
|
|
|
|
|
|
var BrowseWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
BrowseWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
BrowseWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
var self = this;
|
|
|
|
// Remember parent
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute attributes and execute state
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
// Create element
|
|
|
|
var domNode = this.document.createElement("input");
|
|
|
|
domNode.setAttribute("type","file");
|
2014-09-10 20:54:52 +00:00
|
|
|
if(this.browseMultiple) {
|
|
|
|
domNode.setAttribute("multiple","multiple");
|
|
|
|
}
|
2014-12-02 19:16:38 +00:00
|
|
|
if(this.tooltip) {
|
|
|
|
domNode.setAttribute("title",this.tooltip);
|
|
|
|
}
|
2015-11-13 18:47:20 +00:00
|
|
|
// Nw.js supports "nwsaveas" to force a "save as" dialogue that allows a new or existing file to be selected
|
2015-01-11 14:20:34 +00:00
|
|
|
if(this.nwsaveas) {
|
|
|
|
domNode.setAttribute("nwsaveas",this.nwsaveas);
|
|
|
|
}
|
2020-04-21 21:18:34 +00:00
|
|
|
if(this.accept) {
|
|
|
|
domNode.setAttribute("accept",this.accept);
|
|
|
|
}
|
2017-10-03 20:52:51 +00:00
|
|
|
// Nw.js supports "webkitdirectory" and "nwdirectory" to allow a directory to be selected
|
2015-11-13 18:47:20 +00:00
|
|
|
if(this.webkitdirectory) {
|
|
|
|
domNode.setAttribute("webkitdirectory",this.webkitdirectory);
|
|
|
|
}
|
2017-10-03 20:52:51 +00:00
|
|
|
if(this.nwdirectory) {
|
|
|
|
domNode.setAttribute("nwdirectory",this.nwdirectory);
|
|
|
|
}
|
2013-10-25 20:16:03 +00:00
|
|
|
// Add a click event handler
|
|
|
|
domNode.addEventListener("change",function (event) {
|
2014-10-20 15:53:42 +00:00
|
|
|
if(self.message) {
|
2015-01-11 14:20:34 +00:00
|
|
|
self.dispatchEvent({type: self.message, param: self.param, files: event.target.files});
|
2014-10-20 13:57:15 +00:00
|
|
|
} else {
|
2017-07-18 14:32:50 +00:00
|
|
|
self.wiki.readFiles(event.target.files,{
|
|
|
|
callback: function(tiddlerFieldsArray) {
|
|
|
|
self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray)});
|
|
|
|
},
|
|
|
|
deserializer: self.deserializer
|
2014-10-20 15:53:42 +00:00
|
|
|
});
|
2014-10-20 13:57:15 +00:00
|
|
|
}
|
2014-10-20 15:53:42 +00:00
|
|
|
return false;
|
2013-10-25 20:16:03 +00:00
|
|
|
},false);
|
|
|
|
// Insert element
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
BrowseWidget.prototype.execute = function() {
|
2014-09-10 20:54:52 +00:00
|
|
|
this.browseMultiple = this.getAttribute("multiple");
|
2017-07-18 14:32:50 +00:00
|
|
|
this.deserializer = this.getAttribute("deserializer");
|
2014-10-20 13:57:15 +00:00
|
|
|
this.message = this.getAttribute("message");
|
2015-01-11 14:20:34 +00:00
|
|
|
this.param = this.getAttribute("param");
|
2014-12-02 19:16:38 +00:00
|
|
|
this.tooltip = this.getAttribute("tooltip");
|
2015-01-11 14:20:34 +00:00
|
|
|
this.nwsaveas = this.getAttribute("nwsaveas");
|
2020-04-21 21:18:34 +00:00
|
|
|
this.accept = this.getAttribute("accept");
|
2015-11-13 18:47:20 +00:00
|
|
|
this.webkitdirectory = this.getAttribute("webkitdirectory");
|
2017-10-03 20:52:51 +00:00
|
|
|
this.nwdirectory = this.getAttribute("nwdirectory");
|
2013-10-25 20:16:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
BrowseWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.browse = BrowseWidget;
|
|
|
|
|
|
|
|
})();
|