xmpp.chapril.org-conversejs/src/plugins/bookmark-views/mixins.js
JC Brand 1949356ede Work on turning chat views into custom elements
The eventual goal is to avoid UI-related stanza processing if the relevant chats
aren't in the DOM.

With the current architecture, chatboxes are created (and the stanzas
related to them processed) even if `#conversejs` isn't in the DOM.

* Initial work on making controlbox an element
* Create a shared base class
* Ceate ChatBoxViews proxy
* Update sass now that certain classes are moved to converse-chats element
2021-02-09 15:48:21 +01:00

73 lines
2.3 KiB
JavaScript

import { Model } from '@converse/skeletor/src/model.js';
import { __ } from 'i18n';
import { _converse, api, converse } from '@converse/headless/core';
import { invokeMap } from 'lodash-es';
const { u } = converse.env;
export const bookmarkableChatRoomView = {
/**
* Set whether the groupchat is bookmarked or not.
* @private
*/
setBookmarkState () {
if (_converse.bookmarks !== undefined) {
const models = _converse.bookmarks.where({ 'jid': this.model.get('jid') });
if (!models.length) {
this.model.save('bookmarked', false);
} else {
this.model.save('bookmarked', true);
}
}
},
renderBookmarkForm () {
this.hideChatRoomContents();
if (!this.bookmark_form) {
this.bookmark_form = new _converse.MUCBookmarkForm({
'model': this.model,
'chatroomview': this
});
const container_el = this.querySelector('.chatroom-body');
container_el.insertAdjacentElement('beforeend', this.bookmark_form.el);
}
u.showElement(this.bookmark_form.el);
},
toggleBookmark (ev) {
ev?.preventDefault();
const models = _converse.bookmarks.where({ 'jid': this.model.get('jid') });
if (!models.length) {
this.renderBookmarkForm();
} else {
models.forEach(model => model.destroy());
}
}
};
export const eventMethods = {
removeBookmarkViaEvent (ev) {
/* Remove a bookmark as determined by the passed in
* event.
*/
ev.preventDefault();
const name = ev.target.getAttribute('data-bookmark-name');
const jid = ev.target.getAttribute('data-room-jid');
if (confirm(__('Are you sure you want to remove the bookmark "%1$s"?', name))) {
invokeMap(_converse.bookmarks.where({ 'jid': jid }), Model.prototype.destroy);
}
},
addBookmarkViaEvent (ev) {
/* Add a bookmark as determined by the passed in
* event.
*/
ev.preventDefault();
const jid = ev.target.getAttribute('data-room-jid');
api.rooms.open(jid, { 'bring_to_foreground': true });
_converse.chatboxviews.get(jid).renderBookmarkForm();
}
}