odoo.define('web.Sidebar', function (require) { "use strict"; var core = require('web.core'); var data = require('web.data'); var Dialog = require('web.Dialog'); var framework = require('web.framework'); var pyeval = require('web.pyeval'); var session = require('web.session'); var Widget = require('web.Widget'); var QWeb = core.qweb; var _t = core._t; var Sidebar = Widget.extend({ init: function(parent) { var self = this; this._super(parent); this.sections = [ { 'name' : 'print', 'label' : _t('Print'), }, { 'name' : 'other', 'label' : _t('More'), } ]; this.items = { 'print' : [], 'other' : [] }; this.fileupload_id = _.uniqueId('oe_fileupload'); $(window).on(this.fileupload_id, function() { var args = [].slice.call(arguments).slice(1); self.do_attachement_update(self.dataset, self.model_id,args); framework.unblockUI(); }); }, start: function() { var self = this; this._super(this); this.redraw(); this.$el.on('click','.dropdown-menu li a', function(event) { var section = $(this).data('section'); var index = $(this).data('index'); var item = self.items[section][index]; if (item.callback) { item.callback.apply(self, [item]); } else if (item.action) { self.on_item_action_clicked(item); } else if (item.url) { return true; } event.preventDefault(); }); }, redraw: function() { var self = this; self.$el.html(QWeb.render('Sidebar', {widget: self})); // Hides Sidebar sections when item list is empty this.$('.oe_form_dropdown_section').each(function() { $(this).toggle(!!$(this).find('li').length); }); self.$("[title]").tooltip({ delay: { show: 500, hide: 0} }); }, /** * For each item added to the section: * * ``label`` * will be used as the item's name in the sidebar, can be html * * ``action`` * descriptor for the action which will be executed, ``action`` and * ``callback`` should be exclusive * * ``callback`` * function to call when the item is clicked in the sidebar, called * with the item descriptor as its first argument (so information * can be stored as additional keys on the object passed to * ``add_items``) * * ``classname`` (optional) * ``@class`` set on the sidebar serialization of the item * * ``title`` (optional) * will be set as the item's ``@title`` (tooltip) * * @param {String} section_code * @param {Array<{label, action | callback[, classname][, title]}>} items */ add_items: function(section_code, items) { if (items) { this.items[section_code].unshift.apply(this.items[section_code],items); this.redraw(); } }, add_toolbar: function(toolbar) { var self = this; _.each(['print','action','relate'], function(type) { var items = toolbar[type]; if (items) { for (var i = 0; i < items.length; i++) { items[i] = { label: items[i]['name'], action: items[i], classname: 'oe_sidebar_' + type }; } self.add_items(type=='print' ? 'print' : 'other', items); } }); }, on_item_action_clicked: function(item) { var self = this; self.getParent().sidebar_eval_context().done(function (sidebar_eval_context) { var ids = self.getParent().get_selected_ids(); var domain; if (self.getParent().get_active_domain) { domain = self.getParent().get_active_domain(); } else { domain = $.Deferred().resolve(undefined); } if (ids.length === 0) { new Dialog(this, { title: _t("Warning"), size: 'medium',}, $("
").text(_t("You must choose at least one record."))).open(); return false; } var dataset = self.getParent().dataset; var active_ids_context = { active_id: ids[0], active_ids: ids, active_model: dataset.model, }; $.when(domain).done(function (domain) { if (domain !== undefined) { active_ids_context.active_domain = domain; } var c = pyeval.eval('context', new data.CompoundContext( sidebar_eval_context, active_ids_context)); self.rpc("/web/action/load", { action_id: item.action.id, context: new data.CompoundContext( dataset.get_context(), active_ids_context).eval() }).done(function(result) { result.context = new data.CompoundContext( result.context || {}, active_ids_context) .set_eval_context(c); result.flags = result.flags || {}; result.flags.new_window = true; self.do_action(result, { on_close: function() { // reload view self.getParent().reload(); }, }); }); }); }); }, do_attachement_update: function(dataset, model_id, args) { this.dataset = dataset; this.model_id = model_id; if (args && args[0].error) { this.do_warn(_t('Uploading Error'), args[0].error); } 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 data.DataSetSearch(this, 'ir.attachment', dataset.get_context(), dom); ds.read_slice(['name', 'url', 'type', 'create_uid', 'create_date', 'write_uid', 'write_date'], {}).done(this.on_attachments_loaded); } }, on_attachments_loaded: function(attachments) { var self = this; var prefix = session.url('/web/binary/saveas', {model: 'ir.attachment', field: 'datas', filename_field: 'name'}); _.each(attachments,function(a) { a.label = a.name; if(a.type === "binary") { a.url = prefix + '&id=' + a.id + '&t=' + (new Date().getTime()); } }); self.items.files = attachments; self.redraw(); this.$('.oe_sidebar_add_attachment .oe_form_binary_file').change(this.on_attachment_changed); this.$el.find('.oe_sidebar_delete_item').click(this.on_attachment_delete); }, on_attachment_changed: function(e) { var $e = $(e.target); if ($e.val() !== '') { this.$el.find('form.oe_form_binary_form').submit(); $e.parent().find('input[type=file]').prop('disabled', true); $e.parent().find('button').prop('disabled', true).find('img, span').toggle(); this.$('.oe_sidebar_add_attachment a').text(_t('Uploading...')); framework.blockUI(); } }, on_attachment_delete: function(e) { e.preventDefault(); e.stopPropagation(); var self = this; var $e = $(e.currentTarget); if (confirm(_t("Do you really want to delete this attachment ?"))) { (new data.DataSet(this, 'ir.attachment')).unlink([parseInt($e.attr('data-id'), 10)]).done(function() { self.do_attachement_update(self.dataset, self.model_id); }); } } }); return Sidebar; });