CoreFiles by mySites.guru
joomla-field-user.js170 lines, 4,666 bytes, from Joomla_4.0.0-beta5-Beta-Full_Package.zip
Raw History Compare with 4.0.0-beta4
md5
cc5ee8c02f37134dffa6dc83c5cdeb56
sha1
a6613a789a9e3af1743d807c69464867cdc74527
sha256
20300a7c71326db4ede254da459cd797e25ef8c09b6c82d1d70d73d545e64bf7
"use strict";

(() => {
  class JoomlaFieldUser extends HTMLElement {
    constructor() {
      super();
      this.onUserSelect = '';
      this.onchangeStr = '';
      this.buttonClick = this.buttonClick.bind(this);
      this.iframeLoad = this.iframeLoad.bind(this);
    }

    static get observedAttributes() {
      return ['url', 'modal-class', 'modal-width', 'modal-height', 'input', 'input-name', 'button-select'];
    }

    get url() {
      return this.getAttribute('url');
    }

    set url(value) {
      this.setAttribute('url', value);
    }

    get modalClass() {
      return this.getAttribute('modal');
    }

    set modalClass(value) {
      this.setAttribute('modal', value);
    }

    get modalWidth() {
      return this.getAttribute('modal-width');
    }

    set modalWidth(value) {
      this.setAttribute('modal-width', value);
    }

    get modalHeight() {
      return this.getAttribute('modal-height');
    }

    set modalHeight(value) {
      this.setAttribute('modal-height', value);
    }

    get inputId() {
      return this.getAttribute('input');
    }

    set inputId(value) {
      this.setAttribute('input', value);
    }

    get inputNameClass() {
      return this.getAttribute('input-name');
    }

    set inputNameClass(value) {
      this.setAttribute('input-name', value);
    }

    get buttonSelectClass() {
      return this.getAttribute('button-select');
    }

    set buttonSelectClass(value) {
      this.setAttribute('button-select', value);
    }

    connectedCallback() {
      // Set up elements
      this.modal = this.querySelector(this.modalClass);
      this.modalBody = this.querySelector('.modal-body');
      this.input = this.querySelector(this.inputId);
      this.inputName = this.querySelector(this.inputNameClass);
      this.buttonSelect = this.querySelector(this.buttonSelectClass); // Bind events

      this.modalClose = this.modalClose.bind(this);
      this.setValue = this.setValue.bind(this); // Bootstrap modal init

      if (Joomla.Bootstrap && Joomla.Bootstrap.initModal && typeof Joomla.Bootstrap.initModal === 'function') {
        Joomla.Bootstrap.initModal(this.modal);
      }

      if (this.buttonSelect) {
        this.buttonSelect.addEventListener('click', this.modalOpen.bind(this));
        this.modal.addEventListener('hide', this.removeIframe.bind(this)); // Check for onchange callback,

        this.onchangeStr = this.input.getAttribute('data-onchange');

        if (this.onchangeStr) {
          /* eslint-disable */
          this.onUserSelect = new Function(this.onchangeStr);
          this.input.addEventListener('change', this.onUserSelect);
          /* eslint-enable */
        }
      }
    }

    disconnectedCallback() {
      if (this.onchangeStr && this.input) {
        this.input.removeEventListener('change', this.onUserSelect);
      }

      if (this.buttonSelect) {
        this.buttonSelect.removeEventListener('click', this);
      }

      if (this.modal) {
        this.modal.removeEventListener('hide', this);
      }
    }

    buttonClick({
      target
    }) {
      this.setValue(target.getAttribute('data-user-value'), target.getAttribute('data-user-name'));
      this.modalClose();
    }

    iframeLoad() {
      const iframeDoc = this.iframeEl.contentWindow.document;
      const buttons = [].slice.call(iframeDoc.querySelectorAll('.button-select'));
      buttons.forEach(button => {
        button.addEventListener('click', this.buttonClick);
      });
    } // Opens the modal


    modalOpen() {
      // Reconstruct the iframe
      this.removeIframe();
      const iframe = document.createElement('iframe');
      iframe.setAttribute('name', 'field-user-modal');
      iframe.src = this.url.replace('{field-user-id}', this.input.getAttribute('id'));
      iframe.setAttribute('width', this.modalWidth);
      iframe.setAttribute('height', this.modalHeight);
      this.modalBody.appendChild(iframe);
      this.modal.open();
      this.iframeEl = this.modalBody.querySelector('iframe'); // handle the selection on the iframe

      this.iframeEl.addEventListener('load', this.iframeLoad);
    } // Closes the modal


    modalClose() {
      Joomla.Modal.getCurrent().close();
      this.modalBody.innerHTML = '';
    } // Remove the iframe


    removeIframe() {
      this.modalBody.innerHTML = '';
    } // Sets the value


    setValue(value, name) {
      this.input.setAttribute('value', value);
      this.inputName.setAttribute('value', name || value); // trigger change event

      this.input.dispatchEvent(new Event('change'));
    }

  }

  customElements.define('joomla-field-user', JoomlaFieldUser);
})();

The same content is in 2 releases, Joomla 4.0.0-beta5 to 4.0.0-beta6. See them all, or every version of this file.