Sha256: c32e1ccc8b45fcd8c27d569d2f6aba631f9f5af4c74be704d2ec518cf3413198

Contents?: true

Size: 1.53 KB

Versions: 71

Compression:

Stored size: 1.53 KB

Contents

define ->

  DropdownModel = Backbone.Model.extend
    select: ->
      @collection.each (model) =>
        model.set( 'selected', model == @ )
      @trigger('option-clicked',@)

  DropdownCollection = Backbone.Collection.extend
    model: DropdownModel

  DropdownView = Backbone.View.extend
    tagName: 'div'
    className: 'dropdown'

    events:
      "click .drop-indicator": 'clickedDrop'
      "click .button": 'clickedItem'

    initialize: ->
      @collection = new DropdownCollection()
      @collection.on 'all', _.bind(@render,@)
      
      # hackery :(
      $('body').on 'click', _.bind(@clickedOutsideDropdown,@)

    selectedModel: ->
      selected = @collection.find (m)-> m.get('selected')
      selected || @collection.at(0)
    unselectedModels: ->
      _(@collection.reject (m)-> m.get('selected'))

    createButtonFor: (model)->
      $("""<button>#{model.get('text')}</button>""")
        .on 'click', => @clickedItem(model)

    render: ->
      @$el
        .empty()
        .append( @createButtonFor(@selectedModel()) )
        .append( $ul = $("<ul>") )

      @unselectedModels().each (model)=>
        $ul.append( @createButtonFor(model) )

      @$el.append( """<div class="drop-indicator">v</div>""" )

    clickedDrop: (e)->
      e.stopPropagation() #prevent clickedOutsideDropdown from firing
      @$('ul').toggleClass('shown')

    clickedOutsideDropdown: ->
      @$('ul').removeClass('shown')

    clickedItem: (model)->
      model.select()




  {
    DropdownModel : DropdownModel
    DropdownView  : DropdownView
  }

Version data entries

71 entries across 54 versions & 5 rubygems

Version Path
frank-cucumber-1.1.5 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.1.4.pre1 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.1.3 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.1.3.pre1 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.1.2 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.1.1 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.1.0 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.0.0 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-0.9.8 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-0.9.7 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.0.0.pre2 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee