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