summaryrefslogtreecommitdiff
path: root/addons/web_editor/static/src
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2018-04-18 18:30:35 +0200
committerChristophe Simonis <chs@odoo.com>2018-04-18 18:30:35 +0200
commitc309ccd7b3d7e2f4884f6e9e47a98d64ceac8ea1 (patch)
tree82deec2a691d9aee34d70aedac9b68ab75c2825c /addons/web_editor/static/src
parente63e9857411a056538f357b1800d31c835a1647c (diff)
parentfed775bc0c2d70463206bbdddd3cce9ae8469e6a (diff)
[MERGE] forward port branch saas-11.2 up to fed775bc0c2
Diffstat (limited to 'addons/web_editor/static/src')
-rw-r--r--addons/web_editor/static/src/js/backend/fields.js84
-rw-r--r--addons/web_editor/static/src/js/editor/rte.summernote.js10
-rw-r--r--addons/web_editor/static/src/js/widgets/widgets.js13
3 files changed, 104 insertions, 3 deletions
diff --git a/addons/web_editor/static/src/js/backend/fields.js b/addons/web_editor/static/src/js/backend/fields.js
index bbb43fc2f82..479a26469a3 100644
--- a/addons/web_editor/static/src/js/backend/fields.js
+++ b/addons/web_editor/static/src/js/backend/fields.js
@@ -90,11 +90,49 @@ var FieldTextHtmlSimple = basic_fields.DebouncedField.extend(TranslatableFieldMi
//--------------------------------------------------------------------------
/**
+ * Returns the domain for attachments used in media dialog.
+ * We look for attachments related to the current document. If there is a value for the model
+ * field, it is used to search attachments, and the attachments from the current document are
+ * filtered to display only user-created documents.
+ * In the case of a wizard such as mail, we have the documents uploaded and those of the model
+ *
+ * @private
+ * @returns {Array} "ir.attachment" odoo domain.
+ */
+ _getAttachmentsDomain: function () {
+ var domain = ['|', ['id', 'in', _.pluck(this.attachments, 'id')]];
+ var attachedDocumentDomain = [
+ '&',
+ ['res_model', '=', this.model],
+ ['res_id', '=', this.res_id|0]
+ ];
+ // if the document is not yet created, do not see the documents of other users
+ if (!this.res_id) {
+ attachedDocumentDomain.unshift('&');
+ attachedDocumentDomain.push(['create_uid', '=', session.uid]);
+ }
+ if (this.recordData.model) {
+ var relatedDomain = ['&',
+ ['res_model', '=', this.recordData.model],
+ ['res_id', '=', this.recordData.res_id|0]];
+ if (!this.recordData.res_id) {
+ relatedDomain.unshift('&');
+ relatedDomain.push(['create_uid', '=', session.uid]);
+ }
+ domain = domain.concat(['|'], attachedDocumentDomain, relatedDomain);
+ } else {
+ domain = domain.concat(attachedDocumentDomain);
+ }
+ return domain;
+ },
+ /**
* @private
* @returns {Object} the summernote configuration
*/
_getSummernoteConfig: function () {
var summernoteConfig = {
+ model: this.model,
+ id: this.res_id,
focus: false,
height: 180,
toolbar: [
@@ -113,6 +151,22 @@ var FieldTextHtmlSimple = basic_fields.DebouncedField.extend(TranslatableFieldMi
lang: "odoo",
onChange: this._doDebouncedAction.bind(this),
};
+
+ var fieldNameAttachment =_.chain(this.recordData)
+ .pairs()
+ .find(function (value) {
+ return _.isObject(value[1]) && value[1].model === "ir.attachment";
+ })
+ .first()
+ .value();
+
+ if (fieldNameAttachment) {
+ this.fieldNameAttachment = fieldNameAttachment;
+ this.attachments = [];
+ summernoteConfig.onImageUpload = this._onImageUpload.bind(this);
+ summernoteConfig.getMediaDomain = this._getAttachmentsDomain.bind(this);
+ }
+
if (config.debug) {
summernoteConfig.toolbar.splice(7, 0, ['view', ['codeview']]);
}
@@ -131,6 +185,34 @@ var FieldTextHtmlSimple = basic_fields.DebouncedField.extend(TranslatableFieldMi
return this.$content.html();
},
/**
+ * trigger_up 'field_changed' add record into the "ir.attachment" field found in the view.
+ * This method is called when an image is uploaded by the media dialog.
+ *
+ * For e.g. when sending email, this allows people to add attachments with the content
+ * editor interface and that they appear in the attachment list.
+ * The new documents being attached to the email, they will not be erased by the CRON
+ * when closing the wizard.
+ *
+ * @private
+ */
+ _onImageUpload: function (attachments) {
+ var self = this;
+ attachments = _.filter(attachments, function (attachment) {
+ return !_.findWhere(self.attachments, {id: attachment.id});
+ });
+ if (!attachments.length) {
+ return;
+ }
+ this.attachments = this.attachments.concat(attachments);
+ this.trigger_up('field_changed', {
+ dataPointID: this.dataPointID,
+ changes: _.object([this.fieldNameAttachment], [{
+ operation: 'ADD_M2M',
+ ids: attachments
+ }])
+ });
+ },
+ /**
* @override
* @private
*/
@@ -140,8 +222,6 @@ var FieldTextHtmlSimple = basic_fields.DebouncedField.extend(TranslatableFieldMi
this.$textarea.summernote(this._getSummernoteConfig());
this.$content = this.$('.note-editable:first');
this.$content.html(this._textToHtml(this.value));
- this.$content.data('oe-id', this.recordData.res_id || this.res_id);
- this.$content.data('oe-model', this.recordData.model || this.model);
// trigger a mouseup to refresh the editor toolbar
this.$content.trigger('mouseup');
if (this.nodeOptions['style-inline']) {
diff --git a/addons/web_editor/static/src/js/editor/rte.summernote.js b/addons/web_editor/static/src/js/editor/rte.summernote.js
index fb4d6fc7206..f6e2ac63aa9 100644
--- a/addons/web_editor/static/src/js/editor/rte.summernote.js
+++ b/addons/web_editor/static/src/js/editor/rte.summernote.js
@@ -397,6 +397,7 @@ eventHandler.modules.imageDialog.showImageDialog = function ($editable) {
media: media,
options: {
lastFilters: ['background'],
+ onUpload: $editable.data('callbacks').onImageUpload,
},
});
return new $.Deferred().reject();
@@ -788,6 +789,14 @@ eventHandler.attach = function (oLayoutInfo, options) {
$(document).on("keyup", reRangeSelectKey);
var clone_data = false;
+
+ if (options.model) {
+ oLayoutInfo.editable().data({'oe-model': options.model, 'oe-id': options.id});
+ }
+ if (options.getMediaDomain) {
+ oLayoutInfo.editable().data('oe-media-domain', options.getMediaDomain);
+ }
+
var $node = oLayoutInfo.editor();
if ($node.data('oe-model') || $node.data('oe-translation-id')) {
$node.on('content_changed', function () {
@@ -1089,6 +1098,7 @@ var SummernoteManager = Class.extend(mixins.EventDispatcherMixin, {
_.extend({
res_model: data.$editable.data('oe-model'),
res_id: data.$editable.data('oe-id'),
+ domain: data.$editable.data('oe-media-domain'),
}, data.options),
data.$editable,
data.media
diff --git a/addons/web_editor/static/src/js/widgets/widgets.js b/addons/web_editor/static/src/js/widgets/widgets.js
index 53df66e9ba7..f7c1c808016 100644
--- a/addons/web_editor/static/src/js/widgets/widgets.js
+++ b/addons/web_editor/static/src/js/widgets/widgets.js
@@ -209,7 +209,9 @@ var ImageWidget = MediaWidget.extend({
this.options = options;
this.accept = options.accept || (options.document ? '*/*' : 'image/*');
- if (options.res_id) {
+ if (options.domain) {
+ this.domain = typeof options.domain === 'function' ? options.domain() : options.domain;
+ } else if (options.res_id) {
this.domain = ['|',
'&', ['res_model', '=', options.res_model], ['res_id', '=', options.res_id],
['res_model', '=', 'ir.ui.view']];
@@ -330,6 +332,11 @@ var ImageWidget = MediaWidget.extend({
self.$media.css(style);
}
+ if (self.options.onUpload) {
+ // We consider that when selecting an image it is as if we upload it in the html content.
+ self.options.onUpload([img]);
+ }
+
return self.media;
});
},
@@ -504,6 +511,10 @@ var ImageWidget = MediaWidget.extend({
_processFile(attachments[i], error);
}
+ if (self.options.onUpload) {
+ self.options.onUpload(attachments);
+ }
+
function _processFile(attachment, error) {
var $button = self.$('.o_upload_image_button');
if (!error) {