Use native API instead of jquery

This commit is contained in:
JC Brand 2017-02-24 15:10:22 +00:00
parent 5c445a0268
commit 453d98db2c
2 changed files with 39 additions and 40 deletions

View File

@ -10,8 +10,7 @@
define(["converse-core"], factory); define(["converse-core"], factory);
}(this, function (converse) { }(this, function (converse) {
"use strict"; "use strict";
var $ = converse.env.jQuery, var utils = converse.env.utils,
utils = converse.env.utils,
Strophe = converse.env.Strophe, Strophe = converse.env.Strophe,
_ = converse.env._; _ = converse.env._;
@ -40,37 +39,38 @@
notification_icon: '/logo/conversejs128.png' notification_icon: '/logo/conversejs128.png'
}); });
_converse.isOnlyChatStateNotification = function ($msg) { _converse.isOnlyChatStateNotification = function (msg) {
// See XEP-0085 Chat State Notification // See XEP-0085 Chat State Notification
return ( return (
$msg.find('body').length === 0 && ( _.isNull(msg.querySelector('body')) && (
$msg.find(_converse.ACTIVE).length !== 0 || _.isNull(msg.querySelector(_converse.ACTIVE)) ||
$msg.find(_converse.COMPOSING).length !== 0 || _.isNull(msg.querySelector(_converse.COMPOSING)) ||
$msg.find(_converse.INACTIVE).length !== 0 || _.isNull(msg.querySelector(_converse.INACTIVE)) ||
$msg.find(_converse.PAUSED).length !== 0 || _.isNull(msg.querySelector(_converse.PAUSED)) ||
$msg.find(_converse.GONE).length !== 0 _.isNull(msg.querySelector(_converse.GONE))
) )
); );
}; };
_converse.shouldNotifyOfGroupMessage = function ($message) { _converse.shouldNotifyOfGroupMessage = function (message) {
/* Is this a group message worthy of notification? /* Is this a group message worthy of notification?
*/ */
var notify_all = _converse.notify_all_room_messages, var notify_all = _converse.notify_all_room_messages,
jid = $message.attr('from'), jid = message.getAttribute('from'),
resource = Strophe.getResourceFromJid(jid), resource = Strophe.getResourceFromJid(jid),
room_jid = Strophe.getBareJidFromJid(jid), room_jid = Strophe.getBareJidFromJid(jid),
sender = resource && Strophe.unescapeNode(resource) || ''; sender = resource && Strophe.unescapeNode(resource) || '';
if (sender === '' || $message.find('delay').length > 0) { if (sender === '' || message.querySelectorAll('delay').length > 0) {
return false; return false;
} }
var room = _converse.chatboxes.get(room_jid); var room = _converse.chatboxes.get(room_jid);
var $body = $message.children('body'); var body = message.querySelector('body');
if (!$body.length) { if (_.isNull(body)) {
return false; return false;
} }
var mentioned = (new RegExp("\\b"+room.get('nick')+"\\b")).test($body.text()); var mentioned = (new RegExp("\\b"+room.get('nick')+"\\b")).test(body.textContent);
notify_all = notify_all === true || (_.isArray(notify_all) && _.includes(notify_all, room_jid)); notify_all = notify_all === true ||
(_.isArray(notify_all) && _.includes(notify_all, room_jid));
if (sender === room.get('nick') || (!notify_all && !mentioned)) { if (sender === room.get('nick') || (!notify_all && !mentioned)) {
return false; return false;
} }
@ -83,21 +83,21 @@
if (utils.isOTRMessage(message)) { if (utils.isOTRMessage(message)) {
return false; return false;
} }
var $message = $(message), var forwarded = message.querySelector('forwarded');
$forwarded = $message.find('forwarded'); if (!_.isNull(forwarded)) {
if ($forwarded.length) {
return false; return false;
} else if ($message.attr('type') === 'groupchat') { } else if (message.getAttribute('type') === 'groupchat') {
return _converse.shouldNotifyOfGroupMessage($message); return _converse.shouldNotifyOfGroupMessage(message);
} else if (utils.isHeadlineMessage(message)) { } else if (utils.isHeadlineMessage(message)) {
// We want to show notifications for headline messages. // We want to show notifications for headline messages.
return true; return true;
} }
var is_me = Strophe.getBareJidFromJid($message.attr('from')) === _converse.bare_jid; var is_me = Strophe.getBareJidFromJid(
return !_converse.isOnlyChatStateNotification($message) && !is_me; message.getAttribute('from')) === _converse.bare_jid;
return !_converse.isOnlyChatStateNotification(message) && !is_me;
}; };
_converse.playSoundNotification = function ($message) { _converse.playSoundNotification = function () {
/* Plays a sound to notify that a new message was recieved. /* Plays a sound to notify that a new message was recieved.
*/ */
// XXX Eventually this can be refactored to use Notification's sound // XXX Eventually this can be refactored to use Notification's sound
@ -126,31 +126,31 @@
} }
}; };
_converse.showMessageNotification = function ($message) { _converse.showMessageNotification = function (message) {
/* Shows an HTML5 Notification to indicate that a new chat /* Shows an HTML5 Notification to indicate that a new chat
* message was received. * message was received.
*/ */
var n, title, contact_jid, roster_item, var n, title, contact_jid, roster_item,
from_jid = $message.attr('from'); from_jid = message.getAttribute('from');
if ($message.attr('type') === 'headline' || !_.includes(from_jid, '@')) { if (message.getAttribute('type') === 'headline' || !_.includes(from_jid, '@')) {
// XXX: 2nd check is workaround for Prosody which doesn't // XXX: 2nd check is workaround for Prosody which doesn't
// give type "headline" // give type "headline"
title = __(___("Notification from %1$s"), from_jid); title = __(___("Notification from %1$s"), from_jid);
} else { } else {
if ($message.attr('type') === 'groupchat') { if (message.getAttribute('type') === 'groupchat') {
title = __(___("%1$s says"), Strophe.getResourceFromJid(from_jid)); title = __(___("%1$s says"), Strophe.getResourceFromJid(from_jid));
} else { } else {
if (_.isUndefined(_converse.roster)) { if (_.isUndefined(_converse.roster)) {
_converse.log("Could not send notification, because roster is undefined", "error"); _converse.log("Could not send notification, because roster is undefined", "error");
return; return;
} }
contact_jid = Strophe.getBareJidFromJid($message.attr('from')); contact_jid = Strophe.getBareJidFromJid(message.getAttribute('from'));
roster_item = _converse.roster.get(contact_jid); roster_item = _converse.roster.get(contact_jid);
title = __(___("%1$s says"), roster_item.get('fullname')); title = __(___("%1$s says"), roster_item.get('fullname'));
} }
} }
n = new Notification(title, { n = new Notification(title, {
body: $message.children('body').text(), body: message.querySelector('body').textContent,
lang: _converse.i18n.locale_data.converse[""].lang, lang: _converse.i18n.locale_data.converse[""].lang,
icon: _converse.notification_icon icon: _converse.notification_icon
}); });
@ -212,7 +212,8 @@
* Will show an HTML5 notification to indicate that the chat * Will show an HTML5 notification to indicate that the chat
* status has changed. * status has changed.
*/ */
if (_converse.areDesktopNotificationsEnabled() && _converse.show_chatstate_notifications) { if (_converse.areDesktopNotificationsEnabled() &&
_converse.show_chatstate_notifications) {
_converse.showChatStateNotification(contact); _converse.showChatStateNotification(contact);
} }
}; };
@ -221,13 +222,12 @@
/* Event handler for the on('message') event. Will call methods /* Event handler for the on('message') event. Will call methods
* to play sounds and show HTML5 notifications. * to play sounds and show HTML5 notifications.
*/ */
var $message = $(message);
if (!_converse.shouldNotifyOfMessage(message)) { if (!_converse.shouldNotifyOfMessage(message)) {
return false; return false;
} }
_converse.playSoundNotification($message); _converse.playSoundNotification();
if (_converse.areDesktopNotificationsEnabled()) { if (_converse.areDesktopNotificationsEnabled()) {
_converse.showMessageNotification($message); _converse.showMessageNotification(message);
} }
}; };

