app/assets/javascripts/backbone/views/contacts/me.js.coffee
d4872cda
 Xmpp.Views.Contacts ||= {}
 
4b5bb9c5
 class Xmpp.Views.Contacts.MeView extends Backbone.View
d4872cda
   template: JST["backbone/templates/contacts/me"]
   el: $('#js-me')
 
c4a1e746
   events:
6347ea28
     'click .js-edit-status': (e) -> @editTextInput(e)
     'keypress .js-edit-status': (e) -> @confirmMyStatus(e)
 
     'click .js-edit-name': (e) -> @editTextInput(e)
     'keypress .js-edit-name': (e) -> @confirmMyName(e)
 
c4a1e746
     'click .js-change-state': (e) -> @openChangeState(e)
     'click .js-state-clickable': (e) -> @confirmChangeState(e)
 
6fb9497e
     #avatar change events:
6621353c
     'mouseover .avatar': -> $('.change-avatar').show()
     'mouseout .avatar': -> $('.change-avatar').hide()
6fb9497e
     'click .change-avatar': -> $('input[name=avatar]').click()
     'change input[name=avatar]': 'uploadAvatar'
6621353c
 
d4872cda
   initialize: () ->
     _.bindAll(this)
 
     @model.on('change', @updateContact, this)
 
   updateContact: (me) ->
     @model = me
     @render()
 
   render: ->
     contact = @model.toJSON()
     $(@el).html(@template(contact))
c4a1e746
     this
 
6347ea28
   editTextInput: (e) ->
c4a1e746
     $this = $(e.currentTarget)
 
     if ($this.hasClass('empty'))
       $this.val('')
 
     if (! $this.hasClass('editing'))
       $this.removeClass('empty')
            .addClass('editing')
 
   confirmMyStatus: (e) ->
     if (e.which != 13 && e.which != 10)
       return
 
     $this = $(e.currentTarget)
 
     if ($this.val() == '')
       $this.removeClass('editing')
         .addClass('empty')
         .val(I18n.t('chat.roster.change_status_msg'))
     else
       $this.removeClass('editing')
 
     $this.blur()
     App.Com.updateMyStatus(@_getStatusMessage(), @_getState())
 
6347ea28
   confirmMyName: (e) ->
     if (e.which != 13 && e.which != 10)
       return
 
     $this = $(e.currentTarget)
 
     if ($this.val() == '')
       $this.removeClass('editing')
         .val(@model.get('jid'))
     else
       $this.removeClass('editing')
 
     $this.blur()
6fb9497e
     App.Com.updateMyVcardName(@_getName())
56a05761
     @model.set('name', @_getName())
6347ea28
 
c4a1e746
   openChangeState: (e) ->
     $this = $(e.currentTarget)
 
     $this.addClass('editing')
          .find('.hidden').removeClass('hidden')
 
     $('.rolldown').removeClass('js-change-state')
 
   confirmChangeState: (e) ->
     $this = $(e.currentTarget)
 
     $('.rolldown').removeClass('editing')
       .addClass('js-change-state')
       .children().addClass('hidden')
 
     $('.rolldown').find('a').removeClass('active')
 
     $this.removeClass('hidden')
       .find('a').addClass('active')
 
     App.Com.updateMyStatus(@_getStatusMessage(), @_getState())
 
6fb9497e
   uploadAvatar: (e) ->
     fr = new FileReader;
     fr.onloadend = ->
       App.Models.me.set(avatar: fr.result)
       App.Com.updateMyVcardAvatar(fr.result)
 
     fr.readAsDataURL(e.target.files[0]);
 
c4a1e746
   _getStatusMessage: ->
6347ea28
     if $('.js-edit-status').hasClass('empty')
c4a1e746
       return ''
     else
6347ea28
       $('.js-edit-status').val()
c4a1e746
 
   _getState: ->
     $('.js-state-clickable:not(.hidden)').data('state')
6347ea28
 
   _getName: ->
     $('.js-edit-name').val()