27:f59d389adcc4
Anton Shestakov <av6@dwimlabs.net>, Mon, 21 Mar 2016 21:13:17 +0800
contacts: new ui element, contact list

next change 31:d0248d45562f

coffee/contacts.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
class Tram.Contact extends Backbone.Model
idAttribute: 'jid'
defaults:
presence: 'unavailable'
initialize: ->
@on 'add change:avatar', ->
avatar = @get 'avatar'
if avatar?.mime and avatar?.data
@set('d/avatar', "data:#{ avatar.mime };base64,#{ avatar.data }")
else
@unset('d/avatar')
@on 'add change:fullname change:nickname change:jid', ->
@set('d/handle', @get('fullname') or @get('nickname') or @get('jid'))
@on 'add change:show change:presence', ->
@set('d/pip', if @has('show') then @get('show') else @get('presence'))
class Tram.Contacts extends Backbone.Collection
model: Tram.Contact
comparator: (model) ->
if model.get('presence') is 'unavailable'
return 1
else if model.get('type') is 'self'
return -1
else
return 0
initialize: ->
@on('change:presence change:type', @sort)
class Tram.ContactView extends Backbone.View
tagName: 'li'
className: 'contact'
template: $('#contact-template').html()
colors: Tram.colors.presence
initialize: ->
@$el.attr('data-jid', @model.get('jid'))
@$el.html(@template)
@bind()
bind: ->
getPipStyle: ->
"background: #{ @colors[@model.get 'd/pip'] || @colors['default'] };"
render: ->
@rivet = rivets.bind(@el, model: @model, view: this)
@
remove: ->
@rivet.unbind()
super
class Tram.ContactsApp extends Backbone.View
initialize: ->
@listenTo(@collection, 'add', @onAdd)
@listenTo(@collection, 'sort', @onSort)
onAdd: (model, collection) ->
mi = collection.indexOf model
model.view = new Tram.ContactView(model: model)
el = model.view.render().el
if mi is 0
@$el.prepend(el)
else
@$el.children().eq(mi - 1).after(el)
onSort: (collection, options) ->
if not options.add
collection.each (model) =>
model.view.$el.detach()
@$el.append(model.view.el)