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