1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 21:39:57 +00:00
TiddlyWiki5/core/modules/macros/button.js

105 lines
2.9 KiB
JavaScript
Raw Normal View History

/*\
title: $:/core/modules/macros/button.js
type: application/javascript
module-type: macro
Button macro
\*/
(function(){
/*jslint node: true, browser: true */
2012-05-04 17:49:04 +00:00
/*global $tw: false */
"use strict";
exports.info = {
name: "button",
params: {
message: {byName: "default", type: "text"},
param: {byName: true, type: "text"},
set: {byName: true, type: "tiddler"},
setTo: {byName: true, type: "text"},
popup: {byName: true, type: "tiddler"},
qualifyTiddlerTitles: {byName: true, type: "text"},
"class": {byName: true, type: "text"}
}
};
exports.dispatchMessage = function(event) {
var buttonEvent = document.createEvent("Event");
buttonEvent.initEvent("tw-" + this.params.message,true,true);
buttonEvent.param = this.params.param;
buttonEvent.tiddlerTitle = this.tiddlerTitle;
event.target.dispatchEvent(buttonEvent);
};
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;
if(this.hasParameter("qualifyTiddlerTitles") && this.params.qualifyTiddlerTitles === "yes") {
2012-06-19 15:47:35 +00:00
textRef = "(" + this.parents.join(",") + "," + this.tiddlerTitle + ")" + textRef;
}
// Check for cancelling
if(cancel) {
$tw.popup.cancel();
} else {
2012-06-19 15:47:35 +00:00
// Get the current popup state tiddler
var value = this.wiki.getTextReference(textRef,"",this.tiddlerTitle);
// Check if the popup is open by checking whether it matches "(<x>,<y>)"
var popupLocationRegExp = /^\((-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+),(-?[0-9\.E]+)\)$/;
if(popupLocationRegExp.test(value)) {
$tw.popup.cancel();
} 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.popup.popup(textRef);
}
}
};
exports.setTiddler = function() {
var set = this.params.set,
setTo = this.params.setTo,
tiddler = this.wiki.getTiddler(set);
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: set, text: setTo}));
};
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);
}
if(this.hasParameter("set") && this.hasParameter("setTo")) {
this.setTiddler();
}
2012-06-19 15:47:35 +00:00
event.preventDefault();
return false;
}
return true;
};
exports.executeMacro = function() {
2012-07-15 16:35:17 +00:00
var attributes = {"class": []};
if(this.hasParameter("class")) {
$tw.utils.pushTop(attributes["class"],this.params["class"].split(" "));
}
if(this.classes) {
$tw.utils.pushTop(attributes["class"],this.classes);
}
for(var t=0; t<this.content.length; t++) {
this.content[t].execute(this.parents,this.tiddlerTitle);
}
return $tw.Tree.Element("button",attributes,this.content,{
2012-06-19 15:47:35 +00:00
events: ["click"],
eventHandler: this
});
};
})();