react-app-utilities 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +397 -3
- package/lib/com.js +60 -0
- package/lib/core.js +46 -0
- package/lib/diff.js +52 -0
- package/lib/edit.js +92 -0
- package/lib/index.js +296 -0
- package/lib/reflect.js +262 -0
- package/lib/route.js +115 -0
- package/lib/search.js +552 -0
- package/lib/state.js +204 -0
- package/lib/useMessage.js +25 -0
- package/lib/util.js +122 -0
- package/package.json +32 -6
- package/src/com.ts +116 -0
- package/src/core.ts +98 -0
- package/src/diff.ts +72 -0
- package/src/edit.ts +108 -0
- package/src/index.ts +380 -0
- package/src/reflect.ts +244 -0
- package/src/route.ts +152 -0
- package/src/search.ts +582 -0
- package/src/state.ts +225 -0
- package/src/useMessage.ts +31 -0
- package/src/util.ts +133 -0
- package/tsconfig.json +20 -0
package/lib/state.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
var __assign =
|
|
3
|
+
(this && this.__assign) ||
|
|
4
|
+
function () {
|
|
5
|
+
__assign =
|
|
6
|
+
Object.assign ||
|
|
7
|
+
function (t) {
|
|
8
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
9
|
+
s = arguments[i]
|
|
10
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]
|
|
11
|
+
}
|
|
12
|
+
return t
|
|
13
|
+
}
|
|
14
|
+
return __assign.apply(this, arguments)
|
|
15
|
+
}
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
17
|
+
var core_1 = require("./core")
|
|
18
|
+
var reflect_1 = require("./reflect")
|
|
19
|
+
function getDecimalSeparator(ele) {
|
|
20
|
+
var separator = ele.getAttribute("data-decimal-separator")
|
|
21
|
+
if (!separator) {
|
|
22
|
+
var form = ele.form
|
|
23
|
+
if (form) {
|
|
24
|
+
separator = form.getAttribute("data-decimal-separator")
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return separator === "," || separator === "٫" ? separator : "."
|
|
28
|
+
}
|
|
29
|
+
exports.getDecimalSeparator = getDecimalSeparator
|
|
30
|
+
function normalizeInteger(s) {
|
|
31
|
+
if (!s) {
|
|
32
|
+
return ""
|
|
33
|
+
}
|
|
34
|
+
var len = s.length
|
|
35
|
+
var buf = new Array(len)
|
|
36
|
+
var j = 0
|
|
37
|
+
for (var i = 0; i < len; i++) {
|
|
38
|
+
var c = s.charCodeAt(i)
|
|
39
|
+
if (c >= 48 && c <= 57) {
|
|
40
|
+
buf[j++] = s[i]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
44
|
+
}
|
|
45
|
+
exports.normalizeInteger = normalizeInteger
|
|
46
|
+
function removeSeparators(s) {
|
|
47
|
+
if (!s) {
|
|
48
|
+
return ""
|
|
49
|
+
}
|
|
50
|
+
var len = s.length
|
|
51
|
+
var buffer = new Uint16Array(len)
|
|
52
|
+
var write = 0
|
|
53
|
+
for (var i = 0; i < len; i++) {
|
|
54
|
+
var c = s.charCodeAt(i)
|
|
55
|
+
if ((c >= 48 && c <= 57) || c === 46) {
|
|
56
|
+
buffer[write++] = c
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return String.fromCharCode.apply(null, buffer.subarray(0, write))
|
|
60
|
+
}
|
|
61
|
+
exports.removeSeparators = removeSeparators
|
|
62
|
+
function normalizeNumber(s) {
|
|
63
|
+
if (!s) {
|
|
64
|
+
return ""
|
|
65
|
+
}
|
|
66
|
+
var len = s.length
|
|
67
|
+
var buf = new Array(len)
|
|
68
|
+
var j = 0
|
|
69
|
+
for (var i = 0; i < len; i++) {
|
|
70
|
+
var c = s.charCodeAt(i)
|
|
71
|
+
if (c >= 48 && c <= 57) {
|
|
72
|
+
buf[j++] = s[i]
|
|
73
|
+
} else if (c === 44 || c === 1643) {
|
|
74
|
+
buf[j++] = "."
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
78
|
+
}
|
|
79
|
+
exports.normalizeNumber = normalizeNumber
|
|
80
|
+
function updateNumber(e, o, setObj, decimalSeparator, callback, formatStr) {
|
|
81
|
+
var ctrl = e.target
|
|
82
|
+
var v0 = formatStr ? formatStr(ctrl.value) : ctrl.value
|
|
83
|
+
var dataField = ctrl.getAttribute("data-field")
|
|
84
|
+
var field = dataField ? dataField : ctrl.name
|
|
85
|
+
var v = decimalSeparator === "," || decimalSeparator === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
|
|
86
|
+
if (v === "" || v == null) {
|
|
87
|
+
reflect_1.setValue(o, field, undefined)
|
|
88
|
+
} else {
|
|
89
|
+
var val = isNaN(v) ? undefined : parseFloat(v)
|
|
90
|
+
reflect_1.setValue(o, field, val)
|
|
91
|
+
}
|
|
92
|
+
setObj(__assign({}, o))
|
|
93
|
+
if (callback) {
|
|
94
|
+
callback()
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.updateNumber = updateNumber
|
|
98
|
+
function formatAndUpdateState(e, o, setObj, formatStr, decimalSeparator, callback) {
|
|
99
|
+
updateState(e, o, setObj, decimalSeparator, formatStr, callback)
|
|
100
|
+
}
|
|
101
|
+
exports.formatAndUpdateState = formatAndUpdateState
|
|
102
|
+
function updateStateAndCallback(e, o, setObj, callback, decimalSeparator, formatStr) {
|
|
103
|
+
updateState(e, o, setObj, decimalSeparator, formatStr, callback)
|
|
104
|
+
}
|
|
105
|
+
exports.updateStateAndCallback = updateStateAndCallback
|
|
106
|
+
function updateState(e, o, setObj, decimalSeparator, formatStr, callback) {
|
|
107
|
+
var ctrl = e.target
|
|
108
|
+
var v0 = formatStr ? formatStr(ctrl.value) : ctrl.value
|
|
109
|
+
var dataField = ctrl.getAttribute("data-field")
|
|
110
|
+
var field = dataField ? dataField : ctrl.name
|
|
111
|
+
if (ctrl.tagName === "SELECT") {
|
|
112
|
+
if (ctrl.tagName === "SELECT") {
|
|
113
|
+
if (v0 === "" || !v0) {
|
|
114
|
+
ctrl.removeAttribute("data-value")
|
|
115
|
+
} else {
|
|
116
|
+
ctrl.setAttribute("data-value", v0)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
reflect_1.setValue(o, field, v0)
|
|
120
|
+
} else {
|
|
121
|
+
var stype = ctrl.getAttribute("type")
|
|
122
|
+
var type = stype ? stype.toLowerCase() : "text"
|
|
123
|
+
var model = o
|
|
124
|
+
if (type === "checkbox") {
|
|
125
|
+
var value = model[field]
|
|
126
|
+
if (ctrl.id && ctrl.name !== ctrl.id) {
|
|
127
|
+
if (!value || !Array.isArray(value)) {
|
|
128
|
+
value = []
|
|
129
|
+
}
|
|
130
|
+
value.includes(v0)
|
|
131
|
+
? (value = value.filter(function (v) {
|
|
132
|
+
return v !== v0
|
|
133
|
+
}))
|
|
134
|
+
: value.push(v0)
|
|
135
|
+
model[field] = value
|
|
136
|
+
} else {
|
|
137
|
+
var v = valueOfCheckbox(ctrl)
|
|
138
|
+
model[field] = v
|
|
139
|
+
}
|
|
140
|
+
} else if (type === "radio") {
|
|
141
|
+
if (field.indexOf(".") < 0 && field.indexOf("[") < 0) {
|
|
142
|
+
model[field] = v0
|
|
143
|
+
} else {
|
|
144
|
+
reflect_1.setValue(model, field, v0)
|
|
145
|
+
}
|
|
146
|
+
} else if (type === "date" || type === "datetime-local") {
|
|
147
|
+
var date = new Date(v0)
|
|
148
|
+
var val = !isNaN(date.getTime()) ? date.toISOString() : null
|
|
149
|
+
reflect_1.setValue(model, field, val)
|
|
150
|
+
} else if (type === "time") {
|
|
151
|
+
var date = new Date(v0)
|
|
152
|
+
if (!isNaN(date.getTime())) {
|
|
153
|
+
reflect_1.setValue(model, field, date.getTime())
|
|
154
|
+
} else {
|
|
155
|
+
reflect_1.setValue(model, field, null)
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
var datatype = ctrl.getAttribute("data-type")
|
|
159
|
+
if (datatype === "phone") {
|
|
160
|
+
var val = core_1.normalizePhone(v0)
|
|
161
|
+
reflect_1.setValue(o, field, val)
|
|
162
|
+
} else if (datatype === "fax") {
|
|
163
|
+
var val = core_1.normalizeFax(v0)
|
|
164
|
+
reflect_1.setValue(o, field, val)
|
|
165
|
+
} else if (datatype === "integer") {
|
|
166
|
+
var v = normalizeInteger(v0)
|
|
167
|
+
if (v === "" || v == null) {
|
|
168
|
+
reflect_1.setValue(o, field, undefined)
|
|
169
|
+
} else {
|
|
170
|
+
var val = isNaN(v) ? undefined : parseFloat(v)
|
|
171
|
+
reflect_1.setValue(o, field, val)
|
|
172
|
+
}
|
|
173
|
+
} else if (datatype === "number") {
|
|
174
|
+
var sep = decimalSeparator ? decimalSeparator : getDecimalSeparator(ctrl)
|
|
175
|
+
var v = sep === "," || sep === "٫" ? normalizeNumber(v0) : removeSeparators(v0)
|
|
176
|
+
if (v === "" || v == null) {
|
|
177
|
+
reflect_1.setValue(o, field, undefined)
|
|
178
|
+
} else {
|
|
179
|
+
var val = isNaN(v) ? undefined : parseFloat(v)
|
|
180
|
+
reflect_1.setValue(o, field, val)
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
reflect_1.setValue(o, field, v0)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
setObj(__assign({}, o))
|
|
188
|
+
if (callback) {
|
|
189
|
+
callback()
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
exports.updateState = updateState
|
|
193
|
+
function valueOfCheckbox(ctrl) {
|
|
194
|
+
var ctrlOnValue = ctrl.getAttribute("data-on-value")
|
|
195
|
+
var ctrlOffValue = ctrl.getAttribute("data-off-value")
|
|
196
|
+
if (ctrlOnValue && ctrlOffValue) {
|
|
197
|
+
var onValue = ctrlOnValue ? ctrlOnValue : true
|
|
198
|
+
var offValue = ctrlOffValue ? ctrlOffValue : false
|
|
199
|
+
return ctrl.checked === true ? onValue : offValue
|
|
200
|
+
} else {
|
|
201
|
+
return ctrl.checked === true
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.valueOfCheckbox = valueOfCheckbox
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
3
|
+
var React = require("react")
|
|
4
|
+
exports.useMessage = function (initialState) {
|
|
5
|
+
var _a = React.useState(initialState),
|
|
6
|
+
msg = _a[0],
|
|
7
|
+
setMessage = _a[1]
|
|
8
|
+
var hideMessage = function () {
|
|
9
|
+
setMessage({ alertClass: "", message: "" })
|
|
10
|
+
}
|
|
11
|
+
var showMessage = function (ms) {
|
|
12
|
+
setMessage({ alertClass: "alert alert-info", message: ms })
|
|
13
|
+
}
|
|
14
|
+
var showError = function (ms) {
|
|
15
|
+
if (typeof ms === "string") {
|
|
16
|
+
setMessage({ alertClass: "alert alert-error", message: ms })
|
|
17
|
+
} else if (Array.isArray(ms) && ms.length > 0) {
|
|
18
|
+
setMessage({ alertClass: "alert alert-error", message: ms[0].message })
|
|
19
|
+
} else {
|
|
20
|
+
var x = JSON.stringify(ms)
|
|
21
|
+
setMessage({ alertClass: "alert alert-error", message: x })
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return { msg: msg, showError: showError, showMessage: showMessage, hideMessage: hideMessage }
|
|
25
|
+
}
|
package/lib/util.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
3
|
+
exports.enLocale = {
|
|
4
|
+
id: "en-US",
|
|
5
|
+
countryCode: "US",
|
|
6
|
+
dateFormat: "M/d/yyyy",
|
|
7
|
+
firstDayOfWeek: 1,
|
|
8
|
+
decimalSeparator: ".",
|
|
9
|
+
groupSeparator: ",",
|
|
10
|
+
decimalDigits: 2,
|
|
11
|
+
currencyCode: "USD",
|
|
12
|
+
currencySymbol: "$",
|
|
13
|
+
currencyPattern: 0,
|
|
14
|
+
}
|
|
15
|
+
var r1 = / |,|\$|€|£|¥|'|٬|،| /g
|
|
16
|
+
var r2 = / |\.|\$|€|£|¥|'|٬|،| /g
|
|
17
|
+
function valueOf(ctrl, locale, eventType) {
|
|
18
|
+
if (ctrl.type === "checkbox") {
|
|
19
|
+
var ctrlOnValue = ctrl.getAttribute("data-on-value")
|
|
20
|
+
var ctrlOffValue = ctrl.getAttribute("data-off-value")
|
|
21
|
+
if (ctrlOnValue && ctrlOffValue) {
|
|
22
|
+
var onValue = ctrlOnValue ? ctrlOnValue : true
|
|
23
|
+
var offValue = ctrlOffValue ? ctrlOffValue : false
|
|
24
|
+
return ctrl.checked === true ? { mustChange: true, value: onValue } : { mustChange: true, value: offValue }
|
|
25
|
+
} else {
|
|
26
|
+
return ctrl.checked === true ? { mustChange: true, value: true } : { mustChange: true, value: false }
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
var datatype = ctrl.getAttribute("data-type")
|
|
30
|
+
if (datatype === "number" || datatype === "integer") {
|
|
31
|
+
var v = void 0
|
|
32
|
+
if (locale && locale.decimalSeparator !== ".") {
|
|
33
|
+
v = ctrl.value.replace(r2, "")
|
|
34
|
+
if (v.indexOf(locale.decimalSeparator) >= 0) {
|
|
35
|
+
v = v.replace(locale.decimalSeparator, ".")
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
v = ctrl.value.replace(r1, "")
|
|
39
|
+
}
|
|
40
|
+
return isNaN(v) ? { mustChange: false } : { mustChange: true, value: parseFloat(v) }
|
|
41
|
+
} else if (datatype === "currency" || datatype === "string-currency") {
|
|
42
|
+
var ml = ctrl.getAttribute("maxlength")
|
|
43
|
+
var nm = ml != null && !isNaN(ml) ? parseInt(ml, 10) : undefined
|
|
44
|
+
var res = getStringCurrency(ctrl.value, datatype, locale, nm, eventType === "blur")
|
|
45
|
+
return res
|
|
46
|
+
} else {
|
|
47
|
+
return { mustChange: true, value: ctrl.value }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.valueOf = valueOf
|
|
52
|
+
function getStringCurrency(value, datatype, locale, maxLength, isOnBlur) {
|
|
53
|
+
if (locale && locale.decimalSeparator !== ".") {
|
|
54
|
+
value = value.replace(r2, "")
|
|
55
|
+
if (value.indexOf(locale.decimalSeparator) >= 0) {
|
|
56
|
+
value = value.replace(locale.decimalSeparator, ".")
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
value = value.replace(r1, "")
|
|
60
|
+
}
|
|
61
|
+
if (value === "") {
|
|
62
|
+
return { mustChange: true, value: "" }
|
|
63
|
+
}
|
|
64
|
+
value = extractNumber(value, ".")
|
|
65
|
+
if (value.length === 0) {
|
|
66
|
+
return { mustChange: false }
|
|
67
|
+
} else if (value.length > 0 && value.charAt(0) === "0") {
|
|
68
|
+
return { mustChange: true, value: value.substring(1) }
|
|
69
|
+
}
|
|
70
|
+
if (!locale) {
|
|
71
|
+
locale = exports.enLocale
|
|
72
|
+
}
|
|
73
|
+
var decimalDigits = locale ? locale.decimalDigits : 2
|
|
74
|
+
var groupDigits = 3
|
|
75
|
+
var decimalSeparator = locale.decimalSeparator
|
|
76
|
+
var groupSeparator = locale.groupSeparator
|
|
77
|
+
if (isOnBlur) {
|
|
78
|
+
var number = Number(value.replace(/^0+/, ""))
|
|
79
|
+
if (number === 0) {
|
|
80
|
+
return { mustChange: true, value: "" }
|
|
81
|
+
} else {
|
|
82
|
+
value = number.toFixed(decimalDigits)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
var dotPosition = value.indexOf(".")
|
|
86
|
+
var beforeDot = dotPosition >= 0 ? value.substring(0, dotPosition) : value
|
|
87
|
+
if (datatype === "string-currency" || isOnBlur) {
|
|
88
|
+
beforeDot = beforeDot.replace(new RegExp("\\B(?=(\\d{" + groupDigits + "})+(?!\\d))", "g"), groupSeparator)
|
|
89
|
+
}
|
|
90
|
+
var afterDot
|
|
91
|
+
if (dotPosition > 0) {
|
|
92
|
+
afterDot = value.substring(dotPosition + 1)
|
|
93
|
+
if (afterDot.length > decimalDigits) {
|
|
94
|
+
afterDot = afterDot.substring(0, decimalDigits)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (maxLength && beforeDot.length > maxLength - (decimalDigits + 1)) {
|
|
98
|
+
return { mustChange: false }
|
|
99
|
+
}
|
|
100
|
+
value = dotPosition >= 0 ? beforeDot + decimalSeparator + afterDot : beforeDot
|
|
101
|
+
return maxLength && value.length > maxLength ? { mustChange: false } : { mustChange: true, value: value }
|
|
102
|
+
}
|
|
103
|
+
function extractNumber(str, decimalSeparator) {
|
|
104
|
+
var arrs = []
|
|
105
|
+
var d = false
|
|
106
|
+
for (var i = 0; i < str.length; i++) {
|
|
107
|
+
var ch = str.charAt(i)
|
|
108
|
+
if (ch >= "0" && ch <= "9") {
|
|
109
|
+
arrs.push(ch)
|
|
110
|
+
} else if (ch === decimalSeparator) {
|
|
111
|
+
if (d) {
|
|
112
|
+
return arrs.join("")
|
|
113
|
+
} else {
|
|
114
|
+
d = true
|
|
115
|
+
arrs.push(ch)
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
return arrs.join("")
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return arrs.join("")
|
|
122
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "react-app-utilities",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "react-app-utilities",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "react",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build:lib": "tsc",
|
|
9
|
+
"clean:lib": "rimraf lib"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/react": "^17.0.41",
|
|
13
|
+
"@types/react-router": "^5.3.3",
|
|
14
|
+
"query-string": "^9.2.2",
|
|
15
|
+
"react": "^17.0.2",
|
|
16
|
+
"react-router": "^8.0.1",
|
|
17
|
+
"tslint": "^5.10.0",
|
|
18
|
+
"typescript": "^3.3.3333"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"registry": "https://registry.npmjs.org/"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git@github.com/core-ts/react"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"react",
|
|
29
|
+
"react-hook",
|
|
30
|
+
"react-hook-core"
|
|
31
|
+
]
|
|
32
|
+
}
|
package/src/com.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { Locale, pageSizes } from "./core"
|
|
3
|
+
|
|
4
|
+
export interface ErrorProps {
|
|
5
|
+
title?: string
|
|
6
|
+
message?: string
|
|
7
|
+
back?: React.MouseEventHandler<HTMLElement>
|
|
8
|
+
}
|
|
9
|
+
export function Error(p: ErrorProps) {
|
|
10
|
+
return React.createElement(
|
|
11
|
+
"div",
|
|
12
|
+
{},
|
|
13
|
+
React.createElement(
|
|
14
|
+
"header",
|
|
15
|
+
{ className: "error-header" },
|
|
16
|
+
p.back && React.createElement("button", { type: "button", id: "backBtn", name: "backBtn", className: "btn-back", onClick: p.back }),
|
|
17
|
+
React.createElement("h2", {}, p.title),
|
|
18
|
+
),
|
|
19
|
+
React.createElement("div", { className: "error-body" }, React.createElement("h4", { className: "h4" }, p.message)),
|
|
20
|
+
)
|
|
21
|
+
/*
|
|
22
|
+
<div>
|
|
23
|
+
<header className="error-header">
|
|
24
|
+
<button type="button" id="backBtn" name="backBtn" className="btn-back" onClick={() => navigate(-1)}></button>
|
|
25
|
+
<h2>{resource.error_404_title}</h2>
|
|
26
|
+
</header>
|
|
27
|
+
<div className="error-body">
|
|
28
|
+
<h4 className="h4">{resource.error_404_message}</h4>
|
|
29
|
+
</div>
|
|
30
|
+
</div>
|
|
31
|
+
*/
|
|
32
|
+
}
|
|
33
|
+
export interface PageSizeProps {
|
|
34
|
+
id?: string
|
|
35
|
+
name?: string
|
|
36
|
+
className?: string
|
|
37
|
+
size?: number
|
|
38
|
+
sizes?: number[]
|
|
39
|
+
onChange?: React.ChangeEventHandler<HTMLSelectElement>
|
|
40
|
+
}
|
|
41
|
+
export function PageSizeSelect(p: PageSizeProps) {
|
|
42
|
+
const g = p.sizes
|
|
43
|
+
const s = !g || g.length === 0 ? pageSizes : g
|
|
44
|
+
const opts = s.map((pgSize) => React.createElement("option", { key: pgSize, value: pgSize }, pgSize))
|
|
45
|
+
return React.createElement("select", { id: p.id, name: p.name, className: p.className, value: p.size, onChange: p.onChange }, opts)
|
|
46
|
+
}
|
|
47
|
+
interface Item {
|
|
48
|
+
id?: string
|
|
49
|
+
value: string
|
|
50
|
+
text?: string
|
|
51
|
+
}
|
|
52
|
+
export interface SelectProps {
|
|
53
|
+
id?: string
|
|
54
|
+
name?: string
|
|
55
|
+
className?: string
|
|
56
|
+
value?: string
|
|
57
|
+
items: Item[]
|
|
58
|
+
onChange?: React.ChangeEventHandler<HTMLSelectElement>
|
|
59
|
+
}
|
|
60
|
+
export function Select(p: SelectProps) {
|
|
61
|
+
const s = p.items
|
|
62
|
+
const opts = s.map((item) => React.createElement("option", { key: item.value, value: item.value }, item.text))
|
|
63
|
+
return React.createElement("select", { id: p.id, name: p.name, className: p.className, value: p.value, onChange: p.onChange }, opts)
|
|
64
|
+
}
|
|
65
|
+
export interface Props {
|
|
66
|
+
id?: string
|
|
67
|
+
name?: string
|
|
68
|
+
size?: number
|
|
69
|
+
sizes?: number[]
|
|
70
|
+
className?: string
|
|
71
|
+
maxLength?: number
|
|
72
|
+
value?: string
|
|
73
|
+
placeholder?: string
|
|
74
|
+
pageSizeChanged?: (e: React.ChangeEvent<HTMLSelectElement>) => void
|
|
75
|
+
search?: React.MouseEventHandler<HTMLElement>
|
|
76
|
+
toggle?: React.MouseEventHandler<HTMLElement>
|
|
77
|
+
clear?: React.MouseEventHandler<HTMLButtonElement>
|
|
78
|
+
onChange?: (e: any, callback?: (() => void) | undefined, lc?: Locale | undefined) => void
|
|
79
|
+
}
|
|
80
|
+
export function Search(p: Props) {
|
|
81
|
+
return React.createElement(
|
|
82
|
+
React.Fragment,
|
|
83
|
+
null,
|
|
84
|
+
React.createElement(
|
|
85
|
+
"label",
|
|
86
|
+
{ className: p.className },
|
|
87
|
+
p.pageSizeChanged && React.createElement(PageSizeSelect, { size: p.size, sizes: p.sizes, onChange: p.pageSizeChanged, name: p.name, id: p.id }),
|
|
88
|
+
React.createElement("input", {
|
|
89
|
+
type: "text",
|
|
90
|
+
id: "q",
|
|
91
|
+
name: "q",
|
|
92
|
+
value: p.value || "",
|
|
93
|
+
onChange: p.onChange,
|
|
94
|
+
maxLength: p.maxLength,
|
|
95
|
+
placeholder: p.placeholder,
|
|
96
|
+
}),
|
|
97
|
+
p.clear &&
|
|
98
|
+
React.createElement("button", { type: "button", id: "clearQBtn", name: "clearQBtn", hidden: !p.value, className: "btn-remove-text", onClick: p.clear }),
|
|
99
|
+
p.toggle && React.createElement("button", { type: "button", id: "toggleSearchBtn", name: "toggleSearchBtn", className: "btn-filter", onClick: p.toggle }),
|
|
100
|
+
p.search && React.createElement("button", { type: "submit", id: "searchBtn", name: "searchBtn", className: "btn-search", onClick: p.search }),
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
/*
|
|
104
|
+
return (
|
|
105
|
+
<>
|
|
106
|
+
<label className={p.className}>
|
|
107
|
+
{p.pageSizeChanged && <PageSizeSelect size={p.size} sizes={p.sizes} onChange={p.pageSizeChanged} name={p.name} id={p.id} />}
|
|
108
|
+
<input type='text' id='q' name='q' value={p.value || ''} onChange={p.onChange} maxLength={p.maxLength} placeholder={p.placeholder} />
|
|
109
|
+
{p.clear && <button type='button' id='clearQBtn' name='clearQBtn' hidden={!p.value} className='btn-remove-text' onClick={p.clear}/>}
|
|
110
|
+
{p.toggle && <button type='button' id='toggleSearchBtn' name='toggleSearchBtn' className='btn-filter' onClick={p.toggle} />}
|
|
111
|
+
{p.search && <button type='submit' id='searchBtn' name='searchBtn' className='btn-search' onClick={p.search} />}
|
|
112
|
+
</label>
|
|
113
|
+
</>
|
|
114
|
+
);
|
|
115
|
+
*/
|
|
116
|
+
}
|
package/src/core.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export interface LoadingService {
|
|
2
|
+
showLoading(firstTime?: boolean): void
|
|
3
|
+
hideLoading(): void
|
|
4
|
+
}
|
|
5
|
+
export const pageSizes = [12, 24, 60, 100, 120, 180, 300, 600]
|
|
6
|
+
export const sizes = pageSizes
|
|
7
|
+
// tslint:disable-next-line:class-name
|
|
8
|
+
export class resources {
|
|
9
|
+
static _cache: any = {}
|
|
10
|
+
static cache = true
|
|
11
|
+
static fields = "fields"
|
|
12
|
+
static page = "page"
|
|
13
|
+
static limit = "limit"
|
|
14
|
+
static defaultLimit = 24
|
|
15
|
+
static limits = pageSizes
|
|
16
|
+
static pageMaxSize = 7
|
|
17
|
+
static getSortId(field: string): string {
|
|
18
|
+
return field + "Sort"
|
|
19
|
+
}
|
|
20
|
+
static normalizePhone(s?: string | null): string {
|
|
21
|
+
if (!s) {
|
|
22
|
+
return ""
|
|
23
|
+
}
|
|
24
|
+
const len = s.length
|
|
25
|
+
const buf = new Array<string>(len)
|
|
26
|
+
let j = 0
|
|
27
|
+
for (let i = 0; i < len; i++) {
|
|
28
|
+
const c = s.charCodeAt(i)
|
|
29
|
+
if (c === 43 || (c >= 48 && c <= 57)) {
|
|
30
|
+
buf[j++] = s[i]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return j === len ? buf.join("") : buf.slice(0, j).join("")
|
|
34
|
+
}
|
|
35
|
+
static normalizeFax(fax?: string | null): string {
|
|
36
|
+
return resources.normalizePhone(fax)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function normalizePhone(phone?: string | null): string {
|
|
40
|
+
return resources.normalizePhone(phone)
|
|
41
|
+
}
|
|
42
|
+
export function normalizeFax(fax?: string | null): string {
|
|
43
|
+
return resources.normalizeFax(fax)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface StringMap {
|
|
47
|
+
[key: string]: string
|
|
48
|
+
}
|
|
49
|
+
export interface Locale {
|
|
50
|
+
id?: string
|
|
51
|
+
countryCode: string
|
|
52
|
+
dateFormat: string
|
|
53
|
+
firstDayOfWeek: number
|
|
54
|
+
decimalSeparator: string
|
|
55
|
+
groupSeparator: string
|
|
56
|
+
decimalDigits: number
|
|
57
|
+
currencyCode: string
|
|
58
|
+
currencySymbol: string
|
|
59
|
+
currencyPattern: number
|
|
60
|
+
currencySample?: string
|
|
61
|
+
}
|
|
62
|
+
export interface ErrorMessage {
|
|
63
|
+
field: string
|
|
64
|
+
code: string
|
|
65
|
+
param?: string | number | Date
|
|
66
|
+
message?: string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type Type =
|
|
70
|
+
| "ObjectId"
|
|
71
|
+
| "date"
|
|
72
|
+
| "datetime"
|
|
73
|
+
| "time"
|
|
74
|
+
| "boolean"
|
|
75
|
+
| "number"
|
|
76
|
+
| "integer"
|
|
77
|
+
| "string"
|
|
78
|
+
| "text"
|
|
79
|
+
| "object"
|
|
80
|
+
| "array"
|
|
81
|
+
| "binary"
|
|
82
|
+
| "primitives"
|
|
83
|
+
| "booleans"
|
|
84
|
+
| "numbers"
|
|
85
|
+
| "integers"
|
|
86
|
+
| "strings"
|
|
87
|
+
| "dates"
|
|
88
|
+
| "datetimes"
|
|
89
|
+
| "times"
|
|
90
|
+
|
|
91
|
+
export interface Attribute {
|
|
92
|
+
name?: string
|
|
93
|
+
type?: Type
|
|
94
|
+
typeof?: Attributes
|
|
95
|
+
}
|
|
96
|
+
export interface Attributes {
|
|
97
|
+
[key: string]: Attribute
|
|
98
|
+
}
|
package/src/diff.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { clone } from "./reflect"
|
|
2
|
+
|
|
3
|
+
export interface DiffModel<T, ID> {
|
|
4
|
+
id?: ID
|
|
5
|
+
origin?: T
|
|
6
|
+
value: T
|
|
7
|
+
}
|
|
8
|
+
export interface ApprService<ID> {
|
|
9
|
+
approve(id: ID, note?: string): Promise<number>
|
|
10
|
+
reject(id: ID, note?: string): Promise<number>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface DiffService<T, ID> {
|
|
14
|
+
keys(): string[]
|
|
15
|
+
diff(id: ID, ctx?: any): Promise<DiffModel<T, ID>>
|
|
16
|
+
}
|
|
17
|
+
export interface DiffApprService<T, ID> extends DiffService<T, ID>, ApprService<ID> {}
|
|
18
|
+
export interface DiffState<T> {
|
|
19
|
+
origin: T
|
|
20
|
+
value: T
|
|
21
|
+
disabled: boolean
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function formatDiffModel<T, ID>(obj: DiffModel<T, ID>, formatFields?: (obj3: T) => T): DiffModel<T, ID> {
|
|
25
|
+
if (!obj) {
|
|
26
|
+
return obj
|
|
27
|
+
}
|
|
28
|
+
const obj2 = clone(obj)
|
|
29
|
+
if (!obj2.origin) {
|
|
30
|
+
obj2.origin = {}
|
|
31
|
+
} else {
|
|
32
|
+
if (typeof obj2.origin === "string") {
|
|
33
|
+
obj2.origin = JSON.parse(obj2.origin)
|
|
34
|
+
}
|
|
35
|
+
if (formatFields && typeof obj2.origin === "object" && !Array.isArray(obj2.origin)) {
|
|
36
|
+
obj2.origin = formatFields(obj2.origin)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!obj2.value) {
|
|
40
|
+
obj2.value = {}
|
|
41
|
+
} else {
|
|
42
|
+
if (typeof obj2.value === "string") {
|
|
43
|
+
obj2.value = JSON.parse(obj2.value)
|
|
44
|
+
}
|
|
45
|
+
if (formatFields && typeof obj2.value === "object" && !Array.isArray(obj2.value)) {
|
|
46
|
+
obj2.value = formatFields(obj2.value)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return obj2
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function getDataFields(form?: HTMLElement | null): HTMLElement[] {
|
|
53
|
+
let results: HTMLElement[] = []
|
|
54
|
+
if (!form) {
|
|
55
|
+
return results
|
|
56
|
+
}
|
|
57
|
+
const attributeValue = form.getAttribute("data-field")
|
|
58
|
+
if (attributeValue && attributeValue.length > 0) {
|
|
59
|
+
results.push(form)
|
|
60
|
+
}
|
|
61
|
+
const childNodes = form.childNodes
|
|
62
|
+
if (childNodes.length > 0) {
|
|
63
|
+
// tslint:disable-next-line:prefer-for-of
|
|
64
|
+
for (let i = 0; i < childNodes.length; i++) {
|
|
65
|
+
const childNode = childNodes[i] as HTMLElement
|
|
66
|
+
if (childNode.nodeType === Node.ELEMENT_NODE) {
|
|
67
|
+
results = results.concat(getDataFields(childNode))
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return results
|
|
72
|
+
}
|