Skip to content

Commit

Permalink
EC-288: Uncaught TypeError: Cannot read property focus of undefined (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Grabowski authored Dec 22, 2020
1 parent 14692f7 commit c982d65
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/bundle/Resources/public/js/scripts/fieldType/ezimage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function (global) {
(function(global) {
const SELECTOR_FIELD = '.ez-field-edit--ezimage';
const SELECTOR_INPUT_FILE = 'input[type="file"]';
const SELECTOR_LABEL_WRAPPER = '.ez-field-edit__label-wrapper';
Expand All @@ -16,7 +16,7 @@
getImageUrl(file, callback) {
const reader = new FileReader();

reader.onload = event => callback(event.target.result);
reader.onload = (event) => callback(event.target.result);
reader.readAsDataURL(file);
}

Expand All @@ -35,7 +35,7 @@
const files = [].slice.call(event.target.files);
const fileSize = this.formatFileSize(files[0].size);

this.getImageUrl(files[0], url => image.setAttribute('src', url));
this.getImageUrl(files[0], (url) => image.setAttribute('src', url));

nameContainer.innerHTML = files[0].name;
nameContainer.title = files[0].name;
Expand All @@ -48,6 +48,15 @@
}

class EzImageFieldValidator extends global.eZ.BaseFileFieldValidator {
toggleInvalidState(isError, config, input) {
super.toggleInvalidState(isError, config, input);

const container = input.closest('.ez-field-edit--ezimage');
const method = !!container.querySelector(`.${this.classInvalid}`) ? 'add' : 'remove';

container.classList[method](this.classInvalid);
}

/**
* Validates the alternative text input
*
Expand All @@ -59,7 +68,7 @@
validateAltInput(event) {
const isRequired = event.target.required;
const isEmpty = !event.target.value;
const isError = (isEmpty && isRequired);
const isError = isEmpty && isRequired;
const label = event.target.closest(SELECTOR_ALT_WRAPPER).querySelector('.ez-data-source__label').innerHTML;
const result = { isError };

Expand All @@ -71,7 +80,7 @@
}
}

[...document.querySelectorAll(SELECTOR_FIELD)].forEach(fieldContainer => {
[...document.querySelectorAll(SELECTOR_FIELD)].forEach((fieldContainer) => {
const validator = new EzImageFieldValidator({
classInvalid: 'is-invalid',
fieldContainer,
Expand Down Expand Up @@ -103,19 +112,17 @@
callback: 'cancelErrors',
invalidStateSelectors: ['.ez-data-source__field--alternativeText'],
errorNodeSelectors: [`${SELECTOR_ALT_WRAPPER} .ez-data-source__label-wrapper`],
}
},
],
});
const previewField = new EzImageFilePreviewField({
validator,
fieldContainer,
fileTypeAccept: fieldContainer.querySelector(SELECTOR_INPUT_FILE).accept
fileTypeAccept: fieldContainer.querySelector(SELECTOR_INPUT_FILE).accept,
});

previewField.init();

global.eZ.fieldTypeValidators = global.eZ.fieldTypeValidators ?
[...global.eZ.fieldTypeValidators, validator] :
[validator];
})
global.eZ.fieldTypeValidators = global.eZ.fieldTypeValidators ? [...global.eZ.fieldTypeValidators, validator] : [validator];
});
})(window);

0 comments on commit c982d65

Please # to comment.