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.

131 lines
4.3 KiB

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