URI: 
       jquery.table.coffee - warvox - VoIP based wardialing tool, forked from rapid7/warvox.
  HTML git clone git://jay.scot/warvox
   DIR Log
   DIR Files
   DIR Refs
   DIR README
       ---
       jquery.table.coffee (8790B)
       ---
            1 # table plugin
            2 #
            3 # Adds sorting and other dynamic functions to tables.
            4 jQuery ($) ->
            5   $.table =
            6     defaults:
            7       searchable:        true
            8       searchInputHint:   'Search'
            9       sortableClass:     'sortable'
           10       setFilteringDelay: false
           11       datatableOptions:
           12         "bStateSave":    true
           13         "oLanguage":
           14           "sSearch":  ""
           15           "sProcessing":    "Loading..."
           16         "fnDrawCallback": ->
           17           $.table.controlBar.buttons.enable()
           18         "sDom": '<"control-bar"f><"list-table-header clearfix"l>t<"list-table-footer clearfix"ip>r'
           19         "sPaginationType": "full_numbers"
           20         "fnInitComplete": (oSettings, json) ->
           21            # if old search term saved, display it
           22           searchTerm = getParameterByName 'search'
           23           # FIX ME
           24           $searchBox = $('#search', $(this).parents().eq(3))
           25 
           26           if searchTerm
           27             $searchBox.val searchTerm
           28             $searchBox.focus()
           29 
           30           # insert the cancel button to the left of the search box
           31           $searchBox.before('<a class="cancel-search" href="#"></a>')
           32           $a = $('.cancel-search')
           33           table = this
           34           searchTerm = $searchBox.val()
           35           searchBox = $searchBox.eq(0)
           36           $a.hide() if (!searchTerm || searchTerm.length < 1)
           37 
           38           $a.click (e) ->  # called when red X is clicked
           39             $(this).hide()
           40             table.fnFilter ''
           41             $(searchBox).blur()           # blur to trigger filler text
           42             e.preventDefault()            # Other control code can be found in filteringDelay.js plugin.
           43           # bind to fnFilter() calls
           44           # do this by saving fnFilter to fnFilterOld & overriding
           45           table['fnFilterOld'] = table.fnFilter
           46           table.fnFilter = (str) ->
           47             $a = jQuery('.cancel-search')
           48             if str && str.length > 0
           49               $a.show()
           50             else
           51               $a.hide()
           52             table.fnFilterOld(str)
           53 
           54           window.setTimeout ( =>
           55             this.fnFilter(searchTerm)
           56             ), 0
           57 
           58           $('.button a.search').click() if searchTerm
           59 
           60       analysisTabOptions:
           61         "aLengthMenu":      [[10, 50, 100, 250, 500, -1], [10, 50, 100, 250, 500, "All"]]
           62         "iDisplayLength":   10
           63         "bProcessing":      true
           64         "bServerSide":      true
           65         "bSortMulti":       false
           66 
           67     checkboxes:
           68       bind: ->
           69         # TODO: This and any other 'table.list' selectors that appear in the plugin
           70         # code will trigger all sortable tables visible on the page.
           71         $("table.list thead tr th input[type='checkbox']").live 'click', (e) ->
           72           $checkboxes = $("input[type='checkbox']", "table.list tbody tr td:nth-child(1)")
           73           if $(this).attr 'checked'
           74             $checkboxes.attr 'checked', true
           75           else
           76             $checkboxes.attr 'checked', false
           77 
           78     controlBar:
           79       buttons:
           80         # Disables/enables buttons based on number of checkboxes selected,
           81         # and the class name.
           82         enable: ->
           83           numChecked = $("tbody tr td input[type='checkbox']", "table.list").filter(':checked').not('.invisible').size()
           84           disable = ($button) ->
           85             $button.addClass 'disabled'
           86             $button.children('input').attr 'disabled', 'disabled'
           87           enable = ($button) ->
           88             $button.removeClass 'disabled'
           89             $button.children('input').removeAttr 'disabled'
           90 
           91           switch numChecked
           92             when 0
           93               disable $('.btn.single',  '.control-bar')
           94               disable $('.btn.multiple','.control-bar')
           95               disable $('.btn.any',     '.control-bar')
           96             when 1
           97               enable  $('.btn.single',  '.control-bar')
           98               disable $('.btn.multiple','.control-bar')
           99               enable  $('.btn.any',     '.control-bar')
          100             else
          101               disable $('.btn.single',  '.control-bar')
          102               enable  $('.btn.multiple','.control-bar')
          103               enable  $('.btn.any',     '.control-bar')
          104 
          105         show:
          106           bind: ->
          107             # Show button
          108             $showButton = $('span.button a.show', '.control-bar')
          109             if $showButton.length
          110               $showButton.click (e) ->
          111                 unless $showButton.parent('span').hasClass 'disabled'
          112                   $("table.list tbody tr td input[type='checkbox']").filter(':checked').not('.invisible')
          113                   hostHref = $("table.list tbody tr td input[type='checkbox']")
          114                     .filter(':checked')
          115                     .parents('tr')
          116                     .children('td:nth-child(2)')
          117                     .children('a')
          118                     .attr('href')
          119                   window.location = hostHref
          120                 e.preventDefault()
          121 
          122         edit:
          123           bind: ->
          124             # Settings button
          125             $editButton = $('span.button a.edit', '.control-bar')
          126             if $editButton.length
          127               $editButton.click (e) ->
          128                 unless $editButton.parent('span').hasClass 'disabled'
          129                   $("table.list tbody tr td input[type='checkbox']").filter(':checked').not('.invisible')
          130                   hostHref = $("table.list tbody tr td input[type='checkbox']")
          131                     .filter(':checked')
          132                     .parents('tr')
          133                     .children('td:nth-child(2)')
          134                     .children('span.settings-url')
          135                     .html()
          136                   window.location = hostHref
          137                 e.preventDefault()
          138 
          139         bind: (options) ->
          140           # Move the buttons into the control bar.
          141           $('.control-bar').prepend($('.control-bar-items').html())
          142           $('.control-bar-items').remove()
          143 
          144           # Move the control bar to a new location, if specified.
          145           if !!options.controlBarLocation
          146             $('.control-bar').appendTo(options.controlBarLocation)
          147 
          148           this.enable()
          149           this.show.bind()
          150           this.edit.bind()
          151 
          152       bind: (options) ->
          153         this.buttons.bind(options)
          154         # Redraw the buttons with each checkbox click.
          155         $("input[type='checkbox']", "table.list").live 'click', (e) =>
          156           this.buttons.enable()
          157 
          158     searchField:
          159       # Add an input hint to the search field.
          160       addInputHint: (options, $table) ->
          161         if options.searchable
          162           # if the searchbar is in a control bar, expand selector scope to include control bar
          163           searchScope = $table.parents().eq(3) if !!options.controlBarLocation
          164           searchScope ||= $table.parents().eq(2)  # otherwise limit scope to just the table
          165           $searchInput = $('.dataTables_filter input', searchScope)
          166           # We'll need this id set for the checkbox functions.
          167           $searchInput.attr 'id', 'search'
          168           $searchInput.attr 'placeholder', options.searchInputHint
          169           # $searchInput.inputHint()
          170 
          171     bind: ($table, options) ->
          172       $tbody = $table.children('tbody')
          173       dataTable = null
          174       # Turn the table into a DataTable.
          175       if $table.hasClass options.sortableClass
          176         # Don't mess with the search input if there's no control bar.
          177         unless $('.control-bar-items').length
          178           options.datatableOptions["sDom"] = '<"list-table-header clearfix"lfr>t<"list-table-footer clearfix"ip>'
          179 
          180         datatableOptions = options.datatableOptions
          181         # If we're loading under the Analysis tab, then load the standard
          182         # Analysis tab options.
          183         if options.analysisTab
          184           $.extend(datatableOptions, options.analysisTabOptions)
          185           options.setFilteringDelay = true
          186           options.controlBarLocation = $('.analysis-control-bar')
          187 
          188         dataTable = $table.dataTable(datatableOptions)
          189         $table.data('dataTableObject', dataTable)
          190         dataTable.fnSetFilteringDelay(500) if options.setFilteringDelay
          191 
          192         # If we're loading under the Analysis tab, then load the standard Analysis tab functions.
          193         if options.analysisTab
          194           # Gray out the table during loads.
          195           $("##{$table.attr('id')}_processing").watch 'visibility', ->
          196             if $(this).css('visibility') == 'visible'
          197               $table.css opacity: 0.6
          198             else
          199               $table.css opacity: 1
          200 
          201           # Checking a host_ids checkbox should also check the invisible related object checkbox.
          202           $table.find('tbody tr td input[type=checkbox].hosts').live 'change', ->
          203             $(this).siblings('input[type=checkbox]').attr('checked', $(this).attr('checked'))
          204 
          205         this.checkboxes.bind()
          206         this.controlBar.bind(options)
          207         # Add an input hint to the search field.
          208         this.searchField.addInputHint(options, $table)
          209         # Keep width at 100%.
          210         $table.css('width', '100%')
          211 
          212   $.fn.table = (options) ->
          213     settings = $.extend true, {}, $.table.defaults, options
          214     $table   = $(this)
          215     return this.each -> $.table.bind($table, settings)