zet-lib 1.0.48 → 1.0.51
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/lib/Util.js +869 -859
- package/lib/zRoute.js +94 -54
- package/package.json +3 -3
package/lib/Util.js
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
const moment = require('moment')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const randomstring = require(
|
|
4
|
-
const fs = require('fs-extra')
|
|
5
|
-
const { v4: uuidv4 } = require('uuid')
|
|
6
|
-
const sha256 = require('js-sha256')
|
|
1
|
+
const moment = require('moment')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const randomstring = require('randomstring')
|
|
4
|
+
const fs = require('fs-extra')
|
|
5
|
+
const { v4: uuidv4 } = require('uuid')
|
|
6
|
+
const sha256 = require('js-sha256')
|
|
7
7
|
|
|
8
8
|
const Util = {}
|
|
9
9
|
|
|
10
|
-
Util.tab = '\t'
|
|
10
|
+
Util.tab = '\t'
|
|
11
11
|
Util.tabs = (n) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
12
|
+
let ret = ''
|
|
13
|
+
for (var i = 0; i < n; i++) {
|
|
14
|
+
ret += Util.tab
|
|
15
|
+
}
|
|
16
|
+
return ret
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
Util.NEW_LINE = '\n'
|
|
20
|
-
Util.newLine = '\r\n'
|
|
19
|
+
Util.NEW_LINE = '\n'
|
|
20
|
+
Util.newLine = '\r\n'
|
|
21
21
|
Util.newLines = (n) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
22
|
+
let ret = ''
|
|
23
|
+
for (var i = 0; i < n; i++) {
|
|
24
|
+
ret += Util.newLine
|
|
25
|
+
}
|
|
26
|
+
return ret
|
|
27
|
+
}
|
|
28
28
|
|
|
29
29
|
//sha256
|
|
30
30
|
Util.hash = (string) => {
|
|
31
|
-
|
|
32
|
-
}
|
|
31
|
+
return sha256(string)
|
|
32
|
+
}
|
|
33
33
|
|
|
34
34
|
Util.hashCompare = (myPlaintextPassword, hash) => {
|
|
35
|
-
|
|
36
|
-
}
|
|
35
|
+
return Util.hash(myPlaintextPassword) == hash
|
|
36
|
+
}
|
|
37
37
|
|
|
38
38
|
Util.excelSequence = function () {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
char++;
|
|
39
|
+
let abjads = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
|
|
40
|
+
let arr = abjads
|
|
41
|
+
let char = 0
|
|
42
|
+
let num = 26
|
|
43
|
+
for (let x = 2; x < 15; x++) {
|
|
44
|
+
let idx = 0
|
|
45
|
+
for (let i = 1; i <= 26; i++) {
|
|
46
|
+
arr[num] = abjads[char] + abjads[idx]
|
|
47
|
+
idx++
|
|
48
|
+
num++
|
|
51
49
|
}
|
|
50
|
+
char++
|
|
51
|
+
}
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
}
|
|
53
|
+
return arr
|
|
54
|
+
}
|
|
55
55
|
|
|
56
56
|
Util.now = function () {
|
|
57
|
-
|
|
58
|
-
}
|
|
57
|
+
return moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
|
|
58
|
+
}
|
|
59
59
|
|
|
60
60
|
Util.nowShort = function () {
|
|
61
|
-
|
|
62
|
-
}
|
|
61
|
+
return moment(new Date()).format('YYYY-MM-DD')
|
|
62
|
+
}
|
|
63
63
|
|
|
64
64
|
Util.ago = (data) => {
|
|
65
|
-
|
|
66
|
-
}
|
|
65
|
+
return moment(data).fromNow()
|
|
66
|
+
}
|
|
67
67
|
/*
|
|
68
68
|
moment get one month ago from current
|
|
69
69
|
var monthago = moment().subtract(1, 'months').format('YYYY-MM-DD');
|
|
@@ -71,156 +71,150 @@ Util.ago = (data) => {
|
|
|
71
71
|
*/
|
|
72
72
|
|
|
73
73
|
Util.dateSql = function (date, format) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return '';
|
|
79
|
-
};
|
|
74
|
+
format = format || 'YYYY-MM-DD'
|
|
75
|
+
if (date && date != '0000-00-00') return moment(date).format(format)
|
|
76
|
+
else return ''
|
|
77
|
+
}
|
|
80
78
|
|
|
81
79
|
Util.dateFormat = function (date, format) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return '';
|
|
87
|
-
};
|
|
80
|
+
format = format || 'YYYY-MM-DD'
|
|
81
|
+
if (date && date != '0000-00-00') return moment(date).format(format)
|
|
82
|
+
else return ''
|
|
83
|
+
}
|
|
88
84
|
|
|
89
85
|
Util.timePublic = function (date) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return '';
|
|
94
|
-
};
|
|
86
|
+
if (date) return moment(date).format('DD MMM YYYY')
|
|
87
|
+
else return ''
|
|
88
|
+
}
|
|
95
89
|
|
|
96
90
|
Util.timeSql = function (date, format) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return '';
|
|
103
|
-
};
|
|
91
|
+
if (date && date != '0000-00-00 00:00:00') {
|
|
92
|
+
format = format || 'YYYY-MM-DD HH:mm:ss'
|
|
93
|
+
return moment(date).format(format)
|
|
94
|
+
} else return ''
|
|
95
|
+
}
|
|
104
96
|
|
|
105
97
|
Util.getDate = function (date) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
} else {
|
|
115
|
-
return {
|
|
116
|
-
year: 0,
|
|
117
|
-
month: 0,
|
|
118
|
-
date: 0
|
|
119
|
-
}
|
|
98
|
+
date = date + '' || ''
|
|
99
|
+
if (date != '') {
|
|
100
|
+
let explode = date.split('-')
|
|
101
|
+
return {
|
|
102
|
+
year: parseInt(explode[0]),
|
|
103
|
+
month: parseInt(explode[1]),
|
|
104
|
+
date: parseInt(explode[2]),
|
|
120
105
|
}
|
|
121
|
-
}
|
|
106
|
+
} else {
|
|
107
|
+
return {
|
|
108
|
+
year: 0,
|
|
109
|
+
month: 0,
|
|
110
|
+
date: 0,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
122
114
|
|
|
123
115
|
Util.dateIsBetween = (compare, start, end) => {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
116
|
+
if (compare == '' || compare == '0000-00-00') {
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
compare = moment(compare).format('YYYY-MM-DD')
|
|
120
|
+
start = moment(start).format('YYYY-MM-DD')
|
|
121
|
+
end = moment(end).format('YYYY-MM-DD')
|
|
122
|
+
let today = moment(compare)
|
|
123
|
+
let startDate = moment(start)
|
|
124
|
+
let endDate = moment(end)
|
|
125
|
+
|
|
126
|
+
if (compare == start) {
|
|
127
|
+
return true
|
|
128
|
+
} else if (compare == end) {
|
|
129
|
+
return true
|
|
130
|
+
} else {
|
|
131
|
+
return today.isBetween(startDate, endDate)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
142
134
|
|
|
143
135
|
Util.getMonth = function (date) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
136
|
+
if (date.length > 5) {
|
|
137
|
+
let n = new Date(date)
|
|
138
|
+
let m = n.getMonth()
|
|
139
|
+
return parseInt(m) + 1
|
|
140
|
+
}
|
|
141
|
+
return 0
|
|
142
|
+
}
|
|
151
143
|
|
|
152
144
|
Util.getYear = function (date) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
145
|
+
date = Util.dateSql(date) || ''
|
|
146
|
+
return date.slice(0, 4)
|
|
147
|
+
}
|
|
156
148
|
|
|
157
149
|
//first is smaller than second
|
|
158
150
|
Util.calculateDay = function (from, to, holidayInWeek = 0) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
151
|
+
holidayInWeek = parseInt(holidayInWeek) || 0
|
|
152
|
+
let count = 0
|
|
153
|
+
if (holidayInWeek == 1) {
|
|
154
|
+
let days = Util.enumerateDaysBetweenDates(moment(from).format('YYYY-MM-DD'), moment(to).format('YYYY-MM-DD'))
|
|
155
|
+
let countdays = days.filter((item) => parseInt(moment(item).format('d')) != 0)
|
|
156
|
+
count = countdays.length
|
|
157
|
+
} else if (holidayInWeek == 2) {
|
|
158
|
+
let days = Util.enumerateDaysBetweenDates(moment(from).format('YYYY-MM-DD'), moment(to).format('YYYY-MM-DD'))
|
|
159
|
+
let countdays = days.filter((item) => parseInt(moment(item).format('d')) != 0 && parseInt(moment(item).format('d')) != 6)
|
|
160
|
+
count = countdays.length
|
|
161
|
+
} else {
|
|
162
|
+
let a = moment(from)
|
|
163
|
+
let b = moment(to)
|
|
164
|
+
count = b.diff(a, 'days') + 1
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return count
|
|
168
|
+
}
|
|
177
169
|
|
|
178
170
|
var getDaysBetweenDates = function (startDate, endDate) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
171
|
+
let now = startDate.clone(),
|
|
172
|
+
dates = []
|
|
173
|
+
while (now.isSameOrBefore(endDate)) {
|
|
174
|
+
dates.push(now.format('MM/DD/YYYY'))
|
|
175
|
+
now.add(1, 'days')
|
|
176
|
+
}
|
|
177
|
+
return dates
|
|
178
|
+
}
|
|
186
179
|
|
|
187
180
|
//itterate days in array
|
|
188
181
|
Util.enumerateDaysBetweenDates = function (startDate, endDate) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
182
|
+
let now = moment(startDate).clone(),
|
|
183
|
+
dates = []
|
|
184
|
+
while (now.isSameOrBefore(endDate)) {
|
|
185
|
+
dates.push(now.format('MM/DD/YYYY'))
|
|
186
|
+
now.add(1, 'days')
|
|
187
|
+
}
|
|
188
|
+
return dates
|
|
189
|
+
}
|
|
196
190
|
|
|
197
191
|
Util.tableArray = function (arr) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
192
|
+
let r = []
|
|
193
|
+
let tables = arr[0]
|
|
194
|
+
for (let i = 0; i < tables.length; i++) {
|
|
195
|
+
for (let obj in tables[i]) {
|
|
196
|
+
r.push(tables[i][obj])
|
|
204
197
|
}
|
|
205
|
-
|
|
206
|
-
|
|
198
|
+
}
|
|
199
|
+
return r
|
|
200
|
+
}
|
|
207
201
|
|
|
208
202
|
/*
|
|
209
203
|
table array in sql to arr table name
|
|
210
204
|
only for generator
|
|
211
205
|
*/
|
|
212
206
|
Util.tableArrayToObj = (arr) => {
|
|
213
|
-
|
|
214
|
-
}
|
|
207
|
+
return arr.map((m) => Object.values(m)[0])
|
|
208
|
+
}
|
|
215
209
|
|
|
216
210
|
Util.escapeRegExp = function (str) {
|
|
217
|
-
|
|
218
|
-
}
|
|
211
|
+
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1')
|
|
212
|
+
}
|
|
219
213
|
|
|
220
214
|
Util.validateEmail = function (email) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
215
|
+
let re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
216
|
+
return re.test(email)
|
|
217
|
+
}
|
|
224
218
|
|
|
225
219
|
/*
|
|
226
220
|
Replace All like str_replace in PHP
|
|
@@ -228,294 +222,296 @@ Util.validateEmail = function (email) {
|
|
|
228
222
|
*/
|
|
229
223
|
|
|
230
224
|
Util.replaceAll = function (str, find, replace) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
} else {
|
|
240
|
-
if(typeof str == "string") {
|
|
241
|
-
t = str.replace(new RegExp(Util.escapeRegExp(find), 'g'), replace);
|
|
242
|
-
}
|
|
225
|
+
let t = ''
|
|
226
|
+
if (Array.isArray(find)) {
|
|
227
|
+
t = str
|
|
228
|
+
for (let i = 0; i < find.length; i++) {
|
|
229
|
+
if (str.indexOf(find[i]) > -1) {
|
|
230
|
+
t = str.replace(new RegExp(Util.escapeRegExp(find[i]), 'g'), replace)
|
|
231
|
+
}
|
|
243
232
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
Util.phoneFixed = function (str) {
|
|
248
|
-
let ret = ''
|
|
249
|
-
str = Util.replaceAll(str, ' ', '')
|
|
250
|
-
let phone = str.trim()
|
|
251
|
-
phone = Util.replaceAll(phone, '-', '')
|
|
252
|
-
let first = phone.charAt(0)
|
|
253
|
-
if (first == '') {
|
|
254
|
-
ret = '';
|
|
255
|
-
} else if (first == '+') {
|
|
256
|
-
ret = phone;
|
|
257
|
-
} else if (first == '0') {
|
|
258
|
-
ret = '+62' + phone.replace('0', '');
|
|
259
|
-
} else {
|
|
260
|
-
ret = '+' + phone
|
|
233
|
+
} else {
|
|
234
|
+
if (typeof str == 'string') {
|
|
235
|
+
t = str.replace(new RegExp(Util.escapeRegExp(find), 'g'), replace)
|
|
261
236
|
}
|
|
237
|
+
}
|
|
238
|
+
return t
|
|
239
|
+
}
|
|
262
240
|
|
|
263
|
-
|
|
264
|
-
|
|
241
|
+
Util.phoneFixed = function (str) {
|
|
242
|
+
let ret = ''
|
|
243
|
+
str = Util.replaceAll(str, ' ', '')
|
|
244
|
+
let phone = str.trim()
|
|
245
|
+
phone = Util.replaceAll(phone, '-', '')
|
|
246
|
+
let first = phone.charAt(0)
|
|
247
|
+
if (first == '') {
|
|
248
|
+
ret = ''
|
|
249
|
+
} else if (first == '+') {
|
|
250
|
+
ret = phone
|
|
251
|
+
} else if (first == '0') {
|
|
252
|
+
ret = '+62' + phone.replace('0', '')
|
|
253
|
+
} else {
|
|
254
|
+
ret = '+' + phone
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return ret
|
|
258
|
+
}
|
|
265
259
|
|
|
266
260
|
Util.jsonSuccess = function (message) {
|
|
267
|
-
|
|
268
|
-
|
|
261
|
+
message = message || LANGUAGE.data_saved
|
|
262
|
+
let json = { type: 'success', status: 1, title: LANGUAGE.success, message: message }
|
|
269
263
|
|
|
270
|
-
|
|
271
|
-
}
|
|
264
|
+
return json
|
|
265
|
+
}
|
|
272
266
|
|
|
273
267
|
Util.flashError = function (message, errors) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
268
|
+
errors = errors || []
|
|
269
|
+
message = message || LANGUAGE.data_not_found
|
|
270
|
+
let json = { type: 'error', status: 0, title: 'Error', message: message, errors: errors }
|
|
277
271
|
|
|
278
|
-
|
|
279
|
-
}
|
|
272
|
+
return json
|
|
273
|
+
}
|
|
280
274
|
|
|
281
275
|
Util.jsonError = function (path, message) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
276
|
+
let json = {}
|
|
277
|
+
json.errorLog = 1
|
|
278
|
+
json.type = 'error'
|
|
279
|
+
json.status = 0
|
|
280
|
+
json.title = path + ' Error!'
|
|
281
|
+
json.message = message
|
|
282
|
+
json.errors = [{ path: path, message: message }]
|
|
283
|
+
|
|
284
|
+
return json
|
|
285
|
+
}
|
|
292
286
|
|
|
293
287
|
Util.arrayToObject = (array, keyField, isInteger = false) => {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
288
|
+
let obj = {}
|
|
289
|
+
if (array.length) {
|
|
290
|
+
array.forEach(function (item) {
|
|
291
|
+
var name = item[keyField] == null ? 'xxxxxx' : item[keyField] == 'null' ? 'xxxxxx' : isInteger ? item[keyField] : item[keyField] + ''
|
|
292
|
+
obj[name] = item
|
|
293
|
+
})
|
|
294
|
+
}
|
|
295
|
+
return obj
|
|
296
|
+
}
|
|
303
297
|
|
|
304
298
|
//chase id:1,name:'test' t0 {1:'test'}
|
|
305
299
|
Util.modulesSwitch = (arr) => {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
300
|
+
let stores = []
|
|
301
|
+
stores.push({ id: '', name: '' })
|
|
302
|
+
arr.forEach((ar, index) => {
|
|
303
|
+
stores.push({ id: index, name: ar })
|
|
304
|
+
})
|
|
305
|
+
return stores
|
|
306
|
+
}
|
|
314
307
|
|
|
315
308
|
Util.buildArrayObjectPrefix = function (arr) {
|
|
316
|
-
|
|
317
|
-
}
|
|
309
|
+
return Util.modulesSwitch(arr)
|
|
310
|
+
}
|
|
318
311
|
|
|
319
312
|
Util.arrayWithObject = (array, key, field) => {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
}
|
|
313
|
+
if (array.length) {
|
|
314
|
+
return array.reduce((obj, item) => {
|
|
315
|
+
obj[item[key]] = item[field]
|
|
316
|
+
return obj
|
|
317
|
+
}, {})
|
|
318
|
+
}
|
|
319
|
+
}
|
|
327
320
|
|
|
328
321
|
/*
|
|
329
322
|
for movedupload file using single file
|
|
330
323
|
*/
|
|
331
324
|
Util.moveFile = function (buffer, filename) {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
325
|
+
return new Promise(function (resolve, reject) {
|
|
326
|
+
buffer.mv(filename, function (err) {
|
|
327
|
+
if (err) {
|
|
328
|
+
reject(err)
|
|
329
|
+
} else {
|
|
330
|
+
resolve(filename)
|
|
331
|
+
}
|
|
332
|
+
})
|
|
333
|
+
})
|
|
334
|
+
}
|
|
342
335
|
|
|
343
336
|
Util.generateUnique = function (length, charset) {
|
|
344
|
-
|
|
345
|
-
|
|
337
|
+
let random = Util.generate(length, charset)
|
|
338
|
+
let uid = new Date().valueOf().toString(36)
|
|
346
339
|
|
|
347
|
-
|
|
348
|
-
}
|
|
340
|
+
return uid + random
|
|
341
|
+
}
|
|
349
342
|
|
|
350
343
|
Util.generate = function (length, charset) {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
359
|
-
|
|
344
|
+
length = length || 50
|
|
345
|
+
//alphanumeric - [0-9 a-z A-Z] alphabetic - [a-z A-Z] numeric - [0-9] hex - [0-9 a-f] custom - any given characters
|
|
346
|
+
charset = charset || 'alphanumeric'
|
|
347
|
+
return randomstring.generate({
|
|
348
|
+
length: length,
|
|
349
|
+
charset: charset,
|
|
350
|
+
})
|
|
351
|
+
}
|
|
360
352
|
|
|
361
353
|
Util.uuid = () => {
|
|
362
|
-
|
|
363
|
-
}
|
|
354
|
+
return uuidv4()
|
|
355
|
+
}
|
|
364
356
|
/*
|
|
365
357
|
generate random string 8
|
|
366
358
|
*/
|
|
367
359
|
Util.random = function (length) {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
360
|
+
length = length || 5
|
|
361
|
+
let text = ''
|
|
362
|
+
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
363
|
+
for (let i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length))
|
|
373
364
|
|
|
374
|
-
|
|
375
|
-
}
|
|
365
|
+
return text
|
|
366
|
+
}
|
|
376
367
|
|
|
377
368
|
Util.whitelist = function () {
|
|
378
|
-
|
|
379
|
-
}
|
|
369
|
+
return ['www', 'app', 'my', 'sintret', 'https', 'https']
|
|
370
|
+
}
|
|
380
371
|
|
|
381
372
|
Util.convertDate = function (d) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
373
|
+
d = d.trim()
|
|
374
|
+
let myarr = d.split(' ')
|
|
375
|
+
return myarr[2] + '-' + Util.monthConvert(myarr[1]) + '-' + myarr[0]
|
|
376
|
+
}
|
|
386
377
|
|
|
387
378
|
// get string start from to
|
|
388
379
|
Util.cut = function (text, start, end) {
|
|
389
|
-
|
|
390
|
-
}
|
|
380
|
+
return text.substr(start, end)
|
|
381
|
+
}
|
|
391
382
|
|
|
392
383
|
Util.getFormattedDate = function (date) {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
}
|
|
384
|
+
let year = date.getFullYear()
|
|
385
|
+
let month = (1 + date.getMonth()).toString()
|
|
386
|
+
month = month.length > 1 ? month : '0' + month
|
|
387
|
+
let day = date.getDate().toString()
|
|
388
|
+
day = day.length > 1 ? day : '0' + day
|
|
389
|
+
let time = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
|
|
390
|
+
return year + '-' + month + '-' + day + ' ' + time
|
|
391
|
+
}
|
|
401
392
|
|
|
402
393
|
Util.uniqueId = function () {
|
|
403
|
-
|
|
404
|
-
}
|
|
394
|
+
return (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase()
|
|
395
|
+
}
|
|
405
396
|
|
|
406
397
|
Util.typePaperSize = [
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
398
|
+
{ title: 'F4', description: 'FOLIO', width: 215, height: 330 },
|
|
399
|
+
{ title: 'LEGAL', description: 'LEGAL', width: 216, height: 356 },
|
|
400
|
+
{ title: 'LETTER', description: 'LETTER', width: 216, height: 279 },
|
|
401
|
+
{ title: 'A3', description: 'A3', width: 297, height: 420 },
|
|
402
|
+
{ title: 'A4', description: 'A4', width: 210, height: 297 },
|
|
403
|
+
{ title: 'A5', description: 'A5', width: 148, height: 210 },
|
|
404
|
+
{ title: 'A6', description: 'A6', width: 105, height: 148 },
|
|
405
|
+
{ title: 'A7', description: 'A7', width: 74, height: 105 },
|
|
406
|
+
{ title: 'A8', description: 'A8', width: 52, height: 74 },
|
|
407
|
+
{ title: 'A9', description: 'A9', width: 37, height: 52 },
|
|
408
|
+
{ title: 'CUSTOM', description: 'CUSTOM', width: 105, height: 148 },
|
|
418
409
|
]
|
|
419
410
|
|
|
420
411
|
Util.typeFont = [
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
412
|
+
'Verdana, Geneva, sans-serif',
|
|
413
|
+
'"Times New Roman", Times, serif',
|
|
414
|
+
'Georgia, serif',
|
|
415
|
+
'"Palatino Linotype", "Book Antiqua", Palatino, serif',
|
|
416
|
+
'Arial, Helvetica, sans-serif',
|
|
417
|
+
'"Arial Black", Gadget, sans-serif',
|
|
418
|
+
'"Comic Sans MS", cursive, sans-serif',
|
|
419
|
+
'Impact, Charcoal, sans-serif',
|
|
420
|
+
'"Lucida Sans Unicode", "Lucida Grande", sans-serif',
|
|
421
|
+
'Tahoma, Geneva, sans-serif',
|
|
422
|
+
'"Trebuchet MS", Helvetica, sans-serif',
|
|
423
|
+
'"Courier New", Courier, monospace',
|
|
424
|
+
'"Lucida Console", Monaco, monospace',
|
|
434
425
|
]
|
|
435
426
|
|
|
436
427
|
Util.objectToGridFormat = function (obj, isInteger) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
}
|
|
428
|
+
isInteger = isInteger || false
|
|
429
|
+
let arr = []
|
|
430
|
+
arr.push({ id: '', name: '' })
|
|
431
|
+
for (let keys in obj) {
|
|
432
|
+
if (isInteger) {
|
|
433
|
+
arr.push({ id: parseInt(keys), name: obj[keys] })
|
|
434
|
+
} else {
|
|
435
|
+
arr.push({ id: keys, name: obj[keys] })
|
|
446
436
|
}
|
|
447
|
-
|
|
437
|
+
}
|
|
438
|
+
return arr
|
|
448
439
|
}
|
|
449
440
|
|
|
450
441
|
Util.random = function (length) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
442
|
+
length = length || 5
|
|
443
|
+
let text = ''
|
|
444
|
+
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
445
|
+
for (let i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length))
|
|
456
446
|
|
|
457
|
-
|
|
447
|
+
return text
|
|
458
448
|
}
|
|
459
449
|
|
|
460
450
|
Util.typePrint = {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
}
|
|
451
|
+
register: '{"paper_size":"F4","paper_size_width":"215","paper_size_height":"330","padding_top":"8","padding_right":"8","padding_bottom":"8","padding_left":"8","border":"1","font":"0","font_size":"10","header":"SURAT PERINTAH KERJA","header_font":"0","header_font_size":"26"}',
|
|
452
|
+
estimation: '{"paper_size":"F4","paper_size_width":"215","paper_size_height":"330","padding_top":"8","padding_right":"8","padding_bottom":"8","padding_left":"8","border":"1","font":"0","font_size":"12","header":"ESTIMASI BIAYA PERBAIKAN","header_font":"0","header_font_size":"18"}',
|
|
453
|
+
invoice: '{"paper_size":"A5","paper_size_width":"148","paper_size_height":"210","padding_top":"8","padding_right":"8","padding_bottom":"8","padding_left":"8","border":"1","font":"0","font_size":"12","header":"INVOICE","header_font":"0","header_font_size":"18"}',
|
|
454
|
+
currency: '{"symbol":"Rp","name":"Rupiah","thousand":"."}',
|
|
455
|
+
}
|
|
466
456
|
|
|
467
457
|
Util.isJson = function (text) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
458
|
+
if (text) {
|
|
459
|
+
if (
|
|
460
|
+
/^[\],:{}\s]*$/.test(
|
|
461
|
+
text
|
|
462
|
+
.replace(/\\["\\\/bfnrtu]/g, '@')
|
|
463
|
+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
|
|
464
|
+
.replace(/(?:^|:|,)(?:\s*\[)+/g, '')
|
|
465
|
+
)
|
|
466
|
+
) {
|
|
467
|
+
//the json is ok
|
|
468
|
+
return true
|
|
469
|
+
} else {
|
|
470
|
+
return false
|
|
471
|
+
//the json is not ok
|
|
476
472
|
}
|
|
477
|
-
|
|
478
|
-
|
|
473
|
+
}
|
|
474
|
+
return false
|
|
475
|
+
}
|
|
479
476
|
|
|
480
477
|
Util.isEmptyObject = function (obj) {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
}
|
|
478
|
+
for (let prop in obj) {
|
|
479
|
+
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
|
|
480
|
+
return false
|
|
485
481
|
}
|
|
482
|
+
}
|
|
486
483
|
|
|
487
|
-
|
|
488
|
-
}
|
|
484
|
+
return JSON.stringify(obj) === JSON.stringify({})
|
|
485
|
+
}
|
|
489
486
|
|
|
490
487
|
Util.serializeTable = function (table) {
|
|
491
|
-
|
|
492
|
-
}
|
|
488
|
+
return '`' + table + '`'
|
|
489
|
+
}
|
|
493
490
|
|
|
494
491
|
Util.getKey = function (obj, field) {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
492
|
+
let t = ''
|
|
493
|
+
for (let item in obj) {
|
|
494
|
+
if (obj[item] == field) {
|
|
495
|
+
return item
|
|
500
496
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
497
|
+
}
|
|
498
|
+
return t
|
|
499
|
+
}
|
|
504
500
|
|
|
505
501
|
Util.camelize = function (text) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
502
|
+
return text.replace(/^([A-Z])|[\s-_]+(\w)/g, function (match, p1, p2, offset) {
|
|
503
|
+
if (p2) return p2.toUpperCase()
|
|
504
|
+
return p1.toLowerCase()
|
|
505
|
+
})
|
|
510
506
|
}
|
|
511
507
|
|
|
512
508
|
Util.decamelize = function (str, separator) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
509
|
+
separator = typeof separator === 'undefined' ? '_' : separator
|
|
510
|
+
return str
|
|
511
|
+
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
|
|
512
|
+
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
|
|
513
|
+
.replace('_', separator)
|
|
514
|
+
.toLowerCase()
|
|
519
515
|
}
|
|
520
516
|
|
|
521
517
|
/*
|
|
@@ -524,19 +520,19 @@ Util.decamelize = function (str, separator) {
|
|
|
524
520
|
*/
|
|
525
521
|
|
|
526
522
|
Util.toName = function (str, separator) {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
}
|
|
534
|
-
let string = str.replace(/\s+/g, separator).toLowerCase();
|
|
535
|
-
string = string.replace("/","");
|
|
536
|
-
string = string.replace("/\/","");
|
|
537
|
-
string = string.replace("__","_");
|
|
538
|
-
return string.replace(/[^A-Za-z0-9/_]/g, "")
|
|
523
|
+
if (str && str.length) {
|
|
524
|
+
separator = separator || '_'
|
|
525
|
+
str = str.trim()
|
|
526
|
+
//add if first character is number with string character
|
|
527
|
+
if (Util.isInteger(str.charAt(0))) {
|
|
528
|
+
str = 'a' + str
|
|
539
529
|
}
|
|
530
|
+
let string = str.replace(/\s+/g, separator).toLowerCase()
|
|
531
|
+
string = string.replace('/', '')
|
|
532
|
+
string = string.replace('//', '')
|
|
533
|
+
string = string.replace('__', '_')
|
|
534
|
+
return string.replace(/[^A-Za-z0-9/_]/g, '')
|
|
535
|
+
}
|
|
540
536
|
}
|
|
541
537
|
|
|
542
538
|
/*
|
|
@@ -544,637 +540,651 @@ Util.toName = function (str, separator) {
|
|
|
544
540
|
to : Order Step
|
|
545
541
|
*/
|
|
546
542
|
Util.fieldToName = function (str) {
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
543
|
+
let title = Util.capitalizeFirstLetter(Util.decamelize(str))
|
|
544
|
+
title = Util.replaceAll(title, '_', ' ')
|
|
545
|
+
title = Util.replaceAll(title, ' id', '')
|
|
546
|
+
title = Util.capitalizeAfterSpace(title)
|
|
547
|
+
|
|
548
|
+
const lastWords = Util.lastWord(title)
|
|
549
|
+
if (title.length > 4 && lastWords == 'Id') {
|
|
550
|
+
title = title.replace('Id', '')
|
|
551
|
+
}
|
|
556
552
|
|
|
557
|
-
|
|
558
|
-
}
|
|
553
|
+
return title
|
|
554
|
+
}
|
|
559
555
|
|
|
560
556
|
Util.capitalizeWord = (string) => {
|
|
561
|
-
|
|
562
|
-
|
|
557
|
+
return string
|
|
558
|
+
.split(' ')
|
|
559
|
+
.map((m) => m[0].toUpperCase() + m.substr(1))
|
|
560
|
+
.join(' ')
|
|
561
|
+
}
|
|
563
562
|
|
|
564
563
|
Util.capitalizeFirstLetter = (string) => {
|
|
565
|
-
|
|
566
|
-
}
|
|
564
|
+
return string.charAt(0).toUpperCase() + string.slice(1)
|
|
565
|
+
}
|
|
567
566
|
|
|
568
567
|
Util.asyncWrap = (fn) => {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
}
|
|
568
|
+
return (req, res, next) => {
|
|
569
|
+
fn(req, res, next).catch(next)
|
|
570
|
+
}
|
|
571
|
+
}
|
|
573
572
|
|
|
574
573
|
Util.capitalizeAfterSpace = function (str) {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
}
|
|
581
|
-
);
|
|
582
|
-
};
|
|
574
|
+
str = Util.replaceAll(str, '_', ' ')
|
|
575
|
+
return str.replace(/\w\S*/g, function (txt) {
|
|
576
|
+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
|
|
577
|
+
})
|
|
578
|
+
}
|
|
583
579
|
|
|
584
580
|
Util.capitalizeAfterSpaceTitle = function (str) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
}
|
|
591
|
-
);
|
|
592
|
-
};
|
|
581
|
+
str = Util.replaceAll(str, ' ', '_')
|
|
582
|
+
return str.replace(/\w\S*/g, function (txt) {
|
|
583
|
+
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
|
|
584
|
+
})
|
|
585
|
+
}
|
|
593
586
|
|
|
594
587
|
Util.lastWord = function (words) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
}
|
|
588
|
+
let n = words.split(' ')
|
|
589
|
+
return n[n.length - 1]
|
|
590
|
+
}
|
|
598
591
|
|
|
599
592
|
Util.arrayUnShift = function (arr) {
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
}
|
|
593
|
+
let obj = {}
|
|
594
|
+
obj[arr[0]] = ''
|
|
595
|
+
obj[arr[1]] = ''
|
|
596
|
+
return obj
|
|
597
|
+
}
|
|
605
598
|
|
|
606
599
|
Util.in_array = function (needle, haystack) {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
}
|
|
600
|
+
haystack = haystack || []
|
|
601
|
+
if (haystack.length && needle) {
|
|
602
|
+
return haystack.includes(needle)
|
|
603
|
+
} else {
|
|
604
|
+
return false
|
|
605
|
+
}
|
|
606
|
+
}
|
|
614
607
|
|
|
615
608
|
Util.gridSearch = function (visibles, relations, name, value) {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
}
|
|
609
|
+
let index = 0
|
|
610
|
+
let elm = 'input'
|
|
611
|
+
for (let i = 0; i < visibles.length; i++) {
|
|
612
|
+
if (name == visibles[i]) {
|
|
613
|
+
index = i
|
|
622
614
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
615
|
+
}
|
|
616
|
+
if (!Util.isEmptyObject(relations)) {
|
|
617
|
+
let arr = Object.keys(relations)
|
|
618
|
+
for (let i = 0; i < arr.length; i++) {
|
|
619
|
+
if (name == arr[i]) {
|
|
620
|
+
elm = 'select'
|
|
621
|
+
}
|
|
630
622
|
}
|
|
631
|
-
|
|
632
|
-
|
|
623
|
+
}
|
|
624
|
+
return 'searchValue.eq(' + index + ').find("' + elm + '").val("' + value + '");'
|
|
625
|
+
}
|
|
633
626
|
|
|
634
627
|
Util.toNumber = function (num) {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
Util.formatNumber = function (num, thousandSeparator='.') {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
}
|
|
628
|
+
num = num + ''
|
|
629
|
+
return parseFloat(Util.replaceAll(num, '.', ''))
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
Util.formatNumber = function (num, thousandSeparator = '.') {
|
|
633
|
+
num = num || ''
|
|
634
|
+
let sep = '$1' + thousandSeparator
|
|
635
|
+
let numString = num.toString()
|
|
636
|
+
if (numString.indexOf('.') > -1) {
|
|
637
|
+
let explode = numString.split('.')
|
|
638
|
+
return explode[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, sep) + ',' + explode[1]
|
|
639
|
+
} else {
|
|
640
|
+
return numString.replace(/(\d)(?=(\d{3})+(?!\d))/g, sep)
|
|
641
|
+
}
|
|
642
|
+
}
|
|
650
643
|
|
|
651
644
|
Util.fileAttribute = function (filename) {
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
}
|
|
645
|
+
filename = filename.toLowerCase() || ''
|
|
646
|
+
let ext = filename.split('.').pop()
|
|
647
|
+
let obj = {}
|
|
648
|
+
obj.ext = ext
|
|
649
|
+
if (Util.in_array(ext, Util.fileImage)) {
|
|
650
|
+
obj.type = 'image'
|
|
651
|
+
} else {
|
|
652
|
+
obj.type = 'file'
|
|
653
|
+
}
|
|
654
|
+
return obj
|
|
655
|
+
}
|
|
663
656
|
|
|
664
|
-
Util.fileImage = ['jpg', 'jpeg', 'png', 'webp', 'bmp', 'tif', 'gif', 'png','svg']
|
|
657
|
+
Util.fileImage = ['jpg', 'jpeg', 'png', 'webp', 'bmp', 'tif', 'gif', 'png', 'svg']
|
|
665
658
|
|
|
666
659
|
Util.fileExtension = (filename) => {
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
Util.fileView = function (dir, file, attributes={}) {
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
660
|
+
filename = filename.toLowerCase() || ''
|
|
661
|
+
let obj = {}
|
|
662
|
+
let ext = filename.split('.').pop()
|
|
663
|
+
obj.ext = ext
|
|
664
|
+
if (Util.in_array(ext, Util.fileImage)) {
|
|
665
|
+
obj.type = 'image'
|
|
666
|
+
} else {
|
|
667
|
+
obj.type = 'file'
|
|
668
|
+
}
|
|
669
|
+
let file = 'file.png'
|
|
670
|
+
if (ext == 'docx' || ext == 'doc') {
|
|
671
|
+
file = 'word.png'
|
|
672
|
+
} else if (ext == 'xls' || ext == 'xlsx') {
|
|
673
|
+
file = 'excel.png'
|
|
674
|
+
} else if (ext == 'pdf') {
|
|
675
|
+
file = 'pdf.png'
|
|
676
|
+
} else if (ext == 'ppt' || ext == 'pptx') {
|
|
677
|
+
file = 'ppt.png'
|
|
678
|
+
} else if (ext == 'txt') {
|
|
679
|
+
file = 'txt.png'
|
|
680
|
+
} else if (ext == 'zip') {
|
|
681
|
+
file = 'zip.jpg'
|
|
682
|
+
} else if (ext == 'rar') {
|
|
683
|
+
file = 'rar.jpg'
|
|
684
|
+
} else if (ext == 'rar') {
|
|
685
|
+
file = 'file.png'
|
|
686
|
+
}
|
|
687
|
+
obj.file = file
|
|
688
|
+
return obj
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
Util.fileView = function (dir, file, attributes = {}) {
|
|
692
|
+
let filename = dir + file
|
|
693
|
+
let html = ''
|
|
694
|
+
let width = attributes.hasOwnProperty('width') ? attributes.width : '300'
|
|
695
|
+
let withIcon = attributes.hasOwnProperty('withIcon') ? true : false
|
|
696
|
+
let obj = Util.fileExtension(filename)
|
|
697
|
+
let className = attributes.hasOwnProperty('class') ? ` class="${attributes.class}" ` : ''
|
|
698
|
+
if (filename.includes('https')) {
|
|
699
|
+
html = `<img src="${file}" ${className} class="img-responsive">`
|
|
700
|
+
} else {
|
|
701
|
+
if (obj.type == 'image') {
|
|
702
|
+
html = `<img src="${filename}" ${className} width="${width}px">`
|
|
707
703
|
} else {
|
|
708
|
-
|
|
709
|
-
|
|
704
|
+
if (file) {
|
|
705
|
+
if (withIcon) {
|
|
706
|
+
html = `<img class="mb-3 boxy-small" src="/img/${obj.file}" height="45px" width="45px"><a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`
|
|
710
707
|
} else {
|
|
711
|
-
|
|
712
|
-
if(withIcon) {
|
|
713
|
-
html = `<img class="mb-3 boxy-small" src="/img/${obj.file}" height="45px" width="45px"><a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`;
|
|
714
|
-
} else {
|
|
715
|
-
html = `<a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
708
|
+
html = `<a class="text-success" target="_blank" href="${filename}"> ${file.substring(13)}</a>`
|
|
718
709
|
}
|
|
710
|
+
}
|
|
719
711
|
}
|
|
712
|
+
}
|
|
720
713
|
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
};
|
|
714
|
+
return html
|
|
715
|
+
}
|
|
724
716
|
/// end files
|
|
725
717
|
|
|
726
|
-
|
|
727
718
|
Util.arrayDelete = function (arr, value) {
|
|
728
|
-
|
|
729
|
-
}
|
|
719
|
+
return arr.filter((item) => item != value)
|
|
720
|
+
}
|
|
730
721
|
|
|
731
722
|
Util.arrayDeletes = function (arr, array) {
|
|
732
|
-
|
|
733
|
-
}
|
|
723
|
+
return arr.filter((item) => !Util.in_array(item, array))
|
|
724
|
+
}
|
|
734
725
|
|
|
735
726
|
Util.arrayToList = function (arr, array, delimiter, isCount) {
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
}
|
|
743
|
-
html = html.slice(0, (delimiter.length * -1))
|
|
727
|
+
delimiter = delimiter || '<br>'
|
|
728
|
+
isCount = isCount || 1
|
|
729
|
+
let html = ''
|
|
730
|
+
if (arr) {
|
|
731
|
+
for (var i = 0; i < arr.length; i++) {
|
|
732
|
+
html += isCount == 1 ? i + 1 + '. ' + array[arr[i]] + delimiter : ' ' + array[arr[i]] + delimiter
|
|
744
733
|
}
|
|
734
|
+
html = html.slice(0, delimiter.length * -1)
|
|
735
|
+
}
|
|
745
736
|
|
|
746
|
-
|
|
747
|
-
}
|
|
737
|
+
return html
|
|
738
|
+
}
|
|
748
739
|
|
|
749
740
|
Util.menuAccess = function (menu, params) {
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
}
|
|
760
|
-
};
|
|
741
|
+
const roles = require('./role')
|
|
742
|
+
const routes = roles.routes
|
|
743
|
+
if (Util.in_array(menu, routes)) {
|
|
744
|
+
if (Util.in_array(menu, params)) return true
|
|
745
|
+
else return false
|
|
746
|
+
} else {
|
|
747
|
+
return true
|
|
748
|
+
}
|
|
749
|
+
}
|
|
761
750
|
|
|
762
751
|
Util.dropdownHelper = function (data, field, model) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
}
|
|
752
|
+
let fieldsx = field + '[]'
|
|
753
|
+
let name = field + '[]'
|
|
754
|
+
let myvalue = typeof data[fieldsx] == undefined ? ' ' : typeof data[fieldsx] == 'string' ? '["' + data[fieldsx] + '"]' : JSON.stringify(data[name])
|
|
755
|
+
if (myvalue) {
|
|
756
|
+
let unique = myvalue.indexOf('[') > -1 ? myvalue : !myvalue ? '' : '[' + myvalue + ']'
|
|
757
|
+
unique = JSON.parse(unique)
|
|
758
|
+
data[field] = JSON.stringify(unique.filter(Util.arrayUnique))
|
|
759
|
+
delete data[name]
|
|
760
|
+
}
|
|
761
|
+
if (model.fields[field].required) {
|
|
762
|
+
if (!data[field]) {
|
|
763
|
+
return false
|
|
776
764
|
}
|
|
777
|
-
|
|
778
|
-
|
|
765
|
+
}
|
|
766
|
+
return data
|
|
767
|
+
}
|
|
779
768
|
|
|
780
769
|
Util.dropdownAdd = function (data, field, model, datas) {
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
770
|
+
let name = field + '[]'
|
|
771
|
+
let myvalue = typeof datas == undefined ? ' ' : typeof datas == 'string' ? '["' + datas + '"]' : JSON.stringify(datas)
|
|
772
|
+
if (myvalue) {
|
|
773
|
+
let unique = myvalue.indexOf('[') > -1 ? myvalue : !myvalue ? '' : '[' + myvalue + ']'
|
|
774
|
+
unique = JSON.parse(unique)
|
|
775
|
+
myvalue = JSON.stringify(unique.filter(Util.arrayUnique))
|
|
776
|
+
delete data[name]
|
|
777
|
+
|
|
778
|
+
data[field] = myvalue
|
|
779
|
+
}
|
|
780
|
+
if (model.fields[field].required) {
|
|
781
|
+
if (!myvalue) {
|
|
782
|
+
return false
|
|
790
783
|
}
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
return data;
|
|
797
|
-
};
|
|
784
|
+
}
|
|
785
|
+
return data
|
|
786
|
+
}
|
|
798
787
|
//array unique
|
|
799
788
|
// array.filter(Util.arrayUnique);
|
|
800
789
|
Util.arrayUnique = (value, index, self) => {
|
|
801
|
-
|
|
802
|
-
}
|
|
790
|
+
return self.indexOf(value) == index
|
|
791
|
+
}
|
|
803
792
|
|
|
804
793
|
Util.virtualHelper = function (obj) {
|
|
805
|
-
|
|
806
|
-
|
|
794
|
+
if (Util.isEmptyObject(obj)) return
|
|
795
|
+
if (obj == undefined) return
|
|
807
796
|
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
797
|
+
var str = ''
|
|
798
|
+
for (var key in obj) {
|
|
799
|
+
str += ', `' + obj[key] + '` AS ' + key
|
|
800
|
+
}
|
|
812
801
|
|
|
813
|
-
|
|
814
|
-
}
|
|
802
|
+
return str
|
|
803
|
+
}
|
|
815
804
|
|
|
816
|
-
Util.nots = ['id', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy', 'companyId', 'created_at', 'updated_at','updated_by', 'created_by', 'modified_by', 'company_id', 'token', 'version', 'signin_method', 'lastLogin', 'last_login', 'forgotPassword', 'forgot_password','no', 'actionColumn']
|
|
805
|
+
Util.nots = ['id', 'createdAt', 'updatedAt', 'createdBy', 'updatedBy', 'companyId', 'created_at', 'updated_at', 'updated_by', 'created_by', 'modified_by', 'company_id', 'token', 'version', 'signin_method', 'lastLogin', 'last_login', 'forgotPassword', 'forgot_password', 'no', 'actionColumn']
|
|
817
806
|
|
|
818
807
|
Util.requiredFields = function (obj) {
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
}
|
|
808
|
+
var nots = Util.nots
|
|
809
|
+
var arr = []
|
|
810
|
+
for (var key in obj) {
|
|
811
|
+
if (!Util.in_array(key, nots)) {
|
|
812
|
+
if (obj[key].required == true) {
|
|
813
|
+
arr.push(key)
|
|
814
|
+
}
|
|
827
815
|
}
|
|
828
|
-
|
|
829
|
-
|
|
816
|
+
}
|
|
817
|
+
return arr
|
|
818
|
+
}
|
|
830
819
|
|
|
831
820
|
Util.extractDetails = function (obj) {
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
}
|
|
821
|
+
let arr = []
|
|
822
|
+
for (let key in obj) {
|
|
823
|
+
if (obj[key].length > 0) {
|
|
824
|
+
for (var i = 0; i < obj[key].length; i++) {
|
|
825
|
+
arr.push(obj[key][i])
|
|
826
|
+
}
|
|
839
827
|
}
|
|
840
|
-
|
|
841
|
-
|
|
828
|
+
}
|
|
829
|
+
return arr
|
|
830
|
+
}
|
|
842
831
|
|
|
843
832
|
Util.arrayToConcat = function (arr) {
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
}
|
|
833
|
+
let str = 'CONCAT('
|
|
834
|
+
for (let i = 0; i < arr.length; i++) {
|
|
835
|
+
str += arr[i] + '," - ",'
|
|
836
|
+
}
|
|
837
|
+
str = str.slice(0, -9)
|
|
838
|
+
str += ')'
|
|
839
|
+
|
|
840
|
+
return str
|
|
841
|
+
}
|
|
853
842
|
|
|
854
843
|
//sequence all fields based on drag drop generator
|
|
855
844
|
Util.arraySequence = function (arr, left, right) {
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
845
|
+
left = left || []
|
|
846
|
+
right = right || []
|
|
847
|
+
let obj = Util.arrayToObject(arr, 'Field')
|
|
848
|
+
let temp = [],
|
|
849
|
+
stores = []
|
|
850
|
+
for (let i = 0; i < arr.length; i++) {
|
|
851
|
+
if (arr[i].Field == 'id') {
|
|
852
|
+
stores.push(arr[i].Field)
|
|
853
|
+
temp.push(arr[i])
|
|
865
854
|
}
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
855
|
+
}
|
|
856
|
+
if (Array.isArray(left)) {
|
|
857
|
+
for (let i = 0; i < left.length; i++) {
|
|
858
|
+
if (i % 2 == 0) {
|
|
859
|
+
temp.push(obj[left[i]])
|
|
860
|
+
stores.push(left[i])
|
|
861
|
+
}
|
|
873
862
|
}
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
863
|
+
}
|
|
864
|
+
if (Array.isArray(right)) {
|
|
865
|
+
for (let i = 0; i < right.length; i++) {
|
|
866
|
+
if (i % 2 == 0) {
|
|
867
|
+
temp.push(obj[right[i]])
|
|
868
|
+
stores.push(right[i])
|
|
869
|
+
}
|
|
881
870
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
871
|
+
}
|
|
872
|
+
if (Array.isArray(left)) {
|
|
873
|
+
for (let i = 0; i < left.length; i++) {
|
|
874
|
+
if (i % 2 == 1) {
|
|
875
|
+
temp.push(obj[left[i]])
|
|
876
|
+
stores.push(left[i])
|
|
877
|
+
}
|
|
889
878
|
}
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
879
|
+
}
|
|
880
|
+
if (Array.isArray(right)) {
|
|
881
|
+
for (let i = 0; i < right.length; i++) {
|
|
882
|
+
if (i % 2 == 1) {
|
|
883
|
+
temp.push(obj[right[i]])
|
|
884
|
+
stores.push(right[i])
|
|
885
|
+
}
|
|
897
886
|
}
|
|
887
|
+
}
|
|
898
888
|
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
}
|
|
889
|
+
for (let i = 0; i < arr.length; i++) {
|
|
890
|
+
const field = arr[i].Field
|
|
891
|
+
if (!Util.in_array(field, stores)) {
|
|
892
|
+
temp.push(arr[i])
|
|
904
893
|
}
|
|
894
|
+
}
|
|
905
895
|
|
|
906
|
-
|
|
907
|
-
}
|
|
896
|
+
return temp
|
|
897
|
+
}
|
|
908
898
|
|
|
909
899
|
Util.isInteger = (value) => {
|
|
910
|
-
|
|
911
|
-
|
|
900
|
+
return (
|
|
901
|
+
!isNaN(value) &&
|
|
902
|
+
(function (x) {
|
|
903
|
+
return (x | 0) === x
|
|
912
904
|
})(parseFloat(value))
|
|
913
|
-
|
|
905
|
+
)
|
|
906
|
+
}
|
|
914
907
|
|
|
915
908
|
Util.isNumeric = function (value) {
|
|
916
|
-
|
|
917
|
-
}
|
|
909
|
+
return /^-?\d+$/.test(value)
|
|
910
|
+
}
|
|
918
911
|
|
|
919
912
|
Util.tags = function (tags) {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
}
|
|
927
|
-
} else {
|
|
928
|
-
html += `<a href="#" rel="tag">${tags}</a>`;
|
|
913
|
+
let html = ''
|
|
914
|
+
tags = tags || ''
|
|
915
|
+
if (tags.indexOf(',') > -1) {
|
|
916
|
+
let explode = tags.split(',')
|
|
917
|
+
for (var i = 0; i < explode.length; i++) {
|
|
918
|
+
html += `<a href="#" rel="tag">${explode[i]}</a>`
|
|
929
919
|
}
|
|
920
|
+
} else {
|
|
921
|
+
html += `<a href="#" rel="tag">${tags}</a>`
|
|
922
|
+
}
|
|
930
923
|
|
|
931
|
-
|
|
932
|
-
}
|
|
924
|
+
return html
|
|
925
|
+
}
|
|
933
926
|
|
|
934
927
|
/*
|
|
935
928
|
get extension of filename
|
|
936
929
|
*/
|
|
937
930
|
|
|
938
931
|
Util.getExtensionFile = (str) => {
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
}
|
|
932
|
+
str = str || ''
|
|
933
|
+
let extension = str.split('.').pop()
|
|
934
|
+
extension = extension.toLowerCase()
|
|
935
|
+
let ret = extension
|
|
936
|
+
if (extension == 'jpg') {
|
|
937
|
+
ret = 'jpeg'
|
|
938
|
+
}
|
|
939
|
+
return ret
|
|
940
|
+
}
|
|
948
941
|
|
|
949
942
|
Util.badgeError = (msg) => {
|
|
950
|
-
|
|
951
|
-
}
|
|
943
|
+
return `<span class="badge badge-danger">${msg}</span>`
|
|
944
|
+
}
|
|
952
945
|
|
|
953
946
|
Util.badgeSuccess = (msg) => {
|
|
954
|
-
|
|
955
|
-
}
|
|
947
|
+
return `<span class="badge badge-success">${msg}</span>`
|
|
948
|
+
}
|
|
956
949
|
|
|
957
950
|
Util.alertError = function (msg) {
|
|
958
|
-
|
|
959
|
-
}
|
|
951
|
+
return `<div class="alert alert-danger" role="alert">${msg}</div>`
|
|
952
|
+
}
|
|
960
953
|
|
|
961
954
|
Util.alertSuccess = function (msg) {
|
|
962
|
-
|
|
963
|
-
}
|
|
955
|
+
return `<div class="alert alert-success" role="alert">${msg}</div>`
|
|
956
|
+
}
|
|
964
957
|
|
|
965
958
|
Util.alertInfo = function (msg) {
|
|
966
|
-
|
|
967
|
-
}
|
|
959
|
+
return `<div class="alert alert-info" role="alert">${msg}</div>`
|
|
960
|
+
}
|
|
968
961
|
|
|
969
962
|
Util.regexPassword = (lengthMin, lengthMax) => {
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
963
|
+
//minimum length
|
|
964
|
+
lengthMin = lengthMin || 6
|
|
965
|
+
lengthMax = lengthMax || 20
|
|
973
966
|
|
|
974
|
-
|
|
975
|
-
}
|
|
967
|
+
return new RegExp('^[a-zA-Z0-9_-]{' + lengthMin + ',' + lengthMax + '}$', 'i')
|
|
968
|
+
}
|
|
976
969
|
|
|
977
970
|
//huruf dan angka saja
|
|
978
971
|
Util.regexCode = (lengthMin, lengthMax) => {
|
|
979
|
-
|
|
980
|
-
|
|
972
|
+
lengthMin = lengthMin || 2
|
|
973
|
+
lengthMax = lengthMax || 10
|
|
981
974
|
|
|
982
|
-
|
|
983
|
-
}
|
|
975
|
+
return new RegExp('^[A-Z0-9]{' + lengthMin + ',' + lengthMax + '}$', 'i')
|
|
976
|
+
}
|
|
984
977
|
|
|
985
978
|
Util.imageProfile = function (image) {
|
|
986
|
-
|
|
987
|
-
}
|
|
979
|
+
return image ? (image.indexOf('http') > -1 ? image : '/uploads/user/' + image) : '/img/user.png'
|
|
980
|
+
}
|
|
988
981
|
|
|
989
982
|
/*
|
|
990
983
|
Files
|
|
991
984
|
*/
|
|
992
985
|
Util.readFile = function (filename) {
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
}
|
|
998
|
-
} catch (err) {
|
|
986
|
+
try {
|
|
987
|
+
if (Util.fileExist(filename)) {
|
|
988
|
+
const data = fs.readFileSync(filename, 'utf8')
|
|
989
|
+
return data
|
|
999
990
|
}
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
}
|
|
991
|
+
} catch (err) {}
|
|
992
|
+
return ''
|
|
993
|
+
}
|
|
1003
994
|
//check directory if not exist create or not return true/false
|
|
1004
|
-
Util.dirExist = (dir, create = false) =>{
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
return true;
|
|
1013
|
-
}
|
|
1014
|
-
} catch (e) {
|
|
995
|
+
Util.dirExist = (dir, create = false) => {
|
|
996
|
+
try {
|
|
997
|
+
if (create) {
|
|
998
|
+
fs.ensureDir(dir, (err) => {})
|
|
999
|
+
}
|
|
1000
|
+
// check if directory exists
|
|
1001
|
+
if (fs.existsSync(dir)) {
|
|
1002
|
+
return true
|
|
1015
1003
|
}
|
|
1016
|
-
|
|
1017
|
-
|
|
1004
|
+
} catch (e) {}
|
|
1005
|
+
return false
|
|
1006
|
+
}
|
|
1018
1007
|
|
|
1019
1008
|
Util.fileExist = (filename) => {
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
}
|
|
1009
|
+
if (fs.existsSync(filename)) {
|
|
1010
|
+
return true
|
|
1011
|
+
}
|
|
1012
|
+
return false
|
|
1013
|
+
}
|
|
1025
1014
|
|
|
1026
1015
|
Util.getAllFiles = (dir) => {
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
}
|
|
1032
|
-
} catch (e) {
|
|
1033
|
-
return [];
|
|
1016
|
+
let files = []
|
|
1017
|
+
try {
|
|
1018
|
+
if (Util.dirExist(dir, true)) {
|
|
1019
|
+
files = fs.readdirSync(dir)
|
|
1034
1020
|
}
|
|
1035
|
-
|
|
1036
|
-
|
|
1021
|
+
} catch (e) {
|
|
1022
|
+
return []
|
|
1023
|
+
}
|
|
1024
|
+
return files
|
|
1025
|
+
}
|
|
1037
1026
|
|
|
1038
1027
|
Util.writeFile = (filename, content) => {
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
}
|
|
1028
|
+
try {
|
|
1029
|
+
let dir = require('path').dirname(filename)
|
|
1030
|
+
Util.dirExist(dir, true)
|
|
1031
|
+
fs.writeFileSync(filename, content)
|
|
1032
|
+
return true
|
|
1033
|
+
} catch (e) {
|
|
1034
|
+
return false
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1048
1037
|
|
|
1049
1038
|
Util.deleteAllFiles = (dir) => {
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
Util.findFilesName = (arr,filename) => {
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
Util.getFiles = function (dir, token =
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1039
|
+
try {
|
|
1040
|
+
fs.emptyDirSync(dir)
|
|
1041
|
+
return true
|
|
1042
|
+
} catch (e) {
|
|
1043
|
+
return false
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
Util.findFilesName = (arr, filename) => {
|
|
1048
|
+
arr = arr || []
|
|
1049
|
+
return arr.filter((item) => item.includes(filename))
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
Util.getFiles = function (dir, token = '') {
|
|
1053
|
+
let arr = fs.readdirSync(dir)
|
|
1054
|
+
let folders = ''
|
|
1055
|
+
let files = ''
|
|
1056
|
+
arr.forEach(function (item) {
|
|
1057
|
+
if (item.indexOf('.') > -1) {
|
|
1058
|
+
var explode = dir.split('public/')
|
|
1059
|
+
var path = explode[1]
|
|
1060
|
+
var url = '/' + path + '/' + item
|
|
1061
|
+
var extension = item.split('.').pop()
|
|
1062
|
+
files += ` <div class="folder data-file ui-draggable ui-draggable-handle ui-selectee" data-toggle="tooltip" data-type="file" data-url="${url}" data-extension="${extension}" data-name="${item}" filename="${item}" data-original-title="${item}">
|
|
1074
1063
|
<img src="/assets/images/formats/file.png" class="img-responsive ui-selectee">
|
|
1075
1064
|
<p class="text-ellipsis ui-selectee">${item}</p>
|
|
1076
|
-
</div
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1065
|
+
</div>`
|
|
1066
|
+
} else {
|
|
1067
|
+
let explode = dir.split(token)
|
|
1068
|
+
let path = explode[1] || ''
|
|
1069
|
+
let state = ''
|
|
1070
|
+
if (path == '') {
|
|
1071
|
+
state = '/' + item
|
|
1072
|
+
} else {
|
|
1073
|
+
state = path.replace(item, '') + '/' + item
|
|
1074
|
+
}
|
|
1075
|
+
folders += `<div class="folder data-folder ui-draggable ui-draggable-handle ui-droppable ui-selectee" data-toggle="tooltip" data-type="folder" data-state="${state}" data-name="${item}" data-original-title="${item}">
|
|
1087
1076
|
<img src="/assets/images/folder.png" class="img-responsive ui-selectee">
|
|
1088
1077
|
<p class="text-ellipsis ui-selectee">${item}</p>
|
|
1089
|
-
</div
|
|
1090
|
-
|
|
1091
|
-
|
|
1078
|
+
</div>`
|
|
1079
|
+
}
|
|
1080
|
+
})
|
|
1092
1081
|
|
|
1093
|
-
|
|
1094
|
-
}
|
|
1082
|
+
return folders + files
|
|
1083
|
+
}
|
|
1095
1084
|
|
|
1096
|
-
Util.ejsOpen =
|
|
1097
|
-
Util.ejsStart =
|
|
1098
|
-
Util.ejsClose =
|
|
1085
|
+
Util.ejsOpen = '<%- '
|
|
1086
|
+
Util.ejsStart = '<% '
|
|
1087
|
+
Util.ejsClose = ' %>'
|
|
1099
1088
|
Util.ejsFunction = (yourCode, isStatement = false) => {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
}
|
|
1089
|
+
const open = isStatement ? Util.ejsStart : Util.ejsOpen
|
|
1090
|
+
return open + yourCode + Util.ejsClose
|
|
1091
|
+
}
|
|
1103
1092
|
|
|
1104
1093
|
Util.attributeOptions = (obj, defaultObj = {}) => {
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
}
|
|
1113
|
-
html += ` ${key}="${value}" `;
|
|
1114
|
-
}
|
|
1115
|
-
if(arr.length){
|
|
1116
|
-
arr.map(item => html += ` ${item}="${defaultObj[item]}" `)
|
|
1094
|
+
let html = ''
|
|
1095
|
+
let arr = Object.keys(defaultObj) || []
|
|
1096
|
+
for (const key in obj) {
|
|
1097
|
+
let value = obj[key]
|
|
1098
|
+
if (defaultObj.hasOwnProperty(key)) {
|
|
1099
|
+
value = defaultObj[key] + obj[key]
|
|
1100
|
+
Util.arrayDelete(arr, key)
|
|
1117
1101
|
}
|
|
1118
|
-
|
|
1119
|
-
}
|
|
1102
|
+
html += ` ${key}="${value}" `
|
|
1103
|
+
}
|
|
1104
|
+
if (arr.length) {
|
|
1105
|
+
arr.map((item) => (html += ` ${item}="${defaultObj[item]}" `))
|
|
1106
|
+
}
|
|
1107
|
+
return html
|
|
1108
|
+
}
|
|
1120
1109
|
|
|
1121
1110
|
Util.userAvatar = (img) => {
|
|
1122
|
-
|
|
1123
|
-
}
|
|
1111
|
+
return img ? (img.includes('http') ? img : `/uploads/user/${img}`) : `/img/user.png`
|
|
1112
|
+
}
|
|
1124
1113
|
|
|
1125
1114
|
/*
|
|
1126
1115
|
MYSQL HELPER
|
|
1127
1116
|
*/
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
if(relations.hasOwnProperty("zvirtuals")){
|
|
1135
|
-
virtuals = relations.zvirtuals;
|
|
1136
|
-
delete relations.zvirtuals;
|
|
1137
|
-
virtualArray = Object.keys(virtuals);
|
|
1138
|
-
virtualArray.forEach(function (item) {
|
|
1139
|
-
fields = Util.arrayDelete(fields,item);
|
|
1140
|
-
});
|
|
1117
|
+
Util.selectParser = (fields, MYMODEL) => {
|
|
1118
|
+
fields = fields.filter((e) => e !== 'actionColumn')
|
|
1119
|
+
let virtuals = []
|
|
1120
|
+
for (var key in MYMODEL.widgets) {
|
|
1121
|
+
if (MYMODEL.widgets[key].name === 'virtual') {
|
|
1122
|
+
virtuals.push(key)
|
|
1141
1123
|
}
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1124
|
+
}
|
|
1125
|
+
let arr = []
|
|
1126
|
+
fields.forEach((item) => {
|
|
1127
|
+
if (virtuals.includes(item)) {
|
|
1128
|
+
arr.push(MYMODEL.widgets[item].fields)
|
|
1129
|
+
} else if (item === 'no') {
|
|
1130
|
+
arr.push('id')
|
|
1131
|
+
} else {
|
|
1132
|
+
arr.push(item)
|
|
1133
|
+
}
|
|
1134
|
+
})
|
|
1135
|
+
|
|
1136
|
+
return `"${arr.join('","')}"`
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
Util.selectMysql = (fields, relations = {}) => {
|
|
1140
|
+
let obj = {}
|
|
1141
|
+
let arr = []
|
|
1142
|
+
let virtuals = {}
|
|
1143
|
+
let virtualArray = []
|
|
1144
|
+
if (relations.hasOwnProperty('zvirtuals')) {
|
|
1145
|
+
virtuals = relations.zvirtuals
|
|
1146
|
+
delete relations.zvirtuals
|
|
1147
|
+
virtualArray = Object.keys(virtuals)
|
|
1148
|
+
virtualArray.forEach(function (item) {
|
|
1149
|
+
fields = Util.arrayDelete(fields, item)
|
|
1152
1150
|
})
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1151
|
+
}
|
|
1152
|
+
let selects = []
|
|
1153
|
+
fields = fields || []
|
|
1154
|
+
fields.forEach(function (item) {
|
|
1155
|
+
if (item != 'actionColumn') {
|
|
1156
|
+
if (item == 'no') {
|
|
1157
|
+
selects.push('id')
|
|
1158
|
+
} else {
|
|
1159
|
+
selects.push(item)
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
})
|
|
1163
|
+
//make sure id
|
|
1164
|
+
selects.push('id')
|
|
1165
|
+
let select = `"${selects.join('","')}"`
|
|
1166
|
+
if (virtualArray.length) {
|
|
1167
|
+
for (let key in virtuals) {
|
|
1168
|
+
select += `, ${virtuals[key]} `
|
|
1160
1169
|
}
|
|
1161
|
-
|
|
1162
|
-
|
|
1170
|
+
}
|
|
1171
|
+
return select
|
|
1172
|
+
}
|
|
1163
1173
|
|
|
1164
1174
|
/*
|
|
1165
1175
|
Sorting array object with key name
|
|
1166
1176
|
*/
|
|
1167
|
-
Util.sortArray = (arr,key) => {
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
}
|
|
1175
|
-
return 0;
|
|
1177
|
+
Util.sortArray = (arr, key) => {
|
|
1178
|
+
function compare(a, b) {
|
|
1179
|
+
if (a[key] < b[key]) {
|
|
1180
|
+
return -1
|
|
1181
|
+
}
|
|
1182
|
+
if (a[key] > b[key]) {
|
|
1183
|
+
return 1
|
|
1176
1184
|
}
|
|
1177
|
-
return
|
|
1178
|
-
}
|
|
1185
|
+
return 0
|
|
1186
|
+
}
|
|
1187
|
+
return arr.sort(compare)
|
|
1188
|
+
}
|
|
1179
1189
|
|
|
1180
|
-
module.exports = Util
|
|
1190
|
+
module.exports = Util
|