2017-03-23 14:23:33 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/dom/dragndrop.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
Browser data transfer utilities, used with the clipboard and drag and drop
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Options:
|
|
|
|
|
|
|
|
domNode: dom node to make draggable
|
2022-02-24 11:06:18 +00:00
|
|
|
selector: CSS selector to identify element within domNode to be used as drag handle (optional)
|
2021-09-12 13:20:03 +00:00
|
|
|
dragImageType: "pill", "blank" or "dom" (the default)
|
2017-03-23 14:23:33 +00:00
|
|
|
dragTiddlerFn: optional function to retrieve the title of tiddler to drag
|
|
|
|
dragFilterFn: optional function to retreive the filter defining a list of tiddlers to drag
|
2022-02-24 11:06:18 +00:00
|
|
|
widget: widget to use as the context for the filter
|
2017-03-23 14:23:33 +00:00
|
|
|
*/
|
|
|
|
exports.makeDraggable = function(options) {
|
2017-03-23 17:02:26 +00:00
|
|
|
var dragImageType = options.dragImageType || "dom",
|
|
|
|
dragImage,
|
2022-07-15 14:38:09 +00:00
|
|
|
domNode = options.domNode;
|
2017-03-28 12:05:00 +00:00
|
|
|
// Make the dom node draggable (not necessary for anchor tags)
|
2022-07-15 14:38:09 +00:00
|
|
|
if(!options.selector && ((domNode.tagName || "").toLowerCase() !== "a")) {
|
|
|
|
domNode.setAttribute("draggable","true");
|
2017-03-28 12:05:00 +00:00
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
// Add event handlers
|
2022-07-15 14:38:09 +00:00
|
|
|
$tw.utils.addEventListeners(domNode,[
|
2017-03-23 14:23:33 +00:00
|
|
|
{name: "dragstart", handlerFunction: function(event) {
|
2018-01-20 17:28:10 +00:00
|
|
|
if(event.dataTransfer === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
// Collect the tiddlers being dragged
|
|
|
|
var dragTiddler = options.dragTiddlerFn && options.dragTiddlerFn(),
|
|
|
|
dragFilter = options.dragFilterFn && options.dragFilterFn(),
|
2018-04-08 09:29:17 +00:00
|
|
|
titles = dragTiddler ? [dragTiddler] : [],
|
2022-07-15 14:38:09 +00:00
|
|
|
startActions = options.startActions,
|
|
|
|
variables,
|
|
|
|
domNodeRect;
|
2017-03-23 14:23:33 +00:00
|
|
|
if(dragFilter) {
|
|
|
|
titles.push.apply(titles,options.widget.wiki.filterTiddlers(dragFilter,options.widget));
|
|
|
|
}
|
|
|
|
var titleString = $tw.utils.stringifyList(titles);
|
|
|
|
// Check that we've something to drag
|
2022-07-15 14:38:09 +00:00
|
|
|
if(titles.length > 0 && (options.selector && $tw.utils.domMatchesSelector(event.target,options.selector) || event.target === domNode)) {
|
2017-03-23 14:23:33 +00:00
|
|
|
// Mark the drag in progress
|
|
|
|
$tw.dragInProgress = domNode;
|
|
|
|
// Set the dragging class on the element being dragged
|
2022-07-15 14:38:09 +00:00
|
|
|
$tw.utils.addClass(domNode,"tc-dragging");
|
2018-04-08 09:29:17 +00:00
|
|
|
// Invoke drag-start actions if given
|
|
|
|
if(startActions !== undefined) {
|
2022-04-15 12:46:09 +00:00
|
|
|
// Collect our variables
|
|
|
|
variables = $tw.utils.collectDOMVariables(domNode,null,event);
|
|
|
|
variables.modifier = $tw.keyboardManager.getEventModifierKeyDescriptor(event);
|
2022-04-15 16:05:27 +00:00
|
|
|
variables["actionTiddler"] = titleString;
|
2022-04-15 12:46:09 +00:00
|
|
|
options.widget.invokeActionString(startActions,options.widget,event,variables);
|
2018-04-08 09:29:17 +00:00
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
// Create the drag image elements
|
|
|
|
dragImage = options.widget.document.createElement("div");
|
|
|
|
dragImage.className = "tc-tiddler-dragger";
|
|
|
|
var inner = options.widget.document.createElement("div");
|
|
|
|
inner.className = "tc-tiddler-dragger-inner";
|
|
|
|
inner.appendChild(options.widget.document.createTextNode(
|
|
|
|
titles.length === 1 ?
|
|
|
|
titles[0] :
|
|
|
|
titles.length + " tiddlers"
|
|
|
|
));
|
|
|
|
dragImage.appendChild(inner);
|
|
|
|
options.widget.document.body.appendChild(dragImage);
|
|
|
|
// Set the data transfer properties
|
|
|
|
var dataTransfer = event.dataTransfer;
|
|
|
|
// Set up the image
|
2017-03-28 14:09:36 +00:00
|
|
|
dataTransfer.effectAllowed = "all";
|
2017-03-23 14:23:33 +00:00
|
|
|
if(dataTransfer.setDragImage) {
|
2017-03-23 17:02:26 +00:00
|
|
|
if(dragImageType === "pill") {
|
|
|
|
dataTransfer.setDragImage(dragImage.firstChild,-16,-16);
|
2023-07-22 14:47:39 +00:00
|
|
|
} else if(dragImageType === "blank") {
|
2021-09-12 13:20:03 +00:00
|
|
|
dragImage.removeChild(dragImage.firstChild);
|
|
|
|
dataTransfer.setDragImage(dragImage,0,0);
|
2017-03-23 17:02:26 +00:00
|
|
|
} else {
|
2017-03-23 17:52:15 +00:00
|
|
|
var r = domNode.getBoundingClientRect();
|
|
|
|
dataTransfer.setDragImage(domNode,event.clientX-r.left,event.clientY-r.top);
|
2017-03-23 17:02:26 +00:00
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
}
|
|
|
|
// Set up the data transfer
|
2017-03-23 17:02:26 +00:00
|
|
|
if(dataTransfer.clearData) {
|
2021-05-30 18:20:17 +00:00
|
|
|
dataTransfer.clearData();
|
2017-03-23 17:02:26 +00:00
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
var jsonData = [];
|
|
|
|
if(titles.length > 1) {
|
|
|
|
titles.forEach(function(title) {
|
|
|
|
jsonData.push(options.widget.wiki.getTiddlerAsJson(title));
|
|
|
|
});
|
|
|
|
jsonData = "[" + jsonData.join(",") + "]";
|
|
|
|
} else {
|
|
|
|
jsonData = options.widget.wiki.getTiddlerAsJson(titles[0]);
|
|
|
|
}
|
|
|
|
// IE doesn't like these content types
|
|
|
|
if(!$tw.browser.isIE) {
|
|
|
|
dataTransfer.setData("text/vnd.tiddler",jsonData);
|
|
|
|
dataTransfer.setData("text/plain",titleString);
|
|
|
|
dataTransfer.setData("text/x-moz-url","data:text/vnd.tiddler," + encodeURIComponent(jsonData));
|
2023-07-22 14:47:39 +00:00
|
|
|
}
|
|
|
|
// If browser is Chrome-like and has a touch-input device do NOT .setData
|
|
|
|
if(!($tw.browser.isMobileChrome)) {
|
2022-07-15 17:19:44 +00:00
|
|
|
dataTransfer.setData("URL","data:text/vnd.tiddler," + encodeURIComponent(jsonData));
|
2017-03-23 14:23:33 +00:00
|
|
|
}
|
|
|
|
dataTransfer.setData("Text",titleString);
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}},
|
|
|
|
{name: "dragend", handlerFunction: function(event) {
|
2022-07-15 14:38:09 +00:00
|
|
|
if((options.selector && $tw.utils.domMatchesSelector(event.target,options.selector)) || event.target === domNode) {
|
2018-04-08 09:29:17 +00:00
|
|
|
// Collect the tiddlers being dragged
|
|
|
|
var dragTiddler = options.dragTiddlerFn && options.dragTiddlerFn(),
|
|
|
|
dragFilter = options.dragFilterFn && options.dragFilterFn(),
|
|
|
|
titles = dragTiddler ? [dragTiddler] : [],
|
2022-07-15 14:38:09 +00:00
|
|
|
endActions = options.endActions,
|
|
|
|
variables;
|
2018-04-08 09:29:17 +00:00
|
|
|
if(dragFilter) {
|
|
|
|
titles.push.apply(titles,options.widget.wiki.filterTiddlers(dragFilter,options.widget));
|
|
|
|
}
|
|
|
|
var titleString = $tw.utils.stringifyList(titles);
|
2017-03-23 14:23:33 +00:00
|
|
|
$tw.dragInProgress = null;
|
2018-04-08 09:29:17 +00:00
|
|
|
// Invoke drag-end actions if given
|
|
|
|
if(endActions !== undefined) {
|
2022-04-15 12:46:09 +00:00
|
|
|
variables = $tw.utils.collectDOMVariables(domNode,null,event);
|
|
|
|
variables.modifier = $tw.keyboardManager.getEventModifierKeyDescriptor(event);
|
2022-04-15 16:05:27 +00:00
|
|
|
variables["actionTiddler"] = titleString;
|
|
|
|
options.widget.invokeActionString(endActions,options.widget,event,variables);
|
2018-04-08 09:29:17 +00:00
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
// Remove the dragging class on the element being dragged
|
2022-07-15 14:38:09 +00:00
|
|
|
$tw.utils.removeClass(domNode,"tc-dragging");
|
2017-03-23 14:23:33 +00:00
|
|
|
// Delete the drag image element
|
|
|
|
if(dragImage) {
|
|
|
|
dragImage.parentNode.removeChild(dragImage);
|
|
|
|
dragImage = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}}
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.importDataTransfer = function(dataTransfer,fallbackTitle,callback) {
|
|
|
|
// Try each provided data type in turn
|
2018-09-13 19:42:35 +00:00
|
|
|
if($tw.log.IMPORT) {
|
|
|
|
console.log("Available data types:");
|
|
|
|
for(var type=0; type<dataTransfer.types.length; type++) {
|
|
|
|
console.log("type",dataTransfer.types[type],dataTransfer.getData(dataTransfer.types[type]))
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
for(var t=0; t<importDataTypes.length; t++) {
|
|
|
|
if(!$tw.browser.isIE || importDataTypes[t].IECompatible) {
|
|
|
|
// Get the data
|
|
|
|
var dataType = importDataTypes[t];
|
2023-05-06 13:02:16 +00:00
|
|
|
var data = dataTransfer.getData(dataType.type);
|
2017-03-23 14:23:33 +00:00
|
|
|
// Import the tiddlers in the data
|
|
|
|
if(data !== "" && data !== null) {
|
|
|
|
if($tw.log.IMPORT) {
|
|
|
|
console.log("Importing data type '" + dataType.type + "', data: '" + data + "'")
|
|
|
|
}
|
|
|
|
var tiddlerFields = dataType.toTiddlerFieldsArray(data,fallbackTitle);
|
|
|
|
callback(tiddlerFields);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-06 13:02:16 +00:00
|
|
|
exports.importPaste = function(item,fallbackTitle,callback) {
|
|
|
|
// Try each provided data type in turn
|
|
|
|
for(var t=0; t<importDataTypes.length; t++) {
|
|
|
|
if(item.type === importDataTypes[t].type) {
|
|
|
|
// Get the data
|
|
|
|
var dataType = importDataTypes[t];
|
|
|
|
|
|
|
|
item.getAsString(function(data){
|
|
|
|
if($tw.log.IMPORT) {
|
|
|
|
console.log("Importing data type '" + dataType.type + "', data: '" + data + "'")
|
|
|
|
}
|
|
|
|
var tiddlerFields = dataType.toTiddlerFieldsArray(data,fallbackTitle);
|
|
|
|
callback(tiddlerFields);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.itemHasValidDataType = function(item) {
|
|
|
|
for(var t=0; t<importDataTypes.length; t++) {
|
|
|
|
if(!$tw.browser.isIE || importDataTypes[t].IECompatible) {
|
|
|
|
if(item.type === importDataTypes[t].type) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-23 14:23:33 +00:00
|
|
|
var importDataTypes = [
|
|
|
|
{type: "text/vnd.tiddler", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
|
|
|
return parseJSONTiddlers(data,fallbackTitle);
|
|
|
|
}},
|
|
|
|
{type: "URL", IECompatible: true, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
|
|
|
// Check for tiddler data URI
|
2021-08-29 12:39:32 +00:00
|
|
|
var match = $tw.utils.decodeURIComponentSafe(data).match(/^data\:text\/vnd\.tiddler,(.*)/i);
|
2017-03-23 14:23:33 +00:00
|
|
|
if(match) {
|
|
|
|
return parseJSONTiddlers(match[1],fallbackTitle);
|
|
|
|
} else {
|
2017-03-27 08:59:40 +00:00
|
|
|
return [{title: fallbackTitle, text: data}]; // As URL string
|
2017-03-23 14:23:33 +00:00
|
|
|
}
|
|
|
|
}},
|
|
|
|
{type: "text/x-moz-url", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
|
|
|
// Check for tiddler data URI
|
2021-08-29 12:39:32 +00:00
|
|
|
var match = $tw.utils.decodeURIComponentSafe(data).match(/^data\:text\/vnd\.tiddler,(.*)/i);
|
2017-03-23 14:23:33 +00:00
|
|
|
if(match) {
|
|
|
|
return parseJSONTiddlers(match[1],fallbackTitle);
|
|
|
|
} else {
|
2017-03-27 08:59:40 +00:00
|
|
|
return [{title: fallbackTitle, text: data}]; // As URL string
|
2017-03-23 14:23:33 +00:00
|
|
|
}
|
|
|
|
}},
|
|
|
|
{type: "text/html", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
2017-03-27 08:59:40 +00:00
|
|
|
return [{title: fallbackTitle, text: data}];
|
2017-03-23 14:23:33 +00:00
|
|
|
}},
|
|
|
|
{type: "text/plain", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
2017-03-27 08:59:40 +00:00
|
|
|
return [{title: fallbackTitle, text: data}];
|
2017-03-23 14:23:33 +00:00
|
|
|
}},
|
|
|
|
{type: "Text", IECompatible: true, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
2017-03-27 08:59:40 +00:00
|
|
|
return [{title: fallbackTitle, text: data}];
|
2017-03-23 14:23:33 +00:00
|
|
|
}},
|
|
|
|
{type: "text/uri-list", IECompatible: false, toTiddlerFieldsArray: function(data,fallbackTitle) {
|
2023-05-06 13:02:16 +00:00
|
|
|
// Check for tiddler data URI
|
|
|
|
var match = $tw.utils.decodeURIComponentSafe(data).match(/^data\:text\/vnd\.tiddler,(.*)/i);
|
|
|
|
if(match) {
|
|
|
|
return parseJSONTiddlers(match[1],fallbackTitle);
|
|
|
|
} else {
|
|
|
|
return [{title: fallbackTitle, text: data}]; // As URL string
|
|
|
|
}
|
2017-03-23 14:23:33 +00:00
|
|
|
}}
|
|
|
|
];
|
|
|
|
|
|
|
|
function parseJSONTiddlers(json,fallbackTitle) {
|
2022-02-21 15:29:25 +00:00
|
|
|
var data = $tw.utils.parseJSONSafe(json);
|
2017-03-23 14:23:33 +00:00
|
|
|
if(!$tw.utils.isArray(data)) {
|
|
|
|
data = [data];
|
|
|
|
}
|
|
|
|
data.forEach(function(fields) {
|
|
|
|
fields.title = fields.title || fallbackTitle;
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2021-11-21 20:48:16 +00:00
|
|
|
function dragEventContainsType(event,targetType) {
|
2021-05-19 20:52:43 +00:00
|
|
|
if(event.dataTransfer.types) {
|
|
|
|
for(var i=0; i<event.dataTransfer.types.length; i++) {
|
2021-11-21 20:48:16 +00:00
|
|
|
if(event.dataTransfer.types[i] === targetType) {
|
2021-05-19 20:52:43 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-11-21 20:48:16 +00:00
|
|
|
exports.dragEventContainsFiles = function(event) {
|
|
|
|
return (dragEventContainsType(event,"Files") && !dragEventContainsType(event,"text/plain"));
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.dragEventContainsType = dragEventContainsType;
|
|
|
|
|
2017-03-23 14:23:33 +00:00
|
|
|
})();
|