Sha256: 5795dac0de3b449a970ff6913605a328df7e29a38c778200b6ee2e2530cd4257

Contents?: true

Size: 1.35 KB

Versions: 195

Compression:

Stored size: 1.35 KB

Contents

window.Alchemy = {} if typeof(window.Alchemy) is 'undefined'

# The Alchemy list filter
#
# The list items must have a name attribute.
#
# It hides all list items that don't match the term from filter input field.
#
class Alchemy.ListFilterHandler

  # Pass a input field with a data-alchemy-list-filter attribute to this constructor
  constructor: (filter) ->
    @filter_field = $(filter)
    @items = $(@filter_field.data('alchemy-list-filter'))
    @clear = @filter_field.siblings('.js_filter_field_clear')
    @_observe()

  _observe: ->
    @filter_field.on 'keyup', (e) =>
      @clear.show()
      @_filter @filter_field.val()
    @clear.click (e) =>
      e.preventDefault()
      @_clear()
    @filter_field.focus ->
      key.setScope('list_filter')
    key 'esc', 'list_filter', =>
      @_clear()
      @filter_field.blur()

  _filter: (term) ->
    @clear.hide() if term == ''
    @items.map ->
      item = $(this)
      # indexOf is much faster then match()
      if item.attr('name').toLowerCase().indexOf(term.toLowerCase()) != -1
        item.show()
      else
        item.hide()

  _clear: ->
    @filter_field.val ''
    @_filter ''

# Initializes an Alchemy.ListFilterHandler on all input fields with a data-alchemy-list-filter attribute.
#
Alchemy.ListFilter = (scope) ->
  $('[data-alchemy-list-filter]', scope).map ->
    new Alchemy.ListFilterHandler(this)

Version data entries

195 entries across 195 versions & 2 rubygems

Version Path
alchemy_cms-5.3.6 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.7 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.6 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.5 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.4 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.3 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.2 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.1 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.3.5 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.0 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.3.4 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.0.pre.rc7 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.3.3 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.3.2 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.3.1 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.3.0 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-6.0.0.pre.rc6 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.2.7 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.2.6 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee
alchemy_cms-5.1.10 app/assets/javascripts/alchemy/alchemy.list_filter.js.coffee