<commit_before>window.RecurringSelectDialog =
  class RecurringSelectDialog
    constructor: (@recurring_selector) ->
      @current_rule = @recurring_selector.recurring_select('current_rule')
      @initDialogBox()
      if not @current_rule.hash? or not @current_rule.hash.rule_type?
        @freqChanged()
      else
        setTimeout @positionDialogVert, 10 # allow initial render

    initDialogBox: ->
      $(".rs_dialog_holder").remove()

      open_in = $("body")
      open_in = $(".ui-page-active") if $(".ui-page-active").length
      open_in.append @template()
      @outer_holder = $(".rs_dialog_holder")
      @inner_holder = @outer_holder.find ".rs_dialog"
      @content = @outer_holder.find ".rs_dialog_content"
      @positionDialogVert(true)
      @mainEventInit()
      @freqInit()
      @summaryInit()
      @freq_select.focus()
      @outer_holder.trigger "recurring_select:dialog_opened"

    positionDialogVert: (initial_positioning) =>
      window_height = $(window).height()
      window_width  = $(window).width()
      dialog_height = @content.outerHeight()
      if dialog_height < 80
        dialog_height = 80
      margin_top = (window_height - dialog_height)/2 - 30
      margin_top = 10 if margin_top < 10
      # if dialog_height > window_height - 20
      #   dialog_height = window_height - 20

      new_style_hash =
        "margin-top" : margin_top+"px"
        "min-height" : dialog_height+"px"

      if initial_positioning?
        @inner_holder.css new_style_hash
        @inner_holder.trigger "recurring_select:dialog_positioned"
      else
        @content.css {"width": @content.width()+"px"}
        @inner_holder.addClass "animated"
        @inner_holder.animate new_style_hash, 200, =>
          @inner_holder.removeClass "animated"
          @content.css {"width": "auto"}
          @inner_holder.trigger "recurring_select:dialog_positioned"

    cancel: =>
      @outer_holder.remove()
      @recurring_selector.recurring_select('cancel')

    outerCancel: (event) =>
      if $(event.target).hasClass("rs_dialog_holder")
        @cancel()

    save: =>
      return if !@current_rule.str?
      @outer_holder.remove()
      @recurring_selector.recurring_select('save', @current_rule)

# ========================= Init Methods ===============================

    mainEventInit: ->
      # Tap hooks are for jQueryMobile
      @outer_holder.on 'click tap', @outerCancel
      @content.on 'click tap', 'h1 a', @cancel
      @save_button = @content.find('input.rs_save').on "click tap", @save
      @content.find('input.rs_cancel').on "click tap", @cancel

    freqInit: ->
      @freq_select = @outer_holder.find ".rs_frequency"
      if @current_rule.hash? && (rule_type = @current_rule.hash.rule_type)?
        if rule_type.search(/Weekly/) != -1
          @freq_select.prop('selectedIndex', 1)
          @initWeeklyOptions()
        else if rule_type.search(/Monthly/) != -1
          @freq_select.prop('selectedIndex', 2)
          @initMonthlyOptions()
        else if rule_type.search(/Yearly/) != -1
          @freq_select.prop('selectedIndex', 3)
          @initYearlyOptions()
        else
          @initDailyOptions()
      @freq_select.on "change", @freqChanged

    initDailyOptions: ->
      section = @content.find('.daily_options')
      interval_input = section.find('.rs_daily_interval')
      interval_input.val(@current_rule.hash.interval)
      interval_input.on "change keyup", @intervalChanged
      section.show()

    initWeeklyOptions: ->
      section = @content.find('.weekly_options')

      # connect the interval field
      interval_input = section.find('.rs_weekly_interval')
      interval_input.val(@current_rule.hash.interval)
      interval_input.on "change keyup", @intervalChanged

      # connect the day fields
      if @current_rule.hash.validations? && @current_rule.hash.validations.day?
        $(@current_rule.hash.validations.day).each (index, val) ->
          section.find(".day_holder a[data-value='"+val+"']").addClass("selected")

      if !@alreadyInitializedWeeklyOptions?
        @alreadyInitializedWeeklyOptions = true
        section.on "click", ".day_holder a", @daysChanged

      section.show()

    initMonthlyOptions: ->
      section = @content.find('.monthly_options')
      interval_input = section.find('.rs_monthly_interval')
      interval_input.val(@current_rule.hash.interval)
      interval_input.on "change keyup", @intervalChanged

      @current_rule.hash.validations ||= {}
      @current_rule.hash.validations.day_of_month ||= []
      @current_rule.hash.validations.day_of_week ||= {}
      @init_calendar_days(section)<commit_msg>Clear selected day on weekly view load<commit_after>window.RecurringSelectDialog =
  class RecurringSelectDialog
    constructor: (@recurring_selector) ->
      @current_rule = @recurring_selector.recurring_select('current_rule')
      @initDialogBox()
      if not @current_rule.hash? or not @current_rule.hash.rule_type?
        @freqChanged()
      else
        setTimeout @positionDialogVert, 10 # allow initial render

    initDialogBox: ->
      $(".rs_dialog_holder").remove()

      open_in = $("body")
      open_in = $(".ui-page-active") if $(".ui-page-active").length
      open_in.append @template()
      @outer_holder = $(".rs_dialog_holder")
      @inner_holder = @outer_holder.find ".rs_dialog"
      @content = @outer_holder.find ".rs_dialog_content"
      @positionDialogVert(true)
      @mainEventInit()
      @freqInit()
      @summaryInit()
      @freq_select.focus()
      @outer_holder.trigger "recurring_select:dialog_opened"

    positionDialogVert: (initial_positioning) =>
      window_height = $(window).height()
      window_width  = $(window).width()
      dialog_height = @content.outerHeight()
      if dialog_height < 80
        dialog_height = 80
      margin_top = (window_height - dialog_height)/2 - 30
      margin_top = 10 if margin_top < 10
      # if dialog_height > window_height - 20
      #   dialog_height = window_height - 20

      new_style_hash =
        "margin-top" : margin_top+"px"
        "min-height" : dialog_height+"px"

      if initial_positioning?
        @inner_holder.css new_style_hash
        @inner_holder.trigger "recurring_select:dialog_positioned"
      else
        @content.css {"width": @content.width()+"px"}
        @inner_holder.addClass "animated"
        @inner_holder.animate new_style_hash, 200, =>
          @inner_holder.removeClass "animated"
          @content.css {"width": "auto"}
          @inner_holder.trigger "recurring_select:dialog_positioned"

    cancel: =>
      @outer_holder.remove()
      @recurring_selector.recurring_select('cancel')

    outerCancel: (event) =>
      if $(event.target).hasClass("rs_dialog_holder")
        @cancel()

    save: =>
      return if !@current_rule.str?
      @outer_holder.remove()
      @recurring_selector.recurring_select('save', @current_rule)

