2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/macros/button.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: macro
|
|
|
|
|
2012-05-19 17:23:14 +00:00
|
|
|
Button macro
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
2012-05-04 17:49:04 +00:00
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: "button",
|
|
|
|
params: {
|
2012-06-13 08:11:50 +00:00
|
|
|
message: {byName: "default", type: "text"},
|
2012-06-13 12:19:55 +00:00
|
|
|
popup: {byName: true, type: "tiddler"},
|
2012-06-13 08:11:50 +00:00
|
|
|
qualifyTiddlerTitles: {byName: true, type: "text"},
|
2012-04-30 11:23:03 +00:00
|
|
|
"class": {byName: true, type: "text"}
|
2012-06-14 16:15:38 +00:00
|
|
|
}
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
2012-06-13 12:19:55 +00:00
|
|
|
exports.dispatchMessage = function(event) {
|
|
|
|
var buttonEvent = document.createEvent("Event");
|
|
|
|
buttonEvent.initEvent("tw-" + this.params.message,true,true);
|
|
|
|
buttonEvent.tiddlerTitle = this.tiddlerTitle;
|
|
|
|
event.target.dispatchEvent(buttonEvent);
|
|
|
|
};
|
|
|
|
|
2012-06-13 14:37:07 +00:00
|
|
|
exports.triggerPopup = function(event,cancel) {
|
2012-06-19 15:47:35 +00:00
|
|
|
// Get the textref of the popup state tiddler
|
|
|
|
var textRef = this.params.popup;
|
2012-06-13 12:19:55 +00:00
|
|
|
if(this.hasParameter("qualifyTiddlerTitles") && this.params.qualifyTiddlerTitles === "yes") {
|
2012-06-19 15:47:35 +00:00
|
|
|
textRef = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + textRef;
|
2012-06-13 12:19:55 +00:00
|
|
|
}
|
2012-06-13 14:37:07 +00:00
|
|
|
// Check for cancelling
|
|
|
|
if(cancel) {
|
2012-06-19 15:50:10 +00:00
|
|
|
$tw.popupper.cancel();
|
2012-06-13 12:19:55 +00:00
|
|
|
} else {
|
2012-06-19 15:47:35 +00:00
|
|
|
// Get the current popup state tiddler
|
|
|
|
var value = this.wiki.getTextReference(textRef,"",this.tiddlerTitle);
|
2012-06-13 14:37:07 +00:00
|
|
|
// Check if the popup is open by checking whether it matches "(<x>,<y>)"
|
2012-06-14 16:15:38 +00:00
|
|
|
var popupLocationRegExp = /^\((-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+)\)$/;
|
2012-06-13 14:37:07 +00:00
|
|
|
if(popupLocationRegExp.test(value)) {
|
2012-06-19 15:50:10 +00:00
|
|
|
$tw.popupper.cancel();
|
2012-06-13 14:37:07 +00:00
|
|
|
} else {
|
|
|
|
// Set the position if we're opening it
|
2012-06-19 15:47:35 +00:00
|
|
|
this.wiki.setTextReference(textRef,
|
|
|
|
"(" + this.child.domNode.offsetLeft + "," + this.child.domNode.offsetTop + "," +
|
|
|
|
this.child.domNode.offsetWidth + "," + this.child.domNode.offsetHeight + ")",
|
|
|
|
this.tiddlerTitle,true);
|
|
|
|
$tw.popupper.popup(textRef);
|
2012-06-13 14:37:07 +00:00
|
|
|
}
|
2012-06-13 12:19:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
exports.handleEvent = function(event) {
|
2012-06-19 15:47:35 +00:00
|
|
|
if(event.type === "click") {
|
|
|
|
if(this.hasParameter("message")) {
|
|
|
|
this.dispatchMessage(event);
|
|
|
|
}
|
|
|
|
if(this.hasParameter("popup")) {
|
|
|
|
this.triggerPopup(event);
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.executeMacro = function() {
|
2012-06-13 14:37:07 +00:00
|
|
|
var attributes = {"class": ["tw-popup-controller"]};
|
2012-04-30 11:23:03 +00:00
|
|
|
if(this.hasParameter("class")) {
|
2012-06-13 14:37:07 +00:00
|
|
|
$tw.utils.pushTop(attributes["class"],this.params["class"].split(" "));
|
2012-04-30 11:23:03 +00:00
|
|
|
}
|
2012-06-09 17:36:32 +00:00
|
|
|
if(this.classes) {
|
|
|
|
$tw.utils.pushTop(attributes["class"],this.classes);
|
|
|
|
}
|
2012-06-10 16:26:54 +00:00
|
|
|
for(var t=0; t<this.content.length; t++) {
|
|
|
|
this.content[t].execute(this.parents,this.tiddlerTitle);
|
|
|
|
}
|
2012-06-14 16:15:38 +00:00
|
|
|
return $tw.Tree.Element("button",attributes,this.content,{
|
2012-06-19 15:47:35 +00:00
|
|
|
events: ["click"],
|
2012-06-14 16:15:38 +00:00
|
|
|
eventHandler: this
|
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|