Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dynamic forms #3113

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/dashboard/app/javascript/batch_connect_session_contexts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

import { attachPathSelectors } from './path_selector/path_selector';
import { prefillTemplatesHandler } from './prefill_templates/prefill_templates';
import { prefillSubmitHandler } from './prefill_templates/prefill_submit';
import { isBCDynamicJSEnabled } from './config';
import { makeChangeHandlers } from './dynamic_forms';


jQuery(function() {
if(isBCDynamicJSEnabled()){
makeChangeHandlers('batch_connect_session_context');
}

attachPathSelectors();
prefillTemplatesHandler();
prefillSubmitHandler();
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
'use strict';

import { attachPathSelectors } from './path_selector/path_selector'
import { prefillTemplatesHandler } from './prefill_templates/prefill_templates'
import { prefillSubmitHandler } from './prefill_templates/prefill_submit'
import { isBCDynamicJSEnabled } from './config';

const bcPrefix = 'batch_connect_session_context';
const shortNameRex = new RegExp(`${bcPrefix}_([\\w\\-]+)`);
// these are initialized in makeChangeHandlers
var idPrefix = undefined;
var shortNameRex = undefined;

// @example ['NodeType', 'Cluster']
const formTokens = [];
Expand Down Expand Up @@ -35,8 +31,8 @@ const mcRex = /[-_]([a-z])|([_-][0-9])|([\/])/g;
// whether we're still initializing or not
let initializing = true;

function bcElement(name) {
return `${bcPrefix}_${name.toLowerCase()}`;
function idWithPrefix(name) {
return `${idPrefix}_${name.toLowerCase()}`;
};

// here the simple name for 'batch_connect_session_context_cluster'
Expand Down Expand Up @@ -128,8 +124,13 @@ function memorizeElements(elements) {
});
};

function makeChangeHandlers(){
const allElements = $(`[id^=${bcPrefix}]`);
function makeChangeHandlers(prefix){

// initialize some global variables.
idPrefix = prefix;
shortNameRex = new RegExp(`${idPrefix}_([\\w\\-]+)`);

const allElements = $(`[id^=${idPrefix}]`);
memorizeElements(allElements);

allElements.each((_i, element) => {
Expand Down Expand Up @@ -159,6 +160,8 @@ function makeChangeHandlers(){
});
}
});

initializing = false;
};

function addHideHandler(optionId, option, key, configValue) {
Expand Down Expand Up @@ -588,7 +591,7 @@ function idFromToken(str) {

if (match && match.length >= 1) {
let ele = snakeCaseWords(match[0]);
return bcElement(ele);
return idWithPrefix(ele);
}
}).filter((id) => {
return id !== undefined;
Expand Down Expand Up @@ -716,14 +719,7 @@ function optionForFromToken(str) {
document.getElementById(elementId).dispatchEvent((new Event('change', { bubbles: true })));
};

jQuery(function() {
if(isBCDynamicJSEnabled()){
makeChangeHandlers();
}

attachPathSelectors();
prefillTemplatesHandler();
prefillSubmitHandler();

initializing = false;
});
export {
makeChangeHandlers
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<%= f.submit t('dashboard.batch_connect_form_launch'), class: "btn btn-primary btn-block" %>
<% end %>

<%= javascript_include_tag('batch_connect', nonce: true) %>
<%= javascript_include_tag('batch_connect_session_contexts', nonce: true) %>

<% @app.custom_javascript_files.each do |jsfile| %>
<%= javascript_tag "(function(){\n" + jsfile.read + "\n}());" %>
Expand Down