<commit_before>Translation = require '../models/translation'

module.exports = App.ProjectController = Ember.Controller.extend
  currentLocale: null
  isNewLocaleVisible: false
  message: null
  newLocale: null
  
  init: ->
    @userDidChange()
    zooniverse.models.User.on 'change', => @userDidChange()
  
  userDidChange: ->
    @set 'currentUser', zooniverse.models.User.current
  
  messageDidChange: (->
    if @get('message')
      setTimeout =>
        @set 'message', null
      , 8000
  ).observes('message')
  
  newLocaleDidChange: (->
    search = @get 'newLocale'
    if search and search.length > 2
      matcher = new RegExp search, 'i'
      seedLocale = @get 'model.translation.seed_locale'
      @set 'localeSearchResults', zooniverse.util.locales.filter (locale) ->
        if locale.code is seedLocale
          false
        else
          locale.code.match(matcher) or locale.name.match(matcher)
    else
      @set 'localeSearchResults', null
  ).observes('newLocale')
  
  deployable: (->
    !!@get('devUser') and !!@get('currentLocale')
  ).property('devUser', 'currentLocale')
  
  devUser: (->
    currentUser = @get 'currentUser'
    currentUser?.admin or currentUser?.developer
  ).property('currentUser')
  
  locales: (->
    currentLocale = @get 'currentLocale'
    locales = @get 'model.translation.locales'
    locales.addObject(currentLocale) if currentLocale and locales.indexOf(currentLocale) is -1
    locales
  ).property('model.translation.locales')
  
  actions:
    deploy: ->
      deployEndpoint = "/projects/#{ @get('model.name') }/translations/"
      deployEndpoint += if @get('deployable') then 'deploy' else 'test_deploy'

      localeName = zooniverse.util.localeCodes[@get('currentLocale')]
      zooniverse.api.post deployEndpoint, locale: @get('currentLocale'), (response) =>

        url = if @get('model.name') is 'galaxy_zoo'
          "http://#{ @get('model.bucket') }/?lang=#{ response.locale }"
        else
          "http://#{ @get('model.bucket') }?lang=./#{ @get('model.translation.deploy_path') }/#{ response.locale }.json"

        @set 'message', "#{ localeName } was successfully deployed. <a href=\"#{ url }\" target=\"blank\">View on site here.</a>"
      , =>
        @set 'message', "There was a problem deploying #{ localeName }.\nTry again in a moment."
    
    chooseLocale: (locale) ->
      @send 'changeLocale', locale
      @send 'cancelNewLocale'
    
    changeLocale: (locale) ->
      loadingIndicator = new Spinner().spin()
      document.querySelector('#app').appendChild loadingIndicator.el
      
      setTimeout =>
        @set 'currentLocale', locale<commit_msg>Hack for zooniverse-static<commit_after>Translation = require '../models/translation'

module.exports = App.ProjectController = Ember.Controller.extend
  currentLocale: null
  isNewLocaleVisible: false
  message: null
  newLocale: null
  
  init: ->
    @userDidChange()
    zooniverse.models.User.on 'change', => @userDidChange()
  
  userDidChange: ->
    @set 'currentUser', zooniverse.models.User.current
  
  messageDidChange: (->
    if @get('message')
      setTimeout =>
        @set 'message', null
      , 8000
  ).observes('message')
  
  newLocaleDidChange: (->
    search = @get 'newLocale'
    if search and search.length > 2
      matcher = new RegExp search, 'i'
      seedLocale = @get 'model.translation.seed_locale'
      @set 'localeSearchResults', zooniverse.util.locales.filter (locale) ->
        if locale.code is seedLocale
          false
        else
          locale.code.match(matcher) or locale.name.match(matcher)
    else
      @set 'localeSearchResults', null
  ).observes('newLocale')
  
  deployable: (->
    !!@get('devUser') and !!@get('currentLocale')
  ).property('devUser', 'currentLocale')
  
  devUser: (->
    currentUser = @get 'currentUser'
    currentUser?.admin or currentUser?.developer
  ).property('currentUser')
  
  locales: (->
    currentLocale = @get 'currentLocale'
    locales = @get 'model.translation.locales'
    locales.addObject(currentLocale) if currentLocale and locales.indexOf(currentLocale) is -1
    locales
  ).property('model.translation.locales')
  
  actions:
    deploy: ->
      deployEndpoint = "/projects/#{ @get('model.name') }/translations/"
      deployEndpoint += if @get('deployable') then 'deploy' else 'test_deploy'

      localeName = zooniverse.util.localeCodes[@get('currentLocale')]
      zooniverse.api.post deployEndpoint, locale: @get('currentLocale'), (response) =>

        url = "http://"
        url += if @get('model.bucket') is 'zooniverse-static'
          "#{ @get('model.bucket_path') }/"
        else
          "#{ @get('model.bucket') }/"

        url += if @get('model.name') is 'galaxy_zoo'
          "?lang=#{ response.locale }"
        else
          "?lang=./#{ @get('model.translation.deploy_path') }/#{ response.locale }.json"

        @set 'message', "#{ localeName } was successfully deployed. <a href=\"#{ url }\" target=\"blank\">View on site here.</a>"
      , =>
        @set 'message', "There was a problem deploying #{ localeName }.\nTry again in a moment."
    
    chooseLocale: (locale) ->
      @send 'changeLocale', locale
      @send 'cancelNewLocale'
    
    changeLocale: (locale) ->
      loadingIndicator = new Spinner().spin()
      document.querySelector('#app').appendChild loadingIndicator.el
      
      setTimeout =>
        @set 'currentLocale', locale
{'fraction_non_alphanumeric': 0.14773980154355015, 'fraction_numerical': 0.002205071664829107, 'mean_word_length': 2.9904692082111435, 'pattern_counts': {'":': 0, '<': 7, '<?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': '641844', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1628, 'n_words': 361}