Skip to content
Snippets Groups Projects
Commit e1b5c66c authored by Rodrigo Panchiniak Fernandes's avatar Rodrigo Panchiniak Fernandes
Browse files

Fixed switching of multiple submit IDs.

parent 2c6bd020
No related branches found
No related tags found
No related merge requests found
/**
* @file
* Protected Content dialog .
*
* @todo: we must apply this proc-decrypt-dialog library as a gate to all decrypt calls, or change its name to something
* less generic as proc-decrypt-dialog-default-format.
*
*/
(function($, Drupal, once, drupalSettings, window) {
Drupal.behaviors.ProcDecryptDialogBehavior = {
......
/**
* @file
* Helper for entity reference proc field.
*
* @todo: we must apply this proc-field library as a gate to all dialog calls, or change its name to something less
* generic.
*/
(function($, Drupal, drupalSettings, once) {
Drupal.behaviors.ProcBehavior = {
'attach': (context, settings) => {
attach(context, settings) {
once('proc-decrypt', 'html', context).forEach(function(element) {
const procFieldElements = document.querySelectorAll('[proc="true"]');
const procFields = [];
......@@ -22,68 +20,87 @@
});
if (drupalSettings.proc && drupalSettings.proc.submit_element_id) {
const isMultipleSubmits = (drupalSettings.proc.submit_element_id.search(',') > -1);
const submitIdsCsv = drupalSettings.proc.submit_element_id.replace(/ /g, '');
const submitIds = submitIdsCsv.split(',');
let submitElements = [];
submitIds.map((submitId, submitIndex) => {
submitElements.push(document.getElementById(submitId));
});
if (isMultipleSubmits) {
const submitIds = drupalSettings.proc.submit_element_id.split(',');
submitIds.forEach((submitIdValue) =>{
Drupal.behaviors.ProcBehavior.processSubmitButton(submitIdValue.trim(), procFields);
});
}
else {
const submitId = drupalSettings.proc.submit_element_id.trim();
Drupal.behaviors.ProcBehavior.processSubmitButton(submitId, procFields);
}
}
});
},
'processSubmitButton': (submit_element_id, procFields) => {
let submitElement = document.getElementById(submit_element_id);
procFields.map((fieldName, fieldIndex) => {
let input = document.querySelector(`[name^="${fieldName}"]`);
input.addEventListener("input", updateValue);
procFields.map((fieldName, fieldIndex) => {
var field = document.querySelector(`[name^="${fieldName}"]`);
field.oninput = () => {
if (submitElement.disabled === false && field.value != '') {
// Disable submit when something is typed in for encryption:
submitElement.disabled = true;
}
if (submitElement.disabled === true && field.value === '') {
submitElement.disabled = false;
}
};
var encryptCheckbox = document.querySelector(`[id="encrypt-checkbox-${fieldName}"]`);
function updateValue(e) {
if (e.target.value == '') {
// If current field is empty and all other fields are empty:
let empty = 1;
procFields.map((innerFieldName, innerFieldIndex) => {
let innerInput = document.querySelector(`[name^="${innerFieldName}"]`);
// Check if encryption checkbox is checked.
let innerCheckbox = document.querySelector(`[id="encrypt-checkbox-${innerFieldName}"]`);
if (innerInput.value != '' && !innerCheckbox.checked) {
empty = 0;
}
});
if (empty == 1) {
// Enable submit when all fields are empty:
submitElements.map((submitElement, submitIndex) => {
submitElement.disabled = false;
});
}
}
}
if (encryptCheckbox) {
encryptCheckbox.oninput = () => {
if (
encryptCheckbox.value === '1' &&
submitElement.disabled === true
) {
// If for all other fields there is no plain text:
let index = 0;
procFields.map((fieldName, fieldIndex) => {
if (
!document
.querySelector(`[name^="${fieldName}"]`)
.value.startsWith('******* (')
) {
index++;
const field = document.querySelector(`[name^="${fieldName}"]`);
field.oninput = () => {
submitElements.map((submitElement, submitIndex) => {
if (submitElement.disabled === false && field.value != '') {
// Disable submit when something is typed in for encryption:
submitElement.disabled = true;
}
});
if (index === 1) {
submitElement.disabled = false;
};
const encryptCheckbox = document.querySelector(
`[id="encrypt-checkbox-${fieldName}"]`,
);
if (encryptCheckbox) {
encryptCheckbox.addEventListener("input", updateCheckboxValue);
function updateCheckboxValue(checkBoxEvent) {
if (checkBoxEvent.target.checked) {
// If for all other checkboxes are also checked:
let checked = 1;
procFields.map((innerFieldName, innerFieldIndex) => {
let innerCheckbox = document.querySelector(`[id="encrypt-checkbox-${innerFieldName}"]`);
if (!innerCheckbox.checked) {
let innerInput = document.querySelector(`[name^="${innerFieldName}"]`);
checked = 0;
if (innerInput.value == '') {
checked = 1;
}
}
});
if (checked == 1) {
// Enable submit when all checkboxes are checked:
submitElements.map((submitElement, submitIndex) => {
submitElement.disabled = false;
});
}
}
else {
submitElements.map((submitElement, submitIndex) => {
if (submitElement.disabled === false) {
submitElement.disabled = true;
}
});
}
}
}
if (
encryptCheckbox.value === '0' &&
submitElement.disabled === false
) {
submitElement.disabled = true;
}
};
});
}
});
}
},
};
})(jQuery, Drupal, drupalSettings, once);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment