2017-03-19 19:33:56 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/draggable.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Draggable widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var DraggableWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
DraggableWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
DraggableWidget.prototype.render = function(parent,nextSibling) {
|
2022-02-24 11:06:18 +00:00
|
|
|
var self = this,
|
|
|
|
tag,
|
|
|
|
domNode,
|
|
|
|
classes = [];
|
2017-03-19 19:33:56 +00:00
|
|
|
// Save the parent dom node
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute our attributes
|
|
|
|
this.computeAttributes();
|
|
|
|
// Execute our logic
|
|
|
|
this.execute();
|
|
|
|
// Sanitise the specified tag
|
2022-02-24 11:06:18 +00:00
|
|
|
tag = this.draggableTag;
|
2017-03-19 19:33:56 +00:00
|
|
|
if($tw.config.htmlUnsafeElements.indexOf(tag) !== -1) {
|
|
|
|
tag = "div";
|
|
|
|
}
|
|
|
|
// Create our element
|
2022-02-24 11:06:18 +00:00
|
|
|
domNode = this.document.createElement(tag);
|
2017-03-19 19:33:56 +00:00
|
|
|
// Assign classes
|
|
|
|
if(this.draggableClasses) {
|
|
|
|
classes.push(this.draggableClasses);
|
|
|
|
}
|
2022-04-15 13:17:06 +00:00
|
|
|
if(!this.dragHandleSelector && this.dragEnable) {
|
2022-02-24 11:06:18 +00:00
|
|
|
classes.push("tc-draggable");
|
|
|
|
}
|
2017-03-19 19:33:56 +00:00
|
|
|
domNode.setAttribute("class",classes.join(" "));
|
2022-02-24 11:06:18 +00:00
|
|
|
// Insert the node into the DOM and render any children
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
2017-03-19 19:33:56 +00:00
|
|
|
// Add event handlers
|
2022-04-15 13:17:06 +00:00
|
|
|
if(this.dragEnable) {
|
|
|
|
$tw.utils.makeDraggable({
|
|
|
|
domNode: domNode,
|
|
|
|
dragTiddlerFn: function() {return self.getAttribute("tiddler");},
|
|
|
|
dragFilterFn: function() {return self.getAttribute("filter");},
|
|
|
|
startActions: self.startActions,
|
|
|
|
endActions: self.endActions,
|
|
|
|
dragImageType: self.dragImageType,
|
|
|
|
widget: this,
|
|
|
|
selector: self.dragHandleSelector
|
|
|
|
});
|
|
|
|
}
|
2017-03-19 19:33:56 +00:00
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
DraggableWidget.prototype.execute = function() {
|
|
|
|
// Pick up our attributes
|
|
|
|
this.draggableTag = this.getAttribute("tag","div");
|
|
|
|
this.draggableClasses = this.getAttribute("class");
|
2018-04-08 09:29:17 +00:00
|
|
|
this.startActions = this.getAttribute("startactions");
|
|
|
|
this.endActions = this.getAttribute("endactions");
|
2022-02-24 11:06:18 +00:00
|
|
|
this.dragImageType = this.getAttribute("dragimagetype");
|
|
|
|
this.dragHandleSelector = this.getAttribute("selector");
|
2022-04-15 13:17:06 +00:00
|
|
|
this.dragEnable = this.getAttribute("enable","yes") === "yes";
|
2017-03-19 19:33:56 +00:00
|
|
|
// Make the child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
DraggableWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2022-04-15 13:17:06 +00:00
|
|
|
if($tw.utils.count(changedAttributes) > 0) {
|
2017-03-19 19:33:56 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.draggable = DraggableWidget;
|
|
|
|
|
|
|
|
})();
|