CoreFiles by mySites.guru
joomla-field-fancy-select-es5.js264 lines, 11,391 bytes, from Joomla_4.0.0-alpha6-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha5
md5
fb2b21265a13c96788410627672bb686
sha1
67713b7329c4f7c9f0ebd92b7a3359ec77205569
sha256
503c40a38b4a17856b66e2f06587e1aa808d423128e6a9c132b8975221f10ac5
"use strict";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }

function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }

function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

/**
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

/**
 * Fancy select field, which use Choices.js
 *
 * Example:
 * <joomla-field-fancy-select ...attributes>
 *   <select>...</select>
 * </joomla-field-fancy-select>
 *
 * Possible attributes:
 *
 * allow-custom          Whether allow User to dynamically add a new value.
 * new-item-prefix=""    Prefix for a dynamically added value.
 *
 * remote-search         Enable remote search.
 * url=""                Url for remote search.
 * term-key="term"       Variable key name for searched term, will be appended to Url.
 *
 * min-term-length="1"   The minimum length a search value should be before choices are searched.
 * placeholder=""        The value of the inputs placeholder.
 * search-placeholder="" The value of the search inputs placeholder.
 */
;

(function (customElements) {
  "use strict";

  var JoomlaFieldFancySelect =
  /*#__PURE__*/
  function (_HTMLElement) {
    _inherits(JoomlaFieldFancySelect, _HTMLElement);

    function JoomlaFieldFancySelect() {
      _classCallCheck(this, JoomlaFieldFancySelect);

      return _possibleConstructorReturn(this, _getPrototypeOf(JoomlaFieldFancySelect).apply(this, arguments));
    }

    _createClass(JoomlaFieldFancySelect, [{
      key: "connectedCallback",
      value: function connectedCallback() {
        var _this = this;

        if (!window.Choices) {
          throw new Error('JoomlaFieldFancySelect require Choices.js to work');
        } // Get a <select> element


        this.select = this.querySelector('select');

        if (!this.select) {
          throw new Error('JoomlaFieldFancySelect require <select> element to work');
        } // Init Choices


        this.choicesInstance = new Choices(this.select, {
          placeholderValue: this.placeholder,
          searchPlaceholderValue: this.searchPlaceholder,
          removeItemButton: true,
          searchFloor: this.minTermLength,
          searchResultLimit: 10,
          shouldSort: false,
          fuseOptions: {
            threshold: 0.3 // Strict search

          },
          noResultsText: Joomla.Text._('JGLOBAL_SELECT_NO_RESULTS_MATCH', 'No results found'),
          noChoicesText: Joomla.Text._('JGLOBAL_SELECT_NO_RESULTS_MATCH', 'No results found'),
          itemSelectText: Joomla.Text._('JGLOBAL_SELECT_PRESS_TO_SELECT', 'Press to select'),
          // Redefine some classes
          classNames: {
            button: 'choices__button_joomla' // It is need because an original styling use unavailable Icon.svg file

          }
        }); // Collect an existing values, to avoid duplications

        this.choicesCache = {}; // Handle typing of custom term

        if (this.allowCustom) {
          this.addEventListener('keydown', function (event) {
            if (event.keyCode !== 13 || event.target !== _this.choicesInstance.input) return;
            event.preventDefault();
            if (_this.choicesInstance.highlightPosition || !event.target.value || _this.choicesCache[event.target.value]) return; // Make sure nothing is highlighted

            var highlighted = _this.choicesInstance.dropdown.querySelector('.' + _this.choicesInstance.config.classNames.highlightedState);

            if (highlighted) return;

            _this.choicesInstance.setChoices([{
              value: _this.newItemPrefix + event.target.value,
              label: event.target.value,
              selected: true,
              customProperties: {
                value: event.target.value // Store real value, just in case

              }
            }], 'value', 'label', false);

            _this.choicesCache[event.target.value] = event.target.value;
            event.target.value = null;

            _this.choicesInstance.hideDropdown();

            return false;
          });
        } // Handle remote search


        if (this.remoteSearch && this.url) {
          // Cache existing
          this.choicesInstance.presetChoices.forEach(function (choiceItem) {
            _this.choicesCache[choiceItem.value] = choiceItem.label;
          });
          var lookupDelay = 300;
          var lookupTimeout = null;
          this.activeXHR = null;
          this.select.addEventListener('search', function (event) {
            clearTimeout(lookupTimeout);
            lookupTimeout = setTimeout(_this.requestLookup.bind(_this), lookupDelay);
          });
        }
      }
    }, {
      key: "disconnectedCallback",
      value: function disconnectedCallback() {
        // Destroy Choices instance, to unbind an event listeners
        if (this.choicesInstance) {
          this.choicesInstance.destroy();
          this.choicesInstance = null;
        }

        if (this.activeXHR) {
          this.activeXHR.abort();
          this.activeXHR = null;
        }
      }
    }, {
      key: "requestLookup",
      value: function requestLookup() {
        var _this2 = this;

        var url = this.url;
        url += url.indexOf('?') === -1 ? '?' : '&';
        url += encodeURIComponent(this.termKey) + '=' + encodeURIComponent(this.choicesInstance.input.value); // Stop previous request if any

        if (this.activeXHR) {
          this.activeXHR.abort();
        }

        this.activeXHR = Joomla.request({
          url: url,
          onSuccess: function onSuccess(response, xhr) {
            _this2.activeXHR = null;
            var items = response ? JSON.parse(response) : [];
            if (!items.length) return; // Remove duplications

            var item;

            for (var i = items.length - 1; i >= 0; i--) {
              item = items[i];

              if (_this2.choicesCache[item.value]) {
                items.splice(i, 1);
              }
            } // Add new options to field, assume that each item is object, eg {value: "foo", text: "bar"}


            if (items.length) {
              _this2.choicesInstance.setChoices(items, 'value', 'text', false);
            }
          },
          onError: function onError() {
            _this2.activeXHR = null;
          }
        });
      }
    }, {
      key: "allowCustom",
      // Properties getters/setters
      get: function get() {
        return this.hasAttribute('allow-custom');
      }
    }, {
      key: "remoteSearch",
      get: function get() {
        return this.hasAttribute('remote-search');
      }
    }, {
      key: "url",
      get: function get() {
        return this.getAttribute('url');
      }
    }, {
      key: "termKey",
      get: function get() {
        return this.getAttribute('term-key') || 'term';
      }
    }, {
      key: "minTermLength",
      get: function get() {
        return parseInt(this.getAttribute('min-term-length')) || 1;
      }
    }, {
      key: "newItemPrefix",
      get: function get() {
        return this.getAttribute('new-item-prefix') || '';
      }
    }, {
      key: "placeholder",
      get: function get() {
        return this.getAttribute('placeholder');
      }
    }, {
      key: "searchPlaceholder",
      get: function get() {
        return this.getAttribute('search-placeholder');
      }
    }, {
      key: "value",
      get: function get() {
        return this.choicesInstance.getValue(true);
      },
      set: function set($val) {
        this.choicesInstance.setValueByChoice('' + $val);
      }
    }]);

    return JoomlaFieldFancySelect;
  }(_wrapNativeSuper(HTMLElement));

  customElements.define('joomla-field-fancy-select', JoomlaFieldFancySelect);
})(customElements);

No other release has this exact content. See every version of this file.