322:7dfdf32e8577
Anton Shestakov <av6@dwimlabs.net>, Sat, 14 Jul 2018 20:23:13 +0800
index: authorizing contact also adds it to the roster Maybe there are cases when this doesn't make sense, but so far this looks like the right thing to do.

next change 325:2baf34a19915
previous change 270:eea4a3f6b615

coffee/messages.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
class Tram.Message extends Backbone.Model
defaults: ->
stamp: new Date()
initialize: ->
@on 'add change:stamp', ->
@set('d/mstamp', moment(@get('stamp')))
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
m2.unset('d/split')
if @_foldable(m1, m2)
m2.set('d/fold', true)
else
m2.unset('d/fold')
_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
templateEl: $($('#message-template').html())
initialize: ->
@setElement(@templateEl.clone())
@$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)
render: (model) ->
@rivet = rivets.bind(@el, message: @model, view: @)
contact = @model.get('contact')
if contact
@av = new Tram.AvatarView(model: contact)
@$avatarColumn.prepend(@av.render().el)
@
remove: ->
@av?.remove()
@rivet.unbind()
super
class Tram.LogApp extends Backbone.View
templateEl: $($('#chat-template').html())
initialize: ->
@setElement(@templateEl.clone())
@$log = @$('.log')
@rivet = rivets.bind(@el, model: @model, view: @)
@bind()
bind: ->
@listenTo(@collection, 'add', @onAdd)
onAdd: (model, collection) ->
bottomed = @bottomed()
mi = collection.indexOf(model)
view = new Tram.MessageView(model: model)
el = view.render().el
if mi is 0
@$log.prepend(el)
else
@$log.children().eq(mi - 1).after(el)
if bottomed
@scroll()
bottomed: ->
@$el.scrollTop() + @$el.innerHeight() == @el.scrollHeight
scroll: ->
@$el.scrollTop(@el.scrollHeight)
remove: ->
@rivet.unbind()
super