36:96aec8cf1ae9
Anton Shestakov <av6@dwimlabs.net>, Tue, 22 Mar 2016 16:22:03 +0800
contacts: hash bare jid for fallback avatar background color

next change 40:066e4cb38adb
previous change 31:d0248d45562f

js/contacts.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.Contact = (function(superClass) {
extend(Contact, superClass);
function Contact() {
return Contact.__super__.constructor.apply(this, arguments);
}
Contact.prototype.idAttribute = 'jid';
Contact.prototype.defaults = {
presence: 'unavailable'
};
Contact.prototype.initialize = function() {
this.on('add change:avatar', function() {
var avatar;
avatar = this.get('avatar');
if ((avatar != null ? avatar.mime : void 0) && (avatar != null ? avatar.data : void 0)) {
return this.set('d/avatar', "data:" + avatar.mime + ";base64," + avatar.data);
} else {
return this.unset('d/avatar');
}
});
this.on('add change:fullname change:nickname change:jid', function() {
return this.set('d/handle', this.get('fullname') || this.get('nickname') || this.get('jid'));
});
return this.on('add change:show change:presence', function() {
return this.set('d/pip', this.has('show') ? this.get('show') : this.get('presence'));
});
};
return Contact;
})(Backbone.Model);
Tram.Contacts = (function(superClass) {
extend(Contacts, superClass);
function Contacts() {
return Contacts.__super__.constructor.apply(this, arguments);
}
Contacts.prototype.model = Tram.Contact;
Contacts.prototype.comparator = function(model) {
if (model.get('presence') === 'unavailable') {
return 1;
} else if (model.get('type') === 'self') {
return -1;
} else {
return 0;
}
};
Contacts.prototype.initialize = function() {
return this.on('change:presence change:type', this.sort);
};
return Contacts;
})(Backbone.Collection);
Tram.AvatarView = (function(superClass) {
extend(AvatarView, superClass);
function AvatarView() {
return AvatarView.__super__.constructor.apply(this, arguments);
}
AvatarView.prototype.tagName = 'div';
AvatarView.prototype.className = 'vignette';
AvatarView.prototype.colors = Tram.colors.avatar;
AvatarView.prototype.template = $('#avatar-template').html();
AvatarView.prototype.hash = function(string) {
var i, j, ref, result;
result = 0;
for (i = j = 0, ref = string.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
result += string.charCodeAt(i);
}
return result;
};
AvatarView.prototype.getHashedColors = function() {
var ci;
ci = this.hash(this.model.get('bjid')) % this.colors.length;
return "color: white; background: " + this.colors[ci] + ";";
};
AvatarView.prototype.render = function() {
this.$el.html(this.template);
this.rivet = rivets.bind(this.el, {
model: this.model,
view: this
});
return this;
};
AvatarView.prototype.remove = function() {
this.rivet.unbind();
return AvatarView.__super__.remove.apply(this, arguments);
};
return AvatarView;
})(Backbone.View);
Tram.ContactView = (function(superClass) {
extend(ContactView, superClass);
function ContactView() {
return ContactView.__super__.constructor.apply(this, arguments);
}
ContactView.prototype.tagName = 'li';
ContactView.prototype.className = 'contact';
ContactView.prototype.template = $('#contact-template').html();
ContactView.prototype.colors = Tram.colors.presence;
ContactView.prototype.initialize = function() {
this.$el.attr('data-jid', this.model.get('jid'));
this.$el.html(this.template);
this.$avatarColumn = this.$('.avatar-column');
return this.bind();
};
ContactView.prototype.bind = function() {};
ContactView.prototype.getPipStyle = function() {
return "background: " + (this.colors[this.model.get('d/pip')] || this.colors['default']) + ";";
};
ContactView.prototype.render = function() {
var av;
this.rivet = rivets.bind(this.el, {
model: this.model,
view: this
});
av = new Tram.AvatarView({
model: this.model
});
this.$avatarColumn.prepend(av.render().el);
return this;
};
ContactView.prototype.remove = function() {
this.rivet.unbind();
return ContactView.__super__.remove.apply(this, arguments);
};
return ContactView;
})(Backbone.View);
Tram.ContactsApp = (function(superClass) {
extend(ContactsApp, superClass);
function ContactsApp() {
return ContactsApp.__super__.constructor.apply(this, arguments);
}
ContactsApp.prototype.initialize = function() {
this.listenTo(this.collection, 'add', this.onAdd);
return this.listenTo(this.collection, 'sort', this.onSort);
};
ContactsApp.prototype.onAdd = function(model, collection) {
var el, mi;
mi = collection.indexOf(model);
model.view = new Tram.ContactView({
model: model
});
el = model.view.render().el;
if (mi === 0) {
return this.$el.prepend(el);
} else {
return this.$el.children().eq(mi - 1).after(el);
}
};
ContactsApp.prototype.onSort = function(collection, options) {
if (!options.add) {
return collection.each((function(_this) {
return function(model) {
model.view.$el.detach();
return _this.$el.append(model.view.el);
};
})(this));
}
};
return ContactsApp;
})(Backbone.View);
}).call(this);
//# sourceMappingURL=contacts.js.map