converse-http-file-upload.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Adds Support for Http File Upload (XEP-0363)
  3. *
  4. */
  5. (function (root, factory) {
  6. define([
  7. "converse-core",
  8. "tpl!toolbar_fileupload"
  9. ], factory);
  10. }(this, function (converse, tpl_toolbar_fileupload) {
  11. "use strict";
  12. const { Promise, Strophe, _ } = converse.env;
  13. const u = converse.env.utils;
  14. Strophe.addNamespace('HTTPUPLOAD', 'urn:xmpp:http:upload:0');
  15. converse.plugins.add('converse-http-file-upload', {
  16. /* Plugin dependencies are other plugins which might be
  17. * overridden or relied upon, and therefore need to be loaded before
  18. * this plugin.
  19. *
  20. * If the setting "strict_plugin_dependencies" is set to true,
  21. * an error will be raised if the plugin is not found. By default it's
  22. * false, which means these plugins are only loaded opportunistically.
  23. *
  24. * NB: These plugins need to have already been loaded via require.js.
  25. */
  26. dependencies: ["converse-chatboxes", "converse-chatview", "converse-muc-views"],
  27. overrides: {
  28. ChatBoxView: {
  29. events: {
  30. 'click .upload-file': 'toggleFileUpload',
  31. 'change input.fileupload': 'onFileSelection'
  32. },
  33. toggleFileUpload (ev) {
  34. this.el.querySelector('input.fileupload').click();
  35. },
  36. onFileSelection (evt) {
  37. this.model.sendFiles(evt.target.files);
  38. },
  39. addFileUploadButton (options) {
  40. const { __ } = this.__super__._converse;
  41. this.el.querySelector('.chat-toolbar').insertAdjacentHTML(
  42. 'beforeend',
  43. tpl_toolbar_fileupload({'tooltip_upload_file': __('Choose a file to send')}));
  44. },
  45. renderToolbar (toolbar, options) {
  46. const { _converse } = this.__super__;
  47. const result = this.__super__.renderToolbar.apply(this, arguments);
  48. _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then((result) => {
  49. if (result.length) {
  50. this.addFileUploadButton();
  51. }
  52. });
  53. return result;
  54. }
  55. },
  56. ChatRoomView: {
  57. events: {
  58. 'click .upload-file': 'toggleFileUpload',
  59. 'change .input.fileupload': 'onFileSelection'
  60. }
  61. }
  62. }
  63. });
  64. return converse;
  65. }));