diff --git a/addons/web/static/src/js/view_form.js b/addons/web/static/src/js/view_form.js index 5b63ec3506d..a3f0d82bb63 100644 --- a/addons/web/static/src/js/view_form.js +++ b/addons/web/static/src/js/view_form.js @@ -112,7 +112,6 @@ openerp.web.FormView = openerp.web.View.extend({ if (!this.sidebar && this.options.sidebar) { this.sidebar = new openerp.web.Sidebar(this); this.sidebar.appendTo(this.$sidebar); - //this.sidebar.attachments = new openerp.web.form.SidebarAttachments(this.sidebar, this); this.sidebar.add_toolbar(this.fields_view.toolbar); this.sidebar.add_items('other', [{ label: _t('Set Default'), @@ -269,7 +268,7 @@ openerp.web.FormView = openerp.web.View.extend({ self.is_initialized.resolve(); self.do_update_pager(record.id == null); if (self.sidebar) { - // self.sidebar.attachments.do_update(); + self.sidebar.do_attachement_update(self.dataset, self.datarecord.id); } if (self.default_focus_field) { self.default_focus_field.focus(); @@ -697,12 +696,12 @@ openerp.web.FormView = openerp.web.View.extend({ this.dataset.alter_ids(this.dataset.ids.concat([this.datarecord.id])); this.dataset.index = this.dataset.ids.length - 1; } else { - this.dataset.alter_ids([this.datarecord.id].concat(this.dataset.ids)); + this.dataset.alter_ids([this.datarecord.id].concat(this.dataset.ids)); this.dataset.index = 0; } this.do_update_pager(); if (this.sidebar) { - // this.sidebar.attachments.do_update(); + this.sidebar.do_attachement_update(this.dataset, this.datarecord.id); } //openerp.log("The record has been created with id #" + this.datarecord.id); this.reload(); @@ -1221,62 +1220,6 @@ openerp.web.form.FormDialog = openerp.web.Dialog.extend({ } }); - -openerp.web.form.SidebarAttachments = openerp.web.Widget.extend({ - init: function(parent, form_view) { - //var $section = parent.add_section(_t('Attachments'), 'attachments'); - //this.$div = $('
'); - //$section.append(this.$div); - this._super(parent); - this.view = form_view; - }, - do_update: function() { - return; - if (!this.view.datarecord.id) { - this.on_attachments_loaded([]); - } else { - (new openerp.web.DataSetSearch( - this, 'ir.attachment', this.view.dataset.get_context(), - [ - ['res_model', '=', this.view.dataset.model], - ['res_id', '=', this.view.datarecord.id], - ['type', 'in', ['binary', 'url']] - ])).read_slice(['name', 'url', 'type'], {}).then(this.on_attachments_loaded); - } - }, - on_attachments_loaded: function(attachments) { - return; - this.attachments = attachments; - this.$div.html(QWeb.render('FormView.sidebar.attachments', this)); - this.$element.find('.oe-binary-file').change(this.on_attachment_changed); - this.$element.find('.oe-sidebar-attachment-delete').click(this.on_attachment_delete); - }, - on_attachment_changed: function(e) { - return; - window[this.element_id + '_iframe'] = this.do_update; - var $e = $(e.target); - if ($e.val() != '') { - this.$element.find('form.oe-binary-form').submit(); - $e.parent().find('input[type=file]').prop('disabled', true); - $e.parent().find('button').prop('disabled', true).find('img, span').toggle(); - } - }, - on_attachment_delete: function(e) { - return; - var self = this, $e = $(e.currentTarget); - var name = _.str.trim($e.parent().find('a.oe-sidebar-attachments-link').text()); - if (confirm(_.str.sprintf(_t("Do you really want to delete the attachment %s?"), name))) { - this.rpc('/web/dataset/unlink', { - model: 'ir.attachment', - ids: [parseInt($e.attr('data-id'))] - }, function(r) { - $e.parent().remove(); - self.do_notify("Delete an attachment", "The attachment '" + name + "' has been deleted"); - }); - } - } -}); - openerp.web.form.compute_domain = function(expr, fields) { var stack = []; for (var i = expr.length - 1; i >= 0; i--) { diff --git a/addons/web/static/src/js/views.js b/addons/web/static/src/js/views.js index 78f365c1a16..55e87c06904 100644 --- a/addons/web/static/src/js/views.js +++ b/addons/web/static/src/js/views.js @@ -727,8 +727,6 @@ session.web.Sidebar = session.web.Widget.extend({ init: function(parent) { this._super(parent); var view = this.getParent(); - var view_manager = view.getParent(); - var action = view_manager.action; this.sections = [ { 'name' : 'print', 'label' : _t('Print'), }, { 'name' : 'files', 'label' : _t('Attachement'), }, @@ -763,21 +761,22 @@ session.web.Sidebar = session.web.Widget.extend({ var item = self.items[section][index]; if (item.callback) { item.callback.apply(self, [item]); - } - if (item.action) { + } else if (item.action) { self.on_item_action_clicked(item); + } else if (item.url) { + return true; } return false; }); + //this.$div.html(QWeb.render('FormView.sidebar.attachments', this)); + //this.$element.find('.oe-binary-file').change(this.on_attachment_changed); + //this.$element.find('.oe-sidebar-attachment-delete').click(this.on_attachment_delete); }, redraw: function() { var self = this; self.$element.html(QWeb.render('Sidebar', {widget: self})); this.$element.find('ul').hide(); }, - add_default_sections: function() { - var self = this; - }, add_section: function() { var self = this; }, @@ -857,6 +856,52 @@ session.web.Sidebar = session.web.Widget.extend({ }); }); }, + do_attachement_update: function(dataset, model_id) { + if (!model_id) { + this.on_attachments_loaded([]); + } else { + var dom = [ ['res_model', '=', dataset.model], ['res_id', '=', model_id], ['type', 'in', ['binary', 'url']] ]; + var ds = new session.web.DataSetSearch(this, 'ir.attachment', dataset.get_context(), dom); + ds.read_slice(['name', 'url', 'type'], {}).then(this.on_attachments_loaded); + } + }, + on_attachments_loaded: function(attachments) { + var self = this; + var items = []; + // TODO: preprend: _s + + var prefix = '/web/binary/saveas?session_id=' + self.session.session_id + '&model=ir.attachment&field=datas&filename_field=name&id='; + _.each(attachments,function(a) { + a.label = a.name; + if(a.type === "binary") { + a.url = prefix + a.id + '&t=' + (new Date().getTime()); + } + }); + this.add_items('files', attachments); + }, + on_attachment_changed: function(e) { + return; + window[this.element_id + '_iframe'] = this.do_update; + var $e = $(e.target); + if ($e.val() != '') { + this.$element.find('form.oe-binary-form').submit(); + $e.parent().find('input[type=file]').prop('disabled', true); + $e.parent().find('button').prop('disabled', true).find('img, span').toggle(); + } + }, + on_attachment_delete: function(e) { + return; + var self = this, $e = $(e.currentTarget); + var name = _.str.trim($e.parent().find('a.oe-sidebar-attachments-link').text()); + if (confirm(_.str.sprintf(_t("Do you really want to delete the attachment %s?"), name))) { + this.rpc('/web/dataset/unlink', { + model: 'ir.attachment', + ids: [parseInt($e.attr('data-id'))] + }, function(r) { + $e.parent().remove(); + self.do_notify("Delete an attachment", "The attachment '" + name + "' has been deleted"); + }); + } + } }); session.web.TranslateDialog = session.web.Dialog.extend({ @@ -1147,9 +1192,6 @@ session.web.View = session.web.Widget.extend({ }, do_search: function(view) { }, - set_common_sidebar_sections: function(sidebar) { - sidebar.add_default_sections(); - }, on_sidebar_import: function() { var import_view = new session.web.DataImport(this, this.dataset); import_view.start(); diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index fdf5a5a7b5f..0df4ca71777 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -515,7 +515,7 @@