/*\ 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; t