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";
|
|
|
|
|
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");
|
2014-08-28 17:21:08 +00:00
|
|
|
domNode.className = "tc-dropzone";
|
2013-10-25 20:16:03 +00:00
|
|
|
// Add event handlers
|
|
|
|
$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"}
|
|
|
|
]);
|
|
|
|
domNode.addEventListener("click",function (event) {
|
|
|
|
},false);
|
|
|
|
// Insert element
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
2014-11-11 11:44:00 +00:00
|
|
|
DropZoneWidget.prototype.enterDrag = function() {
|
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
|
|
|
// We count enter/leave events
|
|
|
|
this.dragEnterCount = (this.dragEnterCount || 0) + 1;
|
|
|
|
// If we're entering for the first time we need to apply highlighting
|
|
|
|
if(this.dragEnterCount === 1) {
|
2014-08-28 17:21:08 +00:00
|
|
|
$tw.utils.addClass(this.domNodes[0],"tc-dragover");
|
2013-10-25 20:16:03 +00:00
|
|
|
}
|
2014-11-11 11:44:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.leaveDrag = function() {
|
|
|
|
// Reduce the enter count
|
|
|
|
this.dragEnterCount = (this.dragEnterCount || 0) - 1;
|
|
|
|
// Remove highlighting if we're leaving externally
|
|
|
|
if(this.dragEnterCount <= 0) {
|
|
|
|
$tw.utils.removeClass(this.domNodes[0],"tc-dragover");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.handleDragEnterEvent = function(event) {
|
|
|
|
this.enterDrag();
|
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();
|
|
|
|
event.dataTransfer.dropEffect = "copy"; // Explicitly show this is a copy
|
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.handleDragLeaveEvent = function(event) {
|
2014-11-11 11:44:00 +00:00
|
|
|
this.leaveDrag();
|
2013-10-25 20:16:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.handleDropEvent = function(event) {
|
2014-11-11 11:44:00 +00:00
|
|
|
this.leaveDrag();
|
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;
|
|
|
|
// Reset the enter count
|
|
|
|
this.dragEnterCount = 0;
|
|
|
|
// Remove highlighting
|
2014-08-28 17:21:08 +00:00
|
|
|
$tw.utils.removeClass(this.domNodes[0],"tc-dragover");
|
2013-10-25 20:16:03 +00:00
|
|
|
// Import any files in the drop
|
2014-01-24 14:09:06 +00:00
|
|
|
var numFiles = this.wiki.readFiles(dataTransfer.files,function(tiddlerFieldsArray) {
|
2014-08-28 20:43:44 +00:00
|
|
|
self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray)});
|
2013-10-25 20:16:03 +00:00
|
|
|
});
|
2014-01-24 14:09:06 +00:00
|
|
|
// Try to import the various data types we understand
|
|
|
|
if(numFiles === 0) {
|
|
|
|
this.importData(dataTransfer);
|
|
|
|
}
|
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();
|
|
|
|
};
|
|
|
|
|
2013-10-26 07:36:31 +00:00
|
|
|
DropZoneWidget.prototype.importData = function(dataTransfer) {
|
2013-12-17 13:32:15 +00:00
|
|
|
// Try each provided data type in turn
|
2015-02-10 22:33:19 +00:00
|
|
|
for(var t=0; t<this.importDataTypes.length; t++) {
|
2013-12-21 03:57:05 +00:00
|
|
|
if(!$tw.browser.isIE || this.importDataTypes[t].IECompatible) {
|
2013-12-21 20:56:19 +00:00
|
|
|
// Get the data
|
2015-02-10 22:33:19 +00:00
|
|
|
var dataType = this.importDataTypes[t];
|
|
|
|
var data = dataTransfer.getData(dataType.type);
|
2013-12-21 20:56:19 +00:00
|
|
|
// Import the tiddlers in the data
|
|
|
|
if(data !== "" && data !== null) {
|
2014-11-14 10:33:41 +00:00
|
|
|
if($tw.log.IMPORT) {
|
|
|
|
console.log("Importing data type '" + dataType.type + "', data: '" + data + "'")
|
|
|
|
}
|
2015-02-10 22:33:19 +00:00
|
|
|
var tiddlerFields = dataType.convertToFields(data);
|
|
|
|
if(!tiddlerFields.title) {
|
2013-12-21 20:56:19 +00:00
|
|
|
tiddlerFields.title = this.wiki.generateNewTitle("Untitled");
|
|
|
|
}
|
2014-08-28 20:43:44 +00:00
|
|
|
this.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify([tiddlerFields])});
|
2013-12-21 20:56:19 +00:00
|
|
|
return;
|
2013-10-26 07:36:31 +00:00
|
|
|
}
|
2013-12-21 03:57:05 +00:00
|
|
|
}
|
2014-08-30 19:44:26 +00:00
|
|
|
}
|
2013-10-26 07:36:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DropZoneWidget.prototype.importDataTypes = [
|
2013-12-21 03:57:05 +00:00
|
|
|
{type: "text/vnd.tiddler", IECompatible: false, convertToFields: function(data) {
|
2013-12-13 11:12:56 +00:00
|
|
|
return JSON.parse(data);
|
|
|
|
}},
|
2013-12-21 03:57:05 +00:00
|
|
|
{type: "URL", IECompatible: true, convertToFields: function(data) {
|
|
|
|
// Check for tiddler data URI
|
2015-02-11 10:07:10 +00:00
|
|
|
var match = decodeURIComponent(data).match(/^data\:text\/vnd\.tiddler,(.*)/i);
|
2013-12-21 03:57:05 +00:00
|
|
|
if(match) {
|
|
|
|
return JSON.parse(match[1]);
|
|
|
|
} else {
|
|
|
|
return { // As URL string
|
|
|
|
text: data
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}},
|
|
|
|
{type: "text/x-moz-url", IECompatible: false, convertToFields: function(data) {
|
2013-12-17 13:32:15 +00:00
|
|
|
// Check for tiddler data URI
|
2015-02-11 10:07:10 +00:00
|
|
|
var match = decodeURIComponent(data).match(/^data\:text\/vnd\.tiddler,(.*)/i);
|
2013-12-13 01:32:06 +00:00
|
|
|
if(match) {
|
|
|
|
return JSON.parse(match[1]);
|
|
|
|
} else {
|
2013-12-17 13:32:15 +00:00
|
|
|
return { // As URL string
|
2013-12-13 01:32:06 +00:00
|
|
|
text: data
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}},
|
2014-03-09 20:39:03 +00:00
|
|
|
{type: "text/html", IECompatible: false, convertToFields: function(data) {
|
|
|
|
return {
|
|
|
|
text: data
|
|
|
|
};
|
|
|
|
}},
|
2013-12-21 03:57:05 +00:00
|
|
|
{type: "text/plain", IECompatible: false, convertToFields: function(data) {
|
2013-12-13 01:32:06 +00:00
|
|
|
return {
|
|
|
|
text: data
|
|
|
|
};
|
|
|
|
}},
|
2013-12-21 03:57:05 +00:00
|
|
|
{type: "Text", IECompatible: true, convertToFields: function(data) {
|
2013-10-26 07:36:31 +00:00
|
|
|
return {
|
|
|
|
text: data
|
|
|
|
};
|
|
|
|
}},
|
2013-12-21 03:57:05 +00:00
|
|
|
{type: "text/uri-list", IECompatible: false, convertToFields: function(data) {
|
2013-10-26 07:36:31 +00:00
|
|
|
return {
|
|
|
|
text: data
|
|
|
|
};
|
|
|
|
}}
|
|
|
|
];
|
|
|
|
|
2013-10-25 20:16:03 +00:00
|
|
|
DropZoneWidget.prototype.handlePasteEvent = function(event) {
|
|
|
|
// Let the browser handle it if we're in a textarea or input box
|
|
|
|
if(["TEXTAREA","INPUT"].indexOf(event.target.tagName) == -1) {
|
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
|
|
|
|
this.wiki.readFile(item.getAsFile(),function(tiddlerFieldsArray) {
|
|
|
|
self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify(tiddlerFieldsArray)});
|
|
|
|
});
|
|
|
|
} else if(item.kind === "string") {
|
|
|
|
// Create tiddlers from string items
|
|
|
|
var type = item.type;
|
|
|
|
item.getAsString(function(str) {
|
|
|
|
var tiddlerFields = {
|
|
|
|
title: self.wiki.generateNewTitle("Untitled"),
|
|
|
|
text: str,
|
|
|
|
type: type
|
|
|
|
};
|
|
|
|
if($tw.log.IMPORT) {
|
|
|
|
console.log("Importing string '" + str + "', type: '" + type + "'");
|
|
|
|
}
|
|
|
|
self.dispatchEvent({type: "tm-import-tiddlers", param: JSON.stringify([tiddlerFields])});
|
|
|
|
});
|
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() {
|
|
|
|
// 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) {
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.dropzone = DropZoneWidget;
|
|
|
|
|
|
|
|
})();
|