This commit is contained in:
JC Brand 2013-03-24 12:53:29 +02:00
parent b495f83745
commit efad677f16

View File

@ -128,9 +128,10 @@
*/ */
    var numericKeys = [1, 4, 5, 6, 7, 10, 11],     var numericKeys = [1, 4, 5, 6, 7, 10, 11],
struct = /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}\.?\d*)Z\s*$/.exec(datestr), struct = /^\s*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}\.?\d*)Z\s*$/.exec(datestr),
minutesOffset = 0; minutesOffset = 0,
i;
for (var i = 0, k; (k = numericKeys[i]); ++i) { for (i = 0, k; (k = numericKeys[i]); ++i) {
struct[k] = +struct[k] || 0; struct[k] = +struct[k] || 0;
} }
// allow undefined days and months // allow undefined days and months
@ -260,17 +261,15 @@
} else { } else {
sender = 'them'; sender = 'them';
} }
var message = new xmppchat.Message({ this.messages.create({
fullname: fullname, fullname: fullname,
sender: sender, sender: sender,
delayed: delayed, delayed: delayed,
time: time, time: time,
message: body message: body
}); });
this.messages.add(message);
message.save();
} }
}, }
}); });
xmppchat.ChatBoxView = Backbone.View.extend({ xmppchat.ChatBoxView = Backbone.View.extend({
@ -382,7 +381,7 @@
if (match) { if (match) {
if (match[1] === "clear") { if (match[1] === "clear") {
this.$el.find('.chat-content').empty(); this.$el.find('.chat-content').empty();
this.model.messages.reset() this.model.messages.reset();
return; return;
} }
else if (match[1] === "help") { else if (match[1] === "help") {
@ -406,16 +405,13 @@
.cnode(message.tree()); .cnode(message.tree());
xmppchat.connection.send(message); xmppchat.connection.send(message);
xmppchat.connection.send(forwarded); xmppchat.connection.send(forwarded);
// Add the new message // Add the new message
var message = new xmppchat.Message({ this.model.messages.create({
fullname: 'me', fullname: 'me',
sender: 'me', sender: 'me',
time: (new Date()).toLocaleTimeString().substring(0,5), time: (new Date()).toLocaleTimeString().substring(0,5),
message: text message: text
}); });
this.model.messages.add(message);
message.save();
}, },
keyPressed: function (ev) { keyPressed: function (ev) {
@ -521,7 +517,7 @@
var img = new Image(); // Create new Image object var img = new Image(); // Create new Image object
img.onload = function(){ img.onload = function(){
var ratio = img.width/img.height; var ratio = img.width/img.height;
ctx.drawImage(img,0,0, 35*ratio, 35) ctx.drawImage(img,0,0, 35*ratio, 35);
} }
img.src = img_src; img.src = img_src;
return this; return this;
@ -540,7 +536,7 @@
this.$el.css({'opacity': 0, this.$el.css({'opacity': 0,
'display': 'inline'}) 'display': 'inline'})
.animate({opacity: '1'}, 200); .animate({opacity: '1'}, 200);
this.model.set({'visible': true}) this.model.set({'visible': true});
if (xmppchat.connection) { if (xmppchat.connection) {
// Without a connection, we haven't yet initialized // Without a connection, we haven't yet initialized
// localstorage // localstorage
@ -731,7 +727,7 @@
this.render().setUpRoster(); this.render().setUpRoster();
} }
if (_.has(item.changed, 'visible')) { if (_.has(item.changed, 'visible')) {
if (item.changed['visible'] === true) { if (item.changed.visible === true) {
this.show(); this.show();
} }
} }
@ -980,7 +976,7 @@
'time': (new Date()).toLocaleTimeString().substring(0,5), 'time': (new Date()).toLocaleTimeString().substring(0,5),
'message': body, 'message': body,
'username': sender, 'username': sender,
'extra_classes': ($message.find('delay').length > 0) && 'delayed' || '', 'extra_classes': ($message.find('delay').length > 0) && 'delayed' || ''
})); }));
} else { } else {
$chat_content.append( $chat_content.append(
@ -1044,16 +1040,16 @@
}); });
} }
// This will make sure the Roster is set up // This will make sure the Roster is set up
this.get('controlbox').set({connected:true}) this.get('controlbox').set({connected:true});
// Get cached chatboxes from localstorage // Get cached chatboxes from localstorage
this.fetch({ this.fetch({
add: true, success: add: true, success:
$.proxy(function (collection, resp) { $.proxy(function (collection, resp) {
if (_.include(_.pluck(resp, 'id'), 'controlbox')) { if (_.include(_.pluck(resp, 'id'), 'controlbox')) {
// If the controlbox was saved in localstorage, it must be visible // If the controlbox was saved in localstorage, it must be visible
this.get('controlbox').set({visible:true}) this.get('controlbox').set({visible:true});
} }
}, this) }, this)
}); });
}, },
@ -1092,7 +1088,7 @@
'fullname': fullname, 'fullname': fullname,
'image_type': image_type, 'image_type': image_type,
'image': image, 'image': image,
'url': url, 'url': url
}); });
chatbox.messageReceived(message); chatbox.messageReceived(message);
xmppchat.roster.addResource(partner_jid, resource); xmppchat.roster.addResource(partner_jid, resource);
@ -1106,7 +1102,7 @@
chatbox.messageReceived(message); chatbox.messageReceived(message);
xmppchat.roster.addResource(partner_jid, resource); xmppchat.roster.addResource(partner_jid, resource);
return true; return true;
}, }
}); });
xmppchat.ChatBoxesView = Backbone.View.extend({ xmppchat.ChatBoxesView = Backbone.View.extend({
@ -1151,13 +1147,12 @@
'resources': [], 'resources': [],
'chat_status': 'offline', 'chat_status': 'offline',
'status': 'offline', 'status': 'offline',
'sorted': false, 'sorted': false
}); });
this.set(attributes); this.set(attributes);
}, }
}); });
xmppchat.RosterItemView = Backbone.View.extend({ xmppchat.RosterItemView = Backbone.View.extend({
tagName: 'dd', tagName: 'dd',
@ -1181,7 +1176,7 @@
'fullname': this.model.get('fullname'), 'fullname': this.model.get('fullname'),
'image_type': this.model.get('image_type'), 'image_type': this.model.get('image_type'),
'image': this.model.get('image'), 'image': this.model.get('image'),
'url': this.model.get('url'), 'url': this.model.get('url')
}); });
} }
}, },
@ -1271,7 +1266,7 @@
model.get('image'), model.get('image'),
model.get('image_type'), model.get('image_type'),
model.get('url') model.get('url')
) );
} else { } else {
xmppchat.connection.vcard.get($.proxy(function (iq) { xmppchat.connection.vcard.get($.proxy(function (iq) {
$vcard = $(iq).find('vCard'); $vcard = $(iq).find('vCard');
@ -1280,7 +1275,7 @@
img_type = $vcard.find('TYPE').text(), img_type = $vcard.find('TYPE').text(),
url = $vcard.find('URL').text(); url = $vcard.find('URL').text();
callback(jid, fullname, img, img_type, url); callback(jid, fullname, img, img_type, url);
}, this), jid, errback()) }, this), jid, errback());
} }
} }
@ -1336,7 +1331,7 @@
}, },
addRosterItem: function (attributes) { addRosterItem: function (attributes) {
var model = new xmppchat.RosterItem(attributes) var model = new xmppchat.RosterItem(attributes);
this.add(model); this.add(model);
model.save(); model.save();
}, },
@ -1390,7 +1385,7 @@
subscribeBack: function (jid) { subscribeBack: function (jid) {
// XXX: Why the distinction between jid and bare_jid? // XXX: Why the distinction between jid and bare_jid?
var bare_jid = Strophe.getBareJidFromJid(jid) var bare_jid = Strophe.getBareJidFromJid(jid);
if (xmppchat.connection.roster.findItem(bare_jid)) { if (xmppchat.connection.roster.findItem(bare_jid)) {
xmppchat.connection.roster.authorize(bare_jid); xmppchat.connection.roster.authorize(bare_jid);
xmppchat.connection.roster.subscribe(jid); xmppchat.connection.roster.subscribe(jid);
@ -1420,8 +1415,9 @@
getNumOnlineContacts: function () { getNumOnlineContacts: function () {
var count = 0, var count = 0,
models = this.models, models = this.models,
models_length = models.length; models_length = models.length,
for (var i=0; i<models_length; i++) { i;
for (i=0; i<models_length; i++) {
if (_.indexOf(['offline', 'unavailable'], models[i].get('chat_status')) === -1) { if (_.indexOf(['offline', 'unavailable'], models[i].get('chat_status')) === -1) {
count++; count++;
} }
@ -1434,13 +1430,13 @@
* some contacts that aren't actually in our roster anymore. We * some contacts that aren't actually in our roster anymore. We
* therefore need to remove them now. * therefore need to remove them now.
*/ */
var id, var id, i,
roster_ids = []; roster_ids = [];
for (var i=0; i < items.length; ++i) { for (i=0; i < items.length; ++i) {
roster_ids.push(items[i]['jid']); roster_ids.push(items[i]['jid']);
} }
for (var i=0; i < this.models.length; ++i) { for (i=0; i < this.models.length; ++i) {
id = this.models[i].get('id') id = this.models[i].get('id');
if (_.indexOf(roster_ids, id) === -1) { if (_.indexOf(roster_ids, id) === -1) {
this.getItem(id).destroy(); this.getItem(id).destroy();
} }
@ -1470,7 +1466,7 @@
image: img, image: img,
image_type: img_type, image_type: img_type,
url: url, url: url,
is_last: is_last, is_last: is_last
}); });
}, this), }, this),
$.proxy(function () { $.proxy(function () {
@ -1481,7 +1477,7 @@
jid: item.jid, jid: item.jid,
subscription: item.subscription, subscription: item.subscription,
ask: item.ask, ask: item.ask,
is_last: is_last, is_last: is_last
}); });
}, this)); }, this));
@ -1554,7 +1550,7 @@
image: img, image: img,
image_type: img_type, image_type: img_type,
url: url, url: url,
is_last: true, is_last: true
}); });
}, this), }, this),
$.proxy(function (jid, fullname, img, img_type, url) { $.proxy(function (jid, fullname, img, img_type, url) {
@ -1564,7 +1560,7 @@
subscription: 'none', subscription: 'none',
ask: 'request', ask: 'request',
fullname: jid, fullname: jid,
is_last: true, is_last: true
}); });
}, this)); }, this));
} }
@ -1680,7 +1676,7 @@
if (item.get('is_last') && !item.get('sorted')) { if (item.get('is_last') && !item.get('sorted')) {
// this will be true after all of the roster items have been added with the default // this will be true after all of the roster items have been added with the default
// options where all of the items are offline and now we can show the rosterView // options where all of the items are offline and now we can show the rosterView
item.set('sorted', true) item.set('sorted', true);
this.initialSort(); this.initialSort();
this.$el.show(); this.$el.show();
} }
@ -1833,7 +1829,7 @@
} else { } else {
pretty_status = stat || 'online'; pretty_status = stat || 'online';
} }
return pretty_status return pretty_status;
}, },
updateStatusUI: function (ev) { updateStatusUI: function (ev) {
@ -1898,7 +1894,7 @@
tagName: 'div', tagName: 'div',
id: "login-dialog", id: "login-dialog",
events: { events: {
'submit form#xmppchat-login': 'authenticate', 'submit form#xmppchat-login': 'authenticate'
}, },
tab_template: _.template( tab_template: _.template(
'<li><a class="current" href="#login">Sign in</a></li>'), '<li><a class="current" href="#login">Sign in</a></li>'),
@ -1914,7 +1910,7 @@
'</form">'), '</form">'),
authenticate: function (ev) { authenticate: function (ev) {
ev.preventDefault() ev.preventDefault();
var $form = $(ev.target), var $form = $(ev.target),
bosh_service_url = $form.find('input#bosh_service_url').val(), bosh_service_url = $form.find('input#bosh_service_url').val(),
jid = $form.find('input#jid').val(), jid = $form.find('input#jid').val(),
@ -1955,7 +1951,7 @@
this.$parent.find('#controlbox-tabs').append(this.tab_template()); this.$parent.find('#controlbox-tabs').append(this.tab_template());
this.$parent.find('#controlbox-panes').append(this.$el.html(this.template())); this.$parent.find('#controlbox-panes').append(this.$el.html(this.template()));
return this; return this;
}, }
}); });
// Event handlers // Event handlers
@ -1969,24 +1965,24 @@
this.auto_subscribe = chatdata.attr('auto_subscribe') === "True" || false; this.auto_subscribe = chatdata.attr('auto_subscribe') === "True" || false;
this.chatboxes = new this.ChatBoxes(); this.chatboxes = new this.ChatBoxes();
this.chatboxesview = new this.ChatBoxesView({ this.chatboxesview = new this.ChatBoxesView({model: this.chatboxes});
model: this.chatboxes
});
$toggle.bind('click', $.proxy(function (e) { $toggle.bind('click', $.proxy(function (e) {
e.preventDefault(); e.preventDefault();
if ($("div#controlbox").is(':visible')) { if ($("div#controlbox").is(':visible')) {
this.chatboxes.get('controlbox').destroy(); this.chatboxes.get('controlbox').destroy();
} else { } else {
var controlbox = this.chatboxes.get('controlbox') var controlbox = this.chatboxes.get('controlbox');
if (controlbox) { if (controlbox) {
controlbox.trigger('show'); controlbox.trigger('show');
} else { } else {
this.create({ this.chatboxes.create({
id: 'controlbox', id: 'controlbox',
box_id: 'controlbox', box_id: 'controlbox',
visible: true visible: true
}); });
} }
}
}, this)); }, this));
$(document).bind('jarnxmpp.connecting', $.proxy(function (ev, conn) { $(document).bind('jarnxmpp.connecting', $.proxy(function (ev, conn) {