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:
e2f8d966
     'mouseover .avatar': 'mouseoverAvatar'
     'mouseout .avatar': 'mouseoutAvatar'
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) ->
e2f8d966
     return false if App.Models.me.get('isFB')
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')
9d063577
                   .find('.drop-down').addClass('hidden')
c4a1e746
 
   confirmChangeState: (e) ->
     $this = $(e.currentTarget)
 
     $('.rolldown').removeClass('editing')
       .addClass('js-change-state')
       .children().addClass('hidden')
 
     $('.rolldown').find('a').removeClass('active')
9d063577
                   .find('.drop-down').removeClass('hidden')
c4a1e746
 
     $this.removeClass('hidden')
       .find('a').addClass('active')
 
     App.Com.updateMyStatus(@_getStatusMessage(), @_getState())
 
e2f8d966
   mouseoverAvatar: ->
     return false if App.Models.me.get('isFB')
     $('.change-avatar').show()
 
   mouseoutAvatar: ->
     return false if App.Models.me.get('isFB')
     $('.change-avatar').hide()
 
6fb9497e
   uploadAvatar: (e) ->
e2f8d966
     return false if App.Models.me.get('isFB')
 
6fb9497e
     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()