<commit_before>App = @tcglServer || {}

class App.Views.Line extends Backbone.View
  events:
    'change': 'change'

  initialize: ->
    @lineSelect = @$('.js-line_select')
    @daySelect = @$('.js-day_select')
    @originSelect = @$('.js-origin_select')
    @destinationSelect = @$('.js-destination_select')

    @lineSelect.on 'change', @lineChange
    @daySelect.on 'change', @dayChange
    @originSelect.on 'change', @originChange
    @destinationSelect.on 'change', @destinationChange

    @itineraryList = new App.Views.ItineraryList()

    @listenTo(@model, 'remove', @remove)
    @listenTo(@model, 'change:line', @changeLine)

    @schedules = []
    @lineChange()

  lineChange: =>
    @model.set('line', @lineSelect.val())
    App.Collections.Lines.trigger 'change:line_id'

    $.ajax("/lines/#{@model.get('line')}"+
           "/days", {
    success: (res) =>
      @fillDays(res[0])
      @fillOrigins(res[1])
      @fillDestinations(res[2])
      @scheduleChange()
      @itineraryChange()
    })

  dayChange: =>
    @model.set('day', @daySelect.val())

    $.ajax("/lines/#{@model.get('line')}"+
           "/days/#{@model.get('day')}"+
           "/origins", {
    success: (res) =>
      @fillOrigins(res[0])
      @fillDestinations(res[1])
      @scheduleChange()
    })

  originChange: =>
    @model.set('origin', @originSelect.val())

    $.ajax("/lines/#{@model.get('line')}"+
           "/days/#{@model.get('day')}"+
           "/origins/#{@model.get('origin')}"+
           "/destinations", {
    success: (res) =>
      @fillDestinations(res[0])
      @scheduleChange()
    })

  destinationChange: =>
    @model.set('destination', @destinationSelect.val())

    @scheduleChange()

  fillDays: (days) ->
    @daySelect.html('')
    @model.set('day', days[0].id)

    _.each days, (day) =>
      dayEl = $("<option />").val(day.id).text(day.name)

      if moment().day() > 0 and moment().day() < 6 and day.id == 0
        dayEl.prop('selected', 'selected')
      else if moment().day() == 6 and day.id == 1
        dayEl.prop('selected', 'selected')
      else
        dayEl.prop('selected', 'selected')

      @daySelect.append(dayEl)

  fillOrigins: (origins) ->
    @originSelect.html('')
    @model.set('origin', origins[0].id)

    _.each origins, (origin) =>
      @originSelect.append($("<option />").val(origin.id).text(origin.name))

  fillDestinations: (destinations) ->
    @destinationSelect.html('')
    @model.set('destination', destinations[0].id)

    _.each destinations, (destination) =>
      @destinationSelect.append($("<option />")
                        .val(destination.id)
                        .text(destination.name))

  scheduleChange: ->
    $.ajax("/lines/#{@model.get('line')}"+
           "/days/#{@model.get('day')}"+
           "/origins/#{@model.get('origin')}"+<commit_msg>Fix the auto select of the week day based on today.<commit_after>App = @tcglServer || {}

class App.Views.Line extends Backbone.View
  events:
    'change': 'change'

  initialize: ->
    @lineSelect = @$('.js-line_select')
    @daySelect = @$('.js-day_select')
    @originSelect = @$('.js-origin_select')
    @destinationSelect = @$('.js-destination_select')

    @lineSelect.on 'change', @lineChange
    @daySelect.on 'change', @dayChange
    @originSelect.on 'change', @originChange
    @destinationSelect.on 'change', @destinationChange

    @itineraryList = new App.Views.ItineraryList()

    @listenTo(@model, 'remove', @remove)
    @listenTo(@model, 'change:line', @changeLine)

    @schedules = []
    @lineChange()

  lineChange: =>
    @model.set('line', @lineSelect.val())
    App.Collections.Lines.trigger 'change:line_id'

    $.ajax("/lines/#{@model.get('line')}"+
           "/days", {
    success: (res) =>
      @fillDays(res[0])
      @fillOrigins(res[1])
      @fillDestinations(res[2])
      @scheduleChange()
      @itineraryChange()
    })

  dayChange: =>
    @model.set('day', @daySelect.val())

    $.ajax("/lines/#{@model.get('line')}"+
           "/days/#{@model.get('day')}"+
           "/origins", {
    success: (res) =>
      @fillOrigins(res[0])
      @fillDestinations(res[1])
      @scheduleChange()
    })

  originChange: =>
    @model.set('origin', @originSelect.val())

    $.ajax("/lines/#{@model.get('line')}"+
           "/days/#{@model.get('day')}"+
           "/origins/#{@model.get('origin')}"+
           "/destinations", {
    success: (res) =>
      @fillDestinations(res[0])
      @scheduleChange()
    })

  destinationChange: =>
    @model.set('destination', @destinationSelect.val())

    @scheduleChange()

  fillDays: (days) ->
    @daySelect.html('')
    @model.set('day', days[0].id)

    _.each days, (day) =>
      dayEl = $("<option />").val(day.id).text(day.name)

      if moment().day() > 0 and moment().day() < 6 and day.id == 1
        dayEl.prop('selected', 'selected')
      else if moment().day() == 6 and day.id == 2
        dayEl.prop('selected', 'selected')
      else if moment().day() == 7 and day.id == 3
        dayEl.prop('selected', 'selected')

      @daySelect.append(dayEl)

  fillOrigins: (origins) ->
    @originSelect.html('')
    @model.set('origin', origins[0].id)

    _.each origins, (origin) =>
      @originSelect.append($("<option />").val(origin.id).text(origin.name))

  fillDestinations: (destinations) ->
    @destinationSelect.html('')
    @model.set('destination', destinations[0].id)

    _.each destinations, (destination) =>
      @destinationSelect.append($("<option />")
                        .val(destination.id)
                        .text(destination.name))

  scheduleChange: ->
    $.ajax("/lines/#{@model.get('line')}"+
           "/days/#{@model.get('day')}"+
           "/origins/#{@model.get('origin')}"+
{'fraction_non_alphanumeric': 0.20764552562988706, 'fraction_numerical': 0.005212858384013901, 'mean_word_length': 2.8119205298013243, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, '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': '2136390', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1865, 'n_words': 297}