Commit edc9d850 authored by Tomáš Peterka's avatar Tomáš Peterka Committed by Tomáš Peterka

[renderjs_ui] Extract utility function ensureArray, getFirstNonEmpty, and...

[renderjs_ui] Extract utility function ensureArray, getFirstNonEmpty, and isEmpty into global gadget
parent be124a78
...@@ -15,4 +15,46 @@ ...@@ -15,4 +15,46 @@
}); });
}; };
/** Return true if the value truly represents an empty value.
Calling isEmpty(x) is more robust than expression !x.
*/
function isEmpty(value) {
return (value === undefined ||
value === null ||
value.length === 0);
}
window.isEmpty = isEmpty;
/** Make sure that returned object is an Array instance.
*/
function ensureArray(obj) {
if (Array.isArray(obj)) {return obj; }
if (isEmpty(obj)) {return []; }
return [obj];
}
window.ensureArray = ensureArray;
/** Return first non-empty variable or the last one.
Calling getNonEmpy(a, b, "") is more robust way of writing a || b || "".
Variables coercing to false (e.g 0) do not get skipped anymore.
*/
function getFirstNonEmpty() {
var i;
if (arguments.length === 0) {
return null;
}
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];
}
window.getFirstNonEmpty = getFirstNonEmpty;
}(window, RSVP)); }(window, 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>954.14461.31508.53930</string> </value> <value> <string>960.5523.58984.43537</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>1476894652.41</float> <float>1517406504.9</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*global window, rJS*/ /*global window, rJS, isEmpty, getFirstNonEmpty */
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */ /*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
(function (window, rJS) { (function (window, rJS, getFirstNonEmpty) {
"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'
...@@ -32,7 +11,7 @@ ...@@ -32,7 +11,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: getNonEmpty(field_json.value, field_json['default'], ""), value: getFirstNonEmpty(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,
...@@ -132,4 +111,4 @@ ...@@ -132,4 +111,4 @@
return true; return true;
}, {mutex: 'changestate'}); }, {mutex: 'changestate'});
}(window, rJS)); }(window, rJS, getFirstNonEmpty));
\ No newline at end of file \ 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.10402.43703.62668</string> </value> <value> <string>965.10978.44363.39543</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>1517343520.55</float> <float>1517406625.1</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*global window, rJS, document, RSVP*/ /*global window, rJS, document, RSVP, isEmpty, ensureArray */
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80, unparam: true */ /*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80, unparam: true */
(function (window, rJS, document, RSVP) { (function (window, rJS, document, RSVP, isEmpty, ensureArray, getFirstNonEmpty) {
'use strict'; '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 (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) { function appendListField(gadget, value, item_list) {
var div = document.createElement('div'); var div = document.createElement('div');
gadget.element.appendChild(div); gadget.element.appendChild(div);
...@@ -62,7 +34,7 @@ ...@@ -62,7 +34,7 @@
state_dict = { state_dict = {
value_list: JSON.stringify( value_list: JSON.stringify(
ensureArray( ensureArray(
getNonEmpty(field_json.value, field_json['default'], [])) getFirstNonEmpty(field_json.value, field_json['default'], []))
), ),
editable: field_json.editable, editable: field_json.editable,
required: field_json.required, required: field_json.required,
...@@ -222,4 +194,4 @@ ...@@ -222,4 +194,4 @@
return true; return true;
}, {mutex: 'changestate'}); }, {mutex: 'changestate'});
}(window, rJS, document, RSVP)); }(window, rJS, document, RSVP, isEmpty, ensureArray, getFirstNonEmpty));
\ 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.10997.12521.65365</string> </value> <value> <string>965.12125.45407.61132</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>1517344745.48</float> <float>1517413617.75</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*global window, rJS, RSVP, Handlebars */ /*global window, rJS, RSVP, Handlebars, getFirstNonEmpty */
/*jslint indent: 2, maxerr: 3, maxlen: 80, nomen: true */ /*jslint indent: 2, maxerr: 3, maxlen: 80, nomen: true */
(function (window, rJS, RSVP, Handlebars) { (function (window, rJS, RSVP, Handlebars, getFirstNonEmpty) {
"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
...@@ -53,7 +32,7 @@ ...@@ -53,7 +32,7 @@
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
var state_dict = { var state_dict = {
value: getNonEmpty(options.value, ""), value: getFirstNonEmpty(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,
...@@ -169,4 +148,4 @@ ...@@ -169,4 +148,4 @@
return this.notifyInvalid(evt.target.validationMessage); return this.notifyInvalid(evt.target.validationMessage);
}, true, false); }, true, false);
}(window, rJS, RSVP, Handlebars)); }(window, rJS, RSVP, Handlebars, getFirstNonEmpty));
\ No newline at end of file \ 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.10402.53372.47291</string> </value> <value> <string>965.10979.12054.27869</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>1517343550.97</float> <float>1517406671.4</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