Sha256: 12ea51390ed393302fd1c7971ea7b648afbbaac3ce9478d3f333959501dafc42

Contents?: true

Size: 1.33 KB

Versions: 27

Compression:

Stored size: 1.33 KB

Contents

import { Controller } from '@hotwired/stimulus'
import TomSelect from 'tom-select'
import I18n from '../config/i18n'

export default class extends Controller {
  connect () {
    if (this.element.hasAttribute('multiple')) {
      this.initTomSelect()
    }
  }

  defaultOptions (locale) {
    const defaultOptions = {
      en: {
        render: {
          option_create: function (data, escape) {
            return '<div class="create">Add <strong>' + escape(data.input) + '</strong>&hellip;</div>'
          },
          no_results: function (data, escape) {
            return '<div class="no-results">No results found</div>'
          }
        }
      },
      nl: {
        render: {
          option_create: function (data, escape) {
            return '<div class="create">Voeg <strong>' + escape(data.input) + '</strong> toe &hellip;</div>'
          },
          no_results: function (data, escape) {
            return '<div class="no-results">Geen resultaten gevonden</div>'
          }
        }
      }
    }
    return defaultOptions[locale]
  }

  hasTags () {
    return this.element.dataset.tags === 'true'
  }

  initTomSelect () {
    const defaultOptions = this.defaultOptions(I18n.locale)
    const options = { create: this.hasTags() }

    /* eslint-disable no-new */
    new TomSelect(this.element, { ...defaultOptions, ...options })
  }
}

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
headmin-0.6.3 app/assets/javascripts/headmin/controllers/select_controller.js
formstrap-0.3.3 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.3.2 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.3.1 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.3.0 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.2.1 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.2.0 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.1.3 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.1.2 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.1.1 app/assets/javascripts/formstrap/controllers/select_controller.js
formstrap-0.1.0 app/assets/javascripts/formstrap/controllers/select_controller.js
headmin-0.6.2 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.6.1 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.6.0 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.5.9 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.5.8 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.5.7 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.5.6 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.5.5 app/assets/javascripts/headmin/controllers/select_controller.js
headmin-0.5.4 app/assets/javascripts/headmin/controllers/select_controller.js