mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-09 03:19:56 +00:00
707024a118
We no longer need the old setstyle widget
160 lines
4.4 KiB
JavaScript
160 lines
4.4 KiB
JavaScript
/*\
|
|
title: $:/core/modules/new_widgets/button.js
|
|
type: application/javascript
|
|
module-type: new_widget
|
|
|
|
Button widget
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
var Widget = require("$:/core/modules/new_widgets/widget.js").widget;
|
|
|
|
var ButtonWidget = function(parseTreeNode,options) {
|
|
this.initialise(parseTreeNode,options);
|
|
};
|
|
|
|
/*
|
|
Inherit from the base widget class
|
|
*/
|
|
ButtonWidget.prototype = new Widget();
|
|
|
|
/*
|
|
Render this widget into the DOM
|
|
*/
|
|
ButtonWidget.prototype.render = function(parent,nextSibling) {
|
|
var self = this;
|
|
// Remember parent
|
|
this.parentDomNode = parent;
|
|
// Compute attributes and execute state
|
|
this.computeAttributes();
|
|
this.execute();
|
|
// Create element
|
|
var domNode = this.document.createElement("button");
|
|
// Assign classes
|
|
var classes = this["class"].split(" ") || [];
|
|
if(this.selectedClass) {
|
|
if(this.set && this.setTo && this.isSelected()) {
|
|
$tw.utils.pushTop(classes,this.selectedClass.split(" "));
|
|
}
|
|
if(this.popup && this.isPoppedUp()) {
|
|
$tw.utils.pushTop(classes,this.selectedClass.split(" "));
|
|
}
|
|
}
|
|
domNode.className = classes.join(" ");
|
|
// Assign classes
|
|
domNode.setAttribute("style",this.style);
|
|
// Add a click event handler
|
|
domNode.addEventListener("click",function (event) {
|
|
var handled = false;
|
|
if(self.message) {
|
|
self.dispatchMessage(event);
|
|
handled = true;
|
|
}
|
|
if(self.popup) {
|
|
self.triggerPopup(event);
|
|
handled = true;
|
|
}
|
|
if(self.set) {
|
|
self.setTiddler();
|
|
handled = true;
|
|
}
|
|
if(handled) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
return handled;
|
|
},false);
|
|
// Insert element
|
|
parent.insertBefore(domNode,nextSibling);
|
|
this.renderChildren(domNode,null);
|
|
this.domNodes.push(domNode);
|
|
};
|
|
|
|
ButtonWidget.prototype.isSelected = function() {
|
|
var tiddler = this.wiki.getTiddler(this.set);
|
|
return tiddler ? tiddler.fields.text === this.setTo : false;
|
|
};
|
|
|
|
ButtonWidget.prototype.isPoppedUp = function() {
|
|
var tiddler = this.wiki.getTiddler(this.popup);
|
|
var result = tiddler && tiddler.fields.text ? $tw.popup.readPopupState(this.popup,tiddler.fields.text) : false;
|
|
return result;
|
|
};
|
|
|
|
ButtonWidget.prototype.dispatchMessage = function(event) {
|
|
this.dispatchEvent({type: this.message, param: this.param, tiddlerTitle: this.getVariable("tiddlerTitle")});
|
|
};
|
|
|
|
ButtonWidget.prototype.triggerPopup = function(event) {
|
|
$tw.popup.triggerPopup({
|
|
domNode: this.domNodes[0],
|
|
title: this.popup,
|
|
wiki: this.wiki
|
|
});
|
|
};
|
|
|
|
ButtonWidget.prototype.setTiddler = function() {
|
|
var tiddler = this.wiki.getTiddler(this.set);
|
|
this.wiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.set, text: this.setTo}));
|
|
};
|
|
|
|
/*
|
|
Compute the internal state of the widget
|
|
*/
|
|
ButtonWidget.prototype.execute = function() {
|
|
// Get attributes
|
|
this.message = this.getAttribute("message");
|
|
this.param = this.getAttribute("param");
|
|
this.set = this.getAttribute("set");
|
|
this.setTo = this.getAttribute("setTo");
|
|
this.popup = this.getAttribute("popup");
|
|
this.hover = this.getAttribute("hover");
|
|
this.qualifyTiddlerTitles = this.getAttribute("qualifyTiddlerTitles");
|
|
this["class"] = this.getAttribute("class","");
|
|
this.style = this.getAttribute("style");
|
|
this.selectedClass = this.getAttribute("selectedClass");
|
|
// Qualify tiddler titles if required
|
|
if(this.qualifyTiddlerTitles) {
|
|
var qualifier = this.getStateQualifier();
|
|
if(this.set) {
|
|
this.set = this.set + "-" + qualifier;
|
|
}
|
|
if(this.popup) {
|
|
this.popup = this.popup + "-" + qualifier;
|
|
}
|
|
}
|
|
// Make child widgets
|
|
this.makeChildWidgets();
|
|
};
|
|
|
|
/*
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
*/
|
|
ButtonWidget.prototype.refresh = function(changedTiddlers) {
|
|
var changedAttributes = this.computeAttributes();
|
|
if(changedAttributes.message || changedAttributes.param || changedAttributes.set || changedAttributes.setTo || changedAttributes.popup || changedAttributes.hover || changedAttributes.qualifyTiddlerTitles || changedAttributes["class"] || changedAttributes.selectedClass || changedAttributes.style || (this.set && changedTiddlers[this.set]) || (this.popup && changedTiddlers[this.popup])) {
|
|
this.refreshSelf();
|
|
return true;
|
|
}
|
|
return this.refreshChildren(changedTiddlers);
|
|
};
|
|
|
|
/*
|
|
Remove any DOM nodes created by this widget or its children
|
|
*/
|
|
ButtonWidget.prototype.removeChildDomNodes = function() {
|
|
$tw.utils.each(this.domNodes,function(domNode) {
|
|
domNode.parentNode.removeChild(domNode);
|
|
});
|
|
this.domNodes = [];
|
|
};
|
|
|
|
exports.button = ButtonWidget;
|
|
|
|
})();
|