index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @module converse-dragresize
  3. * @copyright 2020, the Converse.js contributors
  4. * @license Mozilla Public License (MPLv2)
  5. */
  6. import './components/dragresize.js';
  7. import 'plugins/chatview/index.js';
  8. import 'plugins/controlbox/index.js';
  9. import { applyDragResistance, onMouseUp, onMouseMove } from './utils.js';
  10. import DragResizableMixin from './mixin.js';
  11. import { _converse, api, converse } from '@converse/headless/core';
  12. converse.plugins.add('converse-dragresize', {
  13. /* Plugin dependencies are other plugins which might be
  14. * overridden or relied upon, and therefore need to be loaded before
  15. * this plugin.
  16. *
  17. * If the setting "strict_plugin_dependencies" is set to true,
  18. * an error will be raised if the plugin is not found. By default it's
  19. * false, which means these plugins are only loaded opportunistically.
  20. *
  21. * NB: These plugins need to have already been loaded via require.js.
  22. */
  23. dependencies: ['converse-chatview', 'converse-headlines-view', 'converse-muc-views'],
  24. enabled (_converse) {
  25. return _converse.api.settings.get('view_mode') == 'overlayed';
  26. },
  27. overrides: {
  28. // Overrides mentioned here will be picked up by converse.js's
  29. // plugin architecture they will replace existing methods on the
  30. // relevant objects or classes.
  31. ChatBox: {
  32. initialize () {
  33. const result = this.__super__.initialize.apply(this, arguments);
  34. const height = this.get('height');
  35. const width = this.get('width');
  36. const save = this.get('id') === 'controlbox' ? a => this.set(a) : a => this.save(a);
  37. save({
  38. 'height': applyDragResistance(height, this.get('default_height')),
  39. 'width': applyDragResistance(width, this.get('default_width'))
  40. });
  41. return result;
  42. }
  43. },
  44. ControlBoxView: {
  45. renderLoginPanel () {
  46. const result = this.__super__.renderLoginPanel.apply(this, arguments);
  47. this.initDragResize().setDimensions();
  48. return result;
  49. },
  50. renderControlBoxPane () {
  51. const result = this.__super__.renderControlBoxPane.apply(this, arguments);
  52. this.initDragResize().setDimensions();
  53. return result;
  54. }
  55. }
  56. },
  57. initialize () {
  58. /* The initialize function gets called as soon as the plugin is
  59. * loaded by converse.js's plugin machinery.
  60. */
  61. api.settings.extend({
  62. 'allow_dragresize': true
  63. });
  64. Object.assign(_converse.ChatBoxView.prototype, DragResizableMixin);
  65. Object.assign(_converse.ChatRoomView.prototype, DragResizableMixin);
  66. Object.assign(_converse.ControlBoxView.prototype, DragResizableMixin);
  67. /************************ BEGIN Event Handlers ************************/
  68. function registerGlobalEventHandlers () {
  69. document.addEventListener('mousemove', onMouseMove);
  70. document.addEventListener('mouseup', onMouseUp);
  71. }
  72. function unregisterGlobalEventHandlers () {
  73. document.removeEventListener('mousemove', onMouseMove);
  74. document.removeEventListener('mouseup', onMouseUp);
  75. }
  76. api.listen.on('registeredGlobalEventHandlers', registerGlobalEventHandlers);
  77. api.listen.on('unregisteredGlobalEventHandlers', unregisterGlobalEventHandlers);
  78. api.listen.on('beforeShowingChatView', view => view.initDragResize().setDimensions());
  79. }
  80. });