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-13 12:19:55 +00:00
|
|
|
// Get the title of the popup state tiddler
|
|
|
|
var title = this.params.popup;
|
|
|
|
if(this.hasParameter("qualifyTiddlerTitles") && this.params.qualifyTiddlerTitles === "yes") {
|
|
|
|
title = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + title;
|
|
|
|
}
|
|
|
|
// Get the popup state tiddler and the the text value
|
|
|
|
var tiddler = this.wiki.getTiddler(title),
|
|
|
|
value = tiddler ? tiddler.fields.text : "";
|
2012-06-13 14:37:07 +00:00
|
|
|
// Check for cancelling
|
|
|
|
if(cancel) {
|
2012-06-13 12:19:55 +00:00
|
|
|
value = "";
|
|
|
|
} else {
|
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)) {
|
|
|
|
value = "";
|
|
|
|
} else {
|
|
|
|
// Set the position if we're opening it
|
2012-06-14 16:15:38 +00:00
|
|
|
value = "(" + this.child.domNode.offsetLeft + "," + this.child.domNode.offsetTop + "," + this.child.domNode.offsetWidth + "," + this.child.domNode.offsetHeight + ")";
|
2012-06-13 14:37:07 +00:00
|
|
|
}
|
2012-06-13 12:19:55 +00:00
|
|
|
}
|
|
|
|
// Update the state tiddler
|
|
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: title, text: value}),true);
|
|
|
|
};
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
exports.handleEvent = function(event) {
|
2012-06-13 14:37:07 +00:00
|
|
|
switch(event.type) {
|
|
|
|
case "click":
|
|
|
|
if(this.hasParameter("message")) {
|
|
|
|
this.dispatchMessage(event);
|
|
|
|
}
|
|
|
|
if(this.hasParameter("popup")) {
|
|
|
|
this.triggerPopup(event);
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
case "tw-cancel-popup":
|
2012-06-14 16:15:38 +00:00
|
|
|
if(this.hasParameter("popup") && this.child.domNode !== event.targetOfCancel && !$tw.utils.domContains(this.child.domNode,event.targetOfCancel)) {
|
2012-06-13 14:37:07 +00:00
|
|
|
this.triggerPopup(event,true);
|
|
|
|
}
|
|
|
|
break;
|
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,{
|
|
|
|
events: ["click","tw-cancel-popup"],
|
|
|
|
eventHandler: this
|
|
|
|
});
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|