<commit_before>class @ConditionalLogic

  self = ConditionalLogic.prototype

  #scan page and find all objects with conditional logic rules
  findRules: ->
    $("[data-rule]").each ->
      $ruleElement = $(this)
      #if $ruleElement.attr('data-rule').length > 0
      rule = $.parseJSON($ruleElement.attr("data-rule")).rule_hash
      matchType = rule.match_type
      #console.log rule
      $(rule.conditions).each ->
        $ruleElement = $ruleElement
        $container = $($ruleElement).closest(".form-container-repeater")
        condition = this
        #console.log condition
        questionId = condition.question_id
        #TODO: this shit needs to be context specific yo

        #  all_questions = $container
        #  all_questions << $($container).find(".form-container-repeater[data-question-id=" + ancestor_id + "],[data-question-id=" + ancestor_id + "],[data-ancestor=" + ancestor_id + "]")

        $triggerContainer = $ruleElement.closest('.form-container-survey').find("[data-question-id=" + questionId + "]")
        # getting element on edit and new page
        $triggerElement = $triggerContainer.find(".form-control")
        # getting element on show page
        if $triggerElement.length < 1
          $triggerElement = $triggerContainer.find(".form-control-static")
        # deal with any condition, once we get a hide_questions = false then we dont need to run through the rules
        hideQuestions = self.evaluateCondition(condition.operator, condition.answer, self.getValue($triggerElement))
        ancestorId = rule.question_id
        # bind conditions based on element type
        # text, textarea, select
        if $triggerElement.length < 2
          self.bindConditions(rule.question_id, $triggerElement, condition.operator, condition.answer)
        # radio buttons
        else
          if $triggerElement.is(":checkbox")
            # limit binding of each checkbox if data-label-value and answer are the same-kdh
            $triggerElement = $triggerContainer.find(".form-control[data-label-value=" + condition.answer + "]")
            self.bindConditions(rule.question_id, $triggerElement, condition.operator, condition.answer)
          else
            for element in $triggerElement
              do (element) ->
                self.bindConditions(rule.question_id, $(element), condition.operator, condition.answer)
        # hide show questions
        if rule.conditions.length > 1
          self.checkConditionsAndHideShow(rule.conditions, ancestorId, $ruleElement, $container, matchType)
        else
          self.hideShowQuestions(hideQuestions, ancestorId, $ruleElement, $container)
    return

  #bind conditions to question
  bindConditions: (ancestor_id, $triggerElement, operator, answer) ->
    $triggerElement.on "change", ->
      # pop out of condition into rules to handle all conditions defined in the rule
      # TODO it seems this is looping through ALL data-rule in the DOM instead of the data-rule associated with the element that triggered the onchange event-kdh
      $($triggerElement).closest(".form-container-repeater").find("[data-rule]").each ->
        $ruleElement = $(this)
        $container = $(this).closest(".form-container-repeater")
        rule = $.parseJSON($ruleElement.attr("data-rule")).rule_hash
        matchType = rule.match_type
        questionId = $triggerElement.closest('.form-container-entry-item').attr('data-question-id')
        conditions = rule.conditions
        ancestorId = rule.question_id
        matchingCondition = _.where(conditions, {question_id: Number(questionId)})
        hideQuestions = self.evaluateCondition(operator, answer, self.getValue($triggerElement))
        if matchingCondition.length > 0
          if conditions.length > 1
            self.checkConditionsAndHideShow(conditions, ancestorId, $ruleElement, $container, matchType)
          else
            self.hideShowQuestions(hideQuestions, ancestor_id, $ruleElement, $container)
    return

  checkConditionsAndHideShow: (conditions, ancestorId, $ruleElement, $container, matchType) ->
    conditionMetTracker = []
    _.each conditions, (condition) ->
      $conditionElement = $("[data-question-id=" + condition.question_id + "]").find('.form-control')
      hideQuestions = self.evaluateCondition(condition.operator, condition.answer, self.getValue($conditionElement))
      conditionMet = !hideQuestions
      conditionMetTracker.push conditionMet
    # match type any (or)
    if matchType == "any"
      if _.indexOf(conditionMetTracker, true) > 0
        self.hideShowQuestions(false, ancestorId, $ruleElement, $container)
      else
        self.hideShowQuestions(true, ancestorId, $ruleElement, $container)
    # match type all
    if matchType == "all"
      if _.indexOf(conditionMetTracker, false) == -1
        self.hideShowQuestions(false, ancestorId, $ruleElement, $container)
      else
        self.hideShowQuestions(true, ancestorId, $ruleElement, $container)


  #hide or show questions
  hideShowQuestions: (hide_questions, ancestor_id, $ruleElement, $container) ->
    all_questions = $container
    all_questions << $($container).find(".form-container-repeater[data-question-id=" + ancestor_id + "],[data-question-id=" + ancestor_id + "],[data-ancestor=" + ancestor_id + "]")
    if hide_questions
      all_questions.addClass("conditional-logic-hidden")
      self.clearQuestions(all_questions)
    else
      all_questions.removeClass("conditional-logic-hidden")

  #clear questions
  clearQuestions: (container) ->
    #console.log container
    # clear out text fields, selects and uncheck radio and checkboxes
    #TODO set these to default values once we implement default values - kdh
    # container.find("input[type!=hidden]").val("")
    textFields = container.find(":text").val("")
    textAreas = container.find("textarea").val("")
    # un-select dropdown
    selects = container.find("select")
    selects.each ->
      $(this).val("")
      $(this).trigger("chosen:updated") if $(this).hasClass('chosen')<commit_msg>Revert "Revert "Revert "update conditional logic to get repeater working, hide show of repeated defaults still broken"""<commit_after>class ConditionalLogic

  self = ConditionalLogic.prototype

  #scan page and find all objects with conditional logic rules
  findRules: ->
    $("[data-rule]").each ->
      $ruleElement = $(this)
      #if $ruleElement.attr('data-rule').length > 0
      rule = $.parseJSON($ruleElement.attr("data-rule")).rule_hash
      matchType = rule.match_type
      #console.log rule
      $(rule.conditions).each ->
        $ruleElement = $ruleElement
        condition = this
        #console.log condition
        questionId = condition.question_id
        $triggerContainer = $ruleElement.closest('.form-container-survey').find("[data-question-id=" + questionId + "]")
        # getting element on edit and new page
        $triggerElement = $triggerContainer.find(".form-control")
        # getting element on show page
        if $triggerElement.length < 1
          $triggerElement = $triggerContainer.find(".form-control-static")
        # deal with any condition, once we get a hide_questions = false then we dont need to run through the rules
        hideQuestions = self.evaluateCondition(condition.operator, condition.answer, self.getValue($triggerElement))
        ancestorId = rule.question_id
        # bind conditions based on element type
        # text, textarea, select
        if $triggerElement.length < 2
          self.bindConditions(rule.question_id, $triggerElement, condition.operator, condition.answer)
        # radio buttons
        else
          if $triggerElement.is(":checkbox")
            # limit binding of each checkbox if data-label-value and answer are the same-kdh
            $triggerElement = $triggerContainer.find(".form-control[data-label-value=" + condition.answer + "]")
            self.bindConditions(rule.question_id, $triggerElement, condition.operator, condition.answer)
          else
            for element in $triggerElement
              do (element) ->
                self.bindConditions(rule.question_id, $(element), condition.operator, condition.answer)
        # hide show questions
        if rule.conditions.length > 1
          self.checkConditionsAndHideShow(rule.conditions, ancestorId, $ruleElement, matchType)
        else
          self.hideShowQuestions(hideQuestions, ancestorId, $ruleElement)
    return

  #bind conditions to question
  bindConditions: (ancestor_id, $triggerElement, operator, answer) ->
    $triggerElement.on "change", ->
      # pop out of condition into rules to handle all conditions defined in the rule
      # TODO it seems this is looping through ALL data-rule in the DOM instead of the data-rule associated with the element that triggered the onchange event-kdh
      $("[data-rule]").each ->
        $ruleElement = $(this)
        rule = $.parseJSON($ruleElement.attr("data-rule")).rule_hash
        matchType = rule.match_type
        questionId = $triggerElement.closest('.form-container-entry-item').attr('data-question-id')
        conditions = rule.conditions
        ancestorId = rule.question_id
        matchingCondition = _.where(conditions, {question_id: Number(questionId)})
        hideQuestions = self.evaluateCondition(operator, answer, self.getValue($triggerElement))
        if matchingCondition.length > 0
          if conditions.length > 1
            self.checkConditionsAndHideShow(conditions, ancestorId, $ruleElement, matchType)
          else
            self.hideShowQuestions(hideQuestions, ancestor_id, $ruleElement)
    return

  checkConditionsAndHideShow: (conditions, ancestorId, $ruleElement, matchType) ->
    conditionMetTracker = []
    _.each conditions, (condition) ->
      $conditionElement = $("[data-question-id=" + condition.question_id + "]").find('.form-control')
      hideQuestions = self.evaluateCondition(condition.operator, condition.answer, self.getValue($conditionElement))
      conditionMet = !hideQuestions
      conditionMetTracker.push conditionMet
    # match type any (or)
    if matchType == "any"
      if _.indexOf(conditionMetTracker, true) > 0
        self.hideShowQuestions(false, ancestorId, $ruleElement)
      else
        self.hideShowQuestions(true, ancestorId, $ruleElement)
    # match type all
    if matchType == "all"
      if _.indexOf(conditionMetTracker, false) == -1
        self.hideShowQuestions(false, ancestorId, $ruleElement)
      else
        self.hideShowQuestions(true, ancestorId, $ruleElement)


  #hide or show questions
  hideShowQuestions: (hide_questions, ancestor_id, $ruleElement) ->
    container = $ruleElement.closest('.form-container-survey').find(".form-container-repeater[data-question-id=" + ancestor_id + "],[data-question-id=" + ancestor_id + "],[data-ancestor=" + ancestor_id + "]")
    if hide_questions
      container.addClass("conditional-logic-hidden")
      self.clearQuestions(container)
    else
      container.removeClass("conditional-logic-hidden")

  #clear questions
  clearQuestions: (container) ->
    #console.log container
    # clear out text fields, selects and uncheck radio and checkboxes
    #TODO set these to default values once we implement default values - kdh
    # container.find("input[type!=hidden]").val("")
    textFields = container.find(":text").val("")
    textAreas = container.find("textarea").val("")
    # un-select dropdown
    selects = container.find("select")
    selects.each ->
      $(this).val("")
      $(this).trigger("chosen:updated") if $(this).hasClass('chosen')
{'fraction_non_alphanumeric': 0.11389894809449905, 'fraction_numerical': 0.0013795481979651664, 'mean_word_length': 3.287985212569316, 'pattern_counts': {'":': 4, '<': 11, '<?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': 0, 'profanity_word_list.json': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '254121', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3051, 'n_words': 757}