Sha256: 47d9fb994dabd97558a6c6a5988558c21687005dceab01a90cf841eabffc93ee

Contents?: true

Size: 1.64 KB

Versions: 25

Compression:

Stored size: 1.64 KB

Contents

window.AA.CheckboxToggler = class AA.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.bind "change", (e) =>
      @_didChangeCheckbox(e.target)
      
    @toggle_all_checkbox.bind "change", (e) =>
      @_didChangeToggleAllCheckbox()

  _didChangeCheckbox: (checkbox) ->
    if @checkboxes.filter(":checked").length == @checkboxes.length - 1
      @_uncheckToggleAllCheckbox()
    else if @checkboxes.filter(":checked").length == @checkboxes.length
      @_checkToggleAllCheckbox()

  _didChangeToggleAllCheckbox: ->
    if @toggle_all_checkbox.attr("checked") == "checked"
      @_checkAllCheckboxes()
    else
      @_uncheckAllCheckboxes()      

  _uncheckToggleAllCheckbox: ->
    @toggle_all_checkbox.removeAttr("checked")

  _checkToggleAllCheckbox: ->
    @toggle_all_checkbox.attr("checked","checked")

  _uncheckAllCheckboxes: ->
    @checkboxes.each (index, el) =>
      $(el).removeAttr("checked")
      @_didChangeCheckbox(el)

  _checkAllCheckboxes: ->
    @checkboxes.each (index, el) =>
      $(el).attr("checked","checked")
      @_didChangeCheckbox(el)
      

( ( $ ) ->
  $.widget.bridge 'checkboxToggler', AA.CheckboxToggler
)( jQuery )

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
activeadmin-0.5.1 app/assets/javascripts/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.12 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.11 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.10 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.9 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.8 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.7 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
activeadmin-0.5.0 app/assets/javascripts/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
activeadmin-0.5.0.pre1 app/assets/javascripts/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
activeadmin-0.5.0.pre app/assets/javascripts/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.6 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.5 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.4 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.3 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.2 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.1 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.1.0 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.0.9 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.0.8 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee
ems-0.0.7 app/assets/javascripts/ems/active_admin/components/jquery.aa.checkbox-toggler.js.coffee