forked from liaozb/APIJSON.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
383 lines (346 loc) · 10.2 KB
/
main.js
File metadata and controls
383 lines (346 loc) · 10.2 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
'use strict';
(function () {
var ApiUrl = 'https://api.awesomes.cn'
Vue.component('vue-item', {
props: ['jsondata', 'theme'],
template: '#item-template'
})
Vue.component('vue-outer', {
props: ['jsondata', 'isend', 'theme'],
template: '#outer-template'
})
Vue.component('vue-expand', {
props: [],
template: '#expand-template'
})
Vue.component('vue-val', {
props: ['field', 'val', 'isend', 'theme'],
template: '#val-template'
})
Vue.use({
install: function (Vue, options) {
// 判断数据类型
Vue.prototype.getTyp = function (val) {
return toString.call(val).split(']')[0].split(' ')[1]
}
// 判断是否是对象或者数组,以对下级进行渲染
Vue.prototype.isObjectArr = function (val) {
return ['Object', 'Array'].indexOf(this.getTyp(val)) > -1
}
// 折叠
Vue.prototype.fold = function ($event) {
var target = Vue.prototype.expandTarget($event)
target.siblings('svg').show()
target.hide().parent().siblings('.expand-view').hide()
target.parent().siblings('.fold-view').show()
}
// 展开
Vue.prototype.expand = function ($event) {
var target = Vue.prototype.expandTarget($event)
target.siblings('svg').show()
target.hide().parent().siblings('.expand-view').show()
target.parent().siblings('.fold-view').hide()
}
//获取展开折叠的target
Vue.prototype.expandTarget = function ($event) {
switch($event.target.tagName.toLowerCase()) {
case 'use':
return $($event.target).parent()
case 'label':
return $($event.target).closest('.fold-view').siblings('.expand-wraper').find('.icon-square-plus').first()
default:
return $($event.target)
}
}
// 格式化值
Vue.prototype.formatVal = function (val) {
switch(Vue.prototype.getTyp(val)) {
case 'String':
return '"' + val + '"'
break
case 'Null':
return 'null'
break
default:
return val
}
}
// 判断值是否是链接
Vue.prototype.isaLink = function (val) {
return /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/.test(val)
}
// 计算对象的长度
Vue.prototype.objLength = function (obj) {
return Object.keys(obj).length
}
}
})
var initJson = {
"[]": {
"page": 1,
"count": 2,
"Moment": {
"content$": "%a%"
},
"User": {
"id@": "/Moment/userId",
"@column": "id,name,head"
},
"Comment[]": {
"count": 2,
"Comment": {
"momentId@": "[]/Moment/id"
}
}
}
};
// 主题 [key, String, Number, Boolean, Null, link-link, link-hover]
let themes = [
['#92278f', '#3ab54a', '#25aae2', '#f3934e', '#f34e5c', '#717171'],
['rgb(19, 158, 170)', '#cf9f19', '#ec4040', '#7cc500', 'rgb(211, 118, 126)', 'rgb(15, 189, 170)'],
['#886', '#25aae2', '#e60fc2', '#f43041', 'rgb(180, 83, 244)', 'rgb(148, 164, 13)'],
['rgb(97, 97, 102)', '#cf4c74', '#20a0d5', '#cd1bc4', '#c1b8b9', 'rgb(25, 8, 174)']
]
var App = new Vue({
el: '#app',
data: {
baseview: 'formater',
view: 'code',
jsoncon: JSON.stringify(initJson, null, ' ') ,
newjsoncon: '{"name": "Json on"}',
jsonhtml: (initJson),
compressStr: '',
error: {},
historys: [],
history: {name: ''},
isSaveShow: false,
isExportTxtShow: false,
exTxt: {
name: 'JSONON'
},
themes: themes,
checkedTheme: 0,
shareKey: '', // 分享后返回的ID
isSharing: false
},
methods: {
hpost: function () {
$.ajax({
url: $('#rest-url').val(),
type: "POST", dataType: "json",
contentType: "application/json;charset=utf-8",
data: JSON.stringify($('#vInput').val()),
success: function (data) {
App.jsonhtml = data;
App.view = 'code';
},
error: function () {
alert('Something went wrong, double-check the URL and callback parameter.');
}
});
},
// 全部展开
expandAll: function () {
$('.icon-square-min').show()
$('.icon-square-plus').hide()
$('.expand-view').show()
$('.fold-view').hide()
},
// 全部折叠
collapseAll: function () {
$('.icon-square-min').hide()
$('.icon-square-plus').show()
$('.expand-view').hide()
$('.fold-view').show()
},
// 压缩
compress: function () {
App.jsoncon = Parse.compress(App.jsoncon)
},
// diff
diffTwo: function () {
var oldJSON = {}
var newJSON = {}
App.view = 'code'
try {
oldJSON = jsonlint.parse(App.jsoncon)
} catch (ex) {
App.view = 'error'
App.error = {
msg: '原 JSON 解析错误\r\n' + ex.message
}
return
}
try {
newJSON = jsonlint.parse(App.newjsoncon)
} catch (ex) {
App.view = 'error'
App.error = {
msg: '新 JSON 解析错误\r\n' + ex.message
}
return
}
var base = difflib.stringAsLines(JSON.stringify(oldJSON, '', 4))
var newtxt = difflib.stringAsLines(JSON.stringify(newJSON, '', 4))
var sm = new difflib.SequenceMatcher(base, newtxt)
var opcodes = sm.get_opcodes()
$('#diffoutput').empty().append(diffview.buildView({
baseTextLines: base,
newTextLines: newtxt,
opcodes: opcodes,
baseTextName: '原 JSON',
newTextName: '新 JSON',
contextSize: 2,
viewType: 0
}))
},
// 清空
clearAll: function () {
App.jsoncon = ''
},
// 美化
beauty: function () {
App.jsoncon = JSON.stringify(JSON.parse(App.jsoncon), '', 4)
},
baseViewToDiff: function () {
App.baseview = 'diff'
App.diffTwo()
},
// 回到格式化视图
baseViewToFormater: function () {
App.baseview = 'formater'
App.view = 'code'
App.showJsonView()
},
// 根据json内容变化格式化视图
showJsonView: function () {
if (App.baseview === 'diff') {
return
}
try {
if (this.jsoncon.trim() === '') {
App.view = 'empty'
} else {
App.view = 'code'
App.jsonhtml = jsonlint.parse(this.jsoncon)
}
} catch (ex) {
App.view = 'error'
App.error = {
msg: ex.message
}
}
},
// 保存当前的JSON
save: function () {
if (App.history.name.trim() === '') {
Helper.alert('名称不能为空!', 'danger')
return
}
var val = {
name: App.history.name,
data: App.jsoncon
}
var key = String(Date.now())
localforage.setItem(key, val, function (err, value) {
Helper.alert('保存成功!', 'success')
App.isSaveShow = false
val.key = key
App.historys.push(val)
})
},
// 删除已保存的
remove: function (item, index) {
localforage.removeItem(item.key, function () {
App.historys.splice(index, 1)
})
},
// 根据历史恢复数据
restore: function (item) {
localforage.getItem(item.key, function (err, value) {
App.jsoncon = item.data
})
},
// 获取所有保存的json
listHistory: function () {
localforage.iterate(function (value, key, iterationNumber) {
if (key[0] !== '#') {
value.key = key
App.historys.push(value)
}
if (key === '#theme') {
// 设置默认主题
App.checkedTheme = value
}
})
},
// 导出文本
exportTxt: function () {
saveTextAs(App.jsoncon, App.exTxt.name + '.txt')
App.isExportTxtShow = false
},
// 切换主题
switchTheme: function (index) {
this.checkedTheme = index
localforage.setItem('#theme', index)
},
// 获取分享的链接
shareUrl: function (key) {
return `${window.location.origin}?key=${key}`
},
// 分享
share: function () {
let con = App.jsoncon
if (con.trim() === '') {
return
}
App.isSharing = true
$.ajax({
type: 'POST',
url: `${ApiUrl}/json`,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({con: con, key: App.shareKey}),
success: (data) => {
App.isSharing = false
App.shareKey = uuidv1()
if (data.status) {
Helper.alert('分享成功,已将链接复制到剪贴板,只能保存24小时', 'success')
} else {
}
}
})
}
},
watch: {
jsoncon: function () {
App.showJsonView()
}
},
computed: {
theme: function () {
let th = this.themes[this.checkedTheme]
let result = {}
let index = 0
;['key', 'String', 'Number', 'Boolean', 'Null', 'link-link'].forEach(key => {
result[key] = th[index]
index++
})
return result
}
},
created () {
this.listHistory()
var clipboard = new Clipboard('.copy-btn')
let sps = window.location.href.split('?key=')
let jsonID = sps[sps.length - 1]
this.shareKey = uuidv1()
if (sps.length > 1 && jsonID.length > 5) {
$.get(`${ApiUrl}/json?key=${jsonID}`, function (data) {
if (data.status) {
App.jsoncon = data.item.con
}
})
}
}
})
})()