summaryrefslogtreecommitdiff
path: root/addons/document/static
diff options
context:
space:
mode:
authorfwi-odoo <fwi@odoo.com>2017-04-25 13:55:38 +0200
committerfwi-odoo <fwi@odoo.com>2017-05-03 13:41:07 +0200
commita53335fdb2fe6e9232bde66f06e9499bece452d4 (patch)
tree662cb9a587ed2b076f0ffe4896d2e75ce120ae34 /addons/document/static
parent30384249e11d1d74b19bf2b63d4fd9fe6a99bad1 (diff)
[FIX] web, document: adapt toolbar to the new views
- Fix the toolbar behaviour since there is no more dataset in the widget. - Improve the communication beetwen the controller and the toolbar. - Move the code linked to the document upload in the right file. - Adapt the tests to mock the search_read done by ´document´ addon - Adapt the mock server to be able to give a custom toolbar action
Diffstat (limited to 'addons/document/static')
-rw-r--r--addons/document/static/src/js/document.js178
1 files changed, 164 insertions, 14 deletions
diff --git a/addons/document/static/src/js/document.js b/addons/document/static/src/js/document.js
index 05885839469..5fc69141583 100644
--- a/addons/document/static/src/js/document.js
+++ b/addons/document/static/src/js/document.js
@@ -1,33 +1,183 @@
-odoo.define('document.document', function(require) {
+odoo.define('document.document', function (require) {
"use strict";
var core = require('web.core');
+var Dialog = require('web.Dialog');
+var framework = require('web.framework');
var Sidebar = require('web.Sidebar');
var _t = core._t;
Sidebar.include({
- init : function(){
+ /**
+ * @override
+ */
+ init : function (parent, options) {
this._super.apply(this, arguments);
- var view = this.getParent();
- if (view.fields_view && view.fields_view.type === "form") {
+ if (options.viewType === "form") {
this.sections.splice(1, 0, { 'name' : 'files', 'label' : _t('Attachment(s)'), });
this.items.files = [];
}
+ this.fileuploadId = _.uniqueId('oe_fileupload');
+ $(window).on(this.fileuploadId, this._onFileUploaded.bind(this));
},
- on_attachments_loaded: function(attachments) {
+ /**
+ * Get the attachment linked to the record when the toolbar started
+ *
+ * @override
+ */
+ start: function () {
+ var _super = this._super.bind(this);
+ this._updateAttachments().then(_super);
+ },
+ /**
+ * @override
+ */
+ destroy: function () {
+ $(window).off(this.fileuploadId);
+ this._super.apply(this, arguments);
+ },
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+ /**
+ * @override
+ */
+ updateEnv: function (env) {
+ this.env = env;
+ this._updateAttachments().then(this._redraw.bind(this));
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * Process the attachments then rerender the toolbar
+ *
+ * @private
+ * @param {Object} attachments
+ */
+ _processAttachments: function (attachments) {
//to display number in name if more then one attachment which has same name.
- var self = this;
_.chain(attachments)
- .groupBy(function(attachment) { return attachment.name; })
- .each(function(attachment){
- if(attachment.length > 1)
- _.map(attachment, function(attachment, i){
- attachment.name = _.str.sprintf(_t("%s (%s)"), attachment.name, i+1);
- });
- });
- self._super(attachments);
+ .groupBy(function (attachment) { return attachment.name; })
+ .each(function (attachment) {
+ if (attachment.length > 1) {
+ _.map(attachment, function (attachment, i) {
+ attachment.name = _.str.sprintf(_t("%s (%s)"), attachment.name, i+1);
+ });
+ }
+ });
+ _.each(attachments,function (a) {
+ a.label = a.name;
+ if (a.type === "binary") {
+ a.url = '/web/content/' + a.id + '?download=true';
+ }
+ });
+ this.items.files = attachments;
+ },
+ /**
+ * @private
+ * @override
+ */
+ _redraw: function () {
+ this._super.apply(this, arguments);
+ this.$('.o_sidebar_add_attachment .o_form_binary_form')
+ .change(this._onAddAttachment.bind(this));
+ this.$('.o_sidebar_delete_attachment')
+ .click(this._onDeleteAttachment.bind(this));
},
+ /**
+ * Update the attachments to be displayed in the attachment section
+ * of the toolbar
+ *
+ * @private
+ */
+ _updateAttachments: function () {
+ var activeId = this.env.activeIds[0];
+ if (!activeId) {
+ this.items.files = [];
+ return $.when();
+ } else {
+ var domain = [
+ ['res_model', '=', this.env.model],
+ ['res_id', '=', activeId],
+ ['type', 'in', ['binary', 'url']]
+ ];
+ var fields = ['name', 'url', 'type',
+ 'create_uid', 'create_date', 'write_uid', 'write_date'];
+ return this._rpc({
+ model: 'ir.attachment',
+ method: 'search_read',
+ context: this.env.context,
+ domain: domain,
+ fields: fields,
+ }).then(this._processAttachments.bind(this));
+ }
+ },
+
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * Method triggered when user click on 'add attachment' and select a file
+ *
+ * @private
+ * @param {Event} event
+ */
+ _onAddAttachment: function (event) {
+ var $event = $(event.target);
+ if ($event.val() !== '') {
+ var $binaryForm = this.$('form.o_form_binary_form');
+ $binaryForm.submit();
+ $binaryForm.find('input[type=file]').prop('disabled', true);
+ $binaryForm.find('button').prop('disabled', true).find('img, span').toggle();
+ this.$('.o_sidebar_add_attachment a').text(_t('Uploading...'));
+ framework.blockUI();
+ }
+ },
+ /**
+ * Method triggered when user delete an attachment
+ *
+ * @private
+ * @param {Event} event
+ */
+ _onDeleteAttachment: function (event) {
+ event.preventDefault();
+ var self = this;
+ var $event = $(event.currentTarget);
+ var options = {
+ confirm_callback: function () {
+ self._rpc({
+ model: 'ir.attachment',
+ method: 'unlink',
+ args: [parseInt($event.attr('data-id'), 10)],
+ })
+ .then(self._updateAttachments.bind(self))
+ .then(self._redraw.bind(self));
+ }
+ };
+ Dialog.confirm(this, _t("Do you really want to delete this attachment ?"), options);
+ },
+ /**
+ * Handler called when the upload is done
+ *
+ * @private
+ */
+ _onFileUploaded: function () {
+ var attachments = Array.prototype.slice.call(arguments, 1);
+ var uploadErrors = _.filter(attachments, function (attachment) {
+ return attachment.error;
+ });
+ if (uploadErrors.length) {
+ this.do_warn(_t('Uploading Error'), uploadErrors[0].error);
+ }
+ this._updateAttachments().then(this._redraw.bind(this));
+ framework.unblockUI();
+ }
});
});