2013-10-25 20:16:03 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/dropzone.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
|
|
|
|
|
|
|
Dropzone widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2021-04-10 08:48:50 +00:00
|
|
|
var IMPORT_TITLE = "$:/Import";
|
|
|
|
|
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 DropZoneWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
DropZoneWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
DropZoneWidget.prototype.render = function(parent,nextSibling) {
|
2015-02-10 22:33:19 +00:00
|
|
|
var self = this;
|
2013-10-25 20:16:03 +00:00
|
|
|
// Remember parent
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute attributes and execute state
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
// Create element
|
2015-02-10 22:33:19 +00:00
|
|
|
var domNode = this.document.createElement("div");
|
2021-05-16 13:01:46 +00:00
|
|
|
this.domNode = domNode;
|
2020-04-03 09:33:39 +00:00
|
|
|
domNode.className = this.dropzoneClass || "tc-dropzone";
|
2013-10-25 20:16:03 +00:00
|
|
|
// Add event handlers
|
2020-03-15 14:46:33 +00:00
|
|
|
if(this.dropzoneEnable) {
|
|
|
|
$tw.utils.addEventListeners(domNode,[
|
|
|
|
{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"},
|
|
|
|
{name: "dragend", handlerObject: this, handlerMethod: "handleDragEndEvent"}
|
2021-05-16 13:01:46 +00:00
|
|
|
]);
|
2020-03-15 14:46:33 +00:00
|
|
|
}
|
2013-10-25 20:16:03 +00:00
|
|
|
// Insert element
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
2017-03-17 14:54:30 +00:00
|
|
|
// Stack of outstanding enter/leave events
|
|
|
|
this.currentlyEntered = [];
|
2013-10-25 20:16:03 +00:00
|
|
|
};
|
|
|
|
|
2021-05-16 13:01:46 +00:00
|
|
|
// Handler for transient event listeners added when the dropzone has an active drag in progress
|
|
|
|
DropZoneWidget.prototype.handleEvent = function(event) {
|
|
|
|
if(event.type === "click") {
|
|
|
|
if(this.currentlyEntered.length) {
|
|
|
|
this.resetState();
|
|
|
|
}
|
|
|
|
} else if(event.type === "dragenter") {
|
|
|
|
if(event.target && event.target !== this.domNode && !$tw.utils.domContains(this.domNode,event.target)) {
|
|
|
|
this.resetState();
|
|
|
|
}
|
|
|
|
} else if(event.type === "dragleave") {
|
|
|
|
// Check if drag left the window
|
|
|
|
if(event.relatedTarget === null || (event.relatedTarget && event.relatedTarget.nodeName === "HTML")) {
|
|
|
|
this.resetState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reset the state of the dropzone after a drag has ended
|
|
|
|
DropZoneWidget.prototype.resetState = function() {
|
|
|
|
$tw.utils.removeClass(this.domNode,"tc-dragover");
|
|
|
|
this.currentlyEntered = [];
|
|
|
|
this.document.body.removeEventListener("click",this,true);
|
|
|
|
this.document.body.removeEventListener("dragenter",this,true);
|
2021-05-30 18:20:17 +00:00
|
|
|
this.document.body.removeEventListener("dragleave",this,true);
|
2021-05-16 13:01:46 +00:00
|
|
|
this.dragInProgress = false;
|
|
|
|
};
|
|
|
|
|
2017-03-17 14:54:30 +00:00
|
|
|
DropZoneWidget.prototype.enterDrag = function(event) {
|
|
|
|
if(this.currentlyEntered.indexOf(event.target) === -1) {
|
|
|
|
this.currentlyEntered.push(event.target);
|
2015-02-10 23:17:38 +00:00
|
|
|
}
|
2021-05-16 13:01:46 +00:00
|
|
|
if(!this.dragInProgress) {
|
|
|
|
this.dragInProgress = true;
|
|
|
|
// If we're entering for the first time we need to apply highlighting
|
2021-05-30 18:20:17 +00:00
|
|
|
$tw.utils.addClass(this.domNodes[0],"tc-dragover");
|
2021-05-16 13:01:46 +00:00
|
|
|
this.document.body.addEventListener("click",this,true);
|
|
|
|
this.document.body.addEventListener("dragenter",this,true);
|
|
|
|
this.document.body.addEventListener("dragleave",this,true);
|
|
|
|
}
|
2014-11-11 11:44:00 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 14:54:30 +00:00
|
|
|
DropZoneWidget.prototype.leaveDrag = function(event) {
|
|
|
|
var pos = this.currentlyEntered.indexOf(event.target);
|
|
|
|
if(pos !== -1) {
|
|
|
|
this.currentlyEntered.splice(pos,1);
|
|
|
|
}
|
2014-11-11 11:44:00 +00:00
|
|
|
// Remove highlighting if we're leaving externally
|
2017-03-17 14:54:30 +00:00
|
|
|
if(this.currentlyEntered.length === 0) {
|
2021-05-16 13:01:46 +00:00
|
|
|
this.resetState();
|
2014-11-11 11:44:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.handleDragEnterEvent = function(event) {
|
2017-03-17 14:54:30 +00:00
|
|
|
if($tw.dragInProgress) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-19 20:52:43 +00:00
|
|
|
if(this.filesOnly && !$tw.utils.dragEventContainsFiles(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-17 14:54:30 +00:00
|
|
|
this.enterDrag(event);
|
2013-10-25 20:16:03 +00:00
|
|
|
// Tell the browser that we're ready to handle the drop
|
|
|
|
event.preventDefault();
|
|
|
|
// Tell the browser not to ripple the drag up to any parent drop handlers
|
|
|
|
event.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.handleDragOverEvent = function(event) {
|
2014-05-07 13:32:14 +00:00
|
|
|
// Check for being over a TEXTAREA or INPUT
|
|
|
|
if(["TEXTAREA","INPUT"].indexOf(event.target.tagName) !== -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-10 23:17:38 +00:00
|
|
|
// Check for this window being the source of the drag
|
|
|
|
if($tw.dragInProgress) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-25 20:16:03 +00:00
|
|
|
// Tell the browser that we're still interested in the drop
|
|
|
|
event.preventDefault();
|
2021-05-19 20:52:43 +00:00
|
|
|
// Check if this is a synthetic event, IE does not allow accessing dropEffect outside of original event handler
|
|
|
|
if(event.isTrusted) {
|
|
|
|
event.dataTransfer.dropEffect = "copy"; // Explicitly show this is a copy
|
|
|
|
}
|
2013-10-25 20:16:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.handleDragLeaveEvent = function(event) {
|
2017-03-17 14:54:30 +00:00
|
|
|
this.leaveDrag(event);
|
2013-10-25 20:16:03 +00:00
|
|
|
};
|
|
|
|
|
2019-03-02 15:13:36 +00:00
|
|
|
DropZoneWidget.prototype.handleDragEndEvent = function(event) {
|
2021-05-16 13:01:46 +00:00
|
|
|
this.resetState();
|
2019-03-02 15:13:36 +00:00
|
|
|
};
|
|
|
|
|
2021-04-10 08:48:50 +00:00
|
|
|
DropZoneWidget.prototype.filterByContentTypes = function(tiddlerFieldsArray) {
|
|
|
|
var filteredTypes,
|
|
|
|
filtered = [],
|
|
|
|
types = [];
|
|
|
|
$tw.utils.each(tiddlerFieldsArray,function(tiddlerFields) {
|
2021-05-16 13:01:46 +00:00
|
|
|
types.push(tiddlerFields.type || "");
|
2021-04-10 08:48:50 +00:00
|
|
|
});
|
|
|
|
filteredTypes = this.wiki.filterTiddlers(this.contentTypesFilter,this,this.wiki.makeTiddlerIterator(types));
|
|
|
|
$tw.utils.each(tiddlerFieldsArray,function(tiddlerFields) {
|
|
|
|
if(filteredTypes.indexOf(tiddlerFields.type) !== -1) {
|
|
|
|
filtered.push(tiddlerFields);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return filtered;
|
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.readFileCallback = function(tiddlerFieldsArray) {
|
|
|
|
if(this.contentTypesFilter) {
|
|
|
|
tiddlerFieldsArray = this.filterByContentTypes(tiddlerFieldsArray);
|
|
|
|
}
|
|
|
|
if(tiddlerFieldsArray.length) {
|
|
|
|
this.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray), autoOpenOnImport: this.autoOpenOnImport, importTitle: this.importTitle});
|
|
|
|
if(this.actions) {
|
|
|
|
this.invokeActionString(this.actions,this,event,{importTitle: this.importTitle});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-25 20:16:03 +00:00
|
|
|
DropZoneWidget.prototype.handleDropEvent = function(event) {
|
2017-07-12 15:42:16 +00:00
|
|
|
var self = this,
|
|
|
|
readFileCallback = function(tiddlerFieldsArray) {
|
2021-04-10 08:48:50 +00:00
|
|
|
self.readFileCallback(tiddlerFieldsArray);
|
2017-07-12 15:42:16 +00:00
|
|
|
};
|
2017-03-17 14:54:30 +00:00
|
|
|
this.leaveDrag(event);
|
2014-05-07 13:32:14 +00:00
|
|
|
// Check for being over a TEXTAREA or INPUT
|
|
|
|
if(["TEXTAREA","INPUT"].indexOf(event.target.tagName) !== -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-10 23:17:38 +00:00
|
|
|
// Check for this window being the source of the drag
|
|
|
|
if($tw.dragInProgress) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-25 20:16:03 +00:00
|
|
|
var self = this,
|
|
|
|
dataTransfer = event.dataTransfer;
|
|
|
|
// Remove highlighting
|
2021-05-16 13:01:46 +00:00
|
|
|
this.resetState();
|
2013-10-25 20:16:03 +00:00
|
|
|
// Import any files in the drop
|
2017-04-21 15:27:44 +00:00
|
|
|
var numFiles = 0;
|
2021-11-21 20:48:16 +00:00
|
|
|
// If we have type text/vnd.tiddlywiki then skip trying to import files
|
|
|
|
if(dataTransfer.files && !$tw.utils.dragEventContainsType(event,"text/vnd.tiddler")) {
|
2017-07-12 15:42:16 +00:00
|
|
|
numFiles = this.wiki.readFiles(dataTransfer.files,{
|
|
|
|
callback: readFileCallback,
|
|
|
|
deserializer: this.dropzoneDeserializer
|
2017-04-21 15:27:44 +00:00
|
|
|
});
|
|
|
|
}
|
2014-01-24 14:09:06 +00:00
|
|
|
// Try to import the various data types we understand
|
|
|
|
if(numFiles === 0) {
|
2021-04-27 09:15:27 +00:00
|
|
|
var fallbackTitle = self.wiki.generateNewTitle("Untitled");
|
|
|
|
//Use the deserializer specified if any
|
|
|
|
if(this.dropzoneDeserializer) {
|
|
|
|
for(var t= 0; t<dataTransfer.items.length; t++) {
|
|
|
|
var item = dataTransfer.items[t];
|
|
|
|
if(item.kind === "string") {
|
|
|
|
item.getAsString(function(str){
|
|
|
|
var tiddlerFields = self.wiki.deserializeTiddlers(null,str,{title: fallbackTitle},{deserializer:self.dropzoneDeserializer});
|
|
|
|
if(tiddlerFields && tiddlerFields.length) {
|
|
|
|
readFileCallback(tiddlerFields);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$tw.utils.importDataTransfer(dataTransfer,fallbackTitle,readFileCallback);
|
|
|
|
}
|
2014-01-24 14:09:06 +00:00
|
|
|
}
|
2013-10-25 20:16:03 +00:00
|
|
|
// Tell the browser that we handled the drop
|
|
|
|
event.preventDefault();
|
|
|
|
// Stop the drop ripple up to any parent handlers
|
|
|
|
event.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.handlePasteEvent = function(event) {
|
2023-01-17 15:02:57 +00:00
|
|
|
var self = this;
|
|
|
|
var readFileCallback = function(tiddlerFieldsArray) {
|
2021-04-10 08:48:50 +00:00
|
|
|
self.readFileCallback(tiddlerFieldsArray);
|
2017-07-12 15:42:16 +00:00
|
|
|
};
|
2023-01-17 15:02:57 +00:00
|
|
|
var getItem = function(type) {
|
|
|
|
type = type || "text/plain";
|
|
|
|
return function(str) {
|
|
|
|
// Use the deserializer specified if any
|
|
|
|
if(self.dropzoneDeserializer) {
|
|
|
|
tiddlerFields = self.wiki.deserializeTiddlers(null,str,{title: self.wiki.generateNewTitle("Untitled " + type)},{deserializer:self.dropzoneDeserializer});
|
|
|
|
if(tiddlerFields && tiddlerFields.length) {
|
|
|
|
readFileCallback(tiddlerFields);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tiddlerFields = {
|
|
|
|
title: self.wiki.generateNewTitle("Untitled " + type),
|
|
|
|
text: str,
|
|
|
|
type: type
|
|
|
|
};
|
|
|
|
if($tw.log.IMPORT) {
|
|
|
|
console.log("Importing string '" + str + "', type: '" + type + "'");
|
|
|
|
}
|
|
|
|
readFileCallback([tiddlerFields]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-10-25 20:16:03 +00:00
|
|
|
// Let the browser handle it if we're in a textarea or input box
|
2023-02-28 08:35:15 +00:00
|
|
|
if(["TEXTAREA","INPUT"].indexOf(event.target.tagName) == -1 && !event.target.isContentEditable && !event.twEditor) {
|
2016-11-28 10:40:55 +00:00
|
|
|
var self = this,
|
|
|
|
items = event.clipboardData.items;
|
2013-10-25 20:16:03 +00:00
|
|
|
// Enumerate the clipboard items
|
2016-11-28 10:40:55 +00:00
|
|
|
for(var t = 0; t<items.length; t++) {
|
|
|
|
var item = items[t];
|
|
|
|
if(item.kind === "file") {
|
|
|
|
// Import any files
|
2017-07-12 15:42:16 +00:00
|
|
|
this.wiki.readFile(item.getAsFile(),{
|
|
|
|
callback: readFileCallback,
|
|
|
|
deserializer: this.dropzoneDeserializer
|
2016-11-28 10:40:55 +00:00
|
|
|
});
|
2023-05-06 13:02:16 +00:00
|
|
|
} else if(item.kind === "string" && !["text/html", "text/plain", "Text"].includes(item.type) && $tw.utils.itemHasValidDataType(item)) {
|
|
|
|
// Try to import the various data types we understand
|
|
|
|
var fallbackTitle = self.wiki.generateNewTitle("Untitled");
|
|
|
|
//Use the deserializer specified if any
|
|
|
|
if(this.dropzoneDeserializer) {
|
|
|
|
item.getAsString(function(str){
|
|
|
|
var tiddlerFields = self.wiki.deserializeTiddlers(null,str,{title: fallbackTitle},{deserializer:self.dropzoneDeserializer});
|
|
|
|
if(tiddlerFields && tiddlerFields.length) {
|
|
|
|
readFileCallback(tiddlerFields);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$tw.utils.importPaste(item,fallbackTitle,readFileCallback);
|
|
|
|
}
|
2016-11-28 10:40:55 +00:00
|
|
|
} else if(item.kind === "string") {
|
|
|
|
// Create tiddlers from string items
|
2023-01-17 15:02:57 +00:00
|
|
|
var tiddlerFields;
|
|
|
|
// It's important to give getAsString a closure with the right type
|
|
|
|
// So it can be added to the import queue
|
|
|
|
item.getAsString(getItem(item.type));
|
2013-10-25 20:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Tell the browser that we've handled the paste
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
DropZoneWidget.prototype.execute = function() {
|
2020-04-03 09:33:39 +00:00
|
|
|
this.dropzoneClass = this.getAttribute("class");
|
2017-07-12 15:42:16 +00:00
|
|
|
this.dropzoneDeserializer = this.getAttribute("deserializer");
|
2020-03-15 14:46:33 +00:00
|
|
|
this.dropzoneEnable = (this.getAttribute("enable") || "yes") === "yes";
|
2020-06-27 11:32:11 +00:00
|
|
|
this.autoOpenOnImport = this.getAttribute("autoOpenOnImport");
|
2021-04-10 08:48:50 +00:00
|
|
|
this.importTitle = this.getAttribute("importTitle",IMPORT_TITLE);
|
|
|
|
this.actions = this.getAttribute("actions");
|
|
|
|
this.contentTypesFilter = this.getAttribute("contentTypesFilter");
|
2021-05-19 20:52:43 +00:00
|
|
|
this.filesOnly = this.getAttribute("filesOnly","no") === "yes";
|
2013-10-25 20:16:03 +00:00
|
|
|
// Make child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
DropZoneWidget.prototype.refresh = function(changedTiddlers) {
|
2020-03-15 14:46:33 +00:00
|
|
|
var changedAttributes = this.computeAttributes();
|
2021-04-10 08:48:50 +00:00
|
|
|
if($tw.utils.count(changedAttributes) > 0) {
|
2020-03-15 14:46:33 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-25 20:16:03 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.dropzone = DropZoneWidget;
|
|
|
|
|
|
|
|
})();
|