mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2026-02-22 18:09:50 +00:00
* Introduced preliminary idea for infinite recurse exception * Better handling of infinite recursion But it could be better still... * the TransclusionError is a proper error Moved the magic number to be on the error's class. Not sure if that's a great idea. * Fixed minor minor issue that came up in conflict The minor fix to the jasmine regexp that escaped a '+' somehow broke some random test. * Removing patch fix for recursion errors * Fixed issue where buttton and other widgets don't clean up * Added release notes for #9548 * Update test-widget.js If I don't fix those indentations, the entire TW codebase will explode or soemthing. * Update test-widget.js These lint problems are wasting my time. * Fixed all core widgets to not leak when renderChildren fails * Updated release notes to reflect what I'm actually fixing * Update test-widget.js Added warning not to use for-of loop for defining tests. The iterating variable needs to have its own method scope, or it risks being the same value for all tests.
131 lines
3.7 KiB
JavaScript
131 lines
3.7 KiB
JavaScript
/*\
|
|
title: $:/core/modules/widgets/draggable.js
|
|
type: application/javascript
|
|
module-type: widget
|
|
|
|
Draggable widget
|
|
|
|
\*/
|
|
|
|
"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) {
|
|
var self = this,
|
|
tag,
|
|
domNode,
|
|
classes = [];
|
|
// Save the parent dom node
|
|
this.parentDomNode = parent;
|
|
// Compute our attributes
|
|
this.computeAttributes();
|
|
// Execute our logic
|
|
this.execute();
|
|
// Sanitise the specified tag
|
|
tag = this.draggableTag;
|
|
if($tw.config.htmlUnsafeElements.indexOf(tag) !== -1) {
|
|
tag = "div";
|
|
}
|
|
// Create our element
|
|
domNode = this.document.createElement(tag);
|
|
// Assign classes
|
|
if(this.draggableClasses) {
|
|
classes.push(this.draggableClasses);
|
|
}
|
|
if(!this.dragHandleSelector && this.dragEnable) {
|
|
classes.push("tc-draggable");
|
|
}
|
|
domNode.setAttribute("class",classes.join(" "));
|
|
// Assign data- attributes and style. attributes
|
|
this.assignAttributes(domNode,{
|
|
sourcePrefix: "data-",
|
|
destPrefix: "data-"
|
|
});
|
|
// Insert the node into the DOM and render any children
|
|
parent.insertBefore(domNode,nextSibling);
|
|
this.domNodes.push(domNode);
|
|
this.renderChildren(domNode,null);
|
|
// Add event handlers
|
|
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
|
|
});
|
|
}
|
|
};
|
|
|
|
/*
|
|
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");
|
|
this.startActions = this.getAttribute("startactions");
|
|
this.endActions = this.getAttribute("endactions");
|
|
this.dragImageType = this.getAttribute("dragimagetype");
|
|
this.dragHandleSelector = this.getAttribute("selector");
|
|
this.dragEnable = this.getAttribute("enable","yes") === "yes";
|
|
// Make the child widgets
|
|
this.makeChildWidgets();
|
|
};
|
|
|
|
|
|
DraggableWidget.prototype.updateDomNodeClasses = function() {
|
|
var domNodeClasses = this.domNodes[0].className.split(" "),
|
|
oldClasses = this.draggableClasses.split(" ");
|
|
this.draggableClasses = this.getAttribute("class");
|
|
//Remove classes assigned from the old value of class attribute
|
|
$tw.utils.each(oldClasses,function(oldClass){
|
|
var i = domNodeClasses.indexOf(oldClass);
|
|
if(i !== -1) {
|
|
domNodeClasses.splice(i,1);
|
|
}
|
|
});
|
|
//Add new classes from updated class attribute.
|
|
$tw.utils.pushTop(domNodeClasses,this.draggableClasses);
|
|
this.domNodes[0].setAttribute("class",domNodeClasses.join(" "))
|
|
}
|
|
|
|
/*
|
|
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();
|
|
if(changedAttributes.tag || changedAttributes.selector || changedAttributes.dragimagetype || changedAttributes.enable || changedAttributes.startactions || changedAttributes.endactions) {
|
|
this.refreshSelf();
|
|
return true;
|
|
} else {
|
|
if(changedAttributes["class"]) {
|
|
this.updateDomNodeClasses();
|
|
}
|
|
this.assignAttributes(this.domNodes[0],{
|
|
changedAttributes: changedAttributes,
|
|
sourcePrefix: "data-",
|
|
destPrefix: "data-"
|
|
});
|
|
}
|
|
return this.refreshChildren(changedTiddlers);
|
|
};
|
|
|
|
exports.draggable = DraggableWidget;
|