file.js

  1. import { queryOne } from '@ecl/dom-utils';
  2. /**
  3. * @param {HTMLElement} element DOM element for component instantiation and scope
  4. * @param {Object} options
  5. * @param {String} options.translationToggleSelector Selector for toggling translatoins section
  6. * @param {String} options.translationContainerSelector Selector for translations section container
  7. * @param {Boolean} options.attachClickListener Whether or not to bind click events on toggle
  8. */
  9. export class FileDownload {
  10. /**
  11. * @static
  12. * Shorthand for instance creation and initialisation.
  13. *
  14. * @param {HTMLElement} root DOM element for component instantiation and scope
  15. *
  16. * @return {FileDownload} An instance of FileDownload.
  17. */
  18. static autoInit(root, { FILE_DOWNLOAD: defaultOptions = {} } = {}) {
  19. const fileDownload = new FileDownload(root, defaultOptions);
  20. fileDownload.init();
  21. root.ECLFileDownload = fileDownload;
  22. return fileDownload;
  23. }
  24. constructor(
  25. element,
  26. {
  27. translationToggleSelector = '[data-ecl-file-translation-toggle]',
  28. translationContainerSelector = '[data-ecl-file-translation-container]',
  29. attachClickListener = true,
  30. } = {},
  31. ) {
  32. // Check element
  33. if (!element || element.nodeType !== Node.ELEMENT_NODE) {
  34. throw new TypeError(
  35. 'DOM element should be given to initialize this widget.',
  36. );
  37. }
  38. this.element = element;
  39. // Options
  40. this.translationToggleSelector = translationToggleSelector;
  41. this.translationContainerSelector = translationContainerSelector;
  42. this.attachClickListener = attachClickListener;
  43. // Private variables
  44. this.translationToggle = null;
  45. this.translationContainer = null;
  46. // Bind `this` for use in callbacks
  47. this.handleClickOnToggle = this.handleClickOnToggle.bind(this);
  48. }
  49. /**
  50. * Initialise component.
  51. */
  52. init() {
  53. if (!ECL) {
  54. throw new TypeError('Called init but ECL is not present');
  55. }
  56. ECL.components = ECL.components || new Map();
  57. this.translationToggle = queryOne(
  58. this.translationToggleSelector,
  59. this.element,
  60. );
  61. this.translationContainer = queryOne(
  62. this.translationContainerSelector,
  63. this.element,
  64. );
  65. // Bind click event on toggle
  66. if (this.attachClickListener && this.translationToggle) {
  67. this.translationToggle.addEventListener(
  68. 'click',
  69. this.handleClickOnToggle,
  70. );
  71. }
  72. // Set ecl initialized attribute
  73. this.element.setAttribute('data-ecl-auto-initialized', 'true');
  74. ECL.components.set(this.element, this);
  75. }
  76. /**
  77. * Destroy component.
  78. */
  79. destroy() {
  80. if (this.attachClickListener && this.translationToggle) {
  81. this.translationToggle.removeEventListener(
  82. 'click',
  83. this.handleClickOnToggle,
  84. );
  85. }
  86. if (this.element) {
  87. this.element.removeAttribute('data-ecl-auto-initialized');
  88. ECL.components.delete(this.element);
  89. }
  90. }
  91. /**
  92. * @param {Event} e
  93. */
  94. handleClickOnToggle(e) {
  95. e.preventDefault();
  96. if (this.translationToggle.getAttribute('aria-expanded') === 'true') {
  97. this.translationToggle.setAttribute('aria-expanded', 'false');
  98. } else {
  99. this.translationToggle.setAttribute('aria-expanded', 'true');
  100. }
  101. return this;
  102. }
  103. }
  104. export default FileDownload;