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 @@ ...@@ -3,6 +3,27 @@
(function (window, rJS) { (function (window, rJS) {
"use strict"; "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) rJS(window)
.setState({ .setState({
tag: 'p' tag: 'p'
...@@ -11,7 +32,7 @@ ...@@ -11,7 +32,7 @@
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
var field_json = options.field_json || {}, var field_json = options.field_json || {},
state_dict = { state_dict = {
value: field_json.value || field_json.default || "", value: getNonEmpty(field_json.value, field_json['default'], ""),
item_list: JSON.stringify(field_json.items), item_list: JSON.stringify(field_json.items),
editable: field_json.editable, editable: field_json.editable,
required: field_json.required, required: field_json.required,
...@@ -23,8 +44,8 @@ ...@@ -23,8 +44,8 @@
// as user may have modified the input value // as user may have modified the input value
render_timestamp: new Date().getTime() render_timestamp: new Date().getTime()
}; };
// first_item means to select the first item by default on empty value
if ((!state_dict.value) && (state_dict.first_item)) { if (isEmpty(state_dict.value) && (state_dict.first_item)) {
state_dict.value = field_json.items[0][1]; state_dict.value = field_json.items[0][1];
} }
return this.changeState(state_dict); return this.changeState(state_dict);
......
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>965.1659.33258.11707</string> </value> <value> <string>965.10402.43703.62668</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1516786204.34</float> <float>1517343520.55</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -3,13 +3,34 @@ ...@@ -3,13 +3,34 @@
(function (window, rJS, document, RSVP) { (function (window, rJS, document, RSVP) {
'use strict'; 'use strict';
function isEmpty(value) {
return (value === undefined ||
value === null ||
value.length === 0);
}
/* Make sure that returned object is an Array instance. */ /* Make sure that returned object is an Array instance. */
function ensureArray(obj) { function ensureArray(obj) {
if (!obj) {return []; }
if (Array.isArray(obj)) {return obj; } if (Array.isArray(obj)) {return obj; }
if (isEmpty(obj)) {return []; }
return [obj]; 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) { function appendListField(gadget, value, item_list) {
var div = document.createElement('div'); var div = document.createElement('div');
gadget.element.appendChild(div); gadget.element.appendChild(div);
...@@ -37,10 +58,11 @@ ...@@ -37,10 +58,11 @@
rJS(window) rJS(window)
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
var field_json = options.field_json || {}, var field_json = options.field_json || {},
item_list = field_json.items, item_list = ensureArray(field_json.items),
state_dict = { state_dict = {
value_list: JSON.stringify( value_list: JSON.stringify(
ensureArray(field_json.value || field_json.default) ensureArray(
getNonEmpty(field_json.value, field_json['default'], []))
), ),
editable: field_json.editable, editable: field_json.editable,
required: field_json.required, required: field_json.required,
...@@ -54,6 +76,11 @@ ...@@ -54,6 +76,11 @@
// as user may have modified the input value // as user may have modified the input value
render_timestamp: new Date().getTime() 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] !== "")) { if ((item_list.length === 0) || (item_list[0][0] !== "")) {
item_list.unshift(["", ""]); item_list.unshift(["", ""]);
} }
...@@ -77,17 +104,13 @@ ...@@ -77,17 +104,13 @@
element.removeChild(element.firstChild); element.removeChild(element.firstChild);
} }
function enQueue() { value_list.forEach(function (value) {
var argument_list = arguments;
queue queue
.push(function () { .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; return queue;
}) })
...@@ -199,4 +222,4 @@ ...@@ -199,4 +222,4 @@
return true; return true;
}, {mutex: 'changestate'}); }, {mutex: 'changestate'});
}(window, rJS, document, RSVP)); }(window, rJS, document, RSVP));
\ No newline at end of file
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>965.241.13759.3805</string> </value> <value> <string>965.10997.12521.65365</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1517321477.39</float> <float>1517344745.48</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -3,6 +3,27 @@ ...@@ -3,6 +3,27 @@
(function (window, rJS, RSVP, Handlebars) { (function (window, rJS, RSVP, Handlebars) {
"use strict"; "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? // How to change html selected option using JavaScript?
// http://stackoverflow.com/a/20662180 // http://stackoverflow.com/a/20662180
...@@ -32,7 +53,7 @@ ...@@ -32,7 +53,7 @@
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
var state_dict = { var state_dict = {
value: options.value || "", value: getNonEmpty(options.value, ""),
item_list: JSON.stringify(options.item_list), item_list: JSON.stringify(options.item_list),
editable: options.editable, editable: options.editable,
required: options.required, required: options.required,
......
...@@ -97,12 +97,6 @@ ...@@ -97,12 +97,6 @@
<none/> <none/>
</value> </value>
</item> </item>
<item>
<key> <string>subject</string> </key>
<value>
<tuple/>
</value>
</item>
<item> <item>
<key> <string>title</string> </key> <key> <string>title</string> </key>
<value> <string>Gadget HTML5 Select JS</string> </value> <value> <string>Gadget HTML5 Select JS</string> </value>
...@@ -236,7 +230,7 @@ ...@@ -236,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>963.62584.36941.22510</string> </value> <value> <string>965.10402.53372.47291</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -254,7 +248,7 @@ ...@@ -254,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1516785191.79</float> <float>1517343550.97</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </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