86:6f227227ac6f
Anton Shestakov <av6@dwimlabs.net>, Sun, 03 Apr 2016 18:51:54 +0800
sidebar: trim status text for sending in a new presence

next change 104:f5614e27bbf1
previous change 75:b3c1925162c8

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
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
tagName: 'div'
className: 'message'
template: $('#message-template').html()
initialize: ->
@$el.attr('data-id', @model.get('id'))
@$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: ->
@$el.scrollTop() + @$el.height() == @$el.get(0).scrollHeight
scroll: ->
@$el.scrollTop(@$el.get(0).scrollHeight)