2014-05-31 17:37:43 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/select.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Select widget:
|
|
|
|
|
|
|
|
```
|
|
|
|
<$select tiddler="MyTiddler" field="text">
|
|
|
|
<$list filter="[tag[chapter]]">
|
|
|
|
<option value=<<currentTiddler>>>
|
|
|
|
<$view field="description"/>
|
|
|
|
</option>
|
|
|
|
</$list>
|
|
|
|
</$select>
|
|
|
|
```
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var SelectWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
SelectWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
SelectWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
|
|
|
this.renderChildren(parent,nextSibling);
|
|
|
|
this.setSelectValue();
|
2022-12-03 08:22:21 +00:00
|
|
|
if(this.selectFocus == "yes") {
|
|
|
|
this.getSelectDomNode().focus();
|
|
|
|
}
|
2014-05-31 17:37:43 +00:00
|
|
|
$tw.utils.addEventListeners(this.getSelectDomNode(),[
|
|
|
|
{name: "change", handlerObject: this, handlerMethod: "handleChangeEvent"}
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Handle a change event
|
|
|
|
*/
|
|
|
|
SelectWidget.prototype.handleChangeEvent = function(event) {
|
2016-04-29 17:53:23 +00:00
|
|
|
// Get the new value and assign it to the tiddler
|
2015-11-13 12:50:34 +00:00
|
|
|
if(this.selectMultiple == false) {
|
|
|
|
var value = this.getSelectDomNode().value;
|
|
|
|
} else {
|
2015-11-13 13:04:13 +00:00
|
|
|
var value = this.getSelectValues()
|
2015-11-13 13:06:51 +00:00
|
|
|
value = $tw.utils.stringifyList(value);
|
2015-11-13 12:50:34 +00:00
|
|
|
}
|
2014-05-31 17:37:43 +00:00
|
|
|
this.wiki.setText(this.selectTitle,this.selectField,this.selectIndex,value);
|
2016-04-29 17:53:23 +00:00
|
|
|
// Trigger actions
|
|
|
|
if(this.selectActions) {
|
|
|
|
this.invokeActionString(this.selectActions,this,event);
|
|
|
|
}
|
2014-05-31 17:37:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
If necessary, set the value of the select element to the current value
|
|
|
|
*/
|
|
|
|
SelectWidget.prototype.setSelectValue = function() {
|
|
|
|
var value = this.selectDefault;
|
|
|
|
// Get the value
|
|
|
|
if(this.selectIndex) {
|
2018-01-23 16:50:03 +00:00
|
|
|
value = this.wiki.extractTiddlerDataItem(this.selectTitle,this.selectIndex,value);
|
2014-05-31 17:37:43 +00:00
|
|
|
} else {
|
|
|
|
var tiddler = this.wiki.getTiddler(this.selectTitle);
|
|
|
|
if(tiddler) {
|
|
|
|
if(this.selectField === "text") {
|
|
|
|
// Calling getTiddlerText() triggers lazy loading of skinny tiddlers
|
|
|
|
value = this.wiki.getTiddlerText(this.selectTitle);
|
|
|
|
} else {
|
|
|
|
if($tw.utils.hop(tiddler.fields,this.selectField)) {
|
|
|
|
value = tiddler.getFieldString(this.selectField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(this.selectField === "title") {
|
|
|
|
value = this.selectTitle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Assign it to the select element if it's different than the current value
|
2015-11-13 12:50:34 +00:00
|
|
|
if (this.selectMultiple) {
|
|
|
|
value = value === undefined ? "" : value;
|
|
|
|
var select = this.getSelectDomNode();
|
2015-11-13 13:04:13 +00:00
|
|
|
var values = Array.isArray(value) ? value : $tw.utils.parseStringArray(value);
|
2015-11-13 12:50:34 +00:00
|
|
|
for(var i=0; i < select.children.length; i++){
|
2019-10-11 16:46:30 +00:00
|
|
|
select.children[i].selected = values.indexOf(select.children[i].value) !== -1
|
2015-11-13 12:50:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var domNode = this.getSelectDomNode();
|
|
|
|
if(domNode.value !== value) {
|
|
|
|
domNode.value = value;
|
|
|
|
}
|
2014-05-31 17:37:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get the DOM node of the select element
|
|
|
|
*/
|
|
|
|
SelectWidget.prototype.getSelectDomNode = function() {
|
|
|
|
return this.children[0].domNodes[0];
|
|
|
|
};
|
|
|
|
|
2015-11-13 12:50:34 +00:00
|
|
|
// Return an array of the selected opion values
|
|
|
|
// select is an HTML select element
|
|
|
|
SelectWidget.prototype.getSelectValues = function() {
|
2015-11-13 16:30:06 +00:00
|
|
|
var select, result, options, opt;
|
|
|
|
select = this.getSelectDomNode();
|
|
|
|
result = [];
|
|
|
|
options = select && select.options;
|
|
|
|
for (var i=0; i<options.length; i++) {
|
2015-11-13 12:50:34 +00:00
|
|
|
opt = options[i];
|
|
|
|
if (opt.selected) {
|
|
|
|
result.push(opt.value || opt.text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-05-31 17:37:43 +00:00
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
SelectWidget.prototype.execute = function() {
|
|
|
|
// Get our parameters
|
2016-04-29 17:53:23 +00:00
|
|
|
this.selectActions = this.getAttribute("actions");
|
2014-05-31 17:37:43 +00:00
|
|
|
this.selectTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler"));
|
|
|
|
this.selectField = this.getAttribute("field","text");
|
|
|
|
this.selectIndex = this.getAttribute("index");
|
|
|
|
this.selectClass = this.getAttribute("class");
|
|
|
|
this.selectDefault = this.getAttribute("default");
|
2015-11-13 12:50:34 +00:00
|
|
|
this.selectMultiple = this.getAttribute("multiple", false);
|
2016-01-05 18:48:21 +00:00
|
|
|
this.selectSize = this.getAttribute("size");
|
2023-07-09 15:52:35 +00:00
|
|
|
this.selectTabindex = this.getAttribute("tabindex");
|
2019-03-04 12:24:16 +00:00
|
|
|
this.selectTooltip = this.getAttribute("tooltip");
|
2022-12-03 08:22:21 +00:00
|
|
|
this.selectFocus = this.getAttribute("focus");
|
2014-05-31 17:37:43 +00:00
|
|
|
// Make the child widgets
|
|
|
|
var selectNode = {
|
|
|
|
type: "element",
|
|
|
|
tag: "select",
|
|
|
|
children: this.parseTreeNode.children
|
|
|
|
};
|
|
|
|
if(this.selectClass) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(selectNode,"class",this.selectClass);
|
|
|
|
}
|
2015-11-13 12:50:34 +00:00
|
|
|
if(this.selectMultiple) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(selectNode,"multiple","multiple");
|
|
|
|
}
|
2016-01-05 18:48:21 +00:00
|
|
|
if(this.selectSize) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(selectNode,"size",this.selectSize);
|
|
|
|
}
|
2023-07-09 15:52:35 +00:00
|
|
|
if(this.selectTabindex) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(selectNode,"tabindex",this.selectTabindex);
|
|
|
|
}
|
2019-03-04 12:24:16 +00:00
|
|
|
if(this.selectTooltip) {
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(selectNode,"title",this.selectTooltip);
|
|
|
|
}
|
2014-05-31 17:37:43 +00:00
|
|
|
this.makeChildWidgets([selectNode]);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
SelectWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
|
|
|
// If we're using a different tiddler/field/index then completely refresh ourselves
|
2021-04-25 15:05:02 +00:00
|
|
|
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.tooltip) {
|
2014-05-31 17:37:43 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
|
|
|
// If the target tiddler value has changed, just update setting and refresh the children
|
|
|
|
} else {
|
2022-10-15 11:26:21 +00:00
|
|
|
if(changedAttributes.class) {
|
|
|
|
this.selectClass = this.getAttribute("class");
|
|
|
|
this.getSelectDomNode().setAttribute("class",this.selectClass);
|
|
|
|
}
|
|
|
|
|
2015-05-09 21:40:03 +00:00
|
|
|
var childrenRefreshed = this.refreshChildren(changedTiddlers);
|
|
|
|
if(changedTiddlers[this.selectTitle] || childrenRefreshed) {
|
2014-05-31 17:37:43 +00:00
|
|
|
this.setSelectValue();
|
|
|
|
}
|
2015-05-09 21:40:03 +00:00
|
|
|
return childrenRefreshed;
|
2014-05-31 17:37:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.select = SelectWidget;
|
|
|
|
|
2015-11-13 13:06:51 +00:00
|
|
|
})();
|