1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-15 06:14:53 +00:00
TiddlyWiki5/core/modules/widgets/radio.js

124 lines
3.6 KiB
JavaScript
Raw Normal View History

/*\
2013-12-12 19:49:43 +00:00
title: $:/core/modules/widgets/radio.js
type: application/javascript
module-type: widget
Set a field or index at a given tiddler via radio buttons
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var Widget = require("$:/core/modules/widgets/widget.js").widget;
2013-12-12 19:49:43 +00:00
var RadioWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
/*
Inherit from the base widget class
*/
2013-12-12 19:49:43 +00:00
RadioWidget.prototype = new Widget();
/*
Render this widget into the DOM
*/
2013-12-12 19:49:43 +00:00
RadioWidget.prototype.render = function(parent,nextSibling) {
// Save the parent dom node
this.parentDomNode = parent;
// Compute our attributes
this.computeAttributes();
// Execute our logic
this.execute();
// Create our elements
this.labelDomNode = this.document.createElement("label");
2013-12-12 19:49:43 +00:00
this.labelDomNode.setAttribute("class",this.radioClass);
this.inputDomNode = this.document.createElement("input");
this.inputDomNode.setAttribute("type","radio");
2013-12-12 19:49:43 +00:00
if(this.getValue() == this.radioValue) {
this.inputDomNode.setAttribute("checked","true");
}
this.labelDomNode.appendChild(this.inputDomNode);
this.spanDomNode = this.document.createElement("span");
this.labelDomNode.appendChild(this.spanDomNode);
// Add a click event handler
$tw.utils.addEventListeners(this.inputDomNode,[
{name: "change", handlerObject: this, handlerMethod: "handleChangeEvent"}
]);
// Insert the label into the DOM and render any children
parent.insertBefore(this.labelDomNode,nextSibling);
this.renderChildren(this.spanDomNode,null);
this.domNodes.push(this.labelDomNode);
};
2013-12-12 19:49:43 +00:00
RadioWidget.prototype.getValue = function() {
var value,
tiddler = this.wiki.getTiddler(this.radioTitle);
if (this.radioIndex) {
value = this.wiki.extractTiddlerDataItem(this.radioTitle,this.radioIndex);
} else {
value = tiddler && tiddler.getFieldString(this.radioField);
}
return value;
};
2013-12-12 19:49:43 +00:00
RadioWidget.prototype.setValue = function() {
if(this.radioIndex) {
this.wiki.setText(this.radioTitle,"",this.radioIndex,this.radioValue);
} else {
2013-12-12 20:36:54 +00:00
var tiddler = this.wiki.getTiddler(this.radioTitle),
addition = {};
addition[this.radioField] = this.radioValue;
this.wiki.addTiddler(new $tw.Tiddler(this.wiki.getCreationFields(),{title: this.radioTitle},tiddler,addition,this.wiki.getModificationFields()));
2013-12-12 19:39:02 +00:00
}
};
2013-12-12 19:49:43 +00:00
RadioWidget.prototype.handleChangeEvent = function(event) {
if(this.inputDomNode.checked) {
2013-12-12 19:39:02 +00:00
this.setValue();
}
};
/*
Compute the internal state of the widget
*/
2013-12-12 19:49:43 +00:00
RadioWidget.prototype.execute = function() {
// Get the parameters from the attributes
2013-12-12 19:49:43 +00:00
this.radioTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
this.radioField = this.getAttribute("field","text");
this.radioIndex = this.getAttribute("index");
2013-12-12 19:49:43 +00:00
this.radioValue = this.getAttribute("value");
this.radioClass = this.getAttribute("class","");
if(this.radioClass !== "") {
2013-12-12 19:49:43 +00:00
this.radioClass += " ";
2013-12-12 19:39:02 +00:00
}
this.radioClass += "tc-radio";
// Make the child widgets
this.makeChildWidgets();
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
2013-12-12 19:49:43 +00:00
RadioWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.value || changedAttributes["class"]) {
this.refreshSelf();
return true;
} else {
var refreshed = false;
2013-12-12 19:49:43 +00:00
if(changedTiddlers[this.radioTitle]) {
this.inputDomNode.checked = this.getValue() === this.radioValue;
refreshed = true;
}
return this.refreshChildren(changedTiddlers) || refreshed;
}
};
2013-12-12 19:49:43 +00:00
exports.radio = RadioWidget;
})();