Default avatar support

Fixes #264

Implement the equivalent of java's String.hashCode on the conversation model.
Change avatar template and attributes. Use css classes for colors.
This commit is contained in:
lilia 2015-06-18 17:05:00 -07:00
parent ada8f77930
commit e26b9bfbc7
9 changed files with 120 additions and 11 deletions

View file

@ -237,11 +237,22 @@
}
},
getAvatarUrl: function() {
getAvatar: function() {
if (this.avatarUrl === undefined) {
this.updateAvatarUrl(true);
}
return this.avatarUrl || '/images/default.png';
if (this.avatarUrl) {
return { url: this.avatarUrl };
} else if (this.isPrivate()) {
var title = this.getTitle() || '';
var initials = title.trim()[0];
return {
color: Math.abs(this.hashCode()) % 17,
content: initials
};
} else {
return { url: '/images/default.png' };
}
},
resolveConflicts: function(number) {
@ -262,6 +273,22 @@
}
});
}.bind(this));
},
hashCode: function() {
if (this.hash === undefined) {
var string = this.getTitle() || '';
if (string.length === 0) {
return 0;
}
var hash = 0;
for (var i = 0; i < string.length; i++) {
hash = ((hash<<5)-hash) + string.charCodeAt(i);
hash = hash & hash; // Convert to 32bit integer
}
this.hash = hash;
}
return this.hash;
}
});

View file

@ -45,7 +45,7 @@
last_message: this.model.get('lastMessage'),
last_message_timestamp: moment(this.model.get('timestamp')).format('MMM D'),
number: this.model.getNumber(),
avatar_url: this.model.getAvatarUrl()
avatar: this.model.getAvatar()
}, this.render_partials())
);

View file

@ -31,9 +31,9 @@
},
render_attributes: function() {
return {
name : this.model.getTitle(),
avatar_url : this.model.getAvatarUrl(),
conflict : this.conflict
name : this.model.getTitle(),
avatar : this.model.getAvatar(),
conflict : this.conflict
};
}
});

View file

@ -62,7 +62,7 @@
message: this.model.get('body'),
timestamp: moment(this.model.get('sent_at')).fromNow(),
sender: (contact && contact.getTitle()) || '',
avatar_url: (contact && contact.getAvatarUrl())
avatar: (contact && contact.getAvatar())
}, this.render_partials())
);

View file

@ -67,7 +67,7 @@
render_attributes: function() {
return {
name: this.model.getTitle(),
avatar_url: this.model.getAvatarUrl()
avatar: {url: this.model.getAvatar()}
};
},
send: function() {