2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-dragresize
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
import "converse-chatview";
|
|
|
|
import "converse-controlbox";
|
2020-03-24 12:26:34 +01:00
|
|
|
import { debounce } from "lodash";
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "@converse/headless/converse-core";
|
2018-10-23 03:41:38 +02:00
|
|
|
import tpl_dragresize from "templates/dragresize.html";
|
|
|
|
|
2019-12-04 16:01:34 +01:00
|
|
|
const u = converse.env.utils;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
function renderDragResizeHandles (_converse, view) {
|
|
|
|
const flyout = view.el.querySelector('.box-flyout');
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = tpl_dragresize();
|
|
|
|
flyout.insertBefore(
|
|
|
|
div,
|
|
|
|
flyout.firstChild
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-dragresize', {
|
|
|
|
/* Plugin dependencies are other plugins which might be
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
* this plugin.
|
|
|
|
*
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
* an error will be raised if the plugin is not found. By default it's
|
|
|
|
* false, which means these plugins are only loaded opportunistically.
|
|
|
|
*
|
|
|
|
* NB: These plugins need to have already been loaded via require.js.
|
|
|
|
*/
|
2019-12-04 16:01:34 +01:00
|
|
|
dependencies: ["converse-chatview", "converse-headlines-view", "converse-muc-views"],
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
enabled (_converse) {
|
2020-03-30 16:29:09 +02:00
|
|
|
return _converse.api.settings.get("view_mode") == 'overlayed';
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
overrides: {
|
|
|
|
// Overrides mentioned here will be picked up by converse.js's
|
|
|
|
// plugin architecture they will replace existing methods on the
|
|
|
|
// relevant objects or classes.
|
|
|
|
ChatBox: {
|
|
|
|
initialize () {
|
2019-10-24 14:29:15 +02:00
|
|
|
const result = this.__super__.initialize.apply(this, arguments);
|
|
|
|
const height = this.get('height'), width = this.get('width');
|
2019-12-04 16:01:34 +01:00
|
|
|
const save = this.get('id') === 'controlbox' ? a => this.set(a) : a => this.save(a);
|
2018-10-23 03:41:38 +02:00
|
|
|
save({
|
2019-12-04 16:01:34 +01:00
|
|
|
'height': u.applyDragResistance(height, this.get('default_height')),
|
|
|
|
'width': u.applyDragResistance(width, this.get('default_width')),
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2017-11-02 15:30:24 +01:00
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
ChatBoxView: {
|
|
|
|
events: {
|
|
|
|
'mousedown .dragresize-top': 'onStartVerticalResize',
|
|
|
|
'mousedown .dragresize-left': 'onStartHorizontalResize',
|
|
|
|
'mousedown .dragresize-topleft': 'onStartDiagonalResize'
|
|
|
|
},
|
2016-05-28 10:55:03 +02:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
render () {
|
|
|
|
const result = this.__super__.render.apply(this, arguments);
|
|
|
|
renderDragResizeHandles(this.__super__._converse, this);
|
|
|
|
this.setWidth();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
HeadlinesBoxView: {
|
|
|
|
events: {
|
|
|
|
'mousedown .dragresize-top': 'onStartVerticalResize',
|
|
|
|
'mousedown .dragresize-left': 'onStartHorizontalResize',
|
|
|
|
'mousedown .dragresize-topleft': 'onStartDiagonalResize'
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2017-11-02 15:30:24 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
render () {
|
|
|
|
const result = this.__super__.render.apply(this, arguments);
|
|
|
|
renderDragResizeHandles(this.__super__._converse, this);
|
|
|
|
this.setWidth();
|
|
|
|
return result;
|
2019-05-24 13:52:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ControlBoxView: {
|
|
|
|
events: {
|
|
|
|
'mousedown .dragresize-top': 'onStartVerticalResize',
|
|
|
|
'mousedown .dragresize-left': 'onStartHorizontalResize',
|
|
|
|
'mousedown .dragresize-topleft': 'onStartDiagonalResize'
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2016-04-01 14:46:19 +02:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
render () {
|
|
|
|
const result = this.__super__.render.apply(this, arguments);
|
|
|
|
renderDragResizeHandles(this.__super__._converse, this);
|
|
|
|
this.setWidth();
|
|
|
|
return result;
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2016-04-01 14:46:19 +02:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
renderLoginPanel () {
|
|
|
|
const result = this.__super__.renderLoginPanel.apply(this, arguments);
|
2018-10-23 03:41:38 +02:00
|
|
|
this.initDragResize().setDimensions();
|
2019-05-24 13:52:15 +02:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderControlBoxPane () {
|
|
|
|
const result = this.__super__.renderControlBoxPane.apply(this, arguments);
|
|
|
|
this.initDragResize().setDimensions();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ChatRoomView: {
|
|
|
|
events: {
|
|
|
|
'mousedown .dragresize-top': 'onStartVerticalResize',
|
|
|
|
'mousedown .dragresize-left': 'onStartHorizontalResize',
|
|
|
|
'mousedown .dragresize-topleft': 'onStartDiagonalResize'
|
2016-04-01 14:46:19 +02:00
|
|
|
},
|
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
render () {
|
|
|
|
const result = this.__super__.render.apply(this, arguments);
|
|
|
|
renderDragResizeHandles(this.__super__._converse, this);
|
|
|
|
this.setWidth();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2020-06-03 09:17:13 +02:00
|
|
|
api.settings.extend({
|
2019-05-24 13:52:15 +02:00
|
|
|
'allow_dragresize': true,
|
|
|
|
});
|
|
|
|
|
2019-12-04 16:01:34 +01:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
const dragResizable = {
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initDragResize () {
|
2019-07-29 20:53:15 +02:00
|
|
|
const view = this;
|
2019-12-04 16:01:34 +01:00
|
|
|
const debouncedSetDimensions = debounce(() => view.setDimensions());
|
2019-07-29 20:53:15 +02:00
|
|
|
window.addEventListener('resize', view.debouncedSetDimensions)
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(this.model, 'destroy', () => window.removeEventListener('resize', debouncedSetDimensions));
|
2019-07-29 20:53:15 +02:00
|
|
|
|
|
|
|
// Determine and store the default box size.
|
|
|
|
// We need this information for the drag-resizing feature.
|
2019-07-12 00:46:17 +02:00
|
|
|
const flyout = this.el.querySelector('.box-flyout');
|
|
|
|
const style = window.getComputedStyle(flyout);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-07-29 10:19:05 +02:00
|
|
|
if (this.model.get('height') === undefined) {
|
2019-07-12 00:46:17 +02:00
|
|
|
const height = parseInt(style.height.replace(/px$/, ''), 10);
|
|
|
|
const width = parseInt(style.width.replace(/px$/, ''), 10);
|
2018-10-23 03:41:38 +02:00
|
|
|
this.model.set('height', height);
|
|
|
|
this.model.set('default_height', height);
|
|
|
|
this.model.set('width', width);
|
|
|
|
this.model.set('default_width', width);
|
2016-04-01 14:46:19 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
const min_width = style['min-width'];
|
|
|
|
const min_height = style['min-height'];
|
|
|
|
this.model.set('min_width', min_width.endsWith('px') ? Number(min_width.replace(/px$/, '')) :0);
|
|
|
|
this.model.set('min_height', min_height.endsWith('px') ? Number(min_height.replace(/px$/, '')) :0);
|
|
|
|
// Initialize last known mouse position
|
|
|
|
this.prev_pageY = 0;
|
|
|
|
this.prev_pageX = 0;
|
2020-03-24 12:26:34 +01:00
|
|
|
if (_converse.connection?.connected) {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.height = this.model.get('height');
|
|
|
|
this.width = this.model.get('width');
|
|
|
|
}
|
|
|
|
return this;
|
2016-04-01 14:46:19 +02:00
|
|
|
},
|
|
|
|
|
2019-12-04 16:01:34 +01:00
|
|
|
resizeChatBox (ev) {
|
2019-05-24 13:52:15 +02:00
|
|
|
let diff;
|
|
|
|
if (_converse.resizing.direction.indexOf('top') === 0) {
|
|
|
|
diff = ev.pageY - this.prev_pageY;
|
|
|
|
if (diff) {
|
|
|
|
this.height = ((this.height-diff) > (this.model.get('min_height') || 0)) ? (this.height-diff) : this.model.get('min_height');
|
|
|
|
this.prev_pageY = ev.pageY;
|
|
|
|
this.setChatBoxHeight(this.height);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:01:34 +01:00
|
|
|
if (_converse.resizing.direction.includes('left')) {
|
2019-05-24 13:52:15 +02:00
|
|
|
diff = this.prev_pageX - ev.pageX;
|
|
|
|
if (diff) {
|
|
|
|
this.width = ((this.width+diff) > (this.model.get('min_width') || 0)) ? (this.width+diff) : this.model.get('min_width');
|
|
|
|
this.prev_pageX = ev.pageX;
|
|
|
|
this.setChatBoxWidth(this.width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setWidth () {
|
|
|
|
// If a custom width is applied (due to drag-resizing),
|
|
|
|
// then we need to set the width of the .chatbox element as well.
|
|
|
|
if (this.model.get('width')) {
|
|
|
|
this.el.style.width = this.model.get('width');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
setDimensions () {
|
|
|
|
// Make sure the chat box has the right height and width.
|
|
|
|
this.adjustToViewport();
|
|
|
|
this.setChatBoxHeight(this.model.get('height'));
|
|
|
|
this.setChatBoxWidth(this.model.get('width'));
|
|
|
|
},
|
|
|
|
|
|
|
|
setChatBoxHeight (height) {
|
|
|
|
if (height) {
|
2019-12-04 16:01:34 +01:00
|
|
|
height = u.applyDragResistance(height, this.model.get('default_height'))+'px';
|
2018-10-23 03:41:38 +02:00
|
|
|
} else {
|
|
|
|
height = "";
|
|
|
|
}
|
|
|
|
const flyout_el = this.el.querySelector('.box-flyout');
|
2019-08-05 01:39:57 +02:00
|
|
|
if (flyout_el !== null) {
|
2018-10-23 03:41:38 +02:00
|
|
|
flyout_el.style.height = height;
|
2016-04-01 14:46:19 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
setChatBoxWidth (width) {
|
|
|
|
if (width) {
|
2019-12-04 16:01:34 +01:00
|
|
|
width = u.applyDragResistance(width, this.model.get('default_width'))+'px';
|
2018-10-23 03:41:38 +02:00
|
|
|
} else {
|
|
|
|
width = "";
|
|
|
|
}
|
|
|
|
this.el.style.width = width;
|
|
|
|
const flyout_el = this.el.querySelector('.box-flyout');
|
2019-08-05 01:39:57 +02:00
|
|
|
if (flyout_el !== null) {
|
2018-10-23 03:41:38 +02:00
|
|
|
flyout_el.style.width = width;
|
2016-12-19 08:46:48 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
adjustToViewport () {
|
|
|
|
/* Event handler called when viewport gets resized. We remove
|
|
|
|
* custom width/height from chat boxes.
|
|
|
|
*/
|
|
|
|
const viewport_width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
|
|
|
|
const viewport_height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
|
|
|
|
if (viewport_width <= 480) {
|
|
|
|
this.model.set('height', undefined);
|
|
|
|
this.model.set('width', undefined);
|
|
|
|
} else if (viewport_width <= this.model.get('width')) {
|
|
|
|
this.model.set('width', undefined);
|
|
|
|
} else if (viewport_height <= this.model.get('height')) {
|
|
|
|
this.model.set('height', undefined);
|
2016-04-01 14:46:19 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-07-25 06:46:05 +02:00
|
|
|
onStartVerticalResize (ev, trigger=true) {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!api.settings.get('allow_dragresize')) {
|
2020-03-30 16:29:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-30 22:37:49 +02:00
|
|
|
ev.preventDefault();
|
2018-10-23 03:41:38 +02:00
|
|
|
// Record element attributes for mouseMove().
|
|
|
|
const flyout = this.el.querySelector('.box-flyout'),
|
|
|
|
style = window.getComputedStyle(flyout);
|
|
|
|
this.height = parseInt(style.height.replace(/px$/, ''), 10);
|
|
|
|
_converse.resizing = {
|
|
|
|
'chatbox': this,
|
|
|
|
'direction': 'top'
|
|
|
|
};
|
|
|
|
this.prev_pageY = ev.pageY;
|
2019-07-25 06:46:05 +02:00
|
|
|
if (trigger) {
|
|
|
|
/**
|
|
|
|
* Triggered once the user starts to vertically resize a {@link _converse.ChatBoxView}
|
|
|
|
* @event _converse#startVerticalResize
|
|
|
|
* @example _converse.api.listen.on('startVerticalResize', (view) => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('startVerticalResize', this);
|
2019-07-25 06:46:05 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
2019-07-25 06:46:05 +02:00
|
|
|
onStartHorizontalResize (ev, trigger=true) {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!api.settings.get('allow_dragresize')) {
|
2020-03-30 16:29:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-30 22:37:49 +02:00
|
|
|
ev.preventDefault();
|
2018-10-23 03:41:38 +02:00
|
|
|
const flyout = this.el.querySelector('.box-flyout'),
|
|
|
|
style = window.getComputedStyle(flyout);
|
|
|
|
this.width = parseInt(style.width.replace(/px$/, ''), 10);
|
|
|
|
_converse.resizing = {
|
|
|
|
'chatbox': this,
|
|
|
|
'direction': 'left'
|
|
|
|
};
|
|
|
|
this.prev_pageX = ev.pageX;
|
2019-07-25 06:46:05 +02:00
|
|
|
if (trigger) {
|
|
|
|
/**
|
|
|
|
* Triggered once the user starts to horizontally resize a {@link _converse.ChatBoxView}
|
|
|
|
* @event _converse#startHorizontalResize
|
|
|
|
* @example _converse.api.listen.on('startHorizontalResize', (view) => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('startHorizontalResize', this);
|
2019-07-25 06:46:05 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onStartDiagonalResize (ev) {
|
2019-07-25 06:46:05 +02:00
|
|
|
this.onStartHorizontalResize(ev, false);
|
|
|
|
this.onStartVerticalResize(ev, false);
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.resizing.direction = 'topleft';
|
2019-07-25 06:46:05 +02:00
|
|
|
/**
|
|
|
|
* Triggered once the user starts to diagonally resize a {@link _converse.ChatBoxView}
|
|
|
|
* @event _converse#startDiagonalResize
|
|
|
|
* @example _converse.api.listen.on('startDiagonalResize', (view) => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('startDiagonalResize', this);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2019-05-24 13:52:15 +02:00
|
|
|
};
|
|
|
|
Object.assign(_converse.ChatBoxView.prototype, dragResizable);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-12-04 16:01:34 +01:00
|
|
|
|
|
|
|
u.applyDragResistance = function (value, default_value) {
|
|
|
|
/* This method applies some resistance around the
|
|
|
|
* default_value. If value is close enough to
|
|
|
|
* default_value, then default_value is returned instead.
|
|
|
|
*/
|
|
|
|
if (value === undefined) {
|
|
|
|
return undefined;
|
|
|
|
} else if (default_value === undefined) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
const resistance = 10;
|
|
|
|
if ((value !== default_value) &&
|
|
|
|
(Math.abs(value- default_value) < resistance)) {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-12-05 21:02:06 +01:00
|
|
|
function onMouseMove (ev) {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!_converse.resizing || !api.settings.get('allow_dragresize')) {
|
2020-03-30 16:29:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-05 21:02:06 +01:00
|
|
|
ev.preventDefault();
|
|
|
|
_converse.resizing.chatbox.resizeChatBox(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onMouseUp (ev) {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!_converse.resizing || !api.settings.get('allow_dragresize')) {
|
2020-03-30 16:29:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-05 21:02:06 +01:00
|
|
|
ev.preventDefault();
|
|
|
|
const height = u.applyDragResistance(
|
|
|
|
_converse.resizing.chatbox.height,
|
|
|
|
_converse.resizing.chatbox.model.get('default_height')
|
|
|
|
);
|
|
|
|
const width = u.applyDragResistance(
|
|
|
|
_converse.resizing.chatbox.width,
|
|
|
|
_converse.resizing.chatbox.model.get('default_width')
|
|
|
|
);
|
2020-03-31 13:15:57 +02:00
|
|
|
if (api.connection.connected()) {
|
2019-12-05 21:02:06 +01:00
|
|
|
_converse.resizing.chatbox.model.save({'height': height});
|
|
|
|
_converse.resizing.chatbox.model.save({'width': width});
|
|
|
|
} else {
|
|
|
|
_converse.resizing.chatbox.model.set({'height': height});
|
|
|
|
_converse.resizing.chatbox.model.set({'width': width});
|
|
|
|
}
|
|
|
|
_converse.resizing = null;
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:01:34 +01:00
|
|
|
/************************ BEGIN Event Handlers ************************/
|
|
|
|
function registerGlobalEventHandlers () {
|
2019-12-05 21:02:06 +01:00
|
|
|
document.addEventListener('mousemove', onMouseMove);
|
|
|
|
document.addEventListener('mouseup', onMouseUp);
|
|
|
|
}
|
2019-12-04 16:01:34 +01:00
|
|
|
|
2019-12-05 21:02:06 +01:00
|
|
|
function unregisterGlobalEventHandlers () {
|
|
|
|
document.removeEventListener('mousemove', onMouseMove);
|
|
|
|
document.removeEventListener('mouseup', onMouseUp);
|
2019-12-04 16:01:34 +01:00
|
|
|
}
|
2019-12-05 21:02:06 +01:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('registeredGlobalEventHandlers', registerGlobalEventHandlers);
|
|
|
|
api.listen.on('unregisteredGlobalEventHandlers', unregisterGlobalEventHandlers);
|
|
|
|
api.listen.on('beforeShowingChatView', view => view.initDragResize().setDimensions());
|
2019-05-24 13:52:15 +02:00
|
|
|
/************************ END Event Handlers ************************/
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|