227:64acb7b50eee
Anton Shestakov <av6@dwimlabs.net>, Fri, 12 Aug 2016 22:26:01 +0800
pipelines: re-enable idempotence testing

next change 236:7e7d68a03bad
previous change 201:9f930147cc6e

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)
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
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) ->
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: ->
@$log.scrollTop() + @$log.height() == @$log.get(0).scrollHeight
scroll: ->
@$log.scrollTop(@$log.get(0).scrollHeight)
remove: ->
@rivet.unbind()
super