Commit bda70490 authored by JC Brand's avatar JC Brand

emoji-views: lazily render picker only once user clicks to open

parent f3472e1b
...@@ -440,7 +440,6 @@ ...@@ -440,7 +440,6 @@
const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit'; const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
await test_utils.openChatBoxFor(_converse, contact_jid); await test_utils.openChatBoxFor(_converse, contact_jid);
const view = _converse.chatboxviews.get(contact_jid); const view = _converse.chatboxviews.get(contact_jid);
await u.waitUntil(() => view.el.querySelector('.emoji-picker'));
const toolbar = view.el.querySelector('.chat-toolbar'); const toolbar = view.el.querySelector('.chat-toolbar');
const counter = toolbar.querySelector('.message-limit'); const counter = toolbar.querySelector('.message-limit');
expect(counter.textContent).toBe('200'); expect(counter.textContent).toBe('200');
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit'; const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
await test_utils.openChatBoxFor(_converse, contact_jid); await test_utils.openChatBoxFor(_converse, contact_jid);
const view = _converse.chatboxviews.get(contact_jid); const view = _converse.chatboxviews.get(contact_jid);
await u.waitUntil(() => view.el.querySelector('.emoji-picker'));
const toolbar = view.el.querySelector('ul.chat-toolbar'); const toolbar = view.el.querySelector('ul.chat-toolbar');
expect(toolbar.querySelectorAll('li.toggle-smiley__container').length).toBe(1); expect(toolbar.querySelectorAll('li.toggle-smiley__container').length).toBe(1);
toolbar.querySelector('a.toggle-smiley').click(); toolbar.querySelector('a.toggle-smiley').click();
...@@ -44,7 +43,6 @@ ...@@ -44,7 +43,6 @@
const muc_jid = 'lounge@montague.lit'; const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo'); await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid); const view = _converse.chatboxviews.get(muc_jid);
await u.waitUntil(() => view.el.querySelector('.emoji-picker'));
const textarea = view.el.querySelector('textarea.chat-textarea'); const textarea = view.el.querySelector('textarea.chat-textarea');
textarea.value = ':gri'; textarea.value = ':gri';
......
...@@ -90,22 +90,26 @@ converse.plugins.add('converse-emoji-views', { ...@@ -90,22 +90,26 @@ converse.plugins.add('converse-emoji-views', {
this.emoji_dropdown.toggle(); this.emoji_dropdown.toggle();
}, },
async createEmojiPickerView () { async createEmojiPicker () {
if (this.emoji_picker_view) {
this.insertEmojiPicker();
return;
} else {
await _converse.api.emojis.initialize() await _converse.api.emojis.initialize()
const id = `converse.emoji-${_converse.bare_jid}-${this.model.get('jid')}`;
const emojipicker = new _converse.EmojiPicker({'id': id});
emojipicker.browserStorage = _converse.createStore(id);
await new Promise(resolve => emojipicker.fetch({ 'success': resolve, 'error': resolve}));
this.emoji_picker_view = new _converse.EmojiPickerView({ this.emoji_picker_view = new _converse.EmojiPickerView({
'model': _converse.emojipicker, 'model': emojipicker,
'chatview': this 'chatview': this
}); });
this.insertEmojiPicker(); const el = this.el.querySelector('.emoji-picker__container');
} el.innerHTML = '';
el.appendChild(this.emoji_picker_view.el);
}, },
createEmojiDropdown () { async createEmojiDropdown () {
if (!this.emoji_dropdown) { if (!this.emoji_dropdown) {
await this.createEmojiPicker();
const el = this.el.querySelector('.emoji-picker'); const el = this.el.querySelector('.emoji-picker');
this.emoji_dropdown = new bootstrap.Dropdown(el, true); this.emoji_dropdown = new bootstrap.Dropdown(el, true);
this.emoji_dropdown.el = el; this.emoji_dropdown.el = el;
...@@ -117,12 +121,6 @@ converse.plugins.add('converse-emoji-views', { ...@@ -117,12 +121,6 @@ converse.plugins.add('converse-emoji-views', {
await this.createEmojiDropdown(); await this.createEmojiDropdown();
this.emoji_dropdown.toggle(); this.emoji_dropdown.toggle();
this.emoji_picker_view.setScrollPosition(); this.emoji_picker_view.setScrollPosition();
},
insertEmojiPicker () {
const el = this.el.querySelector('.emoji-picker__container');
el.innerHTML = '';
el.appendChild(this.emoji_picker_view.el);
} }
}; };
Object.assign(_converse.ChatBoxView.prototype, emoji_aware_chat_view); Object.assign(_converse.ChatBoxView.prototype, emoji_aware_chat_view);
...@@ -258,8 +256,7 @@ converse.plugins.add('converse-emoji-views', { ...@@ -258,8 +256,7 @@ converse.plugins.add('converse-emoji-views', {
const category = el.getAttribute('data-category'); const category = el.getAttribute('data-category');
const old_category = this.model.get('current_category'); const old_category = this.model.get('current_category');
if (old_category !== category) { if (old_category !== category) {
// XXX: Manually set the classes, it's quicker than using the VDOM this.model.save(
this.model.set(
{'current_category': category}, {'current_category': category},
{'silent': true} {'silent': true}
); );
...@@ -453,14 +450,12 @@ converse.plugins.add('converse-emoji-views', { ...@@ -453,14 +450,12 @@ converse.plugins.add('converse-emoji-views', {
if (_converse.visible_toolbar_buttons.emoji) { if (_converse.visible_toolbar_buttons.emoji) {
const html = tpl_emoji_button({'tooltip_insert_smiley': __('Insert emojis')}); const html = tpl_emoji_button({'tooltip_insert_smiley': __('Insert emojis')});
view.el.querySelector('.chat-toolbar').insertAdjacentHTML('afterBegin', html); view.el.querySelector('.chat-toolbar').insertAdjacentHTML('afterBegin', html);
view.createEmojiPickerView();
} }
}); });
// The headlines box doesn't have a toolbar and doesn't show an emoji _converse.api.listen.on('headlinesBoxInitialized', () => _converse.api.emojis.initialize());
// picker, but we still need to make sure that emojis have been _converse.api.listen.on('chatRoomInitialized', () => _converse.api.emojis.initialize());
// initialized in order to show emojis in messages. _converse.api.listen.on('chatBox', () => _converse.api.emojis.initialize());
_converse.api.listen.on('headlinesBoxViewInitialized', () => _converse.api.emojis.initialize());
/************************ END Event Handlers ************************/ /************************ END Event Handlers ************************/
} }
......
...@@ -264,21 +264,6 @@ converse.plugins.add('converse-emoji', { ...@@ -264,21 +264,6 @@ converse.plugins.add('converse-emoji', {
/************************ END Utils ************************/ /************************ END Utils ************************/
function createEmojiPicker () {
if (!_converse.emojipicker) {
const id = `converse.emoji-${_converse.bare_jid}`;
_converse.emojipicker = new _converse.EmojiPicker({'id': id});
_converse.emojipicker.browserStorage = _converse.createStore(id);
_converse.emojipicker.fetch();
}
}
_converse.api.listen.on('logout', () => {
_converse.emojipicker.destroy();
delete _converse.emojipicker;
});
/************************ BEGIN API ************************/ /************************ BEGIN API ************************/
// We extend the default converse.js API to add methods specific to MUC groupchats. // We extend the default converse.js API to add methods specific to MUC groupchats.
Object.assign(_converse.api, { Object.assign(_converse.api, {
...@@ -296,8 +281,6 @@ converse.plugins.add('converse-emoji', { ...@@ -296,8 +281,6 @@ converse.plugins.add('converse-emoji', {
* @returns {Promise} * @returns {Promise}
*/ */
async initialize () { async initialize () {
createEmojiPicker();
if (_converse.emojis.initialized) { if (_converse.emojis.initialized) {
return _converse.emojis.initialized; return _converse.emojis.initialized;
} }
......
...@@ -52,6 +52,12 @@ converse.plugins.add('converse-headlines', { ...@@ -52,6 +52,12 @@ converse.plugins.add('converse-headlines', {
*/ */
const { _converse } = this; const { _converse } = this;
/**
* Shows headline messages
* @class
* @namespace _converse.HeadlinesBox
* @memberOf _converse
*/
_converse.HeadlinesBox = _converse.ChatBox.extend({ _converse.HeadlinesBox = _converse.ChatBox.extend({
defaults () { defaults () {
return { return {
...@@ -67,6 +73,13 @@ converse.plugins.add('converse-headlines', { ...@@ -67,6 +73,13 @@ converse.plugins.add('converse-headlines', {
initialize () { initialize () {
this.initMessages(); this.initMessages();
this.set({'box_id': `box-${btoa(this.get('jid'))}`}); this.set({'box_id': `box-${btoa(this.get('jid'))}`});
/**
* Triggered once a {@link _converse.HeadlinesBox} has been created and initialized.
* @event _converse#headlinesBoxInitialized
* @type { _converse.HeadlinesBox }
* @example _converse.api.listen.on('headlinesBoxInitialized', model => { ... });
*/
_converse.api.trigger('headlinesBoxInitialized', this);
} }
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment