1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-26 18:10:27 +00:00
TiddlyWiki5/core/modules/macros/button.js

101 lines
2.5 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"},
hover: {byName: true, type: "text"},
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);
};
2012-10-16 22:06:37 +00:00
exports.triggerPopup = function(event) {
$tw.popup.triggerPopup({
textRef: this.params.popup,
domNode: this.child.domNode,
qualifyTiddlerTitles: this.params.qualifyTiddlerTitles,
contextTiddlerTitle: this.tiddlerTitle,
contextParents: this.parents,
wiki: this.wiki
});
};
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;
}
if(event.type === "mouseover" || event.type === "mouseout") {
if(this.hasParameter("popup")) {
this.triggerPopup(event);
}
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);
}
var events = ["click"];
if(this.hasParameter("hover") && this.params.hover === "yes") {
events.push("mouseover","mouseout");
}
return $tw.Tree.Element("button",attributes,this.content,{
events: events,
eventHandler: this
});
};
})();