<commit_before>class window.ColumnsHider
  constructor: ->
    @store_key = 'cols'
    @wrap = $('#columns_hider_wrap')
    @menu = @wrap.find('.dropdown-menu')
    @column_names = @columnNames()
    @collection_name = window.location.href.match(/admin\/(\w+)/)?[1]
    return unless @collection_name
    @data = @getData()
    @initButtons()
    @initHandlers()
    @refreshColumns()

  initHandlers: ->
    @menu.on 'click', 'label', (e) =>
      e.preventDefault()
      e.stopPropagation()
      $input = $(e.currentTarget).find('input')
      $input.prop('checked', !$input.is(':checked'))
      colIds = _.map @menu.find('input:not(:checked)'), (el) -> parseInt($(el).val())
      @data[@collection_name] = colIds
      @refreshColumns()
      @setData()

    $(document).on 'pjax:end admin:list_init', =>
      @refreshColumns()

  initButtons: ->
    log @data[@collection_name]
    @menu.empty()
    for col, i in @column_names
      isActive = _.include(@data[@collection_name], i)
      html = "<label class='checkbox'><input type='checkbox' checked='#{isActive}' value='#{i}'>#{col.name}</label>"
      @menu.append(html)

  setData: ->
    res = {}
    _.each @data, (v, k) ->
      res[k] = v.join('_')
    window.storeData(@store_key, res)

  getData: ->
    data = {}
    raw = $.parseJSON(window.fetchData(@store_key)) || {}
    _.each raw, (v, k) ->
      data[k] = []
      _.each v.split('_'), (el) ->
        data[k].push to_i(el) if el
    unless data[@collection_name]
      data[@collection_name] = []
      for col, i in @column_names
        data[@collection_name].push(i) if col.disabled
    data

  columnNames: ->
    _.map $('#list > thead > tr > th'), (el) ->
      $el = $(el)
      {disabled: $el.hasClass('hide_cell'), name: $.trim($el.text().replace(/[▼▲]/g, '')) || ' - '}

  refreshColumns: ->
    if @data[@collection_name]
      @showAll()
      for i in @data[@collection_name]
        @hideByIndex(i + 1)
      $(document).trigger('admin:refresh_columns')

  hideByIndex: (i) ->
    $("#list > thead > tr > th:nth-child(#{i}), #list > tbody > tr > td:nth-child(#{i})").hide()

  showAll: ->
    $("#list > thead > tr > th, #list > tbody > tr > td").show()

$ ->
  new ColumnsHider()
<commit_msg>Fix column hider<commit_after>class window.ColumnsHider
  constructor: ->
    @store_key = 'cols'
    @wrap = $('#columns_hider_wrap')
    @menu = @wrap.find('.dropdown-menu')
    @column_names = @columnNames()
    @collection_name = window.location.href.match(/admin\/(\w+)/)?[1]
    return unless @collection_name
    @data = @getData()
    @initButtons()
    @initHandlers()
    @refreshColumns()

  initHandlers: ->
    @menu.on 'click', 'label', (e) =>
      e.preventDefault()
      e.stopPropagation()
      $input = $(e.currentTarget).find('input')
      $input.prop('checked', !$input.is(':checked'))
      colIds = _.map @menu.find('input:not(:checked)'), (el) -> parseInt($(el).val())
      @data[@collection_name] = colIds
      @refreshColumns()
      @setData()

    $(document).on 'pjax:end admin:list_init', =>
      @refreshColumns()

  initButtons: ->
    log @data[@collection_name]
    @menu.empty()
    for col, i in @column_names
      isActive = !_.include(@data[@collection_name], i)
      html = "<label class='checkbox'><input type='checkbox' #{'checked' if isActive} value='#{i}'>#{col.name}</label>"
      @menu.append(html)

  setData: ->
    res = {}
    _.each @data, (v, k) ->
      res[k] = v.join('_')
    window.storeData(@store_key, res)

  getData: ->
    data = {}
    raw = $.parseJSON(window.fetchData(@store_key)) || {}
    _.each raw, (v, k) ->
      data[k] = []
      _.each v.split('_'), (el) ->
        data[k].push to_i(el) if el
    unless data[@collection_name]
      data[@collection_name] = []
      for col, i in @column_names
        data[@collection_name].push(i) if col.disabled
    data

  columnNames: ->
    _.map $('#list > thead > tr > th'), (el) ->
      $el = $(el)
      {disabled: $el.hasClass('hide_cell'), name: $.trim($el.text().replace(/[▼▲]/g, '')) || ' - '}

  refreshColumns: ->
    if @data[@collection_name]
      @showAll()
      for i in @data[@collection_name]
        @hideByIndex(i + 1)
      $(document).trigger('admin:refresh_columns')

  hideByIndex: (i) ->
    $("#list > thead > tr > th:nth-child(#{i}), #list > tbody > tr > td:nth-child(#{i})").hide()

  showAll: ->
    $("#list > thead > tr > th, #list > tbody > tr > td").show()

$ ->
  window.columnsHider = new ColumnsHider()

{'fraction_non_alphanumeric': 0.2052877138413686, 'fraction_numerical': 0.0008886914019106865, 'mean_word_length': 2.9147826086956523, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5998430', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1577, 'n_words': 299}