/*--------------------------------------------------------- * 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 = []; }, 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 * * @param {Object} item breadcrumb item * @param {Object} item.widget widget containing the view(s) to be added to the breadcrumb added * @param {null|Function} item.show callback triggered whenever the widget should be shown back * @param {null|Function} item.hide callback triggered whenever the widget should be shown hidden * @param {null|Function} item.destroy callback triggered whenever the widget should be destroyed * @param {null|String|Array} item.title title(s) of the view(s) to be displayed in the breadcrumb * @param {null|Function} item.get_title function returning 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(); } item.id = _.uniqueId('breadcrumb_'); _.defaults(item, { show: function(index) { // If multiple titles are used, the index can be used in order // to define which item of the breadcrumb has been trigerred. item.widget.$element.show(); }, hide: function() { // Called on the last pushed item before pusing a new breadcrumb. item.widget.$element.hide(); }, destroy: function() { // Called on all breadcrumbs items when it should be cleared. // Eg: new action triggered from menu item.widget.destroy(); }, get_title: function() { // Should return the title(s) of the widget as a string. // If an array is returned, multiple titles can be used // (eg: a widget switching views). The #show() callback will receive // the index of the selected title. return item.title || item.widget.get('title'); } }); this.breadcrumbs.push(item); }, on_breadcrumb_clicked: function(ev) { var $e = $(ev.target); var id = $e.data('id'); var item; for (var i = this.breadcrumbs.length - 1; i >= 0; i--) { var it = this.breadcrumbs[i]; if (it.id == id) { item = it; break; } this.remove_breadcrumb(i); } var index = $e.parent().find('.oe_breadcrumb_item[data-id=' + $e.data('id') + ']').index($e); item.show(index, $e); this.inner_widget = item.widget; }, 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) { if (this.getParent() && this.getParent().do_push_state) { if (this.inner_action) { state['title'] = this.inner_action.name; state['model'] = this.inner_action.res_model; if (this.inner_action.id) { state['action_id'] = this.inner_action.id; } } this.getParent().do_push_state(state); } }, do_load_state: function(state, warm) { var self = this, action_loaded; if (state.action_id) { var run_action = (!this.inner_widget || !this.inner_widget.action) || this.inner_widget.action.id !== state.action_id; if (run_action) { this.null_action(); action_loaded = this.do_action(state.action_id); instance.webclient.menu.has_been_loaded.then(function() { instance.webclient.menu.open_action(state.action_id); }); } } 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 var self = this; 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); } }); } else if (state.client_action) { this.null_action(); var action = state.client_action; if(_.isString(action)) { action = { tag: action, params: state, }; } this.ir_actions_client(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 (_.isNumber(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; } var type = action.type.replace(/\./g,'_'); var popup = action.target === 'new'; var inline = action.target === 'inline'; 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; } return this[type](action, on_close); }, null_action: function() { this.dialog_stop(); this.clear_breadcrumbs(); }, ir_actions_act_window: function (action, on_close) { var self = this; if (_(['base.module.upgrade', 'base.setup.installer']) .contains(action.res_model)) { var old_close = on_close; on_close = function () { instance.webclient.do_reload().then(old_close); }; } if (action.target === 'new') { if (this.dialog === null) { this.dialog = new instance.web.Dialog(this, { width: '80%' }); if(on_close) this.dialog.on_close.add(on_close); } else { this.dialog_widget.destroy(); } this.dialog.dialog_title = action.name; this.dialog_widget = new instance.web.ViewManagerAction(this.dialog, action); this.dialog_widget.appendTo(this.dialog.$element); this.dialog.open(); } else { this.dialog_stop(); if(action.menu_id) { return this.getParent().do_action(action, function () { instance.webclient.menu.open_menu(action.menu_id); }); } this.inner_action = action; var inner_widget = this.inner_widget = new instance.web.ViewManagerAction(this, action); inner_widget.add_breadcrumb(); this.inner_widget.appendTo(this.$element); } }, ir_actions_act_window_close: function (action, on_closed) { if (!this.dialog && on_closed) { on_closed(); } this.dialog_stop(); }, ir_actions_server: function (action, on_closed) { var self = this; this.rpc('/web/action/run', { action_id: action.id, context: action.context || {} }).then(function (action) { self.do_action(action, on_closed) }); }, ir_actions_client: function (action) { this.dialog_stop(); var ClientWidget = instance.web.client_actions.get_object(action.tag); this.inner_widget = new ClientWidget(this, action.params); this.push_breadcrumb({ widget: this.inner_widget, title: action.name }); this.inner_widget.appendTo(this.$element); }, ir_actions_report_xml: function(action, on_closed) { var self = this; $.blockUI(); self.rpc("/web/session/eval_domain_and_context", { contexts: [action.context], domains: [] }).then(function(res) { action = _.clone(action); action.context = res.context; self.session.get_file({ url: '/web/report', data: {action: JSON.stringify(action)}, complete: $.unblockUI, success: function(){ if (!self.dialog && on_closed) { on_closed(); } self.dialog_stop(); }, error: instance.webclient.crashmanager.on_rpc_error }) }); }, ir_actions_act_url: function (action) { window.open(action.url, action.target === 'self' ? '_self' : '_blank'); }, ir_ui_menu: function (action) { this.getParent().do_action(action); } }); instance.web.ViewManager = instance.web.Widget.extend({ template: "ViewManager", init: function(parent, dataset, views, flags) { this._super(parent); this.model = dataset ? dataset.model : undefined; this.dataset = dataset; this.searchview = null; this.active_view = null; this.views_src = _.map(views, function(x) { if (x instanceof Array) { var View = instance.web.views.get_object(x[1], true); return { view_id: x[0], view_type: x[1], label: View ? View.prototype.display_name : (void 'nope') }; } else { return x; } }); this.views = {}; this.flags = flags || {}; this.registry = instance.web.views; this.views_history = []; }, /** * @returns {jQuery.Deferred} initial view loading promise */ start: function() { this._super(); var self = this; this.$element.find('.oe_view_manager_switch a').click(function() { self.on_mode_switch($(this).data('view-type')); }).tipsy(); var views_ids = {}; _.each(this.views_src, function(view) { self.views[view.view_type] = $.extend({}, view, { deferred : $.Deferred(), controller : null, options : _.extend({ $buttons : self.$element.find('.oe_view_manager_buttons'), $sidebar : self.flags.sidebar ? self.$element.find('.oe_view_manager_sidebar') : undefined, $pager : self.$element.find('.oe_view_manager_pager'), action : self.action, action_views_ids : views_ids }, self.flags, self.flags[view.view_type] || {}, view.options || {}) }); views_ids[view.view_type] = view.view_id; }); if (this.flags.views_switcher === false) { this.$element.find('.oe_view_manager_switch').hide(); } // If no default view defined, switch to the first one in sequence var default_view = this.flags.default_view || this.views_src[0].view_type; return this.on_mode_switch(default_view); }, /** * Asks the view manager to switch visualization mode. * * @param {String} view_type type of view to display * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack * @returns {jQuery.Deferred} new view loading promise */ on_mode_switch: function(view_type, no_store, view_options) { var self = this; var view = this.views[view_type]; var view_promise; if(!view) return $.Deferred().reject(); if (!no_store) { this.views_history.push(view_type); } this.active_view = view_type; if (!view.controller) { view_promise = this.do_create_view(view_type); } else if (this.searchview && self.flags.auto_search && view.controller.searchable !== false) { this.searchview.ready.then(this.searchview.do_search); } if (this.searchview) { this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show'](); } this.$element .find('.oe_view_manager_switch a').parent().removeClass('active'); this.$element .find('.oe_view_manager_switch a').filter('[data-view-type="' + view_type + '"]') .parent().addClass('active'); $.when(view_promise).then(function () { _.each(_.keys(self.views), function(view_name) { var controller = self.views[view_name].controller; if (controller) { var container = self.$element.find(".oe_view_manager_view_" + view_name + ":first"); if (view_name === view_type) { container.show(); controller.do_show(view_options || {}); } else { container.hide(); controller.do_hide(); } } }); }); return view_promise; }, do_create_view: function(view_type) { // Lazy loading of views var self = this; var view = this.views[view_type]; var controllerclass = this.registry.get_object(view_type); var options = _.clone(view.options); if (view_type === "form" && this.action) { switch (this.action.target) { case 'new': case 'inline': options.initial_mode = 'edit'; break; } } var controller = new controllerclass(this, this.dataset, view.view_id, options); controller.on("change:title", this, function() { if (self.active_view === view_type) { self.set_title(controller.get('title')); } }); if (view.embedded_view) { controller.set_embedded_view(view.embedded_view); } controller.do_switch_view.add_last(_.bind(this.switch_view, this)); controller.do_prev_view.add_last(this.on_prev_view); var container = this.$element.find(".oe_view_manager_view_" + view_type); var view_promise = controller.appendTo(container); this.views[view_type].controller = controller; this.views[view_type].deferred.resolve(view_type); return $.when(view_promise).then(function() { self.on_controller_inited(view_type, controller); if (self.searchview && self.flags.auto_search && view.controller.searchable !== false) { self.searchview.ready.then(self.searchview.do_search); } }); }, set_title: function(title) { this.$element.find('.oe_view_title_text:first').text(title); }, add_breadcrumb: function() { var self = this; var views = [this.active_view || this.views_src[0].view_type]; this.on_mode_switch.add(function(mode) { var last = views.slice(-1)[0]; if (mode !== last) { if (mode !== 'form') { views.length = 0; } views.push(mode); } }); this.getParent().push_breadcrumb({ widget: this, show: function(index, $e) { var view_to_select = views[index]; self.$element.show(); if (self.active_view !== view_to_select) { self.on_mode_switch(view_to_select); } }, get_title: function() { return _.map(views, function(v) { return self.views[v].controller.get('title'); }); } }); }, /** * Method used internally when a view asks to switch view. This method is meant * to be extended by child classes to change the default behavior, which simply * consist to switch to the asked view. */ switch_view: function(view_type, no_store, options) { return this.on_mode_switch(view_type, no_store, options); }, /** * Returns to the view preceding the caller view in this manager's * navigation history (the navigation history is appended to via * on_mode_switch) * * @param {Object} [options] * @param {Boolean} [options.created=false] resource was created * @param {String} [options.default=null] view to switch to if no previous view * @returns {$.Deferred} switching end signal */ on_prev_view: function (options) { options = options || {}; var current_view = this.views_history.pop(); var previous_view = this.views_history[this.views_history.length - 1] || options['default']; if (options.created && current_view === 'form' && previous_view === 'list') { // APR special case: "If creation mode from list (and only from a list), // after saving, go to page view (don't come back in list)" return this.on_mode_switch('form'); } else if (options.created && !previous_view && this.action && this.action.flags.default_view === 'form') { // APR special case: "If creation from dashboard, we have no previous view return this.on_mode_switch('form'); } return this.on_mode_switch(previous_view, true); }, /** * Sets up the current viewmanager's search view. * * @param {Number|false} view_id the view to use or false for a default one * @returns {jQuery.Deferred} search view startup deferred */ setup_search_view: function(view_id, search_defaults) { var self = this; if (this.searchview) { this.searchview.destroy(); } this.searchview = new instance.web.SearchView(this, this.dataset, view_id, search_defaults, this.flags.search_view === false); this.searchview.on_search.add(this.do_searchview_search); return this.searchview.appendTo(this.$element.find(".oe_view_manager_view_search")); }, do_searchview_search: function(domains, contexts, groupbys) { var self = this, controller = this.views[this.active_view].controller, action_context = this.action.context || {}; this.rpc('/web/session/eval_domain_and_context', { domains: [this.action.domain || []].concat(domains || []), contexts: [action_context].concat(contexts || []), group_by_seq: groupbys || [] }, function (results) { self.dataset._model = new instance.web.Model( self.dataset.model, results.context, results.domain); var groupby = results.group_by.length ? results.group_by : action_context.group_by; if (_.isString(groupby)) { groupby = [groupby]; } controller.do_search(results.domain, results.context, groupby || []); }); }, /** * Event launched when a controller has been inited. * * @param {String} view_type type of view * @param {String} view the inited controller */ on_controller_inited: function(view_type, view) { }, /** * Called when one of the view want to execute an action */ on_action: function(action) { }, on_create: function() { }, on_remove: function() { }, on_edit: function() { }, /** * Called by children view after executing an action */ on_action_executed: function () { }, }); instance.web.ViewManagerAction = instance.web.ViewManager.extend({ template:"ViewManagerAction", /** * @constructs instance.web.ViewManagerAction * @extends instance.web.ViewManager * * @param {instance.web.ActionManager} parent parent object/widget * @param {Object} action descriptor for the action this viewmanager needs to manage its views. */ init: function(parent, action) { // dataset initialization will take the session from ``this``, so if we // do not have it yet (and we don't, because we've not called our own // ``_super()``) rpc requests will blow up. var flags = action.flags || {}; if (!('auto_search' in flags)) { flags.auto_search = action.auto_search !== false; } if (action.res_model == 'board.board' && action.view_mode === 'form') { // Special case for Dashboards _.extend(flags, { views_switcher : false, display_title : false, search_view : false, pager : false, sidebar : false, action_buttons : false }); } this._super(parent, null, action.views, flags); this.session = parent.session; this.action = action; var dataset = new instance.web.DataSetSearch(this, action.res_model, action.context, action.domain); if (action.res_id) { dataset.ids.push(action.res_id); dataset.index = 0; } this.dataset = dataset; // setup storage for session-wise menu hiding if (this.session.hidden_menutips) { return; } this.session.hidden_menutips = {}; }, /** * Initializes the ViewManagerAction: sets up the searchview (if the * searchview is enabled in the manager's action flags), calls into the * parent to initialize the primary view and (if the VMA has a searchview) * launches an initial search after both views are done rendering. */ start: function() { var self = this, searchview_loaded, search_defaults = {}; _.each(this.action.context, function (value, key) { var match = /^search_default_(.*)$/.exec(key); if (match) { search_defaults[match[1]] = value; } }); // init search view var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0]; searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults); var main_view_loaded = this._super(); var manager_ready = $.when(searchview_loaded, main_view_loaded); this.$element.find('.oe_debug_view').change(this.on_debug_changed); this.$element.addClass("oe_view_manager_" + (this.action.target || 'current')); if (this.action.help && !this.flags.low_profile) { var Users = new instance.web.DataSet(self, 'res.users'), $tips = this.$element.find('.oe_view_manager_menu_tips'); $tips.delegate('blockquote button', 'click', function() { var $this = $(this); //noinspection FallthroughInSwitchStatementJS switch ($this.attr('name')) { case 'disable': Users.write(self.session.uid, {menu_tips:false}); case 'hide': $this.closest('blockquote').hide(); self.session.hidden_menutips[self.action.id] = true; } }); if (!(self.action.id in self.session.hidden_menutips)) { Users.read_ids([this.session.uid], ['menu_tips']).then(function(users) { var user = users[0]; if (!(user && user.id === self.session.uid)) { return; } $tips.find('blockquote').toggle(user.menu_tips); }); } } return manager_ready; }, on_debug_changed: function (evt) { var self = this, $sel = $(evt.currentTarget), $option = $sel.find('option:selected'), val = $sel.val(), current_view = this.views[this.active_view].controller; switch (val) { case 'fvg': var dialog = new instance.web.Dialog(this, { title: _t("Fields View Get"), width: '95%' }).open(); $('
').text(instance.web.json_node_to_xml(current_view.fields_view.arch, true)).appendTo(dialog.$element); break; case 'perm_read': var ids = current_view.get_selected_ids(); if (ids.length === 1) { this.dataset.call('perm_read', [ids]).then(function(result) { var dialog = new instance.web.Dialog(this, { title: _.str.sprintf(_t("View Log (%s)"), self.dataset.model), width: 400 }, QWeb.render('ViewManagerDebugViewLog', { perm : result[0], format : instance.web.format_value })).open(); }); } break; case 'toggle_layout_outline': current_view.rendering_engine.toggle_layout_debugging(); break; case 'fields': this.dataset.call_and_eval( 'fields_get', [false, {}], null, 1).then(function (fields) { var $root = $('