Sha256: 664aee7aa7777c8a14db752df19c817d6154e1d0fa9f74bf72347a80928557bc

Contents?: true

Size: 1.32 KB

Versions: 14

Compression:

Stored size: 1.32 KB

Contents

class ActiveAdmin.CheckboxToggler
  constructor: (@options, @container)->
    defaults = {}
    @options = $.extend defaults, @options
    @_init()
    @_bind()

  _init: ->
    if not @container
      throw new Error('Container element not found')
    else
      @$container = $(@container)

    if not @$container.find('.toggle_all').length
      throw new Error('"toggle all" checkbox not found')
    else
      @toggle_all_checkbox = @$container.find '.toggle_all'

    @checkboxes = @$container.find(':checkbox').not @toggle_all_checkbox

  _bind: ->
    @checkboxes.change       (e)=> @_didChangeCheckbox e.target
    @toggle_all_checkbox.change => @_didChangeToggleAllCheckbox()

  _didChangeCheckbox: (checkbox)->
    numChecked = @checkboxes.filter(':checked').length

    allChecked = numChecked == @checkboxes.length
    someChecked = numChecked > 0 && numChecked < @checkboxes.length

    @toggle_all_checkbox.prop checked: allChecked, indeterminate: someChecked

  _didChangeToggleAllCheckbox: ->
    setting = @toggle_all_checkbox.prop 'checked'
    @checkboxes.prop checked: setting
    setting

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

$.widget.bridge 'checkboxToggler', ActiveAdmin.CheckboxToggler

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
activeadmin-rb-1.6.0 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-rb-1.5.2 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-rb-1.5.1 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-rb-1.5.0 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-rb-1.4.0 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.4.3 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.4.2 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.4.1 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.4.0 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.3.1 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.3.0 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.2.1 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.2.0 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee
activeadmin-1.1.0 app/assets/javascripts/active_admin/lib/checkbox-toggler.js.coffee