forked from tickbh/VisualUIEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
535 lines (482 loc) · 12.1 KB
/
util.js
File metadata and controls
535 lines (482 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
var fs = require('fs')
var Path = require('path')
function gen_uuid() {
var s = []
var hexDigits = '0123456789abcdef'
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '-'
var uuid = s.join('')
return uuid
}
function print_func(o) {
var result = ''
var type = typeof o
switch (type) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
break
case 'function':
result += o
break
case 'object':
default:
result += '['
for (var key in o) {
if (typeof o[key] == 'function') {
result += key + ', '
}
}
result += ']'
}
console.log(result)
return result
}
function print_keys(o) {
var result = ''
var type = typeof o
switch (type) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
break
case 'function':
result += o
break
case 'object':
default:
result += '['
for (var key in o) {
result += key + ', '
}
result += ']'
}
console.log(result)
return result
}
function print_r(o, depth) {
var result = ''
depth || (depth = 1)
if (depth > 3) {
return result
}
var indent = new Array(4 * depth + 1).join(' ')
var indentNext = new Array(4 * (depth + 1) + 1).join(' ')
var indentNextTwo = new Array(4 * (depth + 2) + 1).join(' ')
var tmp = ''
var type = typeof o
switch (type) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
case 'function':
tmp += indent + indentNext + o + '\n'
break
case 'object':
default:
for (var key in o) {
tmp += indentNextTwo + '[' + key + '] = '
tmp += print_r(o[key], (depth + 1))
}
}
result += type + '\n'
result += indentNext + '(' + '\n'
result += tmp
result += indentNext + ')' + '\n'
console.log(result)
return result
}
function getFileName(path) {
var result = path
var index = 0
for (var i = path.length - 2; i >= 0; i--) {
if (path.charAt(i) == '\\' || path.charAt(i) == '/') {
index = Math.min(i + 1, path.length - 1)
break
}
}
return path.substring(index)
}
// 遍历文件夹,获取所有文件夹里面的文件信息
/*
* @param path 路径
*
*/
function getFileList(path, filter) {
var filesList = []
var childList = []
filesList.push({
name: getFileName(path),
isDirectory: true,
children: childList,
path: path
})
readFile(path, childList, filter)
return filesList
}
// 遍历读取文件
function readFile(path, filesList, filter) {
files = fs.readdirSync(path); // 需要用到同步读取
files.forEach(walk)
function walk(file) {
if (filter && filter(file)) {
return
}
states = fs.statSync(path + '/' + file)
if (states.isDirectory()) {
var childList = []
readFile(path + '/' + file, childList, filter)
filesList.push({
name: file,
isDirectory: true,
children: childList,
path: path + '/' + file
})
} else {
filesList.push({
name: file,
isDirectory: false,
path: path + '/' + file
})
}
}
}
isSelfOrAncient = function(node, parentNode) {
let parent = node
while (parent) {
if (parent === parentNode) {
return true
}
parent = parent.getParent()
}
return false
}
/**
*判断str1字符串是否以str2为结束
*@param str1 原字符串
*@param str2 子串
*@author pansen
*@return 是-true,否-false
*/
function endWith(str1, str2) {
if (str1 == null || str2 == null) {
return false
}
if (str1.length < str2.length) {
return false
} else if (str1 == str2) {
return true
} else if (str1.substring(str1.length - str2.length) == str2) {
return true
}
return false
}
/**
*判断str1字符串是否以str2为开头
*@param str1 原字符串
*@param str2 子串
*@author pansen
*@return 是-true,否-false
*/
function startWith(str1, str2) {
if (str1 == null || str2 == null) {
return false
}
if (str1.length < str2.length) {
return false
} else if (str1 == str2) {
return true
} else if (str1.substr(0, str2.length) == str2) {
return true
}
return false
}
function getParentDir(path) {
let ret = Path.parse(path)
return ret.dir
}
function deleteFolderRecursive(path) {
if (!fs.existsSync(path)) {
return
}
if (!fs.statSync(path).isDirectory()) {
return fs.unlinkSync(path)
}
var files = []
files = fs.readdirSync(path)
files.forEach(function(file, index) {
var curPath = path + '/' + file
if (fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath)
} else { // delete file
fs.unlinkSync(curPath)
}
})
fs.rmdirSync(path)
}
function getCanUseFolder(path) {
let prefix = 'UIFolder'
let name = ''
for (var i = 0; i < 100000; i++) {
name = prefix + ' ' + i
if (!fs.existsSync(path + '/' + name)) {
break
}
}
return {
name: name,
isDirectory: true,
children: [],
path: path + '/' + name
}
}
function getCanUseFile(path) {
let prefix = 'UIFile'
let suffix = '.ui'
let name = ''
for (var i = 0; i < 100000; i++) {
name = prefix + ' ' + i + suffix
if (!fs.existsSync(path + '/' + name)) {
break
}
}
return {
name: name,
isDirectory: false,
children: [],
path: path + '/' + name
}
}
function isNum(value) {
return typeof value == 'number'
}
function isNull(value) {
return value === null || value === undefined
}
function isValue(value) {
return !isNull(value)
}
function fixFloatValue(value, precision) {
return parseFloat(parseFloat(value).toFixed(precision || 0))
}
function merge(src, t) {
if (typeof src != 'object' || typeof t != 'object') {
return src
}
for (k in t) {
if (t[k] !== null && t[k] !== undefined) {
src[k] = t[k]
}
}
return src
}
function dup(t) {
if (typeof t != 'object') {
return t
}
let src = {}
for (k in t) {
src[k] = t[k]
}
return src
}
function isCtrlKey(event) {
return event.ctrlKey || event.metaKey
}
// 动态插入script标签
function createScript(url, callback) {
var oScript = document.createElement('script')
oScript.type = 'text/javascript'
oScript.async = true
oScript.src = url
/*
** script标签的onload和onreadystatechange事件
** IE6/7/8支持onreadystatechange事件
** IE9/10支持onreadystatechange和onload事件
** Firefox/Chrome/Opera支持onload事件
*/
// 判断IE8及以下浏览器
var isIE = !-[1]
if (isIE) {
alert('IE')
oScript.onreadystatechange = function() {
if (this.readyState == 'loaded' || this.readyState == 'complete') {
callback()
}
}
} else {
// IE9及以上浏览器,Firefox,Chrome,Opera
oScript.onload = function() {
callback()
}
}
document.body.appendChild(oScript)
}
function AddLinkToScripte(url, callback) {
if (!fs.existsSync(url)) {
callback()
return
}
var head = document.getElementsByTagName('head')[0],
linkTag = document.createElement('link')
linkTag.id = 'dynamic-style'
linkTag.href = url
linkTag.setAttribute('rel', 'import')
linkTag.onload = linkTag.readystatechange = function() {
callback()
}
head.appendChild(linkTag)
}
function GetFileContent(file) {
try {
let content = fs.readFileSync(file)
return content
} catch (e) {
return ''
}
}
function GetFileToData(file) {
try {
let content = fs.readFileSync(file)
return JSON.parse(content || '{}')
} catch (e) {
return {}
}
}
var configFile = 'ProgramConfig.json'
var cacheFile = 'CahceConfig.json'
var cacheConfigData = {}
var cacheReadTime = 0
function GetConfigData(isDirect) {
if (!isDirect && !window.projectFolder) {
return {}
}
let file = isDirect ? cacheFile : window.projectFolder + '/' + configFile
if (!fs.existsSync(file)) {
return {}
}
if (isDirect) {
return GetFileToData(file)
}
var stat = fs.statSync(file)
if (stat.mtime.getTime() == cacheReadTime) {
return cacheConfigData
}
cacheConfigData = GetFileToData(file)
cacheReadTime = stat.mtime.getTime()
return cacheConfigData
}
function AddOrModifyConfig(key, value, isDirect) {
if (!isDirect && !window.projectFolder) {
return {}
}
let data = GetConfigData(isDirect)
data[key] = value
let file = isDirect ? cacheFile : window.projectFolder + '/' + configFile
fs.writeFileSync(file, JSON.stringify(data, null, 4))
}
function GetConfigValueByKey(key, isDirect) {
let data = GetConfigData(isDirect)
return data[key]
}
function mkdirPath(dirpath, mode) {
if (!fs.existsSync(dirpath)) {
if (!fs.mkdirSync(dirpath, mode)) {
if (fs.existsSync(dirpath)) {
return true
}
return false
}
}
return true
}
function getLangPath() {
if (!window.projectFolder) {
return null
}
let langPath = GetConfigValueByKey('langPath')
if (langPath == null) {
AddOrModifyConfig('langPath', '/lang')
langPath = '/lang'
}
return window.projectFolder + langPath
}
function getLangFileName() {
if (!window.projectFolder) {
return null
}
let langFileName = GetConfigValueByKey('langFileName')
if (langFileName == null) {
AddOrModifyConfig('langFileName', 'lang.txt')
langFileName = 'lang.txt'
}
return langFileName
}
function getCurLangSet() {
if (!window.projectFolder) {
return null
}
let langSet = GetConfigValueByKey('langSet')
if (langSet == null) {
AddOrModifyConfig('langSet', 'Zh')
langSet = 'Zh'
}
return langSet
}
function ensureLangExist() {
let langFolder = getLangPath()
if (!langFolder) {
return false
}
if (!mkdirPath(langFolder)) {
return false
}
let langPath = langFolder + '/' + getLangFileName()
if (!fs.existsSync(langPath)) {
let test = { test: { Zh: '测试', En: 'test' } }
fs.writeFileSync(langPath, JSON.stringify(test, null, 4))
}
return true
}
function saveLangData(langPath, data) {
AddOrModifyConfig('modify:langPath', true)
fs.writeFileSync(langPath, JSON.stringify(data, null, 4))
}
function fullPath(path) {
return "file://" + path;
}
var localSaveData;
function getLocalSaveData() {
if (!localSaveData) {
localSaveData = GetFileToData("save.dat")
}
return localSaveData
}
function getSaveData(key) {
try {
return window.localStorage[key]
} catch (e) {
var saveData = getLocalSaveData();
return saveData[key]
}
}
function setSaveData(key, data) {
try {
window.localStorage[key] = data
} catch (e) {
var saveData = getLocalSaveData();
if (!data) {
delete saveData[key]
} else {
saveData[key] = data
}
fs.writeFileSync("save.dat", JSON.stringify(saveData, null, 4))
}
}