39:28ad6d3e2618
Anton Shestakov <av6@dwimlabs.net>, Wed, 23 Mar 2016 16:55:52 +0800
index: maintain only one contact with type 'self' This isn't done in an event handler of contacts collection because doing it in 'add' handler would trigger 'sort' event after the model has been added, but before its 'add' event has propagated, and that's dumb.

next change 72:249e458f53ec
previous change 31:d0248d45562f

coffee/messages.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
class Tram.Message extends Backbone.Model
initialize: ->
@on 'add change:stamp', ->
if @has('stamp')
@set('d/mstamp', moment(@get('stamp')))
else
@unset('d/mstamp')
class Tram.Messages extends Backbone.Collection
model: Tram.Message
splitThreshold: 30 * 60 * 1000
foldThreshold: 60 * 1000
comparator: (model) ->
model.get('stamp').valueOf()
initialize: ->
@on('add', @onAdd)
onAdd: (model) ->
mi = @indexOf(model)
prev = @models[mi - 1]
@_splitOrFold(prev, model)
next = @models[mi + 1]
@_splitOrFold(model, next)
_splitOrFold: (m1, m2) ->
if not m2?
return
if not m1?
m2.unset('d/split')
m2.unset('d/fold')
return
if @_splittable(m1, m2)
m2.set('d/split', true)
else if @_foldable(m1, m2)
m2.set('d/fold', true)
_splittable: (m1, m2) ->
Math.abs(m1.get('stamp').valueOf() - m2.get('stamp').valueOf()) > @splitThreshold
_foldable: (m1, m2) ->
# check stamp, type, etc
m1.get('from') is m2.get('from') and Math.abs(m1.get('stamp').valueOf() - m2.get('stamp').valueOf()) < @foldThreshold
class Tram.MessageView extends Backbone.View
tagName: 'div'
className: 'message'
template: $('#message-template').html()
initialize: ->
@$el.html(@template)
@$avatarColumn = @$('.avatar-column')
@bind()
bind: ->
@listenTo(@model, 'change:contact', @updateContact)
updateContact: ->
if not @model.previous('contact')? and @model.get('contact')?
av = new Tram.AvatarView(model: contact)
@$avatarColumn.prepend(av.render().el)
getHandle: ->
@model.get('contact')?.get('d/handle') or @model.get('from')
render: (model) ->
@rivet = rivets.bind(@el, model: @model, view: this)
contact = @model.get('contact')
if contact
av = new Tram.AvatarView(model: contact)
@$avatarColumn.prepend(av.render().el)
@
remove: ->
@rivet.unbind()
super
class Tram.LogApp extends Backbone.View
initialize: ->
@listenTo(@collection, 'add', @onAdd)
onAdd: (model, collection) ->
mi = collection.indexOf(model)
view = new Tram.MessageView(model: model)
el = view.render().el
if mi is 0
@$el.prepend el
else
@$el.children().eq(mi - 1).after(el)
if @bottomed
@scroll()
bottomed: ->
return @$el.scrollTop() + @$el.height() == @$el.get(0).scrollHeight
scroll: ->
@$el.scrollTop(@$el.get(0).scrollHeight)