MuK IT GmbH
6 years ago
8 changed files with 206 additions and 49 deletions
-
2muk_web_utils/__manifest__.py
-
5muk_web_utils/doc/changelog.rst
-
131muk_web_utils/static/src/js/fields/copy.js
-
52muk_web_utils/static/src/js/fields/share.js
-
31muk_web_utils/static/src/scss/copy.scss
-
2muk_web_utils/static/src/scss/share.scss
-
30muk_web_utils/static/src/xml/copy.xml
-
2muk_web_utils/template/assets.xml
@ -0,0 +1,131 @@ |
|||
/********************************************************************************** |
|||
* |
|||
* Copyright (C) 2017 MuK IT GmbH |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License as |
|||
* published by the Free Software Foundation, either version 3 of the |
|||
* License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
* |
|||
**********************************************************************************/ |
|||
|
|||
odoo.define('muk_web_utils.copy', function (require) { |
|||
"use strict"; |
|||
|
|||
var core = require('web.core'); |
|||
var session = require('web.session'); |
|||
var fields = require('web.basic_fields'); |
|||
var registry = require('web.field_registry'); |
|||
|
|||
var _t = core._t; |
|||
var QWeb = core.qweb; |
|||
|
|||
var BinaryFileCopy = fields.FieldBinaryFile.extend({ |
|||
init: function () { |
|||
this._super.apply(this, arguments); |
|||
if (!this.field.attachment) { |
|||
throw _.str.sprintf(_t( |
|||
"The field '%s' must be a binary field with an set " + |
|||
"attachment flag for the share widget to work." |
|||
), this.field.string); |
|||
} |
|||
this.accessToken = !!this.nodeOptions.token; |
|||
}, |
|||
willStart: function() { |
|||
var def = this.value && this.res_id ? this._fetchShareUrl() : $.when(); |
|||
return $.when(this._super.apply(this, arguments), def); |
|||
}, |
|||
start: function() { |
|||
var self = this; |
|||
var res = this._super.apply(this, arguments); |
|||
var $clipboardBtn = this.$('.mk_copy_binary'); |
|||
this.clipboard = new ClipboardJS($clipboardBtn[0], { |
|||
text: function (trigger) { |
|||
return self.shareUrl; |
|||
}, |
|||
container: self.$el[0] |
|||
}); |
|||
this.clipboard.on('success', function (event) { |
|||
_.defer(function () { |
|||
$clipboardBtn.tooltip('show'); |
|||
_.delay(function () { |
|||
$clipboardBtn.tooltip('hide'); |
|||
}, 800); |
|||
}); |
|||
}); |
|||
$clipboardBtn.click(function(event) { |
|||
event.stopPropagation(); |
|||
}); |
|||
$clipboardBtn.tooltip({ |
|||
title: _t('Link Copied!'), |
|||
trigger: 'manual', |
|||
placement: 'bottom' |
|||
}); |
|||
return res; |
|||
}, |
|||
_fetchShareUrl: function() { |
|||
var self = this; |
|||
var def = $.Deferred(); |
|||
if (this.accessToken) { |
|||
this._rpc({ |
|||
model: 'ir.attachment', |
|||
method: 'search', |
|||
args: [[ |
|||
['res_id', '=', this.res_id], |
|||
['res_field', '=', this.name], |
|||
['res_model', '=', this.model], |
|||
]], |
|||
kwargs: { |
|||
context: session.user_context, |
|||
}, |
|||
}).then(function(attchments) { |
|||
self._rpc({ |
|||
model: 'ir.attachment', |
|||
method: 'generate_access_token', |
|||
args: attchments |
|||
}).then(function(access_token) { |
|||
self.shareUrl = session.url('/web/content', { |
|||
model: self.model, |
|||
field: self.name, |
|||
id: self.res_id, |
|||
access_token: access_token.shift(), |
|||
}); |
|||
def.resolve(); |
|||
}); |
|||
}); |
|||
} else { |
|||
this.shareUrl = session.url('/web/content', { |
|||
model: self.model, |
|||
field: self.name, |
|||
id: self.res_id, |
|||
}); |
|||
def.resolve(); |
|||
} |
|||
return def; |
|||
}, |
|||
_renderReadonly: function () { |
|||
this._super.apply(this, arguments); |
|||
this.$el.addClass('mk_field_copy'); |
|||
this.$el.append($(QWeb.render('muk_web_utils.BinaryFieldCopy'))); |
|||
}, |
|||
destroy: function () { |
|||
this._super.apply(this, arguments); |
|||
this.clipboard.destroy(); |
|||
}, |
|||
}); |
|||
|
|||
registry.add('binary_copy', BinaryFileCopy); |
|||
|
|||
return { |
|||
BinaryFileCopy: BinaryFileCopy, |
|||
}; |
|||
|
|||
}); |
@ -0,0 +1,31 @@ |
|||
/********************************************************************************** |
|||
* |
|||
* Copyright (C) 2017 MuK IT GmbH |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify |
|||
* it under the terms of the GNU Affero General Public License as |
|||
* published by the Free Software Foundation, either version 3 of the |
|||
* License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU Affero General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public License |
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
**********************************************************************************/ |
|||
|
|||
.o_field_widget.mk_field_copy { |
|||
.mk_copy_binary { |
|||
margin-left: 0.5rem; |
|||
.mk_copy_button { |
|||
font-size: 0.8rem; |
|||
line-height: 0.5; |
|||
border-radius: 0.2rem; |
|||
margin-bottom: 0.12rem; |
|||
padding: 0.25rem 0.4rem; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
|
|||
<!-- |
|||
Copyright (C) 2018 MuK IT GmbH |
|||
|
|||
This program is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU Affero General Public License as |
|||
published by the Free Software Foundation, either version 3 of the |
|||
License, or (at your option) any later version. |
|||
|
|||
This program is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU Affero General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Affero General Public License |
|||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
--> |
|||
|
|||
<templates id="template" xml:space="preserve"> |
|||
|
|||
<t t-name="muk_web_utils.BinaryFieldCopy"> |
|||
<div class="d-inline-block mk_copy_binary"> |
|||
<button class="btn btn-sm btn-outline-primary mk_copy_button" type="button"> |
|||
<span class="fa fa-clipboard"></span> |
|||
</button> |
|||
</div> |
|||
</t> |
|||
|
|||
</templates> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue