2013-10-23 18:38:36 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/checkbox.js
|
2013-10-23 18:38:36 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-10-23 18:38:36 +00:00
|
|
|
|
|
|
|
Checkbox widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
2013-10-23 18:38:36 +00:00
|
|
|
|
|
|
|
var CheckboxWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
CheckboxWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
CheckboxWidget.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");
|
|
|
|
this.inputDomNode = this.document.createElement("input");
|
|
|
|
this.inputDomNode.setAttribute("type","checkbox");
|
|
|
|
if(this.getValue()) {
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckboxWidget.prototype.getValue = function() {
|
|
|
|
var tiddler = this.wiki.getTiddler(this.checkboxTitle);
|
2014-07-12 08:09:13 +00:00
|
|
|
if(tiddler) {
|
|
|
|
if(this.checkboxTag) {
|
|
|
|
return tiddler.hasTag(this.checkboxTag);
|
|
|
|
}
|
|
|
|
if(this.checkboxField) {
|
|
|
|
var value = tiddler.fields[this.checkboxField] || this.checkboxDefault || "";
|
|
|
|
if(value === this.checkboxChecked) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(value === this.checkboxUnchecked) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-24 14:43:37 +00:00
|
|
|
} else {
|
|
|
|
if(this.checkboxTag) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(this.checkboxField) {
|
|
|
|
if(this.checkboxDefault === this.checkboxChecked) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(this.checkboxDefault === this.checkboxUnchecked) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-12 08:09:13 +00:00
|
|
|
}
|
|
|
|
return false;
|
2013-10-23 18:38:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckboxWidget.prototype.handleChangeEvent = function(event) {
|
|
|
|
var checked = this.inputDomNode.checked,
|
2014-07-12 08:09:13 +00:00
|
|
|
tiddler = this.wiki.getTiddler(this.checkboxTitle),
|
2014-07-30 10:52:45 +00:00
|
|
|
fallbackFields = {text: ""},
|
|
|
|
newFields = {title: this.checkboxTitle},
|
2014-07-12 08:09:13 +00:00
|
|
|
hasChanged = false;
|
2014-07-24 14:43:37 +00:00
|
|
|
// Set the tag if specified
|
|
|
|
if(this.checkboxTag && (!tiddler || tiddler.hasTag(this.checkboxTag) !== checked)) {
|
|
|
|
newFields.tags = tiddler ? (tiddler.fields.tags || []).slice(0) : [];
|
|
|
|
var pos = newFields.tags.indexOf(this.checkboxTag);
|
|
|
|
if(pos !== -1) {
|
|
|
|
newFields.tags.splice(pos,1);
|
2014-07-12 08:09:13 +00:00
|
|
|
}
|
2014-07-24 14:43:37 +00:00
|
|
|
if(checked) {
|
|
|
|
newFields.tags.push(this.checkboxTag);
|
2013-10-23 18:38:36 +00:00
|
|
|
}
|
2014-07-24 14:43:37 +00:00
|
|
|
hasChanged = true;
|
|
|
|
}
|
|
|
|
// Set the field if specified
|
|
|
|
if(this.checkboxField) {
|
|
|
|
var value = checked ? this.checkboxChecked : this.checkboxUnchecked;
|
|
|
|
if(!tiddler || tiddler.fields[this.checkboxField] !== value) {
|
|
|
|
newFields[this.checkboxField] = value;
|
|
|
|
hasChanged = true;
|
2013-10-23 18:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-24 14:43:37 +00:00
|
|
|
if(hasChanged) {
|
2014-07-30 10:52:45 +00:00
|
|
|
this.wiki.addTiddler(new $tw.Tiddler(fallbackFields,tiddler,newFields,this.wiki.getModificationFields()));
|
2014-07-24 14:43:37 +00:00
|
|
|
}
|
2013-10-23 18:38:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
CheckboxWidget.prototype.execute = function() {
|
|
|
|
// Get the parameters from the attributes
|
2013-10-30 13:36:44 +00:00
|
|
|
this.checkboxTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
2013-10-23 18:38:36 +00:00
|
|
|
this.checkboxTag = this.getAttribute("tag");
|
2014-07-12 08:09:13 +00:00
|
|
|
this.checkboxField = this.getAttribute("field");
|
|
|
|
this.checkboxChecked = this.getAttribute("checked");
|
|
|
|
this.checkboxUnchecked = this.getAttribute("unchecked");
|
|
|
|
this.checkboxDefault = this.getAttribute("default");
|
2013-10-23 18:38:36 +00:00
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
CheckboxWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2014-07-12 08:09:13 +00:00
|
|
|
if(changedAttributes.tiddler || changedAttributes.tag || changedAttributes.field || changedAttributes.checked || changedAttributes.unchecked || changedAttributes["default"] || changedAttributes["class"]) {
|
2013-10-23 18:38:36 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
2013-10-23 18:46:31 +00:00
|
|
|
} else {
|
2013-11-01 17:22:40 +00:00
|
|
|
var refreshed = false;
|
2013-10-23 18:46:31 +00:00
|
|
|
if(changedTiddlers[this.checkboxTitle]) {
|
|
|
|
this.inputDomNode.checked = this.getValue();
|
2013-11-01 17:22:40 +00:00
|
|
|
refreshed = true;
|
2013-10-23 18:46:31 +00:00
|
|
|
}
|
2013-11-01 17:22:40 +00:00
|
|
|
return this.refreshChildren(changedTiddlers) || refreshed;
|
2013-10-23 18:38:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.checkbox = CheckboxWidget;
|
|
|
|
|
|
|
|
})();
|