View File

@ -239,20 +239,19 @@
}, },
isOTRMessage: function (message) { isOTRMessage: function (message) {
var $body = $(message).children('body'), var body = message.querySelector('body'),
text = ($body.length > 0 ? $body.text() : undefined); text = (!_.isNull(body) ? body.textNode : undefined);
return text && !!text.match(/^\?OTR/); return text && !!text.match(/^\?OTR/);
}, },
isHeadlineMessage: function (message) { isHeadlineMessage: function (message) {
var $message = $(message), var from_jid = message.getAttribute('from');
from_jid = $message.attr('from'); if (message.getAttribute('type') === 'headline' ||
if ($message.attr('type') === 'headline' ||
// Some servers (I'm looking at you Prosody) don't set the message // Some servers (I'm looking at you Prosody) don't set the message
// type to "headline" when sending server messages. For now we // type to "headline" when sending server messages. For now we
// check if an @ signal is included, and if not, we assume it's // check if an @ signal is included, and if not, we assume it's
// a headline message. // a headline message.
( $message.attr('type') !== 'error' && ( message.getAttribute('type') !== 'error' &&
!_.isUndefined(from_jid) && !_.isUndefined(from_jid) &&
!_.includes(from_jid, '@') !_.includes(from_jid, '@')
)) { )) {