<commit_before>define [
  'views/base/collection_view'
  'views/community_option'
], (CollectionView, CommunityOption, template) ->
  'use strict'

  class CommunitySelectView extends CollectionView
    autoRender: true
    templateName: 'community_select'
    itemView: CommunityOption
    listSelector: '[name=community_id]'
    fallbackSelector: '#no-communities'

    render: ->
      super
      @listenTo @collection, "add", @collectionUpdateHandler
      @listenTo @collection, "remove", @collectionUpdateHandler
      @setupAutocomplete()

    setupAutocomplete: ->
      input = @$el.find('#autocomplete_location').get(0)
      options = {}
      @autoComplete = new google.maps.places.Autocomplete(input, options)
      google.maps.event.addListener(@autoComplete, 'place_changed', @placeSelected)

    placeSelected: =>
      @populateHiddenFieldsFromPlace()
      @displayCommunitySelect()

    displayCommunitySelect: ->
      place = @autoComplete.getPlace()
      @collection.fetch({data: {latitude: place.geometry.location.k, longitude: place.geometry.location.B}})

    collectionUpdateHandler: ->
      if @collection.length > 0
        $('#community_select').removeClass('hidden')
      else
        $('#community_select').addClass('hidden')

    populateHiddenFieldsFromPlace: ->
      place = @autoComplete.getPlace()
      $('#user_neighbourhood').val('')
      $('#user_postal_code').val('')
      $('#user_city').val('')
      $('#user_province').val('')
      $('#user_country').val('')
      for index, component of place.address_components
        if $.inArray('neighborhood', component.types) >= 0
          $('#user_neighbourhood').val(component.long_name)
        if $.inArray('postal_code', component.types) >= 0
          $('#user_postal_code').val(component.long_name)
        if $.inArray('locality', component.types) >= 0
          $('#user_city').val(component.long_name)
        if $.inArray('administrative_area_level_1', component.types) >= 0
          $('#user_province').val(component.long_name)
        if $.inArray('country', component.types) >= 0
          $('#user_country').val(component.long_name)

      $('#user_latitude').val(place.geometry.location.k)
      $('#user_longitude').val(place.geometry.location.B)
<commit_msg>Fix the community select view so that it triggers model updates when fields change.<commit_after>define [
  'views/base/collection_view'
  'views/community_option'
], (CollectionView, CommunityOption, template) ->
  'use strict'

  class CommunitySelectView extends CollectionView
    autoRender: true
    templateName: 'community_select'
    itemView: CommunityOption
    listSelector: '[name=community_id]'
    fallbackSelector: '#no-communities'

    render: ->
      super
      @listenTo @collection, "add", @collectionUpdateHandler
      @listenTo @collection, "remove", @collectionUpdateHandler
      @setupAutocomplete()

    setupAutocomplete: ->
      input = @$el.find('#autocomplete_location').get(0)
      options = {}
      @autoComplete = new google.maps.places.Autocomplete(input, options)
      google.maps.event.addListener(@autoComplete, 'place_changed', @placeSelected)

    placeSelected: =>
      @populateHiddenFieldsFromPlace()
      @displayCommunitySelect()

    displayCommunitySelect: ->
      place = @autoComplete.getPlace()
      @collection.fetch({data: {latitude: place.geometry.location.k, longitude: place.geometry.location.B}})

    collectionUpdateHandler: ->
      if @collection.length > 0
        $('#community_select').removeClass('hidden')
        $(@listSelector).change()
      else
        $('#community_select').addClass('hidden')

    populateHiddenFieldsFromPlace: ->
      place = @autoComplete.getPlace()
      $('#user_neighbourhood').val('')
      $('#user_postal_code').val('')
      $('#user_city').val('')
      $('#user_province').val('')
      $('#user_country').val('')
      for index, component of place.address_components
        if $.inArray('neighborhood', component.types) >= 0
          $('#user_neighbourhood').val(component.long_name)
          $('#user_neighbourhood').change()
        if $.inArray('postal_code', component.types) >= 0
          $('#user_postal_code').val(component.long_name)
          $('#user_postal_code').change()
        if $.inArray('locality', component.types) >= 0
          $('#user_city').val(component.long_name)
          $('#user_city').change()
        if $.inArray('administrative_area_level_1', component.types) >= 0
          $('#user_province').val(component.long_name)
          $('#user_province').change()
        if $.inArray('country', component.types) >= 0
          $('#user_country').val(component.long_name)
          $('#user_country').change()

      $('#user_latitude').val(place.geometry.location.k)
      $('#user_latitude').change()
      $('#user_longitude').val(place.geometry.location.B)
      $('#user_longitude').change()

{'fraction_non_alphanumeric': 0.1551128278105306, 'fraction_numerical': 0.0032526936369180726, 'mean_word_length': 3.542936288088643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '3748404', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1395, 'n_words': 225}