docbrown/app/javascript/admin/controllers/svg_icon_upload_controller.js
rhymes e5226b9951
Upgrade Stimulus to 3.0 (#14869)
* Upgrade Stimulus to 3.0

* Remove unused variable

* Bump postcss from 8.3.8 to 8.3.9 (#14956)

Bumps [postcss](https://github.com/postcss/postcss) from 8.3.8 to 8.3.9.
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](https://github.com/postcss/postcss/compare/8.3.8...8.3.9)

---
updated-dependencies:
- dependency-name: postcss
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-10-05 20:00:37 +02:00

60 lines
1.8 KiB
JavaScript

import { Controller } from '@hotwired/stimulus';
// eslint-disable-next-line no-restricted-syntax
export default class SvgIconUploadController extends Controller {
static targets = [
'svgIconContent',
'svgIconPreview',
'svgIconMessageValidate',
'navId',
];
selectSvgIcon(event) {
this.clearInvalidIconTypeMessage();
const icon = event.target.files[0];
if (icon.type !== 'image/svg+xml') {
this.invalidIconTypeMessage(icon.type);
const navigationLinkId = this.navIdTarget.attributes['nav-link-id'].value;
const ableToClearSvgIconContent =
!navigationLinkId && this.svgIconContentTarget.value;
if (ableToClearSvgIconContent) {
this.svgIconContentTarget.value = null;
this.setSvgIconPreview(null);
}
return;
}
const reader = new FileReader();
reader.readAsText(icon);
reader.onload = (content) => {
const { result } = content.target;
this.svgIconContentTarget.value = result;
this.setSvgIconPreview(result);
};
}
setSvgIconPreview(content) {
this.svgIconPreviewTarget.innerHTML = content;
if (content) this.svgIconPreviewTarget.classList.add('pb-3');
else if (this.svgIconPreviewTarget.classList.length !== 0)
this.svgIconPreviewTarget.classList.remove('pb-3');
}
clearInvalidIconTypeMessage() {
if (this.svgIconMessageValidateTarget.classList.length !== 0) {
this.svgIconMessageValidateTarget.classList.remove(
'alert',
'alert-danger',
);
this.svgIconMessageValidateTarget.innerHTML = null;
}
}
invalidIconTypeMessage(type) {
this.svgIconMessageValidateTarget.classList.add('alert', 'alert-danger');
this.svgIconMessageValidateTarget.innerHTML = `'${type}' is an invalid Icon type`;
}
}