Sha256: 8a94553bf7f8367773686f7fce09ba1a28e7a74ad5dcb2d73f28ea63ada03f86

Contents?: true

Size: 1.83 KB

Versions: 77

Compression:

Stored size: 1.83 KB

Contents

unless String::contains
  String::contains = (args...) ->
    @indexOf(args...) != -1

KEY =
  UP: 38
  DOWN: 40
  ENTER: 13

class StackSearch

  constructor: (root) ->
    @$root = $(root)
    @$root.on('keyup', '.stack-search', @onKeyUp)
    @$root.on('click', '.show-all-stacks', (event) =>
      @$root.find('.not-matching').removeClass('not-matching')
      event.preventDefault()
    )

  onKeyUp: (event) =>
    @$items = @$root.find('[data-search]')
    switch event.keyCode
      when KEY.ENTER
        event.preventDefault()
        @goToSelectedStack()
      when KEY.UP
        event.preventDefault()
        @selectPrevious()
      when KEY.DOWN
        event.preventDefault()
        @selectNext()
      else
        @filterResults($.trim($(event.target).val()).toLowerCase())

  filterResults: (query) ->
    if query
      for item in @$items
        $item = $(item)
        $item.toggleClass('not-matching', !$item.attr('data-search').toLowerCase().contains(query))
      @selectFirst()
    else
      @$items.removeClass('not-matching')

  selectFirst: ->
    @$items.removeClass('selected').first(':not(.not-matching)').addClass('selected')

  selectNext: ->
    $next = @$items.filter('.selected').removeClass('selected').nextAll(':not(.not-matching)').first()
    $next = @$items.filter(':not(.not-matching)').first() unless $next.length
    $next.addClass('selected')

  selectPrevious: ->
    $previous = @$items.filter('.selected').removeClass('selected').prevAll(':not(.not-matching)').first()
    $previous = @$items.filter(':not(.not-matching)').last() unless $previous.length
    $previous.addClass('selected')

  goToSelectedStack: ->
    if stack = @$items.filter('.selected').filter(':not(.not-matching)').find('.commits-path').attr('href')
      window.location = stack

search = new StackSearch(document)

jQuery ->
  $('.stack-search').focus()

Version data entries

77 entries across 77 versions & 1 rubygems

Version Path
shipit-engine-0.39.0 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.38.0 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.37.0 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.36.1 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.36.0 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.35.1 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.35.0 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.34.0 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.33.0 app/assets/javascripts/shipit/stack_search.js.coffee
shipit-engine-0.32.0 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.31.0 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.30.0 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.29.0 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.28.1 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.28.0 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.27.1 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.27.0 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.26.0 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.25.1 app/assets/javascripts/shipit/search.js.coffee
shipit-engine-0.25.0 app/assets/javascripts/shipit/search.js.coffee