Sha256: 00b5a7526b407992e26e1c85a90ecfc66dd508ff523cc8730785c96826d7727e

Contents?: true

Size: 1.83 KB

Versions: 35

Compression:

Stored size: 1.83 KB

Contents

# https://select2.github.io/examples.html

formatWithGlyphicon = (data, container) ->
  if data.element && data.element.className
    $("<span><i class='glyphicon #{data.element.className}'></i> #{data.text}</span>")
  else
    data.text

(this.EffectiveBootstrap || {}).effective_select = ($element, options) ->
  switch options['template']
    when 'glyphicon'
      options['templateResult'] = formatWithGlyphicon
      options['templateSelection'] = formatWithGlyphicon

  $select = $element.select2(options)

  # effective_select custom class functionality
  # select2 doesn't provide an initializer to add css classes to its input, so we manually support this feature
  # js_options[:containerClass] and js_options[:dropdownClass]
  $select.data('select2').$container.addClass(options['containerClass']) if options['containerClass']
  $select.data('select2').$dropdown.addClass(options['dropdownClass']) if options['dropdownClass']

$(document).on 'turbolinks:before-cache', ->
  $('select.effective_select.initialized').each (i, element) ->
    $input = $(element)
    $input.select2('destroy') if $input.data('select2')
    $input.removeClass('initialized')

# If we're working with a polymorphic select, split out the ID and assign the hidden _type and _id fields
$(document).on 'change', "select.effective_select.polymorphic", (event) ->
  $select = $(event.target)
  value = $select.val() || ''

  $select.siblings("input[type='hidden'][name$='_type]']").val(value.split('_')[0] || '')
  $select.siblings("input[type='hidden'][name$='_id]']").val(value.split('_')[1] || '')

# Keep the order of selected tags
# Fixes https://github.com/select2/select2/issues/3106
$(document).on 'select2:select', 'select', (event) ->
  $el = $(event.params.data.element)

  if $el.closest('select').hasClass('tags-input')
    $(this).append($el.detach()).trigger('change')
  true

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
effective_bootstrap-0.1.5 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.1.4 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.1.3 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.1.2 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.1.1 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.1.0 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.29 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.28 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.27 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.26 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.25 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.24 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.23 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.22 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.21 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.20 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.19 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.18 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.17 app/assets/javascripts/effective_select/initialize.js.coffee
effective_bootstrap-0.0.16 app/assets/javascripts/effective_select/initialize.js.coffee