You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.2 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Web Utils
  6. * (see https://mukit.at).
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. **********************************************************************************/
  22. odoo.define('muk_web_utils.copy', function (require) {
  23. "use strict";
  24. var core = require('web.core');
  25. var session = require('web.session');
  26. var fields = require('web.basic_fields');
  27. var registry = require('web.field_registry');
  28. var _t = core._t;
  29. var QWeb = core.qweb;
  30. var BinaryFileCopy = fields.FieldBinaryFile.extend({
  31. init: function () {
  32. this._super.apply(this, arguments);
  33. if (!this.field.attachment) {
  34. throw _.str.sprintf(_t(
  35. "The field '%s' must be a binary field with an set " +
  36. "attachment flag for the share widget to work."
  37. ), this.field.string);
  38. }
  39. this.accessToken = !!this.nodeOptions.token;
  40. },
  41. willStart: function() {
  42. var def = this.value && this.res_id ? this._fetchShareUrl() : $.when();
  43. return $.when(this._super.apply(this, arguments), def);
  44. },
  45. _fetchShareUrl: function() {
  46. var self = this;
  47. var def = $.Deferred();
  48. if (this.accessToken) {
  49. this._rpc({
  50. model: 'ir.attachment',
  51. method: 'search',
  52. args: [[
  53. ['res_id', '=', this.res_id],
  54. ['res_field', '=', this.name],
  55. ['res_model', '=', this.model],
  56. ]],
  57. kwargs: {
  58. context: session.user_context,
  59. },
  60. }).then(function(attchments) {
  61. self._rpc({
  62. model: 'ir.attachment',
  63. method: 'generate_access_token',
  64. args: attchments
  65. }).then(function(access_token) {
  66. self.shareUrl = session.url('/web/content', {
  67. model: self.model,
  68. field: self.name,
  69. id: self.res_id,
  70. access_token: access_token.shift(),
  71. });
  72. def.resolve();
  73. });
  74. });
  75. } else {
  76. this.shareUrl = session.url('/web/content', {
  77. model: self.model,
  78. field: self.name,
  79. id: self.res_id,
  80. });
  81. def.resolve();
  82. }
  83. return def;
  84. },
  85. _setUpClipboad: function() {
  86. var self = this;
  87. var $clipboardBtn = this.$('.mk_copy_binary');
  88. this.clipboard = new ClipboardJS($clipboardBtn[0], {
  89. text: function (trigger) {
  90. return self.shareUrl;
  91. },
  92. container: self.$el[0]
  93. });
  94. this.clipboard.on('success', function (event) {
  95. _.defer(function () {
  96. $clipboardBtn.tooltip('show');
  97. _.delay(function () {
  98. $clipboardBtn.tooltip('hide');
  99. }, 800);
  100. });
  101. });
  102. $clipboardBtn.click(function(event) {
  103. event.stopPropagation();
  104. });
  105. $clipboardBtn.tooltip({
  106. title: _t('Link Copied!'),
  107. trigger: 'manual',
  108. placement: 'bottom'
  109. });
  110. },
  111. _renderReadonly: function () {
  112. this._super.apply(this, arguments);
  113. this.$el.addClass('mk_field_copy');
  114. this.$el.append($(QWeb.render('muk_web_utils.BinaryFieldCopy')));
  115. this._setUpClipboad();
  116. },
  117. destroy: function () {
  118. this._super.apply(this, arguments);
  119. if (this.clipboard) {
  120. this.clipboard.destroy();
  121. }
  122. },
  123. });
  124. registry.add('copy_binary', BinaryFileCopy);
  125. return {
  126. BinaryFileCopy: BinaryFileCopy,
  127. };
  128. });