151:0178573216e3
Anton Shestakov <av6@dwimlabs.net>, Thu, 14 Apr 2016 02:05:24 +0800
index: remove offline contacts only when they change to being offline Since new contacts are currently added while they still have the default presence of 'unavailable', add event sees them as offline and throws them away immediately, before their presence could change to something else.

next change 191:d2453b933c21
previous change 104:f5614e27bbf1

js/messages.js

Permissions: -rw-r--r--

Other formats: Feeds:
// Generated by CoffeeScript 1.10.0
(function() {
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
Tram.Message = (function(superClass) {
extend(Message, superClass);
function Message() {
return Message.__super__.constructor.apply(this, arguments);
}
Message.prototype.initialize = function() {
return this.on('add change:stamp', function() {
if (this.has('stamp')) {
return this.set('d/mstamp', moment(this.get('stamp')));
} else {
return this.unset('d/mstamp');
}
});
};
return Message;
})(Backbone.Model);
Tram.Messages = (function(superClass) {
extend(Messages, superClass);
function Messages() {
return Messages.__super__.constructor.apply(this, arguments);
}
Messages.prototype.model = Tram.Message;
Messages.prototype.splitThreshold = 30 * 60 * 1000;
Messages.prototype.foldThreshold = 60 * 1000;
Messages.prototype.comparator = function(model) {
return model.get('stamp').valueOf();
};
Messages.prototype.initialize = function() {
return this.on('add', this.onAdd);
};
Messages.prototype.onAdd = function(model) {
var mi, next, prev;
mi = this.indexOf(model);
prev = this.models[mi - 1];
this._splitOrFold(prev, model);
next = this.models[mi + 1];
return this._splitOrFold(model, next);
};
Messages.prototype._splitOrFold = function(m1, m2) {
if (m2 == null) {
return;
}
if (m1 == null) {
m2.unset('d/split');
m2.unset('d/fold');
return;
}
if (this._splittable(m1, m2)) {
return m2.set('d/split', true);
} else {
m2.unset('d/split');
if (this._foldable(m1, m2)) {
return m2.set('d/fold', true);
} else {
return m2.unset('d/fold');
}
}
};
Messages.prototype._splittable = function(m1, m2) {
return Math.abs(m1.get('stamp').valueOf() - m2.get('stamp').valueOf()) > this.splitThreshold;
};
Messages.prototype._foldable = function(m1, m2) {
return m1.get('from') === m2.get('from') && Math.abs(m1.get('stamp').valueOf() - m2.get('stamp').valueOf()) < this.foldThreshold;
};
return Messages;
})(Backbone.Collection);
Tram.MessageView = (function(superClass) {
extend(MessageView, superClass);
function MessageView() {
return MessageView.__super__.constructor.apply(this, arguments);
}
MessageView.prototype.tagName = 'div';
MessageView.prototype.className = 'message';
MessageView.prototype.template = $('#message-template').html();
MessageView.prototype.initialize = function() {
this.$el.attr('data-id', this.model.get('id'));
this.$el.html(this.template);
this.$avatarColumn = this.$('.avatar-column');
return this.bind();
};
MessageView.prototype.bind = function() {
return this.listenTo(this.model, 'change:contact', this.updateContact);
};
MessageView.prototype.updateContact = function() {
var av;
if ((this.model.previous('contact') == null) && (this.model.get('contact') != null)) {
av = new Tram.AvatarView({
model: contact
});
return this.$avatarColumn.prepend(av.render().el);
}
};
MessageView.prototype.getHandle = function() {
var ref;
return ((ref = this.model.get('contact')) != null ? ref.get('d/handle') : void 0) || this.model.get('from');
};
MessageView.prototype.render = function(model) {
var av, contact;
this.rivet = rivets.bind(this.el, {
model: this.model,
view: this
});
contact = this.model.get('contact');
if (contact) {
av = new Tram.AvatarView({
model: contact
});
this.$avatarColumn.prepend(av.render().el);
}
return this;
};
MessageView.prototype.remove = function() {
this.rivet.unbind();
return MessageView.__super__.remove.apply(this, arguments);
};
return MessageView;
})(Backbone.View);
Tram.LogApp = (function(superClass) {
extend(LogApp, superClass);
function LogApp() {
return LogApp.__super__.constructor.apply(this, arguments);
}
LogApp.prototype.tagName = 'div';
LogApp.prototype.className = 'log';
LogApp.prototype.initialize = function() {
return this.listenTo(this.collection, 'add', this.onAdd);
};
LogApp.prototype.onAdd = function(model, collection) {
var el, mi, view;
mi = collection.indexOf(model);
view = new Tram.MessageView({
model: model
});
el = view.render().el;
if (mi === 0) {
this.$el.prepend(el);
} else {
this.$el.children().eq(mi - 1).after(el);
}
if (this.bottomed) {
return this.scroll();
}
};
LogApp.prototype.bottomed = function() {
return this.$el.scrollTop() + this.$el.height() === this.$el.get(0).scrollHeight;
};
LogApp.prototype.scroll = function() {
return this.$el.scrollTop(this.$el.get(0).scrollHeight);
};
return LogApp;
})(Backbone.View);
}).call(this);
//# sourceMappingURL=messages.js.map