Sha256: 94f424d83ce0d9e3416be0de030ffa747afee6e2e1f5e6f4a2f2b405b1ad831e

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

class ActiveAdmin.DropdownMenu

  constructor: (@options, @element) ->
    @$element = $(@element)

    defaults = {
      fadeInDuration: 20,
      fadeOutDuration: 100,
      onClickActionItemCallback: null
    }

    @options = $.extend defaults, options
    @isOpen  = false

    @$menuButton = @$element.find '.dropdown_menu_button'
    @$menuList   = @$element.find '.dropdown_menu_list_wrapper'

    @_buildMenuList()
    @_bind()

  open: ->
    @isOpen = true
    @$menuList.fadeIn @options.fadeInDuration

    @_positionMenuList()
    @_positionNipple()
    @


  close: ->
    @isOpen = false
    @$menuList.fadeOut this.options.fadeOutDuration
    @

  destroy: ->
    @$element.unbind()
    @$element = null
    @

  isDisabled: ->
    @$menuButton.hasClass 'disabled'

  disable: ->
    @$menuButton.addClass 'disabled'

  enable: ->
    @$menuButton.removeClass 'disabled'

  option: (key, value) ->
    if $.isPlainObject(key)
      @options = $.extend(true, @options, key)
    else if key?
      @options[key]
    else
      @options[key] = value

  # Private

  _buildMenuList: ->
    @$nipple = $('<div class="dropdown_menu_nipple"></div>')
    @$menuList.prepend @$nipple
    @$menuList.hide()

  _bind: ->
    $('body').click =>
      @close() if @isOpen

    @$menuButton.click =>
      unless @isDisabled()
        if @isOpen then @close() else @open()
      false

  _positionMenuList: ->
    button_center = @$menuButton.position().left + @$menuButton.outerWidth() / 2
    menu_center   = @$menuList.outerWidth() / 2
    @$menuList.css 'left', button_center - menu_center

  _positionNipple: ->
    @$menuList.css 'top',  @$menuButton.position().top + @$menuButton.outerHeight() + 10
    @$nipple.css   'left', @$menuList.outerWidth() / 2 - @$nipple.outerWidth() / 2

$.widget.bridge 'aaDropdownMenu', ActiveAdmin.DropdownMenu

$ ->
  $('.dropdown_menu').aaDropdownMenu()

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
active_administration-0.0.3 app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee
activeadministration-0.0.2 app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee
active_administration-0.0.2 app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee
activeadministration-0.0.1 app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee
active_administration-0.0.1 app/assets/javascripts/active_admin/lib/dropdown-menu.js.coffee