2012-12-13 21:34:31 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widget/button.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Implements the button widget.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
var ButtonWidget = function(renderer) {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Save state
|
|
|
|
this.renderer = renderer;
|
|
|
|
// Generate child nodes
|
2013-01-03 16:27:55 +00:00
|
|
|
this.generate();
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-03 16:27:55 +00:00
|
|
|
ButtonWidget.prototype.generate = function() {
|
2012-12-13 21:34:31 +00:00
|
|
|
// Get the parameters from the attributes
|
|
|
|
this.message = this.renderer.getAttribute("message");
|
|
|
|
this.param = this.renderer.getAttribute("param");
|
|
|
|
this.set = this.renderer.getAttribute("set");
|
|
|
|
this.setTo = this.renderer.getAttribute("setTo");
|
|
|
|
this.popup = this.renderer.getAttribute("popup");
|
|
|
|
this.hover = this.renderer.getAttribute("hover");
|
|
|
|
this.qualifyTiddlerTitles = this.renderer.getAttribute("qualifyTiddlerTitles");
|
|
|
|
this["class"] = this.renderer.getAttribute("class");
|
2013-03-18 22:18:54 +00:00
|
|
|
this.selectedClass = this.renderer.getAttribute("selectedClass");
|
2012-12-13 21:34:31 +00:00
|
|
|
// Compose the button
|
2013-01-03 16:27:55 +00:00
|
|
|
var classes = ["tw-button"];
|
2012-12-13 21:34:31 +00:00
|
|
|
if(this["class"]) {
|
|
|
|
$tw.utils.pushTop(classes,this["class"]);
|
|
|
|
}
|
|
|
|
var events = [{name: "click", handlerObject: this, handlerMethod: "handleClickEvent"}];
|
|
|
|
if(this.hover === "yes") {
|
|
|
|
events.push({name: "mouseover", handlerObject: this, handlerMethod: "handleMouseOverOrOutEvent"});
|
|
|
|
events.push({name: "mouseout", handlerObject: this, handlerMethod: "handleMouseOverOrOutEvent"});
|
|
|
|
}
|
2013-03-18 22:18:54 +00:00
|
|
|
if(this.set && this.setTo && this.selectedClass) {
|
|
|
|
if(this.isSelected()) {
|
|
|
|
classes.push(this.selectedClass);
|
|
|
|
}
|
|
|
|
}
|
2013-01-03 16:27:55 +00:00
|
|
|
// Set the return element
|
|
|
|
this.tag = "button";
|
|
|
|
this.attributes ={"class": classes.join(" ")};
|
2013-05-15 16:32:17 +00:00
|
|
|
this.children = this.renderer.renderTree.createRenderers(this.renderer,this.renderer.parseTreeNode.children);
|
2013-01-03 16:27:55 +00:00
|
|
|
this.events = events;
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ButtonWidget.prototype.dispatchMessage = function(event) {
|
2012-12-13 21:34:31 +00:00
|
|
|
$tw.utils.dispatchCustomEvent(event.target,this.message,{
|
|
|
|
param: this.param,
|
2013-05-15 16:32:17 +00:00
|
|
|
tiddlerTitle: this.renderer.tiddlerTitle
|
2012-12-13 21:34:31 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ButtonWidget.prototype.triggerPopup = function(event) {
|
2012-12-31 18:36:39 +00:00
|
|
|
var title = this.popup;
|
|
|
|
if(this.qualifyTiddlerTitles) {
|
2013-05-15 16:32:17 +00:00
|
|
|
title = title + "-" + this.renderer.renderTree.getContextScopeId(this.renderer.parentRenderer);
|
2012-12-31 18:36:39 +00:00
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
$tw.popup.triggerPopup({
|
|
|
|
domNode: this.renderer.domNode,
|
2012-12-31 18:36:39 +00:00
|
|
|
title: title,
|
2012-12-13 21:34:31 +00:00
|
|
|
wiki: this.renderer.renderTree.wiki
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-18 22:18:54 +00:00
|
|
|
ButtonWidget.prototype.isSelected = function() {
|
|
|
|
var title = this.set;
|
|
|
|
if(this.qualifyTiddlerTitles) {
|
2013-05-15 16:32:17 +00:00
|
|
|
title = title + "-" + this.renderer.renderTree.getContextScopeId(this.renderer.parentRenderer);
|
2013-03-18 22:18:54 +00:00
|
|
|
}
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(title);
|
|
|
|
return tiddler ? tiddler.fields.text === this.setTo : false;
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ButtonWidget.prototype.setTiddler = function() {
|
2013-03-18 22:18:54 +00:00
|
|
|
var title = this.set;
|
|
|
|
if(this.qualifyTiddlerTitles) {
|
2013-05-15 16:32:17 +00:00
|
|
|
title = title + "-" + this.renderer.renderTree.getContextScopeId(this.renderer.parentRenderer);
|
2013-03-18 22:18:54 +00:00
|
|
|
}
|
|
|
|
var tiddler = this.renderer.renderTree.wiki.getTiddler(title);
|
|
|
|
this.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: title, text: this.setTo}));
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ButtonWidget.prototype.handleClickEvent = function(event) {
|
2013-03-19 16:45:07 +00:00
|
|
|
var handled = false;
|
2012-12-13 21:34:31 +00:00
|
|
|
if(this.message) {
|
|
|
|
this.dispatchMessage(event);
|
2013-03-19 16:45:07 +00:00
|
|
|
handled = true;
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
if(this.popup) {
|
|
|
|
this.triggerPopup(event);
|
2013-03-19 16:45:07 +00:00
|
|
|
handled = true;
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
|
|
|
if(this.set && this.setTo) {
|
|
|
|
this.setTiddler();
|
2013-03-19 16:45:07 +00:00
|
|
|
handled = true;
|
2012-12-13 21:34:31 +00:00
|
|
|
}
|
2013-03-19 16:45:07 +00:00
|
|
|
event.stopPropagation();
|
2012-12-13 21:34:31 +00:00
|
|
|
event.preventDefault();
|
2013-03-19 16:45:07 +00:00
|
|
|
return handled;
|
2012-12-13 21:34:31 +00:00
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ButtonWidget.prototype.handleMouseOverOrOutEvent = function(event) {
|
2012-12-13 21:34:31 +00:00
|
|
|
if(this.popup) {
|
|
|
|
this.triggerPopup(event);
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
ButtonWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
|
2013-03-18 22:18:54 +00:00
|
|
|
var setTitle = this.set,
|
|
|
|
popupTitle = this.popup;
|
|
|
|
if(this.qualifyTiddlerTitles) {
|
2013-05-15 16:32:17 +00:00
|
|
|
var scopeId = this.renderer.renderTree.getContextScopeId(this.renderer.parentRenderer);
|
2013-03-18 22:18:54 +00:00
|
|
|
if(setTitle) {
|
|
|
|
setTitle = setTitle + "-" + scopeId;
|
|
|
|
}
|
|
|
|
if(popupTitle) {
|
|
|
|
popupTitle = popupTitle + "-" + scopeId;
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 21:34:31 +00:00
|
|
|
// Check if any of our attributes have changed, or if a tiddler we're interested in has changed
|
2013-03-18 22:18:54 +00:00
|
|
|
if(changedAttributes.message || changedAttributes.param || changedAttributes.set || changedAttributes.setTo || changedAttributes.popup || changedAttributes.hover || changedAttributes.qualifyTiddlerTitles || changedAttributes["class"] || (setTitle && changedTiddlers[setTitle]) || (popupTitle && changedTiddlers[popupTitle])) {
|
2013-01-03 16:27:55 +00:00
|
|
|
// Regenerate and rerender the widget and replace the existing DOM node
|
|
|
|
this.generate();
|
|
|
|
var oldDomNode = this.renderer.domNode,
|
|
|
|
newDomNode = this.renderer.renderInDom();
|
|
|
|
oldDomNode.parentNode.replaceChild(newDomNode,oldDomNode);
|
2012-12-13 21:34:31 +00:00
|
|
|
} else {
|
|
|
|
// We don't need to refresh ourselves, so just refresh any child nodes
|
|
|
|
$tw.utils.each(this.children,function(node) {
|
|
|
|
if(node.refreshInDom) {
|
|
|
|
node.refreshInDom(changedTiddlers);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-01 17:51:02 +00:00
|
|
|
exports.button = ButtonWidget;
|
|
|
|
|
2012-12-13 21:34:31 +00:00
|
|
|
})();
|