-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogger UI.js
More file actions
119 lines (93 loc) · 3.7 KB
/
Logger UI.js
File metadata and controls
119 lines (93 loc) · 3.7 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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: purple; icon-glyph: file-alt;
const { Files } = importModule("Files");
const { LogLevel } = importModule("Logger");
const { tr } = importModule("Localization");
const {
TextDataField,
UIDataTable,
UIForm,
UIFormField,
UIFormAction,
UIDeleteRowField
} = importModule("CRUD Module");
LOGGER_SCRIPT_NAME = "Logger";
LEVELS_FILE_NAME = "levels.json";
/**
* ENTRY POINT
*/
async function main() {
const tableBuilder = new LoggingTable();
const table = tableBuilder.build();
await table.present();
}
/**
* Used to render table used to edit log levels
* for services.
*
* @class LoggingTable
*/
class LoggingTable {
#serviceNameDataField = new TextDataField("service", "Service");
#logLevelDataField = new TextDataField("level", LogLevel.OFF);
/**
* Used to build UI table.
*
* @return {UITable} table
* @memberof LoggingTable
*/
build() {
const table = new UIDataTable();
table.title = tr("loggerUI_tableTitle");
table.allowCreation();
table.showSeparators();
table.setTableData(Files.readJson(LOGGER_SCRIPT_NAME, LEVELS_FILE_NAME, []));
table.onDataModification((levels) => Files.updateJson(LOGGER_SCRIPT_NAME, LEVELS_FILE_NAME, levels));
table.setDataFields([this.#serviceNameDataField, this.#logLevelDataField]);
table.setUIFields(this.#getUIFields());
return table;
}
/**
* Used to get list of UI fields that needs to be
* displayed in table.
*
* @return {List<UIField>} list of UI fields
* @memberof LoggingTable
*/
#getUIFields() {
// Service Name Field
const serviceNameUIField = new UIForm((log) => log.service, 70);
serviceNameUIField.setFormTitleFunction(() => tr("loggerUI_serviceNameFormTitle"));
const serviceNameFormField = new UIFormField(this.#serviceNameDataField, tr("loggerUI_serviceNameFieldFormLabel"));
serviceNameUIField.addDefaultAction(tr("loggerUI_updateServiceNameAction"));
serviceNameUIField.addFormField(serviceNameFormField);
// Logging Level Field
const logLevelUIField = new UIForm((log) => log.level, 30);
logLevelUIField.setFormTitleFunction(() => tr("loggerUI_logLevelFormTitle"));
const turnOffAction = new UIFormAction(LogLevel.OFF);
const setToInfoAction = new UIFormAction(LogLevel.INFO);
const setToWarnAction = new UIFormAction(LogLevel.WARN);
const setToErrorAction = new UIFormAction(LogLevel.ERROR);
const setToDebugAction = new UIFormAction(LogLevel.DEBUG);
turnOffAction.addCallback(this.#logLevelDataField, (log) => log.level = LogLevel.OFF);
setToInfoAction.addCallback(this.#logLevelDataField, (log) => log.level = LogLevel.INFO);
setToWarnAction.addCallback(this.#logLevelDataField, (log) => log.level = LogLevel.WARN);
setToErrorAction.addCallback(this.#logLevelDataField, (log) => log.level = LogLevel.ERROR);
setToDebugAction.addCallback(this.#logLevelDataField, (log) => log.level = LogLevel.DEBUG);
logLevelUIField.addFormAction(turnOffAction);
logLevelUIField.addFormAction(setToInfoAction);
logLevelUIField.addFormAction(setToWarnAction);
logLevelUIField.addFormAction(setToErrorAction);
logLevelUIField.addFormAction(setToDebugAction);
// Delete Field
const deleteUIField = new UIDeleteRowField(() => tr("loggerUI_deleteFieldLabel"), 15);
return [
serviceNameUIField,
logLevelUIField,
deleteUIField
];
}
}
await main();
Script.complete();