-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
edit.html
499 lines (466 loc) · 23 KB
/
edit.html
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
<span id="customVariables-edit">
<span class="title text-default" style="padding: 0 !important;">
<a class="btn btn-outline-info" style="border: 0 !important;" href="/#/registry/customVariables/list"><i class="fas fa-chevron-circle-left"></i></a>
<span style="position: relative; top: 2px;">{{ title }} </span>
<span style="position: relative; top: 2px;" v-if="isEditation">{{ variableName }}</span>
</span>
<span class="float-right">
<span v-if="_.filter(this.hasError, (o) => o === true).length !== 0" style="cursor: auto;" class="alert-danger btn m-0">{{commons.translate('dialog.formNotValid')}}</span>
<button v-if="saveState === 0" type="button" class="btn btn-primary" v-on:click="saveChanges()">{{ commons.translate('dialog.buttons.saveChanges.idle') }}</button>
<button v-if="saveState === 1" disabled="disabled" type="button" class="btn btn-info"><i class="fas fa-spinner fa-spin"></i> {{ commons.translate('dialog.buttons.saveChanges.progress') }}</button>
<button v-if="saveState === 2" disabled="disabled" type="button" class="btn btn-success"><i class="fas fa-check"></i> {{ commons.translate('dialog.buttons.saveChanges.done') }}</span></button>
<button v-if="saveState === 3" disabled="disabled" type="button" class="btn btn-danger"><i class="fas fa-exclamation"></i> {{ commons.translate('dialog.buttons.something-went-wrong') }}</span></button>
</span>
<div class="widget pt-3">
<!-- Editation stuff here -->
<form>
<div class="form-group col-md-12">
<label for="variable_name_input">{{ commons.translate('registry.customvariables.variable.name') }}</label>
<input v-bind:class="{ 'is-invalid': hasError.variableName || hasError.isNotUnique }" v-model="variableName" type="text" class="form-control" id="variable_name_input" v-bind:placeholder="commons.translate('registry.customvariables.variable.placeholder')">
<small class="form-text text-muted">{{ commons.translate('registry.customvariables.variable.help') }}</small>
<div class="invalid-feedback">
<span v-if="hasError.isNotUnique" v-html="commons.translate('registry.customvariables.variable.error.isNotUnique')"></span>
<span v-if="!hasError.isNotUnique">{{ commons.translate('registry.customvariables.variable.error.isEmpty') }}</span>
</div>
</div>
<div class="form-group col-md-12">
<label for="description_input">{{ commons.translate('registry.customvariables.description.name') }}</label>
<input v-model="description" type="text" class="form-control" id="description_input" v-bind:placeholder="commons.translate('registry.customvariables.description.placeholder')">
<small class="form-text text-muted">{{ commons.translate('registry.customvariables.description.help') }}</small>
</div>
<div class="form-group row col-12 pr-0">
<div class="col-4">
<div class="row ml-0 mr-0">
<label for="description_input">{{ commons.translate('registry.customvariables.response.name') }}</label>
</div>
<button :class="[responseType === 0 ? 'btn-primary' : 'btn-outline-primary']" type="button" class="btn" @click="responseType = 0; responseText = ''">{{ commons.translate('registry.customvariables.response.default') }}</button>
<button :class="[responseType === 1 ? 'btn-primary' : 'btn-outline-primary']" type="button" class="btn" @click="responseType = 1; responseText = ''">{{ commons.translate('registry.customvariables.response.custom') }}</button>
<button :class="[responseType === 2 ? 'btn-primary' : 'btn-outline-primary']" type="button" class="btn" @click="responseType = 2; responseText = ''">{{ commons.translate('registry.customvariables.response.command') }}
<strong style="border-bottom: 1px dotted gray" data-toggle="tooltip" data-placement="bottom" :title="commons.translate('registry.customvariables.useIfInCommand')">?</strong>
</button>
</div>
<div class="col-4">
<label style="margin: 0px 0px 3px; font-size: 11px; font-weight: 400; text-transform: uppercase; letter-spacing: 1px;">{{ commons.translate('registry.customvariables.permissionToChange') }}</label>
<select class="form-control" v-model="permission">
<option v-for="p of permissions" v-bind:value="p.id" :key="p.id">
{{ getPermissionName(p.id) | capitalize }}
</option>
</select>
</div>
<div class="col-12 pr-0">
<input v-if="responseType === 1" v-model="responseText" type="text" class="form-control mt-2" v-bind:placeholder="commons.translate('registry.customvariables.response.default-placeholder')">
<small v-if="responseType === 1" class="form-text text-muted">{{ commons.translate('registry.customvariables.response.default-help') }}</small>
</div>
</div>
<div class="form-group row pl-3 pr-3">
<div class="col-md-4">
<label for="type">{{ commons.translate('registry.customvariables.type.name') }}</label>
<select id="type" class="form-control" v-bind:class="{ 'is-invalid': hasError.selectedType }" v-model="selectedType">
<option value="" selected>{{ commons.translate('registry.customvariables.choose') }}</option>
<option v-for="type in types" v-bind:value="type.value">
{{ type.text }}
</option>
</select>
<div class="invalid-feedback">{{ commons.translate('registry.customvariables.type.error.isNotSelected') }}</div>
</div>
<div class="col-md-8">
<label for="current_value_input">{{ commons.translate('registry.customvariables.currentValue.name') }}</label>
<div class="input-group">
<input v-if="selectedType !== 'options'" v-model="currentValue" type="text" class="form-control" id="current_value_input" :readonly="(['', 'eval'].includes(selectedType)) ? true : false">
<select v-if="selectedType === 'options'" v-model="currentValue" class="form-control">
<option v-for="option in usableOptionsArray" :value="option">{{ option }}</option>
</select>
<div class="input-group-append" v-if="selectedType !== 'eval'">
<button class="btn form-control" v-bind:class="{ 'btn-danger': readOnly, 'btn-success': !readOnly }" v-on:click="toggleReadOnly">
<template v-if="readOnly">
{{ commons.translate('registry.customvariables.isReadOnly') }}
</template>
<template v-else>
{{ commons.translate('registry.customvariables.isNotReadOnly') }}
</template>
</button>
</div>
</div>
<small class="form-text text-muted" v-html="commons.translate('registry.customvariables.currentValue.help')"></small>
</div>
</div>
<div class="form-group col-md-12" v-if="selectedType.toLowerCase() === 'options'">
<label for="usable_options_input">{{ commons.translate('registry.customvariables.usableOptions.name') }}</label>
<input v-bind:class="{ 'is-invalid': hasError.usableOptions }" v-model="usableOptions" type="text" class="form-control" id="usable_options_input" v-bind:placeholder="commons.translate('registry.customvariables.usableOptions.placeholder')">
<small class="form-text text-muted" v-html="commons.translate('registry.customvariables.usableOptions.help')"></small>
<div class="invalid-feedback">{{ commons.translate('registry.customvariables.usableOptions.error.atLeastOneValue') }}</div>
</div>
<div class="form-group row pl-3 pr-3" v-if="selectedType === 'eval'">
<div class="col-md-8">
<label for="variable_name_input">{{ commons.translate('registry.customvariables.scriptToEvaluate') }}</label>
<div id="custom_variables-textarea" class="border"></div>
</div>
<div class="col-md-4">
<label for="runEvery">{{ commons.translate('registry.customvariables.runScript.name') }}</label>
<div class="d-flex">
<select id="runEvery" class="form-control" v-model="selectedRunEvery">
<option v-for="option in runEveryOptions" v-bind:value="option.type">
{{ commons.translate('registry.customvariables.runEvery.' + option.type) }}
</option>
</select>
<input type="text" v-model="runEveryX" class="form-control ml-2" v-if="selectedRunEvery !== 'isUsed'" />
</div>
<button type="button" class="btn btn-block btn-info mt-4" v-on:click="testScript()">{{ commons.translate('registry.customvariables.testCurrentScript.name') }}</button>
<small class="form-text text-muted" v-html="commons.translate('registry.customvariables.testCurrentScript.help')"></small>
<pre v-if="evalError.length > 0" class="alert alert-danger mt-2">{{ evalError }}</pre>
</div>
</div>
<div class="form-group row pl-3 pr-3" v-if="selectedType !== 'eval'">
<div class="col">
<label for="variable_name_input">{{ commons.translate('registry.customvariables.history') }}</label>
<div class="alert alert-danger" v-if="history.length === 0">{{ commons.translate('registry.customvariables.historyIsEmpty') }}</div>
<div v-for="h of history" v-else class="alert alert-light p-0 m-0">
<div class="row">
<div class="col-4">{{moment(h.timestamp).format('LL')}} {{ moment(h.timestamp).format('LTS') }}</div>
<div class="col-2"><strong>{{ h.sender ? h.sender : 'Dashboard'}}</strong></div>
<div class="col">
{{ h.oldValue }} => {{ h.currentValue}}
</div>
</div>
</div>
</div>
</div>
</form>
<div class="form-group col-md-12" v-if="isEditation">
<button type="button" class="btn btn-danger" key="deleting" data-lang="dialog.buttons.delete" v-if="deleteState === 'waiting'" v-on:click="deleteState='deleting'">{{ commons.translate('dialog.buttons.delete') }}</button>
<div class="btn-group" role="group" v-if="deleteState === 'deleting'">
<button type="button" class="btn btn-danger" key="deleted" data-lang="dialog.buttons.yes" v-on:click="deleteState='deleted'">{{ commons.translate('dialog.buttons.yes') }}</button>
<button type="button" class="btn btn-success" key="waiting" data-lang="dialog.buttons.no" v-on:click="deleteState='waiting'">{{ commons.translate('dialog.buttons.no') }}</button>
</div>
<small class="form-text text-danger" v-html="commons.translate('registry.customvariables.warning')"></small>
</div>
<!-- -->
</div>
</span>
<script>
Vue.prototype.commons = commons
function customVariablesEditInit () {
if (_.size(translations) === 0) return setTimeout(() => customVariablesEditInit(), 1)
var customVariablesEdit = new Vue({
el: '#customVariables-edit',
data: {
types: [
{
value: 'number',
text: commons.translate('registry.customvariables.types.number')
},
{
value: 'text',
text: commons.translate('registry.customvariables.types.text')
},
{
value: 'options',
text: commons.translate('registry.customvariables.types.options')
},
{
value: 'eval',
text: commons.translate('registry.customvariables.types.eval')
}
],
runEveryOptions: [
{
value: 0,
type: 'isUsed',
},
{
value: 1000,
type: 'seconds',
},
{
value: 1000 * 60,
type: 'minutes',
},
{
value: 1000 * 60 * 60,
type: 'hours',
},
{
value: 1000 * 60 * 60 * 24,
type: 'days',
},
],
selectedType: '',
selectedRunEvery: null,
runEveryX: 1,
deleteState: 'waiting',
variableNameInitial: '',
variableName: '',
description: '',
currentValue: '',
usableOptions: '',
readOnly: false,
permission: null,
evalValueInit: '',
evalInput: null,
evalError: '',
responseType: 0,
responseText: '',
history: [],
hasError: {
variableName: false,
selectedType: false,
usableOptions: false,
isNotUnique: false
},
psocket: io('/core/permissions', { query: "token=" + token }),
permissions: [],
socket: io('/registry/customVariables', { query: "token=" + token }),
saveState: 0
},
methods: {
getPermissionName: function (id) {
if (!id) return 'Disabled'
const permission = this.permissions.find((o) => {
return o.id === id
})
if (typeof permission !== 'undefined') {
if (permission.name.trim() === '') {
return permission.id
} else {
return permission.name
}
} else {
return null
}
},
moment: function (args) {
return moment(args) // expose moment function
},
toggleReadOnly: function (event) {
if (event) event.preventDefault()
this.readOnly = !this.readOnly
},
testScript: function () {
this.socket.emit('test.script', { evalValue: this.evalValue, currentValue: this.currentValue }, (err, response) => {
if (err) {
this.evalError = err
this.evalOk = false
} else {
this.evalError = ''
this.evalOk = true
}
this.currentValue = response
})
},
validateForm: function () {
// reset errors
for (let [key, value] of Object.entries(this.hasError)) {
if (key !== 'isNotUnique') {
this.$set(this.hasError, key, false)
}
}
if (this.variableName.length === 0 || (this.variableName.length === 2 && this.variableName.startsWith('$_'))) this.hasError.variableName = true
if (this.selectedType === '') {
this.$set(this.hasError, 'selectedType', true)
} else if (this.selectedType === 'number') {
if (_.isNaN(Number(this.currentValue))) this.currentValue = 0
else this.currentValue = Number(this.currentValue) // retype
} else if (this.selectedType === 'options') {
// check options
if (this.usableOptionsArray.length === 0) {
this.$set(this.hasError, 'usableOptions', true)
}
// if current value is not set or is not set as option -> force first item
if (String(this.currentValue).trim().length === 0 || !this.usableOptionsArray.includes(String(this.currentValue).trim())) this.currentValue = this.usableOptionsArray[0]
} else if (this.selectedType === 'eval') {
if (this.runEveryX < 1) {
this.runEveryX = 0
}
}
return _.filter(this.hasError, (o) => o === true).length === 0
},
saveChanges: function () {
if (this.validateForm()) {
this.saveState = 1
const data = {
_id: this.id,
variableName: this.variableName,
description: this.description,
currentValue: this.currentValue,
usableOptions: this.usableOptions || '',
evalValue: this.evalValue,
runEvery: this.selectedType === 'eval' ? this.runEveryOptions.find((o) => o.type === this.selectedRunEvery).value * this.runEveryX : 0,
runEveryType: this.selectedType === 'eval' ? this.selectedRunEvery : 'isUsed',
runEveryTypeValue: this.selectedType === 'eval' ? this.runEveryOptions.find((o) => o.type === this.selectedRunEvery).value : 0,
type: this.selectedType,
readOnly: this.readOnly,
responseType: this.responseType,
responseText: this.responseText,
permission: this.permission
}
this.socket.emit('save', data, (err, _id) => {
if (err) {
console.error(err)
return this.saveState = 3
}
this.saveState = 2
this.variableNameInitial = this.variableName
// if we are creating new -> refresh
if (_.isNil(data._id)) setTimeout(() => page(`?id=${_id}#registry/customVariables/edit`), 3000)
})
}
},
initializeEvalTextArea: function () {
const textarea = document.getElementById('custom_variables-textarea')
if (_.isNil(textarea)) return setTimeout(() => this.initializeEvalTextArea(), 1)
this.evalInput = CodeMirror(textarea, {
value: this.evalValueInit ||
`/* Available functions
user(username?: string): Promise<{ username: string, id: string, is: { follower: boolean, mod: boolean, online: boolean, subscriber: boolean, vip: boolean }}> - returns user object (if null -> sender)
url(url: string, opts?: { method: 'POST' | 'GET', headers: object, data: object}): Promise<{ data: object, status: number, statusText: string}>
*
* Available variables
_, _current, users, param (only in custom command)
sender?: { // (only in custom commands, keyword)
username: string,
userId: string,
}
random: {
online: {
viewer: string,
follower: string,
subscriber: string
},
viewer: string,
follower: string,
subscriber: string
}
stream: {
uptime: string,
chatMessages: number,
currentViewers: number,
currentBits: number,
currentFollowers: number,
currentHosts: number,
currentViews: number,
currentTips: number,
currentWatched: number,
currency: string,
maxViewers: number,
newChatters: number,
game: string,
status: string
}
*
* IMPORTANT: Must contain return statement!
*/
return '';`,
mode: "javascript",
lineNumbers: true,
matchBrackets: true,
lint: {
esversion: 6
},
gutters: ["CodeMirror-lint-markers"]
})
this.evalInput.setSize(null, 1000);
},
isUnique: function () {
if (this.variableName !== this.variableNameInitial) {
this.socket.emit('isUnique', this.variableName, (err, isUnique) => {
this.hasError.isNotUnique = !isUnique
})
}
}
},
created: function () {
// _.debounce is a function provided by lodash to limit how
// often a particularly expensive operation can be run.
// In this case, we want to limit how often we access
// yesno.wtf/api, waiting until the user has completely
// finished typing before making the ajax request. To learn
// more about the _.debounce function (and its cousin
// _.throttle), visit: https://lodash.com/docs#debounce
this.debouncedIsUnique =_.debounce(this.isUnique, 500)
this.psocket.emit('find', {}, (err, data) => {
if (err) return console.error(err)
this.permissions = _.orderBy(data, 'order', 'asc')
if (!commons.urlParam('id')) {
if (!this.permission) {
this.permission = _.orderBy(this.permissions, 'order', 'asc').pop().id
}
}
})
},
watch: {
deleteState: function (cur, old) {
if (cur === 'deleted') {
this.socket.emit('delete', commons.urlParam('id'), () => {
page('/#registry/customVariables/list')
})
}
},
saveState: function (cur, old) {
if (cur === 2 || cur === 3) setTimeout(() => this.saveState = 0, 3000)
},
variableName: function (cur, old) {
if (!cur.startsWith('$_')) this.variableName = '$_' + cur
this.variableName = this.variableName.replace(/ /g, '_')
this.debouncedIsUnique()
},
selectedType: function (cur, old) {
if (cur === 'eval') {
this.initializeEvalTextArea()
this.selectedRunEvery = this.selectedRunEvery || 'isUsed'
} else if (cur === 'options') {
// check if value or change it
if (!this.usableOptionsArray.includes(this.currentValue)) this.currentValue = this.usableOptionsArray[0]
} else {
this.evalInput = null
}
},
usableOptionsArray: function (cur, old) {
if (!cur.includes(this.currentValue)) this.currentValue = cur[0] // set initial value only if value doesn't exist
}
},
computed: {
evalValue: function () {
return !_.isNil(this.evalInput) ? this.evalInput.getValue() : ''
},
usableOptionsArray: function () {
if (typeof this.usableOptions === 'string') {
return this.usableOptions.split(',').map((o) => o.trim()).filter((o) => o.length > 0)
} else {
return []
}
},
isEditation: function () {
return !_.isNil(commons.urlParam('id'))
},
title: function () {
return commons.translate(this.isEditation ? 'dialog.title.edit' : 'dialog.title.add')
}
}
})
// load up from db
if (commons.urlParam('id')) {
customVariablesEdit.socket.emit('load', commons.urlParam('id'), (data) => {
customVariablesEdit.id = commons.urlParam('id')
customVariablesEdit.variableNameInitial = data.variable.variableName
customVariablesEdit.variableName = data.variable.variableName
customVariablesEdit.description = data.variable.description
customVariablesEdit.currentValue = data.variable.currentValue
customVariablesEdit.usableOptions = data.variable.usableOptions
customVariablesEdit.evalValueInit = data.variable.evalValue
customVariablesEdit.selectedRunEvery = data.variable.runEveryType
customVariablesEdit.runEveryX = data.variable.runEvery / data.variable.runEveryTypeValue
customVariablesEdit.selectedType = data.variable.type
customVariablesEdit.responseType = data.variable.responseType
customVariablesEdit.responseText = data.variable.responseText
customVariablesEdit.permission = data.variable.permission || 0
customVariablesEdit.readOnly = data.variable.readOnly || false
for (let h of data.history) {
// change timestamp to milliseconds
h.timestamp = new Date(h.timestamp).getTime()
}
customVariablesEdit.history = _.chunk(_.orderBy(data.history, 'timestamp', 'desc'), 15)[0] || []
})
}
}
customVariablesEditInit()
</script>