From 05c00c5e1c67357c5a99a8b0707c23c54a7e8987 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Mon, 25 Jan 2021 22:04:01 +0100 Subject: [PATCH] Call minimize on the model Fix minimizing by listening to change event Fixes around showing/hiding and minimizing/maximizing Don't trigger a `show` event on the model to show a chat, instead rely on the `hidden` attribute which causes a rerender and will show/hide chats. --- spec/mock.js | 2 ++ src/headless/plugins/chat/model.js | 5 +++-- src/plugins/chatboxviews/view.js | 5 +++-- src/plugins/chatview/view.js | 4 ++-- src/plugins/controlbox/api.js | 6 ++++-- src/plugins/controlbox/toggle.js | 6 +----- src/plugins/minimize/index.js | 12 +----------- src/plugins/minimize/mixins.js | 2 ++ src/plugins/minimize/utils.js | 4 ++-- src/plugins/muc-views/muc.js | 11 ++++++----- 10 files changed, 26 insertions(+), 31 deletions(-) diff --git a/spec/mock.js b/spec/mock.js index 990e11795..37ab73204 100644 --- a/spec/mock.js +++ b/spec/mock.js @@ -641,6 +641,8 @@ window.addEventListener('converse-loaded', () => { 'view_mode': mock.view_mode }, settings || {})); + window._converse = _converse; + _converse.api.vcard.get = function (model, force) { let jid; if (typeof model === 'string' || model instanceof String) { diff --git a/src/headless/plugins/chat/model.js b/src/headless/plugins/chat/model.js index 7dddc4c65..c0ea86bd8 100644 --- a/src/headless/plugins/chat/model.js +++ b/src/headless/plugins/chat/model.js @@ -910,11 +910,12 @@ const ChatBox = ModelWithContact.extend({ } u.safeSave(this, {'hidden': false}); } - if (_converse.isUniView() && this.get('hidden')) { + if (_converse.isUniView()) { return; } else { - return this.trigger("show"); + u.safeSave(this, {'hidden': false}); } + return this; }, /** diff --git a/src/plugins/chatboxviews/view.js b/src/plugins/chatboxviews/view.js index 4bbd79e22..051444400 100644 --- a/src/plugins/chatboxviews/view.js +++ b/src/plugins/chatboxviews/view.js @@ -9,11 +9,12 @@ class ConverseChats extends ElementView { initialize () { this.model = _converse.chatboxes; - this.listenTo(this.model, 'destroy', this.render); this.listenTo(this.model, 'add', this.render); - this.listenTo(this.model, 'change:hidden', this.render); this.listenTo(this.model, 'change:closed', this.render); + this.listenTo(this.model, 'change:hidden', this.render); this.listenTo(this.model, 'change:jid', this.render); + this.listenTo(this.model, 'change:minimized', this.render); + this.listenTo(this.model, 'destroy', this.render); const bg = document.getElementById('conversejs-bg'); if (bg && !bg.innerHTML.trim()) { diff --git a/src/plugins/chatview/view.js b/src/plugins/chatview/view.js index ddb175da2..59cbe00ea 100644 --- a/src/plugins/chatview/view.js +++ b/src/plugins/chatview/view.js @@ -44,9 +44,9 @@ export default class ChatView extends BaseChatView { api.listen.on('windowStateChanged', this.onWindowStateChanged); this.listenTo(this.model, 'change:composing_spoiler', this.renderMessageForm); - this.listenTo(this.model, 'change:hidden', m => (!m.get('hidden') && this.show())); + this.listenTo(this.model, 'change:hidden', () => this.afterShown()); + this.listenTo(this.model, 'change:minimized', () => this.afterShown()); this.listenTo(this.model, 'change:status', this.onStatusMessageChanged); - this.listenTo(this.model, 'show', this.show); this.listenTo(this.model, 'vcard:change', this.renderHeading); if (this.model.contact) { diff --git a/src/plugins/controlbox/api.js b/src/plugins/controlbox/api.js index b7b548abc..bc3bee8c3 100644 --- a/src/plugins/controlbox/api.js +++ b/src/plugins/controlbox/api.js @@ -1,4 +1,6 @@ -import { _converse, api } from "@converse/headless/core"; +import { _converse, api, converse } from "@converse/headless/core"; + +const { u } = converse.env; export default { /** @@ -18,7 +20,7 @@ export default { await api.waitUntil('chatBoxesFetched'); const model = await api.chatboxes.get('controlbox') || api.chatboxes.create('controlbox', {}, _converse.Controlbox); - model.trigger('show'); + u.safeSave(model, {'closed': false}); return model; }, diff --git a/src/plugins/controlbox/toggle.js b/src/plugins/controlbox/toggle.js index efdcbdb82..adf813e86 100644 --- a/src/plugins/controlbox/toggle.js +++ b/src/plugins/controlbox/toggle.js @@ -27,11 +27,7 @@ class ControlBoxToggle extends ElementView { if (!controlbox) { controlbox = addControlBox(); } - if (api.connection.connected()) { - controlbox.save({'closed': false}); - } else { - controlbox.trigger('show'); - } + u.safeSave(controlbox, {'closed': false}); } onClick (e) { diff --git a/src/plugins/minimize/index.js b/src/plugins/minimize/index.js index 348212ed1..7653a795c 100644 --- a/src/plugins/minimize/index.js +++ b/src/plugins/minimize/index.js @@ -50,7 +50,7 @@ converse.plugins.add('converse-minimize', { ChatBox: { initialize () { this.__super__.initialize.apply(this, arguments); - this.on('show', this.maximize, this); + this.on('change:hidden', m => !m.get('hidden') && this.maximize(), this); if (this.get('id') === 'controlbox') { return; @@ -75,16 +75,6 @@ converse.plugins.add('converse-minimize', { }, ChatBoxView: { - show () { - const { _converse } = this.__super__; - if (_converse.api.settings.get("view_mode") === 'overlayed' && this.model.get('minimized')) { - this.model.minimize(); - return this; - } else { - return this.__super__.show.apply(this, arguments); - } - }, - isNewMessageHidden () { return this.model.get('minimized') || this.__super__.isNewMessageHidden.apply(this, arguments); diff --git a/src/plugins/minimize/mixins.js b/src/plugins/minimize/mixins.js index 2473ea13a..5420dfa0e 100644 --- a/src/plugins/minimize/mixins.js +++ b/src/plugins/minimize/mixins.js @@ -5,6 +5,7 @@ const u = converse.env.utils; export const minimizableChatBox = { maximize () { u.safeSave(this, { + 'hidden': false, 'minimized': false, 'time_opened': new Date().getTime() }); @@ -12,6 +13,7 @@ export const minimizableChatBox = { minimize () { u.safeSave(this, { + 'hidden': true, 'minimized': true, 'time_minimized': new Date().toISOString() }); diff --git a/src/plugins/minimize/utils.js b/src/plugins/minimize/utils.js index b7d217d44..8cda215b1 100644 --- a/src/plugins/minimize/utils.js +++ b/src/plugins/minimize/utils.js @@ -104,7 +104,7 @@ function getOldestMaximizedChat (exclude_ids) { export function addMinimizeButtonToChat (view, buttons) { const data = { 'a_class': 'toggle-chatbox-button', - 'handler': ev => view.minimize(ev), + 'handler': ev => view.model.minimize(ev), 'i18n_text': __('Minimize'), 'i18n_title': __('Minimize this chat'), 'icon_class': "fa-minus", @@ -119,7 +119,7 @@ export function addMinimizeButtonToChat (view, buttons) { export function addMinimizeButtonToMUC (view, buttons) { const data = { 'a_class': 'toggle-chatbox-button', - 'handler': ev => view.minimize(ev), + 'handler': ev => view.model.minimize(ev), 'i18n_text': __('Minimize'), 'i18n_title': __('Minimize this groupchat'), 'icon_class': "fa-minus", diff --git a/src/plugins/muc-views/muc.js b/src/plugins/muc-views/muc.js index 965465c36..14b9738b3 100644 --- a/src/plugins/muc-views/muc.js +++ b/src/plugins/muc-views/muc.js @@ -89,6 +89,8 @@ export default class MUCView extends BaseChatView { debounce(() => this.renderHeading(), 250) ); this.listenTo(this.model, 'change:composing_spoiler', this.renderMessageForm); + this.listenTo(this.model, 'change:hidden', () => this.afterShown()); + this.listenTo(this.model, 'change:minimized', () => this.afterShown()); this.listenTo(this.model, 'change:hidden_occupants', this.onSidebarToggle); this.listenTo(this.model, 'configurationNeeded', this.getAndRenderConfigurationForm); this.listenTo(this.model, 'show', this.show); @@ -695,11 +697,10 @@ export default class MUCView extends BaseChatView { * @method _converse.ChatRoomView#afterShown */ afterShown () { - // Override from converse-chatview, specifically to avoid - // the 'active' chat state from being sent out prematurely. - // This is instead done in `onConnectionStatusChanged` below. - this.model.clearUnreadMsgCounter(); - this.scrollDown(); + if (!this.model.get('hidden') && !this.model.get('minimized')) { + this.model.clearUnreadMsgCounter(); + this.scrollDown(); + } } onConnectionStatusChanged () {