diff --git a/src/main/webapp/resources/js/base/code-support.js b/src/main/webapp/resources/js/base/code-support.js new file mode 100644 index 0000000..361ceb0 --- /dev/null +++ b/src/main/webapp/resources/js/base/code-support.js @@ -0,0 +1,44 @@ +class CommonCodes { + constructor(codeList, asObject) { + codeList.forEach(item => this[item.code] = !asObject ? item.value : item); + this.codes = () => codeList.map(item => item.code); + this.list = () => codeList; + } + + _value(code, field) { + let found = this[code]; + if (!found) + return null; + + return "string" == typeof found ? found : found[field] + } + + value(code, nt) { + return ifEmpty(this._value(code, "value"), nt); + } + + format(code) { + return this.value(code, ""); + } + + parse(value) { + for (let code in this) { + let val = this.value(code); + if (value == val) + return code; + } + return ""; + } + + etc1(code) { + return this._value(code, "etc1"); + } + + etc2(code) { + return this._etc(code, "etc2"); + } + + etc3(code) { + return this._etc(code, "etc3"); + } +} \ No newline at end of file diff --git a/src/main/webapp/resources/js/base/dataset.js b/src/main/webapp/resources/js/base/dataset.js index fd760a2..8655b00 100644 --- a/src/main/webapp/resources/js/base/dataset.js +++ b/src/main/webapp/resources/js/base/dataset.js @@ -3,6 +3,10 @@ /**@file Classes and objects to help control user data in HTML pages */ +function lpad(v) { + return v < 10 ? "0" + v : v; +} + /** value format for numbers */ const numberFormat = { /**Parses the value for a number @@ -44,12 +48,8 @@ const dateFormat = { let _format = v => { let date = "number" == typeof v ? new Date(v) : v; let year = date.getFullYear(), - month = date.getMonth() + 1, - day = date.getDate(); - if (month < 10) - month = "0" + month; - if (day < 10) - day = "0" + day; + month = lpad(date.getMonth() + 1), + day = lpad(date.getDate()); return year + "-" + month + "-" + day; }; @@ -62,22 +62,43 @@ const dateFormat = { } }; -/** value format for datetimes */ -const datetimeFormat = { +/** value format for time */ +const timeFormat = { /**Formats the value * @param {number} value value to format * @returns {string} formatted value */ format(value) { let _format = v => { - let date = "number" == typeof v ? new Date(v) : v; - return date.toLocaleTimeString(); + let date = "number" == typeof v ? new Date(v) : v, + hours = lpad(date.getHours()), + minutes = lpad(date.getMinutes()), + seconds = lpad(date.getSeconds()); + return hours + ":" + minutes + ":" + seconds; }; switch (value instanceof Date ? "date" : typeof value) { case "number": - case "date": return dateFormat.format(value) + " " + _format(value); - case "string": return dateFormat.format(value) + " " + value.substr(8, 2) + ":" + value.substr(10, 2) + ":" + value.substr(12) + case "date": return _format(value); + case "string": + let offset = value.length - 6; + return value.substr(0 + offset, 2) + ":" + value.substr(2 + offset, 2) + ":" + value.substr(4 + offset) + default: return ""; + } + } +} + +/** value format for datetimes */ +const datetimeFormat = { + /**Formats the value + * @param {number} value value to format + * @returns {string} formatted value + */ + format(value) { + switch (value instanceof Date ? "date" : typeof value) { + case "number": + case "date": return dateFormat.format(value) + " " + timeFormat.format(value); + case "string": return dateFormat.format(value) + " " + timeFormat.format(value); default: return ""; } } @@ -450,6 +471,10 @@ class Dataset { this._byKeys = {}; this._current = null; + let data = this._getDataItems(obj); + this._items = data.items; + this._byKeys = data.byKeys; + /* obj = obj || {}; let array = Array.isArray(obj) ? obj : this.conf.dataGetter(obj) || []; if (!Array.isArray(array)) @@ -460,12 +485,63 @@ class Dataset { let key = "key-" + this.getKey(item.data); this._byKeys[key] = item; }); + */ + this.onDatasetChange(obj); + this.setState(!Array.isArray(obj) ? obj.state : state); + this.onDirtiesChange(this.dirty); + + return this; + } + + _getDataItems(obj) { + obj = obj || {}; + let array = Array.isArray(obj) ? obj : this.conf.dataGetter(obj) || []; + if (!Array.isArray(array)) + throw new Error("The data must be an array"); + + let _items = array.map(e => new DataItem(e, this._formats)), + _byKeys = {}; + _items.forEach(item => { + let key = "key-" + this.getKey(item.data); + _byKeys[key] = item; + }); + + return { + items: _items, + byKeys: _byKeys + }; + } + + /**Adds user data to the Dataset. + * To get user data from an object, the dataGetter configured is called. + * After user data is set, the methods + *