var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } (function ($) { 'use strict'; var _defaults = { classes: '' }; /** * @class * */ var Select = function (_Component) { _inherits(Select, _Component); /** * Construct Select instance * @constructor * @param {Element} el * @param {Object} options */ function Select(el, options) { _classCallCheck(this, Select); var _this = _possibleConstructorReturn(this, (Select.__proto__ || Object.getPrototypeOf(Select)).call(this, Select, el, options)); _this.el.M_Select = _this; /** * Options for the select * @member Select#options */ _this.options = $.extend({}, Select.defaults, options); _this.isMultiple = _this.$el.prop('multiple'); // Setup _this._keysSelected = {}; _this._valueDict = {}; // Maps key to original and generated option element. _this._setupDropdown(); _this._setupEventHandlers(); return _this; } _createClass(Select, [{ key: 'destroy', /** * Teardown component */ value: function destroy() { this._removeEventHandlers(); this._removeDropdown(); this.el.M_Select = undefined; } /** * Setup Event Handlers */ }, { key: '_setupEventHandlers', value: function _setupEventHandlers() { var _this2 = this; this._handleSelectChangeBound = this._handleSelectChange.bind(this); this._handleOptionClickBound = this._handleOptionClick.bind(this); this._handleInputClickBound = this._handleInputClick.bind(this); $(this.dropdownOptions).find('li:not(.optgroup)').each(function (el) { el.addEventListener('click', _this2._handleOptionClickBound); }); this.el.addEventListener('change', this._handleSelectChangeBound); this.input.addEventListener('click', this._handleInputClickBound); } /** * Remove Event Handlers */ }, { key: '_removeEventHandlers', value: function _removeEventHandlers() { var _this3 = this; $(this.dropdownOptions).find('li:not(.optgroup)').each(function (el) { el.removeEventListener('click', _this3._handleOptionClickBound); }); this.el.removeEventListener('change', this._handleSelectChangeBound); this.input.removeEventListener('click', this._handleInputClickBound); this.input.removeEventListener('focus', this._handleInputFocusBound); } /** * Handle Select Change * @param {Event} e */ }, { key: '_handleSelectChange', value: function _handleSelectChange(e) { this._setValueToInput(); } /** * Handle Option Click * @param {Event} e */ }, { key: '_handleOptionClick', value: function _handleOptionClick(e) { e.preventDefault(); var option = $(e.target).closest('li')[0]; var key = option.id; if (!$(option).hasClass('disabled') && !$(option).hasClass('optgroup') && key.length) { var selected = true; if (this.isMultiple) { // Deselect placeholder option if still selected. var placeholderOption = $(this.dropdownOptions).find('li.disabled.selected'); if (placeholderOption.length) { placeholderOption.removeClass('selected'); placeholderOption.find('input[type="checkbox"]').prop('checked', false); this._toggleEntryFromArray(placeholderOption[0].id); } var checkbox = $(option).find('input[type="checkbox"]'); checkbox.prop('checked', !checkbox.prop('checked')); selected = this._toggleEntryFromArray(key); } else { $(this.dropdownOptions).find('li').removeClass('active'); $(option).toggleClass('active'); this.input.value = option.textContent; } this._activateOption($(this.dropdownOptions), option); $(this._valueDict[key].el).prop('selected', selected); this.$el.trigger('change'); } e.stopPropagation(); } /** * Handle Input Click */ }, { key: '_handleInputClick', value: function _handleInputClick() { if (this.dropdown && this.dropdown.isOpen) { this._setValueToInput(); this._setSelectedStates(); } } /** * Setup dropdown */ }, { key: '_setupDropdown', value: function _setupDropdown() { var _this4 = this; this.wrapper = document.createElement('div'); $(this.wrapper).addClass('select-wrapper' + ' ' + this.options.classes); this.$el.before($(this.wrapper)); this.wrapper.appendChild(this.el); if (this.el.disabled) { this.wrapper.classList.add('disabled'); } // Create dropdown this.$selectOptions = this.$el.children('option, optgroup'); this.dropdownOptions = document.createElement('ul'); this.dropdownOptions.id = 'select-options-' + M.guid(); $(this.dropdownOptions).addClass('dropdown-content select-dropdown ' + (this.isMultiple ? 'multiple-select-dropdown' : '')); // Create dropdown structure. if (this.$selectOptions.length) { this.$selectOptions.each(function (el) { if ($(el).is('option')) { // Direct descendant option. var optionEl = void 0; if (_this4.isMultiple) { optionEl = _this4._appendOptionWithIcon(_this4.$el, el, 'multiple'); } else { optionEl = _this4._appendOptionWithIcon(_this4.$el, el); } _this4._addOptionToValueDict(el, optionEl); } else if ($(el).is('optgroup')) { // Optgroup. var selectOptions = $(el).children('option'); $(_this4.dropdownOptions).append($('