chatview: Replace lodash methods with built-in ones

This commit is contained in:
JC Brand 2019-10-08 21:29:43 +02:00
parent f9b09052eb
commit 8b1c17c12b

View File

@ -11,7 +11,7 @@ import "backbone.nativeview";
import "converse-chatboxviews"; import "converse-chatboxviews";
import "converse-message-view"; import "converse-message-view";
import "converse-modal"; import "converse-modal";
import BrowserStorage from "backbone.browserStorage"; import { debounce, get, isString } from "lodash";
import { Overview } from "backbone.overview"; import { Overview } from "backbone.overview";
import converse from "@converse/headless/converse-core"; import converse from "@converse/headless/converse-core";
import tpl_chatbox from "templates/chatbox.html"; import tpl_chatbox from "templates/chatbox.html";
@ -29,7 +29,8 @@ import tpl_toolbar_fileupload from "templates/toolbar_fileupload.html";
import tpl_user_details_modal from "templates/user_details_modal.html"; import tpl_user_details_modal from "templates/user_details_modal.html";
import xss from "xss/dist/xss"; import xss from "xss/dist/xss";
const { $msg, Backbone, Strophe, _, sizzle, dayjs } = converse.env;
const { Backbone, Strophe, sizzle, dayjs } = converse.env;
const u = converse.env.utils; const u = converse.env.utils;
@ -87,7 +88,7 @@ converse.plugins.add('converse-chatview', {
initialize () { initialize () {
this.listenTo(this.model, 'change:status', this.onStatusMessageChanged); this.listenTo(this.model, 'change:status', this.onStatusMessageChanged);
this.debouncedRender = _.debounce(this.render, 50); this.debouncedRender = debounce(this.render, 50);
if (this.model.vcard) { if (this.model.vcard) {
this.listenTo(this.model.vcard, 'change', this.debouncedRender); this.listenTo(this.model.vcard, 'change', this.debouncedRender);
} }
@ -100,7 +101,7 @@ converse.plugins.add('converse-chatview', {
}, },
render () { render () {
const vcard = _.get(this.model, 'vcard'), const vcard = get(this.model, 'vcard'),
vcard_json = vcard ? vcard.toJSON() : {}; vcard_json = vcard ? vcard.toJSON() : {};
this.el.innerHTML = tpl_chatbox_head( this.el.innerHTML = tpl_chatbox_head(
Object.assign( Object.assign(
@ -157,12 +158,11 @@ converse.plugins.add('converse-chatview', {
}, },
toHTML () { toHTML () {
const vcard = _.get(this.model, 'vcard'), const vcard = get(this.model, 'vcard'),
vcard_json = vcard ? vcard.toJSON() : {}; vcard_json = vcard ? vcard.toJSON() : {};
return tpl_user_details_modal(Object.assign( return tpl_user_details_modal(Object.assign(
this.model.toJSON(), this.model.toJSON(),
vcard_json, { vcard_json, {
'_': _,
'__': __, '__': __,
'view': this, 'view': this,
'_converse': _converse, '_converse': _converse,
@ -204,9 +204,7 @@ converse.plugins.add('converse-chatview', {
if (result === true) { if (result === true) {
this.modal.hide(); this.modal.hide();
this.model.contact.removeFromRoster( this.model.contact.removeFromRoster(
(iq) => { () => this.model.contact.destroy(),
this.model.contact.destroy();
},
(err) => { (err) => {
_converse.log(err, Strophe.LogLevel.ERROR); _converse.log(err, Strophe.LogLevel.ERROR);
_converse.api.alert.show( _converse.api.alert.show(
@ -281,8 +279,8 @@ converse.plugins.add('converse-chatview', {
}, },
initDebounced () { initDebounced () {
this.scrollDown = _.debounce(this._scrollDown, 100); this.scrollDown = debounce(this._scrollDown, 100);
this.markScrolled = _.debounce(this._markScrolled, 100); this.markScrolled = debounce(this._markScrolled, 100);
}, },
render () { render () {
@ -302,7 +300,7 @@ converse.plugins.add('converse-chatview', {
if (!_converse.show_toolbar) { if (!_converse.show_toolbar) {
return this; return this;
} }
const options = _.assign( const options = Object.assign(
this.model.toJSON(), this.model.toJSON(),
this.getToolbarOptions() this.getToolbarOptions()
); );
@ -324,11 +322,11 @@ converse.plugins.add('converse-chatview', {
form_container.innerHTML = tpl_chatbox_message_form( form_container.innerHTML = tpl_chatbox_message_form(
Object.assign(this.model.toJSON(), { Object.assign(this.model.toJSON(), {
'message_limit': _converse.message_limit, 'message_limit': _converse.message_limit,
'hint_value': _.get(this.el.querySelector('.spoiler-hint'), 'value'), 'hint_value': get(this.el.querySelector('.spoiler-hint'), 'value'),
'label_message': this.model.get('composing_spoiler') ? __('Hidden message') : __('Message'), 'label_message': this.model.get('composing_spoiler') ? __('Hidden message') : __('Message'),
'label_send': __('Send'), 'label_send': __('Send'),
'label_spoiler_hint': __('Optional hint'), 'label_spoiler_hint': __('Optional hint'),
'message_value': _.get(this.el.querySelector('.chat-textarea'), 'value'), 'message_value': get(this.el.querySelector('.chat-textarea'), 'value'),
'show_send_button': _converse.show_send_button, 'show_send_button': _converse.show_send_button,
'show_toolbar': _converse.show_toolbar, 'show_toolbar': _converse.show_toolbar,
'unread_msgs': __('You have unread messages') 'unread_msgs': __('You have unread messages')
@ -354,7 +352,7 @@ converse.plugins.add('converse-chatview', {
this.user_details_modal.show(ev); this.user_details_modal.show(ev);
}, },
toggleFileUpload (ev) { toggleFileUpload () {
this.el.querySelector('input.fileupload').click(); this.el.querySelector('input.fileupload').click();
}, },
@ -555,10 +553,8 @@ converse.plugins.add('converse-chatview', {
* them here, otherwise we get a null reference later * them here, otherwise we get a null reference later
* upon element insertion. * upon element insertion.
*/ */
const msg_dates = _.invokeMap( const sel = '.message:not(.chat-state-notification)';
sizzle('.message:not(.chat-state-notification)', this.content), const msg_dates = sizzle(sel, this.content).map(e => e.getAttribute('data-isodate'));
Element.prototype.getAttribute, 'data-isodate'
);
const cutoff_iso = cutoff.toISOString(); const cutoff_iso = cutoff.toISOString();
msg_dates.push(cutoff_iso); msg_dates.push(cutoff_iso);
msg_dates.sort(); msg_dates.sort();
@ -890,13 +886,11 @@ converse.plugins.add('converse-chatview', {
return this.editLaterMessage(); return this.editLaterMessage();
} }
} }
if (_.includes([ if ([_converse.keycodes.SHIFT,
_converse.keycodes.SHIFT, _converse.keycodes.META,
_converse.keycodes.META, _converse.keycodes.META_RIGHT,
_converse.keycodes.META_RIGHT, _converse.keycodes.ESCAPE,
_converse.keycodes.ESCAPE, _converse.keycodes.ALT].includes(ev.keyCode)) {
_converse.keycodes.ALT]
, ev.keyCode)) {
return; return;
} }
if (this.model.get('chat_state') !== _converse.COMPOSING) { if (this.model.get('chat_state') !== _converse.COMPOSING) {
@ -916,9 +910,8 @@ converse.plugins.add('converse-chatview', {
onEscapePressed (ev) { onEscapePressed (ev) {
ev.preventDefault(); ev.preventDefault();
const idx = this.model.messages.findLastIndex('correcting'), const idx = this.model.messages.findLastIndex('correcting');
message = idx >=0 ? this.model.messages.at(idx) : null; const message = idx >=0 ? this.model.messages.at(idx) : null;
if (message) { if (message) {
message.save('correcting', false); message.save('correcting', false);
} }
@ -989,7 +982,7 @@ converse.plugins.add('converse-chatview', {
} }
} }
} }
message = message || _.findLast(this.getOwnMessages(), msg => msg.get('message')); message = message || this.getOwnMessages().reverse().find(m => m.get('message'));
if (message) { if (message) {
this.insertIntoTextArea(message.get('message'), true, true); this.insertIntoTextArea(message.get('message'), true, true);
message.save('correcting', true); message.save('correcting', true);
@ -1344,10 +1337,10 @@ converse.plugins.add('converse-chatview', {
); );
return null; return null;
} }
if (_.isString(jids)) { if (isString(jids)) {
return _converse.chatboxviews.get(jids); return _converse.chatboxviews.get(jids);
} }
return jids.map(jid => _converse.chatboxviews.get(jids)); return jids.map(jid => _converse.chatboxviews.get(jid));
} }
} }
}); });