code-support.js, timeFormat 추가, datetimeFormat 수정

master
mjkhan21 1 year ago
parent d214c72630
commit d5f177fa99

@ -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");
}
}

@ -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
* <ul> <li>{@link Dataset#onDatasetChange}</li>
* <li>{@link Dataset#onCurrentChange}</li>
* <li>{@link Dataset#onSelectionChange}</li>
* <li>{@link Dataset#onDirtiesChange}</li>
* </ul>
* are called back.
* @param {array|object} obj user data or an object that has user data
* @returns {Dataset} the Dataset
*/
addData(obj) {
if (this.empty)
return this.setData(obj);
let state = this.state;
let data = this._getDataItems(obj);
this._items = this._items.concat(data.items);
this._byKeys = {
...this._byKeys,
...data.byKeys
};
this.onDatasetChange(obj);
this.setState(!Array.isArray(obj) ? obj.state : state);
this.onDirtiesChange(this.dirty);
return this;
}
/**Clears the Dataset's user data.
@ -1140,6 +1216,7 @@ class DatasetControl {
this.prefix = conf.prefix;
this.prefixName = conf.prefixName;
this.infoSize = conf.infoSize;
this.appendData = conf.appendData;
this.query = {};
@ -1181,10 +1258,16 @@ class DatasetControl {
_load() {
if (!this.query.pageNum)
this.query.pageNum = 1;
json.get({
ajax.get({
url:this.urls.load,
data:this.query,
success:resp => this.setData(resp)
success:resp => {
if (!this.appendData || this.query.pageNum == 1)
this.setData(resp);
else {
this.addData(resp);
}
}
});
}
@ -1192,6 +1275,10 @@ class DatasetControl {
this.dataset.setData(obj);
}
addData(obj) {
this.dataset.addData(obj);
}
onDatasetChange(obj) {
debug("onDatasetChange", obj);
}

Loading…
Cancel
Save