Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/admin_ui/media/javascript/ui/panel/AutoRunModuleForm.js
1154 views
1
2
loadModuleInfo = async function(token, moduleId, moduleName) {
3
try {
4
// If all we have is the name then we need to get the ID first.
5
if (moduleId === undefined) {
6
const searchResponse = await fetch(`/api/modules/search/${moduleName}?token=${token}`);
7
if (!searchResponse.ok) {
8
throw new Error(`Getting auto run rules failed with status ${searchResponse.status}`);
9
}
10
const searchData = await searchResponse.json();
11
if (typeof searchData.id === 'number') {
12
moduleId = searchData.id;
13
} else {
14
throw new Error("Searching module name failed.");
15
}
16
}
17
18
const infoResponse = await fetch(`/api/modules/${moduleId}?token=${token}`);
19
const infoData = await infoResponse.json();
20
if (!infoData) {
21
throw new Error(`Module with name ${moduleName} and ID ${moduleId} couldn't be retrived.`);
22
}
23
// Set the module Id incase we need it later.
24
infoData.id = moduleId;
25
return infoData;
26
27
} catch(error) {
28
console.error(error);
29
console.error("Failed to get module information.");
30
return null;
31
}
32
}
33
34
35
/**
36
* Form that displays fields for a module.
37
*
38
* moduleData: The object definition of this moduleData from the Auto Run Engine.
39
* deleteFn: callback function to delete this moduleData.
40
* moveUp: moves the module up one spot in the Auto Run execution order.
41
* moveDown: moves the module down one spot in the Auto Run exection order.
42
*/
43
AutoRunModuleForm = function(moduleData, deleteFn, moveUp, moveDown, ruleId, index, moduleList) {
44
const moduleTextAreaId = `rule-${ruleId}-module-textarea-${index}`;
45
const chainModeComboId = `rule-${ruleId}-module-combo-${index}`;
46
const token = BeefWUI.get_rest_token();
47
48
const comboStore = new Ext.data.Store({
49
data: moduleList,
50
reader: new Ext.data.JsonReader({
51
fields: ['id', 'name'],
52
}),
53
proxy: new Ext.data.MemoryProxy(moduleList)
54
});
55
56
const moduleSelect = new Ext.form.ComboBox({
57
fieldLabel: 'Change Module',
58
store: comboStore,
59
queryMode: 'local',
60
displayField: 'name',
61
valueField: 'id',
62
editable: false, // Disable manual editing of the field
63
forceSelection: true, // Force selection from the list
64
triggerAction: 'all',
65
typeAhead: true,
66
listeners: {
67
select: async function(combo) {
68
const selectedModuleId = combo.getValue()
69
const moduleInfo = await loadModuleInfo(token, selectedModuleId, undefined);
70
if (!moduleInfo) {
71
console.error("Failed to load new module.");
72
return;
73
}
74
// Update the module data to reflect the new module.
75
moduleData.name = moduleInfo.name;
76
moduleData.condition = moduleInfo.condition ? moduleInfo.condition : null;
77
moduleData.options = {};
78
for (let i = 0; i < moduleInfo.options.length; i++) {
79
const newOption = moduleInfo.options[i];
80
moduleData.options[newOption.name] = newOption.value ? newOption.value : '';
81
}
82
loadModule(moduleInfo);
83
}
84
}
85
});
86
87
const moduleOptionsContainer = new Ext.Panel({
88
title: `Module ${index + 1}`,
89
tbar: [moduleSelect],
90
layout: 'form',
91
border: false,
92
listeners: {
93
afterrender: function() {loadModule(undefined)}
94
}
95
});
96
97
async function loadModule(moduleInfo) {
98
if (!moduleInfo)
99
moduleInfo = await loadModuleInfo(token, undefined, moduleData.name);
100
if (!moduleInfo) {
101
moduleOptionsContainer.update("<p>Failed to load module information.</p>");
102
return;
103
}
104
105
// Update the combobox default value to be this module.
106
// Can't use the moduleData name since it doesn't match the ID.
107
moduleSelect.setValue(moduleInfo.id);
108
109
// Setup module form elements. Remove all incase we're switching from a different module.
110
moduleOptionsContainer.removeAll();
111
moduleOptionsContainer.add(new Ext.form.DisplayField({
112
fieldLabel: 'Module Name',
113
value: moduleInfo.name
114
}))
115
moduleOptionsContainer.add(new Ext.form.DisplayField({
116
fieldLabel: 'Module Author',
117
value: moduleInfo.author ? moduleInfo.author : 'anonymous'
118
}))
119
120
for (let i = 0; i < moduleInfo.options.length; i++) {
121
const inputField = generate_form_input_field(
122
moduleOptionsContainer,
123
moduleInfo.options[i],
124
moduleData.options[moduleInfo.options[i].name],
125
false,
126
{session: `${moduleInfo.name}-module-${index}-field-${i}`}
127
);
128
// Ensure any changes to the element are reflected in the newRule object.
129
// When the user saves the rule the whole newRule object will be saved,
130
// including any changes made to these input fields.
131
inputField.on('change', function(_inputF, newValue, oldValue) {
132
moduleData.options[moduleInfo.options[i].name] = newValue;
133
});
134
135
};
136
moduleOptionsContainer.doLayout();
137
}
138
139
const buttonContainer = new Ext.Container({
140
layout: {
141
type: 'hbox',
142
pack: 'end',
143
},
144
items: [
145
{
146
xtype: 'button',
147
text: 'Delete',
148
handler: deleteFn,
149
},{
150
xtype: 'button',
151
text: 'Move Forward',
152
handler: moveUp,
153
disabled: moveUp == undefined,
154
},{
155
xtype: 'button',
156
text: 'Move Back',
157
handler: moveDown,
158
disabled: moveDown == undefined,
159
}
160
]
161
});
162
163
164
AutoRunModuleForm.superclass.constructor.call(this, {
165
items: [
166
moduleOptionsContainer,
167
buttonContainer
168
]
169
});
170
};
171
172
Ext.extend(AutoRunModuleForm, Ext.Container, {});
173