2013-10-13 20:31:00 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/button.js
|
2013-10-13 20:31:00 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-13 20:31:00 +00:00
|
|
|
|
|
|
|
Button 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-13 20:31:00 +00:00
|
|
|
|
2022-12-01 21:16:44 +00:00
|
|
|
var Popup = require("$:/core/modules/utils/dom/popup.js");
|
|
|
|
|
2013-10-13 20:31:00 +00:00
|
|
|
var ButtonWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
ButtonWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
ButtonWidget.prototype.render = function(parent,nextSibling) {
|
2020-11-22 21:13:24 +00:00
|
|
|
var self = this,
|
|
|
|
tag = "button",
|
|
|
|
domNode;
|
2013-10-13 20:31:00 +00:00
|
|
|
// Remember parent
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute attributes and execute state
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
// Create element
|
2015-04-21 18:29:54 +00:00
|
|
|
if(this.buttonTag && $tw.config.htmlUnsafeElements.indexOf(this.buttonTag) === -1) {
|
|
|
|
tag = this.buttonTag;
|
|
|
|
}
|
2020-11-22 21:13:24 +00:00
|
|
|
domNode = this.document.createElement(tag);
|
|
|
|
this.domNode = domNode;
|
2013-10-13 20:31:00 +00:00
|
|
|
// Assign classes
|
2020-11-24 21:19:20 +00:00
|
|
|
var classes = this["class"].split(" ") || [],
|
|
|
|
isPoppedUp = (this.popup || this.popupTitle) && this.isPoppedUp();
|
|
|
|
if(this.selectedClass) {
|
|
|
|
if((this.set || this.setTitle) && this.setTo && this.isSelected()) {
|
2022-06-28 13:05:52 +00:00
|
|
|
$tw.utils.pushTop(classes, this.selectedClass.split(" "));
|
|
|
|
domNode.setAttribute("aria-checked", "true");
|
2020-11-24 21:19:20 +00:00
|
|
|
}
|
|
|
|
if(isPoppedUp) {
|
|
|
|
$tw.utils.pushTop(classes,this.selectedClass.split(" "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isPoppedUp) {
|
|
|
|
$tw.utils.pushTop(classes,"tc-popup-handle");
|
|
|
|
}
|
|
|
|
domNode.className = classes.join(" ");
|
2023-11-22 20:05:40 +00:00
|
|
|
// Assign data- attributes
|
|
|
|
this.assignAttributes(domNode,{
|
|
|
|
sourcePrefix: "data-",
|
|
|
|
destPrefix: "data-"
|
|
|
|
});
|
2014-06-14 17:06:56 +00:00
|
|
|
// Assign other attributes
|
2013-11-04 09:58:56 +00:00
|
|
|
if(this.style) {
|
|
|
|
domNode.setAttribute("style",this.style);
|
|
|
|
}
|
2014-09-10 09:25:50 +00:00
|
|
|
if(this.tooltip) {
|
|
|
|
domNode.setAttribute("title",this.tooltip);
|
2014-06-14 17:06:56 +00:00
|
|
|
}
|
|
|
|
if(this["aria-label"]) {
|
|
|
|
domNode.setAttribute("aria-label",this["aria-label"]);
|
|
|
|
}
|
2022-06-28 13:05:52 +00:00
|
|
|
if (this.role) {
|
|
|
|
domNode.setAttribute("role", this.role);
|
|
|
|
}
|
2021-03-01 17:59:29 +00:00
|
|
|
if(this.popup || this.popupTitle) {
|
|
|
|
domNode.setAttribute("aria-expanded",isPoppedUp ? "true" : "false");
|
|
|
|
}
|
2020-02-05 12:14:38 +00:00
|
|
|
// Set the tabindex
|
|
|
|
if(this.tabIndex) {
|
|
|
|
domNode.setAttribute("tabindex",this.tabIndex);
|
2020-11-08 22:32:27 +00:00
|
|
|
}
|
|
|
|
if(this.isDisabled === "yes") {
|
|
|
|
domNode.setAttribute("disabled",true);
|
|
|
|
}
|
2013-10-13 20:31:00 +00:00
|
|
|
// Add a click event handler
|
|
|
|
domNode.addEventListener("click",function (event) {
|
|
|
|
var handled = false;
|
2018-03-23 11:08:53 +00:00
|
|
|
if(self.invokeActions(self,event)) {
|
2014-10-08 16:45:26 +00:00
|
|
|
handled = true;
|
|
|
|
}
|
2014-02-22 16:13:16 +00:00
|
|
|
if(self.to) {
|
2014-03-08 10:22:09 +00:00
|
|
|
self.navigateTo(event);
|
2014-02-22 16:13:16 +00:00
|
|
|
handled = true;
|
|
|
|
}
|
2013-10-13 20:31:00 +00:00
|
|
|
if(self.message) {
|
|
|
|
self.dispatchMessage(event);
|
|
|
|
handled = true;
|
|
|
|
}
|
2018-11-18 19:16:46 +00:00
|
|
|
if(self.popup || self.popupTitle) {
|
2013-10-13 20:31:00 +00:00
|
|
|
self.triggerPopup(event);
|
|
|
|
handled = true;
|
|
|
|
}
|
2018-11-18 19:16:46 +00:00
|
|
|
if(self.set || self.setTitle) {
|
2013-10-13 20:31:00 +00:00
|
|
|
self.setTiddler();
|
|
|
|
handled = true;
|
|
|
|
}
|
2016-04-29 17:53:23 +00:00
|
|
|
if(self.actions) {
|
2020-07-14 16:04:06 +00:00
|
|
|
var modifierKey = $tw.keyboardManager.getEventModifierKeyDescriptor(event);
|
|
|
|
self.invokeActionString(self.actions,self,event,{modifier: modifierKey});
|
2016-04-29 17:53:23 +00:00
|
|
|
}
|
2013-10-13 20:31:00 +00:00
|
|
|
if(handled) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
return handled;
|
|
|
|
},false);
|
2017-03-23 14:23:33 +00:00
|
|
|
// Make it draggable if required
|
|
|
|
if(this.dragTiddler || this.dragFilter) {
|
|
|
|
$tw.utils.makeDraggable({
|
|
|
|
domNode: domNode,
|
|
|
|
dragTiddlerFn: function() {return self.dragTiddler;},
|
|
|
|
dragFilterFn: function() {return self.dragFilter;},
|
|
|
|
widget: this
|
|
|
|
});
|
|
|
|
}
|
2013-10-13 20:31:00 +00:00
|
|
|
// Insert element
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
2015-03-25 22:13:22 +00:00
|
|
|
/*
|
|
|
|
We don't allow actions to propagate because we trigger actions ourselves
|
|
|
|
*/
|
|
|
|
ButtonWidget.prototype.allowActionPropagation = function() {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2014-10-08 16:45:26 +00:00
|
|
|
ButtonWidget.prototype.getBoundingClientRect = function() {
|
|
|
|
return this.domNodes[0].getBoundingClientRect();
|
2015-05-10 11:26:00 +00:00
|
|
|
};
|
2014-10-08 16:45:26 +00:00
|
|
|
|
2013-10-13 20:31:00 +00:00
|
|
|
ButtonWidget.prototype.isSelected = function() {
|
2018-11-18 19:16:46 +00:00
|
|
|
return this.setTitle ? (this.setField ? this.wiki.getTiddler(this.setTitle).getFieldString(this.setField) === this.setTo :
|
|
|
|
(this.setIndex ? this.wiki.extractTiddlerDataItem(this.setTitle,this.setIndex) === this.setTo :
|
|
|
|
this.wiki.getTiddlerText(this.setTitle))) || this.defaultSetValue || this.getVariable("currentTiddler") :
|
|
|
|
this.wiki.getTextReference(this.set,this.defaultSetValue,this.getVariable("currentTiddler")) === this.setTo;
|
2013-10-13 20:31:00 +00:00
|
|
|
};
|
|
|
|
|
2013-10-14 15:56:13 +00:00
|
|
|
ButtonWidget.prototype.isPoppedUp = function() {
|
2018-11-18 19:16:46 +00:00
|
|
|
var tiddler = this.popupTitle ? this.wiki.getTiddler(this.popupTitle) : this.wiki.getTiddler(this.popup);
|
2022-12-01 21:16:44 +00:00
|
|
|
var result = tiddler && tiddler.fields.text ? Popup.readPopupState(tiddler.fields.text) : false;
|
2013-10-14 15:56:13 +00:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2014-03-08 10:22:09 +00:00
|
|
|
ButtonWidget.prototype.navigateTo = function(event) {
|
2014-10-08 16:45:26 +00:00
|
|
|
var bounds = this.getBoundingClientRect();
|
2014-03-08 10:22:09 +00:00
|
|
|
this.dispatchEvent({
|
2014-08-28 20:43:44 +00:00
|
|
|
type: "tm-navigate",
|
2014-03-08 10:22:09 +00:00
|
|
|
navigateTo: this.to,
|
|
|
|
navigateFromTitle: this.getVariable("storyTiddler"),
|
|
|
|
navigateFromNode: this,
|
|
|
|
navigateFromClientRect: { top: bounds.top, left: bounds.left, width: bounds.width, right: bounds.right, bottom: bounds.bottom, height: bounds.height
|
|
|
|
},
|
2016-10-20 08:44:52 +00:00
|
|
|
navigateSuppressNavigation: event.metaKey || event.ctrlKey || (event.button === 1),
|
|
|
|
event: event
|
2014-03-08 10:22:09 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-10-13 20:31:00 +00:00
|
|
|
ButtonWidget.prototype.dispatchMessage = function(event) {
|
2016-10-20 08:44:52 +00:00
|
|
|
this.dispatchEvent({type: this.message, param: this.param, tiddlerTitle: this.getVariable("currentTiddler"), event: event});
|
2013-10-13 20:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ButtonWidget.prototype.triggerPopup = function(event) {
|
2018-11-18 19:16:46 +00:00
|
|
|
if(this.popupTitle) {
|
|
|
|
$tw.popup.triggerPopup({
|
|
|
|
domNode: this.domNodes[0],
|
2022-12-01 21:16:44 +00:00
|
|
|
absolute: (this.popupAbsCoords === "yes"),
|
2018-11-18 19:16:46 +00:00
|
|
|
title: this.popupTitle,
|
|
|
|
wiki: this.wiki,
|
|
|
|
noStateReference: true
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$tw.popup.triggerPopup({
|
|
|
|
domNode: this.domNodes[0],
|
2022-12-01 21:16:44 +00:00
|
|
|
absolute: (this.popupAbsCoords === "yes"),
|
2018-11-18 19:16:46 +00:00
|
|
|
title: this.popup,
|
|
|
|
wiki: this.wiki
|
|
|
|
});
|
|
|
|
}
|
2013-10-13 20:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ButtonWidget.prototype.setTiddler = function() {
|
2018-11-18 19:16:46 +00:00
|
|
|
if(this.setTitle) {
|
|
|
|
this.setField ? this.wiki.setText(this.setTitle,this.setField,undefined,this.setTo) :
|
|
|
|
(this.setIndex ? this.wiki.setText(this.setTitle,undefined,this.setIndex,this.setTo) :
|
|
|
|
this.wiki.setText(this.setTitle,"text",undefined,this.setTo));
|
|
|
|
} else {
|
|
|
|
this.wiki.setTextReference(this.set,this.setTo,this.getVariable("currentTiddler"));
|
|
|
|
}
|
2013-10-13 20:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
ButtonWidget.prototype.execute = function() {
|
|
|
|
// Get attributes
|
2016-04-29 17:53:23 +00:00
|
|
|
this.actions = this.getAttribute("actions");
|
2014-02-22 16:13:16 +00:00
|
|
|
this.to = this.getAttribute("to");
|
2013-10-13 20:31:00 +00:00
|
|
|
this.message = this.getAttribute("message");
|
|
|
|
this.param = this.getAttribute("param");
|
|
|
|
this.set = this.getAttribute("set");
|
|
|
|
this.setTo = this.getAttribute("setTo");
|
|
|
|
this.popup = this.getAttribute("popup");
|
|
|
|
this.hover = this.getAttribute("hover");
|
2014-06-14 17:06:56 +00:00
|
|
|
this["aria-label"] = this.getAttribute("aria-label");
|
2022-06-28 13:05:52 +00:00
|
|
|
this.role = this.getAttribute("role");
|
2014-09-10 09:25:50 +00:00
|
|
|
this.tooltip = this.getAttribute("tooltip");
|
2013-10-14 20:38:12 +00:00
|
|
|
this.style = this.getAttribute("style");
|
2020-11-24 21:19:20 +00:00
|
|
|
this["class"] = this.getAttribute("class","");
|
2013-10-13 20:31:00 +00:00
|
|
|
this.selectedClass = this.getAttribute("selectedClass");
|
2015-05-13 23:07:53 +00:00
|
|
|
this.defaultSetValue = this.getAttribute("default","");
|
2015-04-21 18:29:54 +00:00
|
|
|
this.buttonTag = this.getAttribute("tag");
|
2017-03-23 14:23:33 +00:00
|
|
|
this.dragTiddler = this.getAttribute("dragTiddler");
|
|
|
|
this.dragFilter = this.getAttribute("dragFilter");
|
2018-11-18 19:16:46 +00:00
|
|
|
this.setTitle = this.getAttribute("setTitle");
|
|
|
|
this.setField = this.getAttribute("setField");
|
|
|
|
this.setIndex = this.getAttribute("setIndex");
|
|
|
|
this.popupTitle = this.getAttribute("popupTitle");
|
2022-12-01 21:16:44 +00:00
|
|
|
this.popupAbsCoords = this.getAttribute("popupAbsCoords", "no");
|
2020-02-05 12:14:38 +00:00
|
|
|
this.tabIndex = this.getAttribute("tabindex");
|
2020-11-08 22:32:27 +00:00
|
|
|
this.isDisabled = this.getAttribute("disabled","no");
|
2013-10-13 20:31:00 +00:00
|
|
|
// Make child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
2020-11-24 21:19:20 +00:00
|
|
|
ButtonWidget.prototype.updateDomNodeClasses = function() {
|
|
|
|
var domNodeClasses = this.domNode.className.split(" "),
|
|
|
|
oldClasses = this.class.split(" "),
|
2021-05-30 18:20:17 +00:00
|
|
|
newClasses;
|
2020-11-24 21:19:20 +00:00
|
|
|
this["class"] = this.getAttribute("class","");
|
|
|
|
newClasses = this.class.split(" ");
|
|
|
|
//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);
|
2020-11-22 21:13:24 +00:00
|
|
|
}
|
2020-11-24 21:19:20 +00:00
|
|
|
});
|
|
|
|
//Add new classes from updated class attribute.
|
|
|
|
$tw.utils.pushTop(domNodeClasses,newClasses);
|
|
|
|
this.domNode.className = domNodeClasses.join(" ");
|
2023-11-22 20:05:40 +00:00
|
|
|
};
|
2020-11-22 21:13:24 +00:00
|
|
|
|
2013-10-13 20:31:00 +00:00
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
ButtonWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2024-06-12 09:20:40 +00:00
|
|
|
if(changedAttributes.tooltip || changedAttributes.actions || changedAttributes.to || changedAttributes.message || changedAttributes.param || changedAttributes.set || changedAttributes.setTo || changedAttributes.popup || changedAttributes.hover || changedAttributes.selectedClass || changedAttributes.style || changedAttributes.dragFilter || changedAttributes.dragTiddler || (this.set && changedTiddlers[this.set]) || (this.popup && changedTiddlers[this.popup]) || (this.popupTitle && changedTiddlers[this.popupTitle]) || changedAttributes.popupAbsCoords || changedAttributes.setTitle || changedAttributes.setField || changedAttributes.setIndex || changedAttributes.popupTitle || changedAttributes.disabled || changedAttributes["default"]) {
|
2013-10-13 20:31:00 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
2023-11-22 20:05:40 +00:00
|
|
|
} else {
|
|
|
|
if(changedAttributes["class"]) {
|
|
|
|
this.updateDomNodeClasses();
|
|
|
|
}
|
|
|
|
this.assignAttributes(this.domNodes[0],{
|
|
|
|
changedAttributes: changedAttributes,
|
|
|
|
sourcePrefix: "data-",
|
|
|
|
destPrefix: "data-"
|
|
|
|
});
|
2013-10-13 20:31:00 +00:00
|
|
|
}
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.button = ButtonWidget;
|
|
|
|
|
|
|
|
})();
|