From c287d9d6d00ca342b48664e04bc1e53abdf5b54c Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Mon, 1 Apr 2013 19:06:39 +0100 Subject: [PATCH] First pass at a widget for importing tiddlers --- core/modules/widgets/import.js | 196 ++++++++++++++++++ core/styles/base.tid | 17 +- core/templates/ControlPanel.tid | 6 + core/templates/PageTemplate.tid | 9 + editions/tw5.com/tiddlers/Improvements.tid | 6 +- .../tiddlers/features/ImportTiddlers.tid | 11 + 6 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 core/modules/widgets/import.js create mode 100644 editions/tw5.com/tiddlers/features/ImportTiddlers.tid diff --git a/core/modules/widgets/import.js b/core/modules/widgets/import.js new file mode 100644 index 000000000..077fda023 --- /dev/null +++ b/core/modules/widgets/import.js @@ -0,0 +1,196 @@ +/*\ +title: $:/core/modules/widget/import.js +type: application/javascript +module-type: widget + +Implements the import widget. + +``` +<$import> +Import using the "browse..." button or drag files onto this text + +``` + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var ImportWidget = function(renderer) { + // Save state + this.renderer = renderer; + // Generate child nodes + this.generate(); +}; + +ImportWidget.prototype.generate = function() { + // Get the parameters from the attributes + this.browse = this.renderer.getAttribute("browse","yes"); + this["class"] = this.renderer.getAttribute("class"); + // Compute classes + var classes = ["tw-import"]; + if(this["class"]) { + $tw.utils.pushTop(classes,this["class"]); + } + // Create the file input and container elements + var fileInput = { + type: "element", + tag: "input", + attributes: { + type: {type: "string", value: "file"}, + style: {type: "string", value: this.browse === "no" ? "display: none;" : "display: block;"} + }, + events: [{name: "change", handlerObject: this, handlerMethod: "handleChangeEvent"}] + }, + container = { + type: "element", + tag: "div", + children: this.renderer.parseTreeNode.children, + events: [ + {name: "dragenter", handlerObject: this, handlerMethod: "handleDragEnterEvent"}, + {name: "dragover", handlerObject: this, handlerMethod: "handleDragOverEvent"}, + {name: "dragleave", handlerObject: this, handlerMethod: "handleDragLeaveEvent"}, + {name: "drop", handlerObject: this, handlerMethod: "handleDropEvent"}, + {name: "paste", handlerObject: this, handlerMethod: "handlePasteEvent"}] + }; + // Set the return element + this.tag = "div"; + this.attributes = { + "class": classes.join(" ") + }; + this.children = this.renderer.renderTree.createRenderers(this.renderer.renderContext,[fileInput,container]); +}; + +ImportWidget.prototype.handleChangeEvent = function(event) { + event.stopPropagation(); + this.importFiles(event.target.files); +}; + +ImportWidget.prototype.handleDragEnterEvent = function(event) { + this.renderer.domNode.classList.add("tw-dragover"); +}; + +ImportWidget.prototype.handleDragOverEvent = function(event) { + event.stopPropagation(); + event.preventDefault(); + event.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy. +}; + +ImportWidget.prototype.handleDragLeaveEvent = function(event) { + this.renderer.domNode.classList.remove("tw-dragover"); +}; + +ImportWidget.prototype.handleDropEvent = function(event) { + this.renderer.domNode.classList.remove("tw-dragover"); + event.stopPropagation(); + event.preventDefault(); + this.importFiles(event.dataTransfer.files); + return false; +}; + +ImportWidget.prototype.handlePasteEvent = function(event) { + var self = this, + items = event.clipboardData.items; + for(var t = 0; tNew Tiddler +--- + +Import: <$import/> + +--- + <$button message="tw-save-wiki" class="btn btn-mini btn-primary">Save Changes --- diff --git a/core/templates/PageTemplate.tid b/core/templates/PageTemplate.tid index 32371bfe0..6ee5d5279 100644 --- a/core/templates/PageTemplate.tid +++ b/core/templates/PageTemplate.tid @@ -3,6 +3,13 @@ title: $:/templates/PageTemplate <$navigator story="$:/StoryList" history="$:/HistoryList"> + +<$import browse="no"> + +
+Drop files here to import them as tiddlers +
+