2014-05-17 06:48:46 +02:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var destroyer = Backbone.View.extend({
|
|
|
|
tagName: 'button',
|
|
|
|
className: 'btn btn-square btn-sm destroy',
|
2014-06-08 05:32:17 +02:00
|
|
|
events: {
|
|
|
|
'click': 'destroy'
|
|
|
|
},
|
2014-05-17 06:48:46 +02:00
|
|
|
initialize: function() {
|
|
|
|
this.$el.html('×');
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
2014-07-22 16:14:33 +02:00
|
|
|
this.model.messages().each(function(message) { message.destroy(); });
|
2014-06-08 03:00:51 +02:00
|
|
|
this.model.set('active', false);
|
|
|
|
this.model.save();
|
|
|
|
this.model.trigger('destroy');
|
2014-05-17 06:48:46 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var menu = Backbone.View.extend({
|
|
|
|
tagName: 'ul',
|
|
|
|
className: 'menu',
|
|
|
|
initialize: function() {
|
|
|
|
this.$el.html("<li>delete</li>");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.ConversationView = Backbone.View.extend({
|
|
|
|
tagName: 'li',
|
|
|
|
className: 'conversation',
|
|
|
|
|
2014-06-08 05:32:17 +02:00
|
|
|
events: {
|
|
|
|
'click': 'toggle',
|
|
|
|
'submit form': 'sendMessage'
|
|
|
|
},
|
2014-05-17 06:48:46 +02:00
|
|
|
initialize: function() {
|
|
|
|
this.listenTo(this.model, 'change', this.render); // auto update
|
|
|
|
this.listenTo(this.model, 'message', this.addMessage); // auto update
|
|
|
|
this.listenTo(this.model, 'destroy', this.remove); // auto update
|
2014-06-04 04:37:10 +02:00
|
|
|
this.listenTo(Whisper.Messages, 'reset', this.addAllMessages); // auto update
|
2014-05-17 06:48:46 +02:00
|
|
|
|
|
|
|
this.$el.addClass('closed');
|
|
|
|
this.$destroy = (new destroyer({model: this.model})).$el;
|
|
|
|
|
|
|
|
this.$image = $('<div class="image">');
|
|
|
|
this.$name = $('<span class="name">');
|
|
|
|
this.$header = $('<div class="header">').append(this.$image, this.$name);
|
|
|
|
|
|
|
|
this.$button = $('<button class="btn">').append($('<span>').text('Send'));
|
|
|
|
this.$input = $('<input type="text">').attr('autocomplete','off');
|
|
|
|
this.$form = $("<form class=''>").append(this.$input);
|
|
|
|
|
|
|
|
this.$messages = $('<ul class="messages">');
|
|
|
|
this.$collapsable = $('<div class="collapsable">').hide();
|
|
|
|
this.$collapsable.append(this.$messages, this.$form);
|
|
|
|
|
|
|
|
this.$el.append(this.$destroy, this.$header, this.$collapsable);
|
2014-06-08 05:32:17 +02:00
|
|
|
},
|
2014-05-17 06:48:46 +02:00
|
|
|
|
2014-06-08 05:32:17 +02:00
|
|
|
sendMessage: function(e) {
|
|
|
|
if (!this.$input.val().length) { return false; }
|
|
|
|
this.model.sendMessage(this.$input.val());
|
|
|
|
this.$input.val("");
|
|
|
|
e.preventDefault();
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
2014-06-08 05:32:17 +02:00
|
|
|
remove: function() {
|
|
|
|
this.$el.remove();
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
if (!this.$el.hasClass('closed')) {
|
|
|
|
this.$el.addClass('closed').find('.collapsable').slideUp(600);
|
|
|
|
}
|
|
|
|
},
|
2014-05-17 06:48:46 +02:00
|
|
|
|
|
|
|
open: function(e) {
|
|
|
|
if (this.$el.hasClass('closed')) {
|
|
|
|
this.$el.removeClass('closed');
|
2014-07-22 12:34:02 +02:00
|
|
|
this.$el.find('.collapsable').slideDown(600);
|
2014-05-17 06:48:46 +02:00
|
|
|
}
|
2014-07-22 12:34:02 +02:00
|
|
|
this.$el.find('input').focus();
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
|
|
|
|
2014-06-08 05:32:17 +02:00
|
|
|
toggle: function() {
|
|
|
|
if (this.$el.hasClass('closed')) {
|
|
|
|
this.open();
|
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-17 06:48:46 +02:00
|
|
|
addMessage: function (message) {
|
|
|
|
var view = new Whisper.MessageView({ model: message });
|
|
|
|
this.$messages.append(view.render().el);
|
|
|
|
},
|
|
|
|
|
|
|
|
addAllMessages: function () {
|
2014-07-22 16:14:33 +02:00
|
|
|
this.model.messages().each(this.addMessage, this);
|
2014-05-17 06:48:46 +02:00
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
this.$name.text(this.model.get('name'));
|
|
|
|
this.$image.css('background-image: ' + this.model.get('image') + ';');
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|