Commit 133b8ed7 authored by Tomáš Peterka's avatar Tomáš Peterka Committed by Tomáš Peterka

[renderjs_ui] Allow values which resolves to FALSE to be the default values in *list* fields

parent 32de851f
......@@ -3,6 +3,27 @@
(function (window, rJS) {
"use strict";
function isEmpty(value) {
return (value === undefined ||
value === null ||
value.length === 0);
}
/** More robust way of writing a || b || "" because if b===0 it gets skipped.
*/
function getNonEmpty() {
var i;
for (i = 0; i < arguments.length; i++) {
if (!isEmpty(arguments[i])) {
return arguments[i];
}
}
if (arguments.length === 1) {
return arguments[0];
}
return arguments[arguments.length - 1];
}
rJS(window)
.setState({
tag: 'p'
......@@ -11,7 +32,7 @@
.declareMethod('render', function (options) {
var field_json = options.field_json || {},
state_dict = {
value: field_json.value || field_json.default || "",
value: getNonEmpty(field_json.value, field_json['default'], ""),
item_list: JSON.stringify(field_json.items),
editable: field_json.editable,
required: field_json.required,
......@@ -23,8 +44,8 @@
// as user may have modified the input value
render_timestamp: new Date().getTime()
};
if ((!state_dict.value) && (state_dict.first_item)) {
// first_item means to select the first item by default on empty value
if (isEmpty(state_dict.value) && (state_dict.first_item)) {
state_dict.value = field_json.items[0][1];
}
return this.changeState(state_dict);
......
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>965.1659.33258.11707</string> </value>
<value> <string>965.10402.43703.62668</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1516786204.34</float>
<float>1517343520.55</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -3,13 +3,34 @@
(function (window, rJS, document, RSVP) {
'use strict';
function isEmpty(value) {
return (value === undefined ||
value === null ||
value.length === 0);
}
/* Make sure that returned object is an Array instance. */
function ensureArray(obj) {
if (!obj) {return []; }
if (Array.isArray(obj)) {return obj; }
if (isEmpty(obj)) {return []; }
return [obj];
}
/** More robust way of writing a || b || "" because if b===0 it gets skipped.
*/
function getNonEmpty() {
var i;
for (i = 0; i < arguments.length; i++) {
if (!isEmpty(arguments[i])) {
return arguments[i];
}
}
if (arguments.length === 1) {
return arguments[0];
}
return arguments[arguments.length - 1];
}
function appendListField(gadget, value, item_list) {
var div = document.createElement('div');
gadget.element.appendChild(div);
......@@ -37,10 +58,11 @@
rJS(window)
.declareMethod('render', function (options) {
var field_json = options.field_json || {},
item_list = field_json.items,
item_list = ensureArray(field_json.items),
state_dict = {
value_list: JSON.stringify(
ensureArray(field_json.value || field_json.default)
ensureArray(
getNonEmpty(field_json.value, field_json['default'], []))
),
editable: field_json.editable,
required: field_json.required,
......@@ -54,6 +76,11 @@
// as user may have modified the input value
render_timestamp: new Date().getTime()
};
// Items can be simply an array of values. It is a valid input produced
// usually by TALES expression
if (item_list.length > 0 && !Array.isArray(item_list[0])) {
item_list = item_list.map(function (item) {return [item, item]; });
}
if ((item_list.length === 0) || (item_list[0][0] !== "")) {
item_list.unshift(["", ""]);
}
......@@ -77,17 +104,13 @@
element.removeChild(element.firstChild);
}
function enQueue() {
var argument_list = arguments;
value_list.forEach(function (value) {
queue
.push(function () {
return appendListField.apply(this, argument_list);
return appendListField(gadget, value, item_list);
});
}
});
for (i = 0; i < value_list.length; i += 1) {
enQueue(gadget, value_list[i], item_list);
}
return queue;
})
......@@ -199,4 +222,4 @@
return true;
}, {mutex: 'changestate'});
}(window, rJS, document, RSVP));
\ No newline at end of file
}(window, rJS, document, RSVP));
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>965.241.13759.3805</string> </value>
<value> <string>965.10997.12521.65365</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1517321477.39</float>
<float>1517344745.48</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -3,6 +3,27 @@
(function (window, rJS, RSVP, Handlebars) {
"use strict";
function isEmpty(value) {
return (value === undefined ||
value === null ||
value.length === 0);
}
/** More robust way of writing a || b || "" because if b===0 it gets skipped.
*/
function getNonEmpty() {
var i;
for (i = 0; i < arguments.length; i++) {
if (!isEmpty(arguments[i])) {
return arguments[i];
}
}
if (arguments.length === 1) {
return arguments[0];
}
return arguments[arguments.length - 1];
}
// How to change html selected option using JavaScript?
// http://stackoverflow.com/a/20662180
......@@ -32,7 +53,7 @@
.declareMethod('render', function (options) {
var state_dict = {
value: options.value || "",
value: getNonEmpty(options.value, ""),
item_list: JSON.stringify(options.item_list),
editable: options.editable,
required: options.required,
......
......@@ -97,12 +97,6 @@
<none/>
</value>
</item>
<item>
<key> <string>subject</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Gadget HTML5 Select JS</string> </value>
......@@ -236,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>963.62584.36941.22510</string> </value>
<value> <string>965.10402.53372.47291</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -254,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1516785191.79</float>
<float>1517343550.97</float>
<string>UTC</string>
</tuple>
</state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment