<commit_before>createInlineDateRangePicker = require '/imports/ui/inlineDateRangePicker.coffee'
validator = require 'bootstrap-validator'

_keyboardSelect = (event) ->
  keyCode = event.keyCode
  keyCode in [13, 32]

_selectInput = (event, instance, prop, isCheckbox) ->
  return if not _keyboardSelect(event) and event.type is 'keyup'
  if isCheckbox is 'checkbox'
    prop = instance[prop]
    prop.set(not prop.get())
  else
    instance[prop].set(instance.$(event.target).attr('for'))

Template.incidentForm.onCreated ->
  @incidentStatus = new ReactiveVar('')
  @incidentType = new ReactiveVar('')
  incident = @data.incident
  @suggestedFields = incident?.suggestedFields or new ReactiveVar([])

  @incidentData =
    species: 'Human'
    dateRange:
      type: 'day'

  if incident
    @incidentData = _.extend(@incidentData, incident)
    if incident.dateRange
      @incidentData.dateRange = incident.dateRange

    cases = @incidentData.cases
    deaths = @incidentData.deaths
    specify = @incidentData.specify
    @incidentData.value = cases or deaths or specify
    if cases
      type = 'cases'
    else if deaths
      type = 'deaths'
    else if specify
      type = 'other'
    else
      type = ''

    @incidentType.set(type)

    if @incidentData.url
      @incidentData.articleSource = _.findWhere(@data.articles,
        url: @incidentData.url[0]
      )?._id

    @incidentStatus.set(incident.status or '')

Template.incidentForm.onRendered ->
  instance = @
  datePickerOptions = {}
  if @incidentData.dateRange.start and @incidentData.dateRange.end
    datePickerOptions.startDate = @incidentData.dateRange.start
    datePickerOptions.endDate = @incidentData.dateRange.end
  createInlineDateRangePicker(@$('#rangePicker'), datePickerOptions)
  datePickerOptions.singleDatePicker = true
  createInlineDateRangePicker(@$('#singleDatePicker'), datePickerOptions)

  @$('#add-incident').validator()
  #Update the validator when Blaze adds incident type related inputs
  @autorun ->
    instance.incidentType.get()
    Meteor.defer ->
      instance.$('#add-incident').validator('update')

Template.incidentForm.helpers
  incidentData: ->
    Template.instance().incidentData

  incidentStatusChecked: (status) ->
    status is Template.instance().incidentStatus.get()

  incidentTypeChecked: (type) ->
    type is Template.instance().incidentType.get()

  articles: ->
    Template.instance().data.articles

  showCountForm: ->
    type = Template.instance().incidentType.get()
    type is 'cases' or type is 'deaths'

  showOtherForm: ->
    Template.instance().incidentType.get() is 'other'

  dayTabClass: ->
    if Template.instance().incidentData.dateRange.type is 'day'
      'active'

  rangeTabClass: ->
    if Template.instance().incidentData.dateRange.type is 'precise'
      'active'

  selectedIncidentType: ->
    Template.instance().incidentType.get().slice(0, -1)

  suggestedField: (fieldName)->
    if fieldName in Template.instance().suggestedFields.get()
      "suggested"
<commit_msg>capitalize selectedIncidentType<commit_after>createInlineDateRangePicker = require '/imports/ui/inlineDateRangePicker.coffee'
validator = require 'bootstrap-validator'

_keyboardSelect = (event) ->
  keyCode = event.keyCode
  keyCode in [13, 32]

_selectInput = (event, instance, prop, isCheckbox) ->
  return if not _keyboardSelect(event) and event.type is 'keyup'
  if isCheckbox is 'checkbox'
    prop = instance[prop]
    prop.set(not prop.get())
  else
    instance[prop].set(instance.$(event.target).attr('for'))

Template.incidentForm.onCreated ->
  @incidentStatus = new ReactiveVar('')
  @incidentType = new ReactiveVar('')
  incident = @data.incident
  @suggestedFields = incident?.suggestedFields or new ReactiveVar([])

  @incidentData =
    species: 'Human'
    dateRange:
      type: 'day'

  if incident
    @incidentData = _.extend(@incidentData, incident)
    if incident.dateRange
      @incidentData.dateRange = incident.dateRange

    cases = @incidentData.cases
    deaths = @incidentData.deaths
    specify = @incidentData.specify
    @incidentData.value = cases or deaths or specify
    if cases
      type = 'cases'
    else if deaths
      type = 'deaths'
    else if specify
      type = 'other'
    else
      type = ''

    @incidentType.set(type)

    if @incidentData.url
      @incidentData.articleSource = _.findWhere(@data.articles,
        url: @incidentData.url[0]
      )?._id

    @incidentStatus.set(incident.status or '')

Template.incidentForm.onRendered ->
  instance = @
  datePickerOptions = {}
  if @incidentData.dateRange.start and @incidentData.dateRange.end
    datePickerOptions.startDate = @incidentData.dateRange.start
    datePickerOptions.endDate = @incidentData.dateRange.end
  createInlineDateRangePicker(@$('#rangePicker'), datePickerOptions)
  datePickerOptions.singleDatePicker = true
  createInlineDateRangePicker(@$('#singleDatePicker'), datePickerOptions)

  @$('#add-incident').validator()
  #Update the validator when Blaze adds incident type related inputs
  @autorun ->
    instance.incidentType.get()
    Meteor.defer ->
      instance.$('#add-incident').validator('update')

Template.incidentForm.helpers
  incidentData: ->
    Template.instance().incidentData

  incidentStatusChecked: (status) ->
    status is Template.instance().incidentStatus.get()

  incidentTypeChecked: (type) ->
    type is Template.instance().incidentType.get()

  articles: ->
    Template.instance().data.articles

  showCountForm: ->
    type = Template.instance().incidentType.get()
    type is 'cases' or type is 'deaths'

  showOtherForm: ->
    Template.instance().incidentType.get() is 'other'

  dayTabClass: ->
    if Template.instance().incidentData.dateRange.type is 'day'
      'active'

  rangeTabClass: ->
    if Template.instance().incidentData.dateRange.type is 'precise'
      'active'

  selectedIncidentType: ->
    switch Template.instance().incidentType.get()
      when "cases" then "Case"
      when "deaths" then "Death"

  suggestedField: (fieldName)->
    if fieldName in Template.instance().suggestedFields.get()
      "suggested"

{'fraction_non_alphanumeric': 0.12092036553524804, 'fraction_numerical': 0.0019582245430809398, 'mean_word_length': 3.765940902021773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '1082568', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1870, 'n_words': 383}