<commit_before>Template.registerHelper 'match', (val, {hash:{regex}})->
  val.match(new RegExp(regex))

Template.registerHelper 'isEmpty', (val)->
  if val.count
    val.count() == 0
  else
    _.isEmpty val

Template.add_question.onCreated ->
  @form = @data.form
  @questions = @data.questions
  @type = new ReactiveVar 'inputText'
  @choices = new Meteor.Collection(null)

Template.add_question.helpers
  types: ->
    [
      {
        text: 'Multiple Choice'
        name: 'multipleChoice'
      }
      {
        text: 'Checkboxes'
        name: 'checkboxes'
      }
      {
        text: 'Short Answer'
        name: 'shortAnswer'
      }
      {
        text: 'Long Answer'
        name: 'longAnswer'
      }
      {
        text: 'Number'
        name: 'number'
      }
      {
        text: 'Date'
        name: 'date'
      }
      {
        text: 'Datetime'
        name: 'datetime'
      }
      {
        text: 'Scale'
        name: 'scale'
      }
    ]
  type: ->
    Template.instance().type.get()
  selected: ->
    @name is Template.instance().type.get()
  choices: ->
    Template.instance().choices.find()

Template.add_question.events
  'keyup .choice': (event, instance) ->
    instance.choices.update($(event.currentTarget).data('id'),
      name: $(event.currentTarget).val()
    )
  'click .delete-choice': (event, instance)->
    instance.choices.remove($(event.currentTarget).data('id'))
  'click .type': (event, instance) ->
    instance.type.set $(event.currentTarget).data 'type'
  'submit form': (event, instance) ->
    event.preventDefault()

    form = event.currentTarget

    unless form.checkValidity()
      toastr.error('Please fill out all required fields')
      return

    formData = _.object $(form).serializeArray().map(
      ({name, value})-> [name, value]
    )
    questionProperties = _.omit(formData, 'text', '_id')

    if not instance.type.get()
      toastr.error('Please select a type')
      return

    if instance.type.get() == 'multipleChoice' or instance.type.get() == 'checkboxes'
      console.log instance.choices
      if instance.choices.find().count() == 0
        toastr.error('Please add some choices')
        return
      else
        choiceStrings = instance.choices.find().map(({name})-> name)
        if _.any(choiceStrings, _.isEmpty)
          toastr.error('Please fill in all the choices')
          return
        questionProperties.choices = choiceStrings

    questionProperties.required = questionProperties.required is 'on'

    if questionProperties.min?.length
      questionProperties.min = Number(questionProperties.min)
    else
      delete questionProperties.min

    if questionProperties.max?.length
      questionProperties.max = Number(questionProperties.max)
    else
      delete questionProperties.max

    question =
      text: formData.text
      question_type: instance.type.get()
      properties: questionProperties

    Meteor.call "createQuestion", instance.form.id, question,
      (error, response) ->
        if error
          toastr.error("Something went wrong")
        else
          form.reset()
          instance.choices.find().forEach ({_id})->
            instance.choices.remove(_id)
          instance.type.set('inputText')
          question.parseId = response.id
          instance.questions.insert question
          toastr.success('Question added')

  'click .add-choice': (event, instance)->
    instance.choices.insert({})<commit_msg>Use camel case for question properties<commit_after>Template.registerHelper 'match', (val, {hash:{regex}})->
  val.match(new RegExp(regex))

Template.registerHelper 'isEmpty', (val)->
  if val.count
    val.count() == 0
  else
    _.isEmpty val

Template.add_question.onCreated ->
  @form = @data.form
  @questions = @data.questions
  @type = new ReactiveVar 'inputText'
  @choices = new Meteor.Collection(null)

Template.add_question.helpers
  types: ->
    [
      {
        text: 'Multiple Choice'
        name: 'multipleChoice'
      }
      {
        text: 'Checkboxes'
        name: 'checkboxes'
      }
      {
        text: 'Short Answer'
        name: 'shortAnswer'
      }
      {
        text: 'Long Answer'
        name: 'longAnswer'
      }
      {
        text: 'Number'
        name: 'number'
      }
      {
        text: 'Date'
        name: 'date'
      }
      {
        text: 'Datetime'
        name: 'datetime'
      }
      {
        text: 'Scale'
        name: 'scale'
      }
    ]
  type: ->
    Template.instance().type.get()
  selected: ->
    @name is Template.instance().type.get()
  choices: ->
    Template.instance().choices.find()

Template.add_question.events
  'keyup .choice': (event, instance) ->
    instance.choices.update($(event.currentTarget).data('id'),
      name: $(event.currentTarget).val()
    )
  'click .delete-choice': (event, instance)->
    instance.choices.remove($(event.currentTarget).data('id'))
  'click .type': (event, instance) ->
    instance.type.set $(event.currentTarget).data 'type'
  'submit form': (event, instance) ->
    event.preventDefault()

    form = event.currentTarget

    unless form.checkValidity()
      toastr.error('Please fill out all required fields')
      return

    formData = _.object $(form).serializeArray().map(
      ({name, value})-> [name, value]
    )
    questionProperties = _.omit(formData, 'text', '_id')

    if not instance.type.get()
      toastr.error('Please select a type')
      return

    if instance.type.get() == 'multipleChoice' or instance.type.get() == 'checkboxes'
      console.log instance.choices
      if instance.choices.find().count() == 0
        toastr.error('Please add some choices')
        return
      else
        choiceStrings = instance.choices.find().map(({name})-> name)
        if _.any(choiceStrings, _.isEmpty)
          toastr.error('Please fill in all the choices')
          return
        questionProperties.choices = choiceStrings

    questionProperties.required = questionProperties.required is 'on'

    if questionProperties.min?.length
      questionProperties.min = Number(questionProperties.min)
    else
      delete questionProperties.min

    if questionProperties.max?.length
      questionProperties.max = Number(questionProperties.max)
    else
      delete questionProperties.max

    question =
      text: formData.text
      questionType: instance.type.get()
      properties: questionProperties

    Meteor.call "createQuestion", instance.form.id, question,
      (error, response) ->
        if error
          toastr.error("Something went wrong")
        else
          form.reset()
          instance.choices.find().forEach ({_id})->
            instance.choices.remove(_id)
          instance.type.set('inputText')
          question.parseId = response.id
          instance.questions.insert question
          toastr.success('Question added')

  'click .add-choice': (event, instance)->
    instance.choices.insert({})
{'fraction_non_alphanumeric': 0.13054755043227664, 'fraction_numerical': 0.0005763688760806917, 'mean_word_length': 2.39247311827957, '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': '354914', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1994, 'n_words': 430}