<commit_before>do (document, $=jQuery, edsc_date=@<EMAIL>, temporalModel=@edsc.page.ui.temporal, plugin=@edsc.util.plugin, page=@edsc.page) ->

  current_year = new Date().getUTCFullYear()

  validateTemporalInputs = (root) ->
    start = root.find(".temporal-start:visible")
    end = root.find(".temporal-stop:visible")
    start_val = start.val()
    end_val = end.val()

    if start and end
      error = root.find(".tab-pane:visible .temporal-error")
      error.show()

      if start.hasClass("temporal-recurring-start")
        # Recurring start and stop must both be selected
        if start_val == "" ^ end_val == ""
          error.text("Start and End dates must both be selected")
        else if start_val > end_val
          error.text("Start must be no later than End")
        else
          error.hide()
      else
        if start_val == "" or end_val == "" or start_val <= end_val
          error.hide()
        else
          error.text("Start must be no later than End")


  setMinMaxOptions = (root, datetimepicker, $input, temporal_type) ->
    min_date = false
    max_date = false
    format = if temporal_type == "range" then 'Y-m-d' else 'm-d'

    start_val = root.find('input.temporal-' + temporal_type + '-start').val()
    stop_val = root.find('input.temporal-' + temporal_type + '-stop').val()

    if $input.hasClass('temporal-' + temporal_type + '-start') and stop_val
      max_date = stop_val.split(' ')[0]
    else if $input.hasClass('temporal-' + temporal_type + '-stop') and start_val
      min_date = start_val.split(' ')[0]

    datetimepicker.setOptions({
      minDate: min_date,
      maxDate: max_date,
      formatDate: format
    })

  updateMonthButtons = (month_label) ->
    prev_button = month_label.siblings('button.xdsoft_prev')
    next_button = month_label.siblings('button.xdsoft_next')
    prev_button.show()
    next_button.show()
    month = month_label.find('span').text()
    if month == "January"
      prev_button.hide()
    else if month == "December"
      next_button.hide()

  parseOrdinal = (value) ->
    match = /(\d{4})-(\d{3})/.exec(value)
    return null  unless match # Date is in the wrong format
    year = parseInt(match[1], 10)
    day = parseInt(match[2], 10)
    date = new Date(Date.UTC(year, 0, day))
    return null  unless date.getUTCFullYear() is year # Date is higher than the number of days in the year
    edsc_date.isoUtcDateTimeString(date)

  $.fn.temporalSelectors = (options) ->
    root = this
    uiModel = options["uiModel"]
    uiModelPath = options["modelPath"]
    prefix = options["prefix"]

    # Sanity check
    console.error "Temporal selectors double initialization" if root.data('temporal-selectors')
    root.data('temporal-selectors', true)

    root.find('.temporal-range-picker').datetimepicker
      format: 'Y-m-d H:i:s',
      className: prefix + '-datetimepicker',
      yearStart: '1960',
      yearEnd: current_year,
      onShow: (dp,$input) ->
        setMinMaxOptions(root, this, $input, 'range')
      onChangeDateTime: (dp,$input) ->
        # Default minutes and seconds to 00
        datetime = $input.val().split(":")
        datetime[1] = "00"
        datetime[2] = "00"
        $input.val(datetime.join(":"))

        validateTemporalInputs(root)

        $input.trigger('change')
      onGenerate: ($dp, input) ->
        picker = this
        if picker.find('.day-of-year-picker')?.length == 0
          which = if input.is('.temporal-start') then 'start' else 'stop'
          day_of_year_div = $("<div class='day-of-year-picker' data-bind='if: #{uiModelPath}'>
            <label for='day-of-year-#{which}-input'>Day of Year:</label>
            <input id='day-of-year-#{which}-input' data-bind='value: #{uiModelPath}.#{which}.dayOfYearString()' class='day-of-year-input' type='text' placeholder='YYYY-DDD' >
            <button class='button text-button day-of-year-submit' data-input='" + input.attr("id") + "'>Set</button>
            </div>")
          day_of_year_div.appendTo(picker)
          day_of_year_div.find('.day-of-year-submit').on 'click', ->
            value = $(this).prev().val()
            date = parseOrdinal(value)
            if date
              $("#" + $(this).attr("data-input") + ":visible").val(date).trigger('change')
              $(this).parents('.xdsoft_datetimepicker').hide()
              validateTemporalInputs(root)
          ko.applyBindings(page, day_of_year_div[0])

    root.find('.temporal-recurring-picker').datetimepicker
      format: 'm-d H:i:s',
      className: prefix + '-datetimepicker recurring-datetimepicker',
      yearStart: 2007,
      yearEnd: 2007,
      onShow: (dp,$input) ->
        updateMonthButtons($(this).find('.xdsoft_month'))
        setMinMaxOptions(root, this, $input, 'recurring')
      onChangeDateTime: (dp,$input) ->
        # Default minutes and seconds to 00
        datetime = $input.val().split(":")
        datetime[1] = "00"
        datetime[2] = "00"
        $input.val(datetime.join(":"))

        validateTemporalInputs(root)

        $input.trigger('change')
      onChangeMonth: (dp,$input) ->
        updateMonthButtons($(this).find('.xdsoft_month'))

    root.find('.temporal-recurring-year-range').slider({
      min: 1960,
      max: current_year,
      value: [1960, current_year],
      tooltip: 'hide'
    }).on 'slide', (e) ->
      uiModel.pending.years(e.value)

    # Set the slider when the years change<commit_msg>EDSC-277: Default time selection to midnight.  Do not set a time value when clicking off the time picker.<commit_after>do (document, $=jQuery, edsc_date=@<EMAIL>, temporalModel=@edsc.page.ui.temporal, plugin=@<EMAIL>, page=@edsc.page) ->

  now = new Date()
  today = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())
  console.log today
  current_year = new Date().getUTCFullYear()

  validateTemporalInputs = (root) ->
    start = root.find(".temporal-start:visible")
    end = root.find(".temporal-stop:visible")
    start_val = start.val()
    end_val = end.val()

    if start and end
      error = root.find(".tab-pane:visible .temporal-error")
      error.show()

      if start.hasClass("temporal-recurring-start")
        # Recurring start and stop must both be selected
        if start_val == "" ^ end_val == ""
          error.text("Start and End dates must both be selected")
        else if start_val > end_val
          error.text("Start must be no later than End")
        else
          error.hide()
      else
        if start_val == "" or end_val == "" or start_val <= end_val
          error.hide()
        else
          error.text("Start must be no later than End")


  setMinMaxOptions = (root, datetimepicker, $input, temporal_type) ->
    min_date = false
    max_date = false
    format = if temporal_type == "range" then 'Y-m-d' else 'm-d'

    start_val = root.find('input.temporal-' + temporal_type + '-start').val()
    stop_val = root.find('input.temporal-' + temporal_type + '-stop').val()

    if $input.hasClass('temporal-' + temporal_type + '-start') and stop_val
      max_date = stop_val.split(' ')[0]
    else if $input.hasClass('temporal-' + temporal_type + '-stop') and start_val
      min_date = start_val.split(' ')[0]

    datetimepicker.setOptions({
      minDate: min_date,
      maxDate: max_date,
      startDate: today,
      formatDate: format
    })

  updateMonthButtons = (month_label) ->
    prev_button = month_label.siblings('button.xdsoft_prev')
    next_button = month_label.siblings('button.xdsoft_next')
    prev_button.show()
    next_button.show()
    month = month_label.find('span').text()
    if month == "January"
      prev_button.hide()
    else if month == "December"
      next_button.hide()

  parseOrdinal = (value) ->
    match = /(\d{4})-(\d{3})/.exec(value)
    return null  unless match # Date is in the wrong format
    year = parseInt(match[1], 10)
    day = parseInt(match[2], 10)
    date = new Date(Date.UTC(year, 0, day))
    return null  unless date.getUTCFullYear() is year # Date is higher than the number of days in the year
    edsc_date.isoUtcDateTimeString(date)

  $.fn.temporalSelectors = (options) ->
    root = this
    uiModel = options["uiModel"]
    uiModelPath = options["modelPath"]
    prefix = options["prefix"]

    # Sanity check
    console.error "Temporal selectors double initialization" if root.data('temporal-selectors')
    root.data('temporal-selectors', true)

    onChangeDateTime = (dp, $input) ->
      validateTemporalInputs(root)
      $input.trigger('change')


    root.find('.temporal-range-picker').datetimepicker
      format: 'Y-m-d H:i:s',
      allowBlank: true,
      className: prefix + '-datetimepicker',
      yearStart: '1960',
      yearEnd: current_year,
      startDate: today,
      onShow: (dp,$input) ->
        setMinMaxOptions(root, this, $input, 'range')
      onChangeDateTime: onChangeDateTime
      onGenerate: (time, input) ->
        time.setHours(0)
        time.setMinutes(0)
        time.setSeconds(0)
        picker = this
        if picker.find('.day-of-year-picker')?.length == 0
          which = if input.is('.temporal-start') then 'start' else 'stop'
          day_of_year_div = $("<div class='day-of-year-picker' data-bind='if: #{uiModelPath}'>
            <label for='day-of-year-#{which}-input'>Day of Year:</label>
            <input id='day-of-year-#{which}-input' data-bind='value: #{uiModelPath}.#{which}.dayOfYearString()' class='day-of-year-input' type='text' placeholder='YYYY-DDD' >
            <button class='button text-button day-of-year-submit' data-input='" + input.attr("id") + "'>Set</button>
            </div>")
          day_of_year_div.appendTo(picker)
          day_of_year_div.find('.day-of-year-submit').on 'click', ->
            value = $(this).prev().val()
            date = parseOrdinal(value)
            if date
              $("#" + $(this).attr("data-input") + ":visible").val(date).trigger('change')
              $(this).parents('.xdsoft_datetimepicker').hide()
              validateTemporalInputs(root)
          ko.applyBindings(page, day_of_year_div[0])

    root.find('.temporal-recurring-picker').datetimepicker
      format: 'm-d H:i:s',
      allowBlank: true,
      className: prefix + '-datetimepicker recurring-datetimepicker',
      yearStart: 2007,
      yearEnd: 2007,
      startDate: today,
      onShow: (dp,$input) ->
        updateMonthButtons($(this).find('.xdsoft_month'))
        setMinMaxOptions(root, this, $input, 'recurring')
      onChangeDateTime: onChangeDateTime
      onChangeMonth: (dp,$input) ->
        updateMonthButtons($(this).find('.xdsoft_month'))
      onGenerate: (time, input) ->
        time.setHours(0)
        time.setMinutes(0)
        time.setSeconds(0)

    root.find('.temporal-recurring-year-range').slider({
      min: 1960,
      max: current_year,
      value: [1960, current_year],
      tooltip: 'hide'
    }).on 'slide', (e) ->
      uiModel.pending.years(e.value)

    # Set the slider when the years change
{'fraction_non_alphanumeric': 0.15079437131184747, 'fraction_numerical': 0.0082614616432138, 'mean_word_length': 2.7907777013076394, 'pattern_counts': {'":': 6, '<': 22, '<?xml version=': 0, '>': 50, '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': '7251846', 'n_tokens_mistral': 3600, 'n_tokens_neox': 3445, 'n_words': 741}