# ========================= Init Methods ===============================

    mainEventInit: ->
      # Tap hooks are for jQueryMobile
      @outer_holder.on 'click tap', @outerCancel
      @content.on 'click tap', 'h1 a', @cancel
      @save_button = @content.find('input.rs_save').on "click tap", @save
      @content.find('input.rs_cancel').on "click tap", @cancel

    freqInit: ->
      @freq_select = @outer_holder.find ".rs_frequency"
      if @current_rule.hash? && (rule_type = @current_rule.hash.rule_type)?
        if rule_type.search(/Weekly/) != -1
          @freq_select.prop('selectedIndex', 1)
          @initWeeklyOptions()
        else if rule_type.search(/Monthly/) != -1
          @freq_select.prop('selectedIndex', 2)
          @initMonthlyOptions()
        else if rule_type.search(/Yearly/) != -1
          @freq_select.prop('selectedIndex', 3)
          @initYearlyOptions()
        else
          @initDailyOptions()
      @freq_select.on "change", @freqChanged

    initDailyOptions: ->
      section = @content.find('.daily_options')
      interval_input = section.find('.rs_daily_interval')
      interval_input.val(@current_rule.hash.interval)
      interval_input.on "change keyup", @intervalChanged
      section.show()

    initWeeklyOptions: ->
      section = @content.find('.weekly_options')

      # connect the interval field
      interval_input = section.find('.rs_weekly_interval')
      interval_input.val(@current_rule.hash.interval)
      interval_input.on "change keyup", @intervalChanged

      # clear selected days
      section.find(".day_holder a").each (index, element) ->
        $(element).removeClass("selected")

      # connect the day fields
      if @current_rule.hash.validations? && @current_rule.hash.validations.day?
        $(@current_rule.hash.validations.day).each (index, val) ->
          section.find(".day_holder a[data-value='"+val+"']").addClass("selected")

      if !@alreadyInitializedWeeklyOptions?
        @alreadyInitializedWeeklyOptions = true
        section.on "click", ".day_holder a", @daysChanged

      section.show()

    initMonthlyOptions: ->
      section = @content.find('.monthly_options')
      interval_input = section.find('.rs_monthly_interval')
      interval_input.val(@current_rule.hash.interval)
      interval_input.on "change keyup", @intervalChanged

      @current_rule.hash.validations ||= {}
      @current_rule.hash.validations.day_of_month ||= []
      @current_rule.hash.validations.day_of_week ||= {}
      @init_calendar_days(section)
{'fraction_non_alphanumeric': 0.1376291921535541, 'fraction_numerical': 0.005695001054629825, 'mean_word_length': 3.0647235319331334, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 32, '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': '2367592', 'n_tokens_mistral': 2911, 'n_tokens_neox': 2811, 'n_words': 491}