import Component from './component'; import Popover from './popover'; import Modal from './modal'; import { check as checkIcon, arrow_down as arrowIcon } from './icons'; import * as Helpers from './dom-helpers'; /* options class: String, appended to uniformSelect-edit button as class limit: int | false - number of options to limit to, or false to not limit showAll: function(button_options) to run if/when "Show All" is clicked label: string, used for mobile menu container: selector for where to render dropdown */ export default class Select extends Component { initialize (options = {}) { this.options = { label: false, class: "", showAll: function (button_options){ Helpers.removeClass(button_options.querySelectorAll('button.hide'), 'hide'); var button = button_options.querySelector('.uniformSelect-show-all'); button.parentNode.removeChild(button); return false; }, limit: 8, container: false } Object.assign(this.options, this.pick(options, Object.keys(this.options))); this.listenTo(this.el, 'change', this.renderSelected); this.listenTo(this.el, 'close', this.hideOptions); this.el.uniformSelect = this; this.activeIcon = document.createElement('span'); this.activeIcon.innerHTML = checkIcon; Helpers.addClass(this.activeIcon, 'uniformSelect-option-icon'); } remove () { Component.prototype.remove.apply(this, arguments); this.edit_button.parentNode.removeChild(this.edit_button); delete this.this.edit_button; this.activeIcon.parentNode.removeChild(this.activeIcon); delete this.activeIcon; if(this.button_options_popover) this.button_options_popover.remove(); if(this.button_options_modal) this.button_options_modal.remove(); if(this.button_options) { this.button_options.parentNode.removeChild(this.button_options); delete this.button_options; } } render () { this.edit_button = Helpers.createElement(` `); if (this.el.name) { Helpers.addClass(this.edit_button, this.el.name.toLowerCase().replace(/[^a-z0-9\-_]+/g, '-')); } this.el.style.display = "none"; this.el.insertAdjacentElement('beforebegin', this.edit_button); if(!this.options.container) { this.options.container = this.edit_button.parentElement } // Set Min-Width for when empty const option = [...this.el.querySelectorAll("option")].sort((a, b) => { return a.textContent.length > b.textContent.length }).reverse()[0]; this.edit_button.querySelector('.text-js').style.opacity = 0; this.edit_button.querySelector('.text-js').innerHTML = option.textContent; const min_width = this.edit_button.querySelector('.text-js').offsetWidth; this.edit_button.style.minWidth = min_width + "px"; this.edit_button.querySelector('.text-js').innerHTML = ""; this.edit_button.querySelector('.text-js').style.opacity = null; this.renderSelected(); this.listenTo(this.edit_button, 'click', this.showOptions); this.listenTo(this.edit_button, 'click', '.uniformSelect-remove', this.removeSelection); return this; } renderOptions () { this.button_options = Helpers.createElement("