Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrichards-lr committed Dec 17, 2021
1 parent 99ee385 commit 127ff86
Showing 1 changed file with 96 additions and 49 deletions.
145 changes: 96 additions & 49 deletions forms/fragments/form-populator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (!search) { // If it is not set then there is no query string to map
console.debug("There is no query string, so nothing to do");
return;
}

const isJsonString = (str) => {
try {
JSON.parse(str);
Expand All @@ -23,7 +24,7 @@ const isJsonString = (str) => {
}
return true;
};

if (!configuration.fieldMapping) { // If it is not set then the fragment has not been configured
console.error("Field mapping has not been configured.");
return;
Expand All @@ -40,7 +41,33 @@ if (!Array.isArray(fieldMapping)) {
return;
}

const searchObj = JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}');
const typeText = (field, text, current = 0, time = typeTextDelay) => {
const l = text.length;
field.value += text[current];
if(current < l - 1) {
current++;
setTimeout(function(){typeText(field, text, current)}, time);
} else {
field.setAttribute('value',field.value);
}
};

const setNativeValue = (el, value) => {
const previousValue = el.value;

if (el.type === 'checkbox' || el.type === 'radio') {
if ((!!value && !el.checked) || (!!!value && el.checked)) {
el.click();
}
} else el.value = value;

const tracker = el._valueTracker;
if (tracker) {
tracker.setValue(previousValue);
}

el.dispatchEvent(new Event('change', { bubbles: true }));
};

const populateFieldRetry = (config, fieldSelectorCallback, fieldSetterCallback) => {
if (!(typeof config === 'object') || !(typeof fieldSelectorCallback === 'function') || !(typeof fieldSetterCallback === 'function')) {
Expand Down Expand Up @@ -79,41 +106,27 @@ const populateFieldRetry = (config, fieldSelectorCallback, fieldSetterCallback)
const defaultFieldSelector = (config) => {
if (enableDebug)
console.debug("defaultFieldSelector");
if (config && config["rootElement"]) {
const selector = "input:not([type=hidden])";
return config.rootElement.querySelector(selector);

if (!config) {
if (enableDebug)
console.debug("config was not set");
return null;
}
const fieldDivSelector = "div[data-field-name='" + config.fieldReference + "']";
const fieldDiv = document.querySelector(fieldDivSelector);

if (!fieldDiv) {
if (enableDebug) {
console.log(config.fieldReference + " - unable to find div wrapper");
}
return null;
}

const selector = "input:not([type=hidden])";
return fieldDiv.querySelector(selector);
};

const typeText = (field, text, current = 0, time = typeTextDelay) => {
const l = text.length;
field.value += text[current];
if(current < l - 1) {
current++;
setTimeout(function(){typeText(field, text, current)}, time);
} else {
field.setAttribute('value',field.value);
}
};

const setNativeValue = (el, value) => {
const previousValue = el.value;

if (el.type === 'checkbox' || el.type === 'radio') {
if ((!!value && !el.checked) || (!!!value && el.checked)) {
el.click();
}
} else el.value = value;

const tracker = el._valueTracker;
if (tracker) {
tracker.setValue(previousValue);
}

el.dispatchEvent(new Event('change', { bubbles: true }));
};

const defaultFieldSetter = async (field, config) => {
const defaultFieldSetter = (field, config) => {
if (enableDebug)
console.debug("defaultFieldSetter");
if (config && config["fieldValue"]) {
Expand All @@ -124,9 +137,51 @@ const defaultFieldSetter = async (field, config) => {
const selectFieldSelector = (config) => {
if (enableDebug)
console.debug("selectFieldSelector");
if (config && config["fieldValue"]) {

if (!config) {
if (enableDebug)
console.debug("config was not set");
return null;
}

const fieldDivSelector = "div.dropdown-menu.ddm-btn-full.ddm-select-dropdown";
const fieldDivs = document.querySelectorAll(fieldDivSelector);
let fieldDiv;

if (fieldDivs.length === 0) {
if (enableDebug) {
console.log(config.fieldReference + " - Unable to find div wrapper");
}
return null;
} else if (fieldDivs.length === 1) {
fieldDiv = fieldDivs.item(0);
} else if (fieldDivs.length > 1 && !config["fieldConfig"]) {
console.warn(config.fieldReference + " - no fieldConfig was provided. Unable to identify the correct SelectFromList field. It will be skipped");
return null;
} else if (fieldDivs.length > 1 && config["fieldConfig"]) {
const fieldConfig = config.fieldConfig;
if (!fieldConfig["listPosition"]) {
console.warn(config.fieldReference + " - the fieldConfig did not contain the listPosition. Unable to identify the correct SelectFromList field. It will be skipped");
return null;
}

const index = fieldConfig.listPosition - 1;
if (index < 0 || index > fieldDivs.length) {
console.warn(config.fieldReference + " - the listPosition out of range of the fieldDiv collection. Unable to identify the correct SelectFromList field. It will be skipped");
return null;
}

fieldDiv = fieldDivs.item(index);
}

if (!fieldDiv) {
console.warn(config.fieldReference + " - unable to identify the correct SelectFromList field. It will be skipped");
return null;
}

if (config["fieldValue"]) {
const selector = "button[label='" + config.fieldValue + "'][class='dropdown-item']";
return document.querySelector(selector);
return fieldDiv.querySelector(selector);
}
return null;
};
Expand All @@ -147,10 +202,12 @@ const populateFields = (mapping) => {
return;
}

const queryStringObj = JSON.parse('{"' + decodeURI(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}');
const queryStringParameter = mapping.parameter;
const fieldReference = mapping.fieldReference;
const fieldValue = queryStringObj[queryStringParameter];
const fieldType = mapping.fieldType;
const fieldValue = searchObj[queryStringParameter];
const fieldReference = mapping.fieldReference;
const fieldConfig = mapping.fieldConfig;

if (!fieldValue) {
if (enableDebug)
Expand All @@ -166,16 +223,6 @@ const populateFields = (mapping) => {
fieldType
});

const fieldDivSelector = "div[data-field-name='" + fieldReference + "']";
const fieldDiv = document.querySelector(fieldDivSelector);

if (!fieldDiv) {
if (enableDebug) {
console.log("Unable to find div wrapper for " + fieldReference);
return;
}
}

let fieldSelectorFunc;
let fieldSetterFunc;

Expand All @@ -194,8 +241,8 @@ const populateFields = (mapping) => {

populateFieldRetry({
fieldReference,
rootElement: fieldDiv,
fieldValue,
fieldConfig
}, fieldSelectorFunc, fieldSetterFunc);
};

Expand Down

0 comments on commit 127ff86

Please sign in to comment.