Cable-Desktop/js/views/new_conversation_view.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2014-09-01 02:58:36 +02:00
var Whisper = Whisper || {};
(function () {
'use strict';
var MessageRecipientInputView = Backbone.View.extend({
events: {
'change': 'verifyNumber',
'focus' : 'removeError'
},
removeError: function() {
this.$el.removeClass('error');
},
verifyNumber: function() {
2014-09-01 02:58:36 +02:00
try {
var val = this.$el.val();
if (val[0] === '+') {
// assume that the country code is specified
var number = libphonenumber.util.verifyNumber(val);
} else {
// assume that the country code should match our own
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
var myRegionCode = libphonenumber.util.getRegionCodeForNumber(me);
var number = libphonenumber.util.verifyNumber(val, myRegionCode);
2014-09-01 02:58:36 +02:00
}
this.removeError();
2014-10-23 01:07:16 +02:00
return number;
} catch(ex) {
this.$el.addClass('error');
console.log(ex);
}
2014-09-01 02:58:36 +02:00
}
});
Whisper.NewConversationView = Backbone.View.extend({
className: 'conversation',
initialize: function() {
this.template = $('#new-message-form').html();
Mustache.parse(this.template);
this.render();
2014-10-23 01:07:16 +02:00
this.input = new MessageRecipientInputView({el: this.$el.find('input.number')});
2014-09-01 02:58:36 +02:00
},
events: {
'submit .send': 'send',
'close': 'remove'
},
send: function(e) {
e.preventDefault();
var number = this.input.verifyNumber();
if (number) {
var thread = Whisper.Threads.findOrCreateForRecipient(number);
var message_input = this.$el.find('input.send-message');
thread.sendMessage(message_input.val());
this.remove();
thread.trigger('render');
}
2014-09-01 02:58:36 +02:00
},
render: function() {
this.$el.html(Mustache.render(this.template));
Whisper.Layout.setContent(this.$el.show());
return this;
}
});
})();