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
sc-frank-ios-1.2.3 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.2.5 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.2.4.pre1 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-pivotal-1.3.0.pre.2 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.bba52d7.debug frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.e1e3ccb.debug frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.1a856ca.debug frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-pivotal-1.3.0.pre.1 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-pivotal-1.2.4 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-pivotal-1.2.3.pre.2 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-pivotal-1.2.3.pre.1 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-pivotal-1.2.3 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.b7791c5.debug frank-skeleton/frank_static_resources.bundle/bundle/bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.b7791c5.debug frank-skeleton/frank_static_resources.bundle/bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.b7791c5.debug frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
frank-cucumber-1.2.3 frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.3256dc1.debug frank-skeleton/frank_static_resources.bundle/bundle/bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.3256dc1.debug frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.3256dc1.debug frank-skeleton/frank_static_resources.bundle/bundle/js/dropdown_control.coffee
sc-frank-cucumber-1.2.1.9c95c03.debug frank-skeleton/frank_static_resources.bundle/js/dropdown_control.coffee