Some initial work on removing jQuery

This commit is contained in:
JC Brand 2016-11-24 02:07:32 +01:00
parent 3494691286
commit 77f93a6471
3 changed files with 79 additions and 81 deletions

View File

@ -375,12 +375,12 @@
this.giveFeedback = function (subject, klass, message) { this.giveFeedback = function (subject, klass, message) {
$('.conn-feedback').each(function (idx, el) { $('.conn-feedback').each(function (idx, el) {
var $el = $(el); el.classList.add('conn-feedback');
$el.addClass('conn-feedback').text(subject); el.textContent = subject;
if (klass) { if (klass) {
$el.addClass(klass); el.classList.add(klass);
} else { } else {
$el.removeClass('error'); el.classList.remove('error');
} }
}); });
converse.emit('feedback', { converse.emit('feedback', {
@ -639,7 +639,7 @@
}) })
.c('enable', {xmlns: Strophe.NS.CARBONS}); .c('enable', {xmlns: Strophe.NS.CARBONS});
this.connection.addHandler(function (iq) { this.connection.addHandler(function (iq) {
if ($(iq).find('error').length > 0) { if (iq.querySelectorAll('error').length > 0) {
converse.log('ERROR: An error occured while trying to enable message carbons.'); converse.log('ERROR: An error occured while trying to enable message carbons.');
} else { } else {
this.session.save({carbons_enabled: true}); this.session.save({carbons_enabled: true});
@ -936,10 +936,13 @@
}, },
subscribeToSuggestedItems: function (msg) { subscribeToSuggestedItems: function (msg) {
$(msg).find('item').each(function () { _.each(msg.querySelectorAll('item'), function (item) {
if (this.getAttribute('action') === 'add') { if (item.getAttribute('action') === 'add') {
converse.roster.addAndSubscribe( converse.roster.addAndSubscribe(
this.getAttribute('jid'), null, converse.xmppstatus.get('fullname')); item.getAttribute('jid'),
null,
converse.xmppstatus.get('fullname')
);
} }
}); });
return true; return true;
@ -1090,10 +1093,9 @@
return true; return true;
} }
converse.connection.send($iq({type: 'result', id: id, from: converse.connection.jid})); converse.connection.send($iq({type: 'result', id: id, from: converse.connection.jid}));
$(iq).children('query').find('item').each(function (idx, item) {
this.updateContact(item);
}.bind(this));
var items = iq.querySelectorAll('query[xmlns="'+Strophe.NS.ROSTER+'"] item');
_.each(items, this.updateContact.bind(this));
converse.emit('rosterPush', iq); converse.emit('rosterPush', iq);
return true; return true;
}, },
@ -1112,9 +1114,8 @@
/* An IQ stanza containing the roster has been received from /* An IQ stanza containing the roster has been received from
* the XMPP server. * the XMPP server.
*/ */
$(iq).children('query').find('item').each(function (idx, item) { var items = iq.querySelectorAll('query[xmlns="'+Strophe.NS.ROSTER+'"] item');
this.updateContact(item); _.each(items, this.updateContact.bind(this));
}.bind(this));
converse.emit('roster', iq); converse.emit('roster', iq);
}, },
@ -1162,13 +1163,13 @@
* Note: this method gets completely overridden by converse-vcard.js * Note: this method gets completely overridden by converse-vcard.js
*/ */
var bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from')); var bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from'));
var nick = $(presence).children('nick[xmlns='+Strophe.NS.NICK+']').text(); var nick_el = presence.querySelector('nick[xmlns='+Strophe.NS.NICK+']');
var user_data = { var user_data = {
jid: bare_jid, jid: bare_jid,
subscription: 'none', subscription: 'none',
ask: null, ask: null,
requesting: true, requesting: true,
fullname: nick || bare_jid, fullname: nick_el && nick_el.textContent || bare_jid,
}; };
this.create(user_data); this.create(user_data);
converse.emit('contactRequest', user_data); converse.emit('contactRequest', user_data);
@ -1204,14 +1205,13 @@
}, },
presenceHandler: function (presence) { presenceHandler: function (presence) {
var $presence = $(presence), var presence_type = presence.getAttribute('type');
presence_type = presence.getAttribute('type');
if (presence_type === 'error') { return true; } if (presence_type === 'error') { return true; }
var jid = presence.getAttribute('from'), var jid = presence.getAttribute('from'),
bare_jid = Strophe.getBareJidFromJid(jid), bare_jid = Strophe.getBareJidFromJid(jid),
resource = Strophe.getResourceFromJid(jid), resource = Strophe.getResourceFromJid(jid),
chat_status = $presence.find('show').text() || 'online', chat_status = _.propertyOf(presence.querySelector('show'))('textContent') || 'online',
status_message = $presence.find('status'), status_message = _.propertyOf(presence.querySelector('status'))('textContent'),
contact = this.get(bare_jid); contact = this.get(bare_jid);
if (this.isSelf(bare_jid)) { if (this.isSelf(bare_jid)) {
@ -1223,18 +1223,16 @@
// synchronize_availability option set to update, // synchronize_availability option set to update,
// we'll update ours as well. // we'll update ours as well.
converse.xmppstatus.save({'status': chat_status}); converse.xmppstatus.save({'status': chat_status});
if (status_message.length) { if (status_message) {
converse.xmppstatus.save({ converse.xmppstatus.save({'status_message': status_message});
'status_message': status_message.text()
});
} }
} }
return; return;
} else if (($presence.find('x').attr('xmlns') || '').indexOf(Strophe.NS.MUC) === 0) { } else if (presence.querySelectorAll('x[xmlns="'+Strophe.NS.MUC+'"]').length) {
return; // Ignore MUC return; // Ignore MUC
} }
if (contact && (status_message.text() !== contact.get('status'))) { if (contact && (status_message !== contact.get('status'))) {
contact.save({'status': status_message.text()}); contact.save({'status': status_message});
} }
if (presence_type === 'subscribed' && contact) { if (presence_type === 'subscribed' && contact) {
contact.ackSubscribe(); contact.ackSubscribe();
@ -1324,35 +1322,35 @@
}); });
}, },
getMessageAttributes: function ($message, $delay, original_stanza) { getMessageAttributes: function (message, delay, original_stanza) {
$delay = $delay || $message.find('delay'); delay = delay || message.querySelector('delay');
var type = $message.attr('type'), var type = message.getAttribute('type'),
body, stamp, time, sender, from; body, stamp, time, sender, from;
if (type === 'error') { if (type === 'error') {
body = $message.find('error').children('text').text(); body = _.propertyOf(message.querySelector('error text'))('textContent');
} else { } else {
body = $message.children('body').text(); body = _.propertyOf(message.querySelector('body'))('textContent');
} }
var delayed = $delay.length > 0, var delayed = !_.isNull(delay),
fullname = this.get('fullname'), fullname = this.get('fullname'),
is_groupchat = type === 'groupchat', is_groupchat = type === 'groupchat',
chat_state = $message.find(converse.COMPOSING).length && converse.COMPOSING || chat_state = message.getElementsByTagName(converse.COMPOSING).length && converse.COMPOSING ||
$message.find(converse.PAUSED).length && converse.PAUSED || message.getElementsByTagName(converse.PAUSED).length && converse.PAUSED ||
$message.find(converse.INACTIVE).length && converse.INACTIVE || message.getElementsByTagName(converse.INACTIVE).length && converse.INACTIVE ||
$message.find(converse.ACTIVE).length && converse.ACTIVE || message.getElementsByTagName(converse.ACTIVE).length && converse.ACTIVE ||
$message.find(converse.GONE).length && converse.GONE; message.getElementsByTagName(converse.GONE).length && converse.GONE;
if (is_groupchat) { if (is_groupchat) {
from = Strophe.unescapeNode(Strophe.getResourceFromJid($message.attr('from'))); from = Strophe.unescapeNode(Strophe.getResourceFromJid(message.getAttribute('from')));
} else { } else {
from = Strophe.getBareJidFromJid($message.attr('from')); from = Strophe.getBareJidFromJid(message.getAttribute('from'));
} }
if (_.isEmpty(fullname)) { if (_.isEmpty(fullname)) {
fullname = from; fullname = from;
} }
if (delayed) { if (delayed) {
stamp = $delay.attr('stamp'); stamp = delay.getAttribute('stamp');
time = stamp; time = stamp;
} else { } else {
time = moment().format(); time = moment().format();
@ -1368,13 +1366,13 @@
'delayed': delayed, 'delayed': delayed,
'fullname': fullname, 'fullname': fullname,
'message': body || undefined, 'message': body || undefined,
'msgid': $message.attr('id'), 'msgid': message.getAttribute('id'),
'sender': sender, 'sender': sender,
'time': time 'time': time
}; };
}, },
createMessage: function () { createMessage: function (message, delay, original_stanza) {
return this.messages.create(this.getMessageAttributes.apply(this, arguments)); return this.messages.create(this.getMessageAttributes.apply(this, arguments));
} }
}); });
@ -1421,8 +1419,7 @@
/* Handler method for all incoming error message stanzas /* Handler method for all incoming error message stanzas
*/ */
// TODO: we can likely just reuse "onMessage" below // TODO: we can likely just reuse "onMessage" below
var $message = $(message), var from_jid = Strophe.getBareJidFromJid(message.getAttribute('from'));
from_jid = Strophe.getBareJidFromJid($message.attr('from'));
if (from_jid === converse.bare_jid) { if (from_jid === converse.bare_jid) {
return true; return true;
} }
@ -1431,7 +1428,7 @@
if (!chatbox) { if (!chatbox) {
return true; return true;
} }
chatbox.createMessage($message, null, message); chatbox.createMessage(message, null, message);
return true; return true;
}, },
@ -1439,12 +1436,12 @@
/* Handler method for all incoming single-user chat "message" /* Handler method for all incoming single-user chat "message"
* stanzas. * stanzas.
*/ */
var $message = $(message), var original_stanza = message,
contact_jid, $forwarded, $delay, from_bare_jid, contact_jid, forwarded, delay, from_bare_jid,
from_resource, is_me, msgid, from_resource, is_me, msgid,
chatbox, resource, chatbox, resource,
from_jid = $message.attr('from'), from_jid = message.getAttribute('from'),
to_jid = $message.attr('to'), to_jid = message.getAttribute('to'),
to_resource = Strophe.getResourceFromJid(to_jid); to_resource = Strophe.getResourceFromJid(to_jid);
if (converse.filter_by_resource && (to_resource && to_resource !== converse.resource)) { if (converse.filter_by_resource && (to_resource && to_resource !== converse.resource)) {
@ -1463,24 +1460,25 @@
); );
return true; return true;
} }
$forwarded = $message.find('forwarded'); forwarded = message.querySelector('forwarded');
if ($forwarded.length) { if (!_.isNull(forwarded)) {
var $forwarded_message = $forwarded.children('message'); var forwarded_message = forwarded.querySelector('message');
if (Strophe.getBareJidFromJid($forwarded_message.attr('from')) !== from_jid) { var forwarded_from = forwarded_message.getAttribute('from');
if (Strophe.getBareJidFromJid(forwarded_from) !== from_jid) {
// Prevent message forging via carbons // Prevent message forging via carbons
// //
// https://xmpp.org/extensions/xep-0280.html#security // https://xmpp.org/extensions/xep-0280.html#security
return true; return true;
} }
$message = $forwarded_message; message = forwarded_message;
$delay = $forwarded.children('delay'); delay = forwarded.querySelector('delay');
from_jid = $message.attr('from'); from_jid = message.getAttribute('from');
to_jid = $message.attr('to'); to_jid = message.getAttribute('to');
} }
from_bare_jid = Strophe.getBareJidFromJid(from_jid); from_bare_jid = Strophe.getBareJidFromJid(from_jid);
from_resource = Strophe.getResourceFromJid(from_jid); from_resource = Strophe.getResourceFromJid(from_jid);
is_me = from_bare_jid === converse.bare_jid; is_me = from_bare_jid === converse.bare_jid;
msgid = $message.attr('id'); msgid = message.getAttribute('id');
if (is_me) { if (is_me) {
// I am the sender, so this must be a forwarded message... // I am the sender, so this must be a forwarded message...
contact_jid = Strophe.getBareJidFromJid(to_jid); contact_jid = Strophe.getBareJidFromJid(to_jid);
@ -1491,14 +1489,14 @@
} }
converse.emit('message', message); converse.emit('message', message);
// Get chat box, but only create a new one when the message has a body. // Get chat box, but only create a new one when the message has a body.
chatbox = this.getChatBox(contact_jid, $message.find('body').length > 0); chatbox = this.getChatBox(contact_jid, !_.isNull(message.querySelector('body')));
if (!chatbox) { if (!chatbox) {
return true; return true;
} }
if (msgid && chatbox.messages.findWhere({msgid: msgid})) { if (msgid && chatbox.messages.findWhere({msgid: msgid})) {
return true; // We already have this message stored. return true; // We already have this message stored.
} }
chatbox.createMessage($message, $delay, message); chatbox.createMessage(message, delay, original_stanza);
return true; return true;
}, },
@ -1741,12 +1739,13 @@
}, },
onItems: function (stanza) { onItems: function (stanza) {
$(stanza).find('query item').each(function (idx, item) { var that = this;
_.each(stanza.querySelectorAll('query item'), function (item) {
converse.connection.disco.info( converse.connection.disco.info(
$(item).attr('jid'), item.getAttribute('jid'),
null, null,
this.onInfo.bind(this)); that.onInfo.bind(that));
}.bind(this)); });
}, },
onInfo: function (stanza) { onInfo: function (stanza) {
@ -1757,11 +1756,11 @@
return; return;
} }
$stanza.find('feature').each(function (idx, feature) { $stanza.find('feature').each(function (idx, feature) {
var namespace = $(feature).attr('var'); var namespace = feature.getAttribute('var');
this[namespace] = true; this[namespace] = true;
this.create({ this.create({
'var': namespace, 'var': namespace,
'from': $stanza.attr('from') 'from': stanza.getAttribute('from')
}); });
}.bind(this)); }.bind(this));
} }

