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
|
|
|
|
|
|
|
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) {
|
|
|
|
var self = this;
|
|
|
|
// Remember parent
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute attributes and execute state
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
// Create element
|
|
|
|
var domNode = this.document.createElement("button");
|
|
|
|
// Assign classes
|
|
|
|
var classes = this["class"].split(" ") || [];
|
2013-10-14 15:56:13 +00:00
|
|
|
if(this.selectedClass) {
|
|
|
|
if(this.set && this.setTo && this.isSelected()) {
|
|
|
|
$tw.utils.pushTop(classes,this.selectedClass.split(" "));
|
|
|
|
}
|
|
|
|
if(this.popup && this.isPoppedUp()) {
|
|
|
|
$tw.utils.pushTop(classes,this.selectedClass.split(" "));
|
2013-10-13 20:31:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
domNode.className = classes.join(" ");
|
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"]);
|
|
|
|
}
|
2013-10-13 20:31:00 +00:00
|
|
|
// Add a click event handler
|
|
|
|
domNode.addEventListener("click",function (event) {
|
|
|
|
var handled = false;
|
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;
|
|
|
|
}
|
|
|
|
if(self.popup) {
|
|
|
|
self.triggerPopup(event);
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
if(self.set) {
|
|
|
|
self.setTiddler();
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
if(handled) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
return handled;
|
|
|
|
},false);
|
|
|
|
// Insert element
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
|
|
|
ButtonWidget.prototype.isSelected = function() {
|
2013-10-14 12:52:03 +00:00
|
|
|
var tiddler = this.wiki.getTiddler(this.set);
|
2013-11-04 09:59:26 +00:00
|
|
|
return tiddler ? tiddler.fields.text === this.setTo : this.defaultSetValue === this.setTo;
|
2013-10-13 20:31:00 +00:00
|
|
|
};
|
|
|
|
|
2013-10-14 15:56:13 +00:00
|
|
|
ButtonWidget.prototype.isPoppedUp = function() {
|
|
|
|
var tiddler = this.wiki.getTiddler(this.popup);
|
|
|
|
var result = tiddler && tiddler.fields.text ? $tw.popup.readPopupState(this.popup,tiddler.fields.text) : false;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2014-03-08 10:22:09 +00:00
|
|
|
ButtonWidget.prototype.navigateTo = function(event) {
|
|
|
|
var bounds = this.domNodes[0].getBoundingClientRect();
|
|
|
|
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
|
|
|
|
},
|
|
|
|
navigateSuppressNavigation: event.metaKey || event.ctrlKey || (event.button === 1)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-10-13 20:31:00 +00:00
|
|
|
ButtonWidget.prototype.dispatchMessage = function(event) {
|
2013-10-28 23:40:45 +00:00
|
|
|
this.dispatchEvent({type: this.message, param: this.param, tiddlerTitle: this.getVariable("currentTiddler")});
|
2013-10-13 20:31:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ButtonWidget.prototype.triggerPopup = function(event) {
|
|
|
|
$tw.popup.triggerPopup({
|
|
|
|
domNode: this.domNodes[0],
|
2013-10-14 12:52:03 +00:00
|
|
|
title: this.popup,
|
2013-10-13 20:31:00 +00:00
|
|
|
wiki: this.wiki
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ButtonWidget.prototype.setTiddler = function() {
|
2014-03-15 17:02:13 +00:00
|
|
|
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
|
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");
|
|
|
|
this["class"] = this.getAttribute("class","");
|
2014-06-14 17:06:56 +00:00
|
|
|
this["aria-label"] = this.getAttribute("aria-label");
|
2014-09-10 09:25:50 +00:00
|
|
|
this.tooltip = this.getAttribute("tooltip");
|
|
|
|
// DEPRECATION WARNING
|
|
|
|
var title = this.getAttribute("title");
|
|
|
|
if(title) {
|
|
|
|
console.log("WARNING: attribute 'title' on button widget should be replaced with 'tooltip'");
|
|
|
|
this.tooltip = title;
|
|
|
|
}
|
|
|
|
|
2013-10-14 20:38:12 +00:00
|
|
|
this.style = this.getAttribute("style");
|
2013-10-13 20:31:00 +00:00
|
|
|
this.selectedClass = this.getAttribute("selectedClass");
|
2013-11-04 09:59:26 +00:00
|
|
|
this.defaultSetValue = this.getAttribute("default");
|
2013-10-13 20:31:00 +00:00
|
|
|
// Make child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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();
|
2014-02-22 16:13:16 +00:00
|
|
|
if(changedAttributes.to || changedAttributes.message || changedAttributes.param || changedAttributes.set || changedAttributes.setTo || changedAttributes.popup || changedAttributes.hover || changedAttributes["class"] || changedAttributes.selectedClass || changedAttributes.style || (this.set && changedTiddlers[this.set]) || (this.popup && changedTiddlers[this.popup])) {
|
2013-10-13 20:31:00 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.button = ButtonWidget;
|
|
|
|
|
|
|
|
})();
|