/*--------------------------------------------------------- * OpenERP web library *---------------------------------------------------------*/ openerp.web.views = function(instance) { var QWeb = instance.web.qweb, _t = instance.web._t; instance.web.ActionManager = instance.web.Widget.extend({ init: function(parent) { this._super(parent); this.inner_action = null; this.inner_widget = null; this.dialog = null; this.dialog_widget = null; this.breadcrumbs = []; this.on('history_back', this, function() { return this.history_back(); }); }, start: function() { this._super.apply(this, arguments); this.$element.on('click', '.oe_breadcrumb_item', this.on_breadcrumb_clicked); }, dialog_stop: function () { if (this.dialog) { this.dialog_widget.destroy(); this.dialog_widget = null; this.dialog.destroy(); this.dialog = null; } }, /** * Add a new item to the breadcrumb * * If the title of an item is an array, the multiple title mode is in use. * (eg: a widget with multiple views might need to display a title for each view) * In multiple title mode, the show() callback can check the index it receives * in order to detect which of its titles has been clicked on by the user. * * @param {Object} item breadcrumb item * @param {Object} item.widget widget containing the view(s) to be added to the breadcrumb added * @param {Function} [item.show] triggered whenever the widget should be shown back * @param {Function} [item.hide] triggered whenever the widget should be shown hidden * @param {Function} [item.destroy] triggered whenever the widget should be destroyed * @param {String|Array} [item.title] title(s) of the view(s) to be displayed in the breadcrumb * @param {Function} [item.get_title] should return the title(s) of the view(s) to be displayed in the breadcrumb */ push_breadcrumb: function(item) { var last = this.breadcrumbs.slice(-1)[0]; if (last) { last.hide(); } var item = _.extend({ show: function(index) { this.widget.$element.show(); }, hide: function() { this.widget.$element.hide(); }, destroy: function() { this.widget.destroy(); }, get_title: function() { return this.title || this.widget.get('title'); } }, item); item.id = _.uniqueId('breadcrumb_'); this.breadcrumbs.push(item); }, history_back: function() { var last = this.breadcrumbs.slice(-1)[0]; if (!last) { return false; } var title = last.get_title(); if (_.isArray(title) && title.length > 1) { return this.select_breadcrumb(this.breadcrumbs.length - 1, title.length - 2); } else if (this.breadcrumbs.length === 1) { // Only one single titled item in breadcrumb, most of the time you want to trigger back to home return false; } else { var prev = this.breadcrumbs[this.breadcrumbs.length - 2]; title = prev.get_title(); return this.select_breadcrumb(this.breadcrumbs.length - 2, _.isArray(title) ? title.length - 1 : undefined); } }, on_breadcrumb_clicked: function(ev) { var $e = $(ev.target); var id = $e.data('id'); var index; for (var i = this.breadcrumbs.length - 1; i >= 0; i--) { if (this.breadcrumbs[i].id == id) { index = i; break; } } var subindex = $e.parent().find('.oe_breadcrumb_item[data-id=' + $e.data('id') + ']').index($e); this.select_breadcrumb(index, subindex); }, select_breadcrumb: function(index, subindex) { for (var i = this.breadcrumbs.length - 1; i >= 0; i--) { if (i > index) { this.remove_breadcrumb(i); } } var item = this.breadcrumbs[index]; item.show(subindex); this.inner_widget = item.widget; return true; }, clear_breadcrumbs: function() { while (this.breadcrumbs.length) { this.remove_breadcrumb(0); } }, remove_breadcrumb: function(index) { var item = this.breadcrumbs.splice(index, 1)[0]; if (item) { var dups = _.filter(this.breadcrumbs, function(it) { return item.widget === it.widget; }); if (!dups.length) { item.destroy(); } } }, get_title: function() { var titles = []; for (var i = 0; i < this.breadcrumbs.length; i += 1) { var item = this.breadcrumbs[i]; var tit = item.get_title(); if (!_.isArray(tit)) { tit = [tit]; } for (var j = 0; j < tit.length; j += 1) { var label = _.escape(tit[j]); if (i === this.breadcrumbs.length - 1 && j === tit.length - 1) { titles.push(label); } else { titles.push(_.str.sprintf('%s', item.id, label)); } } } return titles.join(' / '); }, do_push_state: function(state) { state = state || {}; if (this.getParent() && this.getParent().do_push_state) { if (this.inner_action) { state['title'] = this.inner_action.name; if(this.inner_action.type == 'ir.actions.act_window') { state['model'] = this.inner_action.res_model; } if (this.inner_action.id) { state['action'] = this.inner_action.id; } else if (this.inner_action.type == 'ir.actions.client') { state['action'] = this.inner_action.tag; //state = _.extend(this.inner_action.params || {}, state); } } if(!this.dialog) { this.getParent().do_push_state(state); } } }, do_load_state: function(state, warm) { var self = this, action_loaded; if (state.action) { if (_.isString(state.action) && instance.web.client_actions.contains(state.action)) { var action_client = {type: "ir.actions.client", tag: state.action, params: state}; this.null_action(); action_loaded = this.do_action(action_client); } else { var run_action = (!this.inner_widget || !this.inner_widget.action) || this.inner_widget.action.id !== state.action; if (run_action) { this.null_action(); action_loaded = this.do_action(state.action); instance.webclient.menu.has_been_loaded.then(function() { instance.webclient.menu.open_action(state.action); }); } } } else if (state.model && state.id) { // TODO handle context & domain ? this.null_action(); var action = { res_model: state.model, res_id: state.id, type: 'ir.actions.act_window', views: [[false, 'form']] }; action_loaded = this.do_action(action); } else if (state.sa) { // load session action this.null_action(); action_loaded = this.rpc('/web/session/get_session_action', {key: state.sa}).pipe(function(action) { if (action) { return self.do_action(action); } }); } $.when(action_loaded || null).then(function() { if (self.inner_widget && self.inner_widget.do_load_state) { self.inner_widget.do_load_state(state, warm); } }); }, do_action: function(action, on_close) { if (_.isString(action) && instance.web.client_actions.contains(action)) { var action_client = { type: "ir.actions.client", tag: action }; return this.do_action(action_client); } else if (_.isNumber(action) || _.isString(action)) { var self = this; return self.rpc("/web/action/load", { action_id: action }, function(result) { self.do_action(result.result, on_close); }); } if (!action.type) { console.error("No type for action", action); return null; } var type = action.type.replace(/\./g,'_'); var popup = action.target === 'new'; var inline = action.target === 'inline' || action.target === 'inlineview'; action.flags = _.extend({ views_switcher : !popup && !inline, search_view : !popup && !inline, action_buttons : !popup && !inline, sidebar : !popup && !inline, pager : !popup && !inline, display_title : !popup }, action.flags || {}); if (!(type in this)) { console.error("Action manager can't handle action of type " + action.type, action); return null; } return this[type](action, on_close); }, null_action: function() { this.dialog_stop(); this.clear_breadcrumbs(); }, ir_actions_common: function(action, on_close) { var self = this, klass, widget, post_process; if (action.type === 'ir.actions.client') { var ClientWidget = instance.web.client_actions.get_object(action.tag); widget = new ClientWidget(this, action.params); klass = 'oe_act_client'; post_process = function() { self.push_breadcrumb({ widget: widget, title: action.name }); if (action.tag !== 'reload') { self.do_push_state({}); } }; } else { widget = new instance.web.ViewManagerAction(this, action); klass = 'oe_act_window'; post_process = widget.proxy('add_breadcrumb'); } if (action.target === 'new') { if (this.dialog === null) { // These buttons will be overwrited by