View File

@ -1804,25 +1804,25 @@
this.scrollDown(); this.scrollDown();
}, },
onChatRoomMessage: function (msg) { onChatRoomMessage: function (message) {
/* Given a <message> stanza, create a message /* Given a <message> stanza, create a message
* Backbone.Model if appropriate. * Backbone.Model if appropriate.
* *
* Parameters: * Parameters:
* (XMLElement) msg: The received message stanza * (XMLElement) msg: The received message stanza
*/ */
var $message = $(msg), var original_stanza = message,
$forwarded = $message.find('forwarded'), forwarded = message.querySelector('forwarded'),
$delay; delay;
if ($forwarded.length) { if (!_.isNull(forwarded)) {
$message = $forwarded.children('message'); message = forwarded.querySelector('message');
$delay = $forwarded.children('delay'); delay = forwarded.querySelector('delay');
} }
var jid = msg.getAttribute('from'), var jid = message.getAttribute('from'),
msgid = msg.getAttribute('id'), msgid = message.getAttribute('id'),
resource = Strophe.getResourceFromJid(jid), resource = Strophe.getResourceFromJid(jid),
sender = resource && Strophe.unescapeNode(resource) || '', sender = resource && Strophe.unescapeNode(resource) || '',
subject = $message.children('subject').text(), subject = _.propertyOf(message.querySelector('subject'))('textContent'),
dupes = msgid && this.model.messages.filter(function (msg) { dupes = msgid && this.model.messages.filter(function (msg) {
// Find duplicates. // Find duplicates.
// Some bots (like HAL in the prosody chatroom) // Some bots (like HAL in the prosody chatroom)
@ -1839,7 +1839,7 @@
if (sender === '') { if (sender === '') {
return true; return true;
} }
this.model.createMessage($message, $delay, msg); this.model.createMessage(message, delay, original_stanza);
if (sender !== this.model.get('nick')) { if (sender !== this.model.get('nick')) {
// We only emit an event if it's not our own message // We only emit an event if it's not our own message
converse.emit('message', msg); converse.emit('message', msg);

View File

@ -107,10 +107,9 @@
!(utils.isOTRMessage($message[0]) && !_.includes([UNVERIFIED, VERIFIED], this.get('otr_status'))); !(utils.isOTRMessage($message[0]) && !_.includes([UNVERIFIED, VERIFIED], this.get('otr_status')));
}, },
createMessage: function ($message, $delay, original_stanza) { createMessage: function (message, delay, original_stanza) {
var converse = this.__super__.converse, var converse = this.__super__.converse,
$body = $message.children('body'), text = _.propertyOf(message.querySelector('body'))('textContent');
text = ($body.length > 0 ? $body.text() : undefined);
if ((!text) || (!converse.allow_otr)) { if ((!text) || (!converse.allow_otr)) {
return this.__super__.createMessage.apply(this, arguments); return this.__super__.createMessage.apply(this, arguments);