aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/selectable_table.coffee
blob: 1b4b95c0b49b5b592457a0fddeb99a29c5b87611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@selectTable = ->
  $('.select_table').each ->
    selection = []
    toolbox = $(this).children('.select_toolbox')

    $(this).on 'click', "[type='checkbox']", (e)->
      if e.currentTarget.id == '0'
        selection = []
        if e.currentTarget.checked
          $(this).closest('.table').find("[type='checkbox']").each ->
            $(this).prop('checked', true)
            # Add each element to selection
            selection.push($(this).attr('id'))

          # Remove th checkbox from selection
          selection.splice(0, 1)

        else
          $(this).closest('.table').find("[type='checkbox']").each ->
            $(this).prop('checked', false)
          # Empty selection
          selection = []

      else
        if e.currentTarget.checked
          selection.push(e.currentTarget.id)
        else
          elm = selection.indexOf(e.currentTarget.id)
          selection.splice(elm, 1)

      # Updating toolbox, according to selection
      if selection.length > 0
        toolbox
          .removeClass 'noselect'
          .children('.info-msg').children('span').text(selection.length)
      else
        toolbox
          .addClass 'noselect'
          .children('.info-msg').children('span').text(selection.length)

$(document).on 'ready page:load', selectTable