<commit_before>{React, ReactBootstrap, jQuery} = window
{Grid, Col, Table} = ReactBootstrap

AkashicRecordsCheckboxArea = require './akashic-records-checkbox-area'
AkashicRecordsTableArea = require './akashic-records-table-area'

dateToString = (date)->
  month = date.getMonth() + 1
  if month < 10
    month = "0#{month}"
  day = date.getDate()
  if day < 10
    day = "0#{day}"
  hour = date.getHours()
  if hour < 10
    hour = "0#{hour}"
  minute = date.getMinutes()
  if minute < 10
    minute = "0#{minute}"
  second = date.getSeconds()
  if second < 10
    second = "0#{second}"
  "#{date.getFullYear()}/#{month}/#{day} #{hour}:#{minute}:#{second}"

configList = ['显示表头', '显示筛选框', '自动判断', '隐藏筛选框时取消筛选']

AkashicLog = React.createClass
  getInitialState: ->
    rowChooseChecked: [true, true, true, true, true, true, true, true, true, true, true, true,
                      true, true]
    configChecked: [true, true, false, false]
    filterKeys:['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    showAmount: 10
    activePage: 0
    filterVersion: 0
    showRulesVersion: 0
    dataShow: []
    dataAfterFilter: []
    dataAfterFilterLength: 0
  filterVersion: 0
  showRulesVersion: 0
  dataVersion: -1
  configChecked: [true, true, false, false]
  rowChooseChecked: [true, true, true, true, true, true, true, true, true, true, true, true,
                      true, true]
  _testFilter: (log, keyWords)->
    {rowChooseChecked} = @state
    regFlag = []
    filterKeys = []
    for item, index in keyWords
      regFlag[index] =false
      filterKeys[index] = item
      if item isnt ''
        res = item.match /^\/(.+)\/([gim]*)$/
        if res?
          try
            reg = new RegExp res[1], res[2]
            regFlag[index] = true
          catch e
            regFlag[index] = false
            # ...
          finally
            if regFlag[index]
              filterKeys[index] = reg
    for item, index in log
      if rowChooseChecked[index+1]
        if keyWords[index+1] isnt ''
          if index is 0
            testText = dateToString(new Date(item)).toLowerCase().trim()
          else
            testText = "#{item}".toLowerCase().trim()
          if regFlag[index+1]
            match = filterKeys[index+1].test testText
          else
            match = testText.indexOf(filterKeys[index+1].toLowerCase().trim()) >= 0
          if not match
            return false
    return true
  _filter: (rawData, keyWords)->
    enableFilter = false
    for item, index in @props.tableTab
      continue if index is 0
      enableFilter = true if keyWords[index] isnt ''
    if enableFilter
      rawData.filter (row)=>
        @_testFilter row, keyWords
    else rawData
  refreshDataShow: (data, filterKeys, activePage, showAmount)->
    dataAfterFilter = @_filter data, filterKeys
    dataAfterFilterLength = dataAfterFilter.length
    if activePage < 1
      activePage = 1
    if activePage > Math.ceil(dataAfterFilterLength/showAmount)
      activePage = Math.ceil(dataAfterFilterLength/showAmount)
    if dataAfterFilterLength > 0
      dataShow = dataAfterFilter.slice((activePage - 1) * showAmount, activePage * showAmount)
    else
      dataShow = []
    {dataShow, dataAfterFilter, dataAfterFilterLength, activePage}

  ## about tab checkbox
  tabFilterRules: (checked) ->
    {dataShow, dataAfterFilter, dataAfterFilterLength, activePage} = @refreshDataShow @props.data, @state.filterKeys, @state.activePage, @state.showAmount
    @setState
      rowChooseChecked: checked
      dataAfterFilter: dataAfterFilter
      dataAfterFilterLength: dataAfterFilterLength
      dataShow: dataShow
      activePage: activePage

  #about filter    
  filterRules: (filterKeys) ->
    {dataShow, dataAfterFilter, dataAfterFilterLength, activePage} = @refreshDataShow @props.data, filterKeys, @state.activePage, @state.showAmount
    @setState
      filterKeys: filterKeys
      filterVersion: @state.filterVersion + 1
      dataAfterFilter: dataAfterFilter
      dataAfterFilterLength: dataAfterFilterLength
      dataShow: dataShow
      activePage: activePage

  #about the amount of data showed and active page
  showRules: (showAmount, activePage)->
    if activePage > Math.ceil(@state.dataAfterFilterLength/showAmount)
      activePage = Math.ceil(@state.dataAfterFilterLength/showAmount)
    if showAmount isnt @state.showAmount
      config.set "plugin.Akashic.#{@props.contentType}.showAmount", showAmount
    if showAmount isnt @state.showAmount or activePage isnt @state.activePage
      dataShow = @state.dataAfterFilter.slice((activePage - 1) * showAmount, activePage * showAmount)
      @setState
        showAmount: showAmount
        activePage: activePage
        showRulesVersion: @state.showRulesVersion + 1
        dataShow: dataShow
  handlePageChange: (activePage)->
    if activePage isnt @state.activePage
      dataShow = @state.dataAfterFilter.slice((activePage - 1) * @state.showAmount, activePage * @state.showAmount)
      @setState
        activePage: activePage
        dataShow: dataShow
        showRulesVersion: @state.showRulesVersion + 1

  #click config checkbox
  configCheckboxClick: (index)->
    {configChecked} = @state
    configChecked[index] = not configChecked[index]
    if configChecked[2] is true
      configChecked[0] = false
      configChecked[1] = false
    @setState
      configChecked: configChecked

  componentWillMount: ->
    {activePage} = @state
    @rowChooseChecked = JSON.parse config.get "plugin.Akashic.#{@props.contentType}.checkbox", JSON.stringify @state.rowChooseChecked
    rowChooseChecked = JSON.parse JSON.stringify @rowChooseChecked
    showAmount = config.get "plugin.Akashic.#{@props.contentType}.showAmount", @state.showAmount
    dataAfterFilter = @_filter @props.data, @state.filterKeys
    dataAfterFilterLength = dataAfterFilter.length
    if dataAfterFilterLength > 0
      if activePage < 1
        activePage = 1
      dataShow = dataAfterFilter.slice((activePage - 1) * showAmount, activePage * showAmount)
    else
      dataShow = []
    @setState
      rowChooseChecked: rowChooseChecked
      showAmount: showAmount<commit_msg>add config-save<commit_after>{React, ReactBootstrap, jQuery} = window
{Grid, Col, Table} = ReactBootstrap

AkashicRecordsCheckboxArea = require './akashic-records-checkbox-area'
AkashicRecordsTableArea = require './akashic-records-table-area'

dateToString = (date)->
  month = date.getMonth() + 1
  if month < 10
    month = "0#{month}"
  day = date.getDate()
  if day < 10
    day = "0#{day}"
  hour = date.getHours()
  if hour < 10
    hour = "0#{hour}"
  minute = date.getMinutes()
  if minute < 10
    minute = "0#{minute}"
  second = date.getSeconds()
  if second < 10
    second = "0#{second}"
  "#{date.getFullYear()}/#{month}/#{day} #{hour}:#{minute}:#{second}"

configList = ['显示表头', '显示筛选框', '自动判断', '隐藏筛选框时取消筛选']

AkashicLog = React.createClass
  getInitialState: ->
    rowChooseChecked: [true, true, true, true, true, true, true, true, true, true, true, true,
                      true, true]
    configChecked: [true, true, false, false]
    filterKeys:['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    showAmount: 10
    activePage: 0
    filterVersion: 0
    showRulesVersion: 0
    dataShow: []
    dataAfterFilter: []
    dataAfterFilterLength: 0
  filterVersion: 0
  showRulesVersion: 0
  dataVersion: -1
  configChecked: [true, true, false, false]
  rowChooseChecked: [true, true, true, true, true, true, true, true, true, true, true, true,
                      true, true]
  _testFilter: (log, keyWords)->
    {rowChooseChecked} = @state
    regFlag = []
    filterKeys = []
    for item, index in keyWords
      regFlag[index] =false
      filterKeys[index] = item
      if item isnt ''
        res = item.match /^\/(.+)\/([gim]*)$/
        if res?
          try
            reg = new RegExp res[1], res[2]
            regFlag[index] = true
          catch e
            regFlag[index] = false
            # ...
          finally
            if regFlag[index]
              filterKeys[index] = reg
    for item, index in log
      if rowChooseChecked[index+1]
        if keyWords[index+1] isnt ''
          if index is 0
            testText = dateToString(new Date(item)).toLowerCase().trim()
          else
            testText = "#{item}".toLowerCase().trim()
          if regFlag[index+1]
            match = filterKeys[index+1].test testText
          else
            match = testText.indexOf(filterKeys[index+1].toLowerCase().trim()) >= 0
          if not match
            return false
    return true
  _filter: (rawData, keyWords)->
    enableFilter = false
    for item, index in @props.tableTab
      continue if index is 0
      enableFilter = true if keyWords[index] isnt ''
    if enableFilter
      rawData.filter (row)=>
        @_testFilter row, keyWords
    else rawData
  refreshDataShow: (data, filterKeys, activePage, showAmount)->
    dataAfterFilter = @_filter data, filterKeys
    dataAfterFilterLength = dataAfterFilter.length
    if activePage < 1
      activePage = 1
    if activePage > Math.ceil(dataAfterFilterLength/showAmount)
      activePage = Math.ceil(dataAfterFilterLength/showAmount)
    if dataAfterFilterLength > 0
      dataShow = dataAfterFilter.slice((activePage - 1) * showAmount, activePage * showAmount)
    else
      dataShow = []
    {dataShow, dataAfterFilter, dataAfterFilterLength, activePage}

  ## about tab checkbox
  tabFilterRules: (checked) ->
    {dataShow, dataAfterFilter, dataAfterFilterLength, activePage} = @refreshDataShow @props.data, @state.filterKeys, @state.activePage, @state.showAmount
    @setState
      rowChooseChecked: checked
      dataAfterFilter: dataAfterFilter
      dataAfterFilterLength: dataAfterFilterLength
      dataShow: dataShow
      activePage: activePage

  #about filter    
  filterRules: (filterKeys) ->
    {dataShow, dataAfterFilter, dataAfterFilterLength, activePage} = @refreshDataShow @props.data, filterKeys, @state.activePage, @state.showAmount
    @setState
      filterKeys: filterKeys
      filterVersion: @state.filterVersion + 1
      dataAfterFilter: dataAfterFilter
      dataAfterFilterLength: dataAfterFilterLength
      dataShow: dataShow
      activePage: activePage

  #about the amount of data showed and active page
  showRules: (showAmount, activePage)->
    if activePage > Math.ceil(@state.dataAfterFilterLength/showAmount)
      activePage = Math.ceil(@state.dataAfterFilterLength/showAmount)
    if showAmount isnt @state.showAmount
      config.set "plugin.Akashic.#{@props.contentType}.showAmount", showAmount
    if showAmount isnt @state.showAmount or activePage isnt @state.activePage
      dataShow = @state.dataAfterFilter.slice((activePage - 1) * showAmount, activePage * showAmount)
      @setState
        showAmount: showAmount
        activePage: activePage
        showRulesVersion: @state.showRulesVersion + 1
        dataShow: dataShow
  handlePageChange: (activePage)->
    if activePage isnt @state.activePage
      dataShow = @state.dataAfterFilter.slice((activePage - 1) * @state.showAmount, activePage * @state.showAmount)
      @setState
        activePage: activePage
        dataShow: dataShow
        showRulesVersion: @state.showRulesVersion + 1

  #click config checkbox
  configCheckboxClick: (index)->
    {configChecked} = @state
    configChecked[index] = not configChecked[index]
    if configChecked[2] is true
      configChecked[0] = false
      configChecked[1] = false
    config.set "plugin.Akashic.#{@props.contentType}.configChecked", JSON.stringify configChecked
    @setState
      configChecked: configChecked

  componentWillMount: ->
    {activePage} = @state
    @rowChooseChecked = JSON.parse config.get "plugin.Akashic.#{@props.contentType}.checkbox", JSON.stringify @state.rowChooseChecked
    rowChooseChecked = JSON.parse JSON.stringify @rowChooseChecked
    @configChecked = JSON.parse config.get "plugin.Akashic.#{@props.contentType}.configChecked", JSON.stringify @state.configChecked
    configChecked = JSON.parse JSON.stringify @configChecked
    showAmount = config.get "plugin.Akashic.#{@props.contentType}.showAmount", @state.showAmount
    dataAfterFilter = @_filter @props.data, @state.filterKeys
    dataAfterFilterLength = dataAfterFilter.length
    if dataAfterFilterLength > 0
      if activePage < 1
        activePage = 1
      dataShow = dataAfterFilter.slice((activePage - 1) * showAmount, activePage * showAmount)
    else
      dataShow = []
    @setState
      rowChooseChecked: rowChooseChecked
      configChecked: configChecked
      showAmount: showAmount
{'fraction_non_alphanumeric': 0.11046190288195815, 'fraction_numerical': 0.008053691275167786, 'mean_word_length': 3.066131621187801, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1661280', 'n_tokens_mistral': 3809, 'n_tokens_neox': 3742, 'n_words': 912}