1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.web.views = function(session) {
7 var _t = session.web._t;
10 * Registry for all the client actions key: tag value: widget
12 session.web.client_actions = new session.web.Registry();
15 * Registry for all the main views
17 session.web.views = new session.web.Registry();
19 session.web.ActionManager = session.web.Widget.extend({
20 identifier_prefix: "actionmanager",
21 init: function(parent) {
23 this.inner_viewmanager = null;
25 this.dialog_viewmanager = null;
26 this.client_widget = null;
27 this.client_widget_name = null;
30 return "<div id='"+this.element_id+"'></div>";
32 dialog_stop: function () {
34 this.dialog_viewmanager.stop();
35 this.dialog_viewmanager = null;
40 content_stop: function () {
41 if (this.inner_viewmanager) {
42 this.inner_viewmanager.stop();
43 this.inner_viewmanager = null;
45 if (this.client_widget) {
46 this.client_widget.stop();
47 this.client_widget = null;
48 this.client_widget_name = null;
52 do_push_state: function(state, overwrite) {
55 do_load_state: function(state) {
56 if (state.action_id) {
58 this.do_action(state.action_id);
60 else if (state.model && state.id) {
65 else if (state.client_action) {
67 this.ir_actions_client(state.client_action);
70 if (this.inner_viewmanager) {
71 this.inner_viewmanager.do_load_state(state);
75 do_action: function(action, on_close) {
76 if (_.isNumber(action)) {
78 self.rpc("/web/action/load", { action_id: action }, function(result) {
79 self.do_action(result.result, on_close);
84 console.error("No type for action", action);
87 var type = action.type.replace(/\./g,'_');
88 var popup = action.target === 'new';
89 action.flags = _.extend({
90 views_switcher : !popup,
92 action_buttons : !popup,
95 }, action.flags || {});
96 if (!(type in this)) {
97 console.error("Action manager can't handle action of type " + action.type, action);
100 return this[type](action, on_close);
102 null_action: function() {
106 ir_actions_act_window: function (action, on_close) {
107 if (_(['base.module.upgrade', 'base.setup.installer'])
108 .contains(action.res_model)) {
109 var old_close = on_close;
110 on_close = function () {
111 session.webclient.do_reload();
112 if (old_close) { old_close(); }
115 if (action.target === 'new') {
116 if (this.dialog == null) {
117 this.dialog = new session.web.Dialog(this, { title: action.name, width: '80%' });
119 this.dialog.on_close.add(on_close);
122 this.dialog_viewmanager.stop();
124 this.dialog_viewmanager = new session.web.ViewManagerAction(this, action);
125 this.dialog_viewmanager.appendTo(this.dialog.$element);
130 this.inner_viewmanager = new session.web.ViewManagerAction(this, action);
131 this.inner_viewmanager.do_push_state.add(this.do_push_state);
132 this.inner_viewmanager.appendTo(this.$element);
134 this.do_push_state({action_id: action.id}, true);
138 ir_actions_act_window_close: function (action, on_closed) {
139 if (!this.dialog && on_closed) {
144 ir_actions_server: function (action, on_closed) {
146 this.rpc('/web/action/run', {
147 action_id: action.id,
148 context: action.context || {}
149 }).then(function (action) {
150 self.do_action(action, on_closed)
153 ir_actions_client: function (action) {
155 this.client_widget_name = action.tag;
156 var ClientWidget = session.web.client_actions.get_object(action.tag);
157 (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
159 var client_action = {tag: action.tag};
160 if (action.params) _.extend(client_action, {params: action.params});
161 this.do_push_state({client_action: client_action}, true);
163 ir_actions_report_xml: function(action, on_closed) {
166 self.rpc("/web/session/eval_domain_and_context", {
167 contexts: [action.context],
169 }).then(function(res) {
170 action = _.clone(action);
171 action.context = res.context;
172 self.session.get_file({
174 data: {action: JSON.stringify(action)},
175 complete: $.unblockUI,
177 if (!self.dialog && on_closed) {
185 ir_actions_act_url: function (action) {
186 window.open(action.url, action.target === 'self' ? '_self' : '_blank');
190 session.web.ViewManager = session.web.Widget.extend(/** @lends session.web.ViewManager# */{
191 identifier_prefix: "viewmanager",
192 template: "ViewManager",
194 * @constructs session.web.ViewManager
195 * @extends session.web.Widget
201 init: function(parent, dataset, views) {
203 this.model = dataset ? dataset.model : undefined;
204 this.dataset = dataset;
205 this.searchview = null;
206 this.active_view = null;
207 this.views_src = _.map(views, function(x) {return x instanceof Array? {view_id: x[0], view_type: x[1]} : x;});
209 this.flags = this.flags || {};
210 this.registry = session.web.views;
211 this.views_history = [];
214 return session.web.qweb.render(this.template, {
216 prefix: this.element_id,
217 views: this.views_src});
220 * @returns {jQuery.Deferred} initial view loading promise
225 this.$element.find('.oe_vm_switch button').click(function() {
226 self.on_mode_switch($(this).data('view-type'));
229 _.each(this.views_src, function(view) {
230 self.views[view.view_type] = $.extend({}, view, {
231 deferred : $.Deferred(),
234 sidebar_id : self.element_id + '_sidebar_' + view.view_type,
235 action : self.action,
236 action_views_ids : views_ids
237 }, self.flags, self.flags[view.view_type] || {}, view.options || {})
239 views_ids[view.view_type] = view.view_id;
241 if (this.flags.views_switcher === false) {
242 this.$element.find('.oe_vm_switch').hide();
244 // switch to the first one in sequence
245 return this.on_mode_switch(this.views_src[0].view_type);
248 * Asks the view manager to switch visualization mode.
250 * @param {String} view_type type of view to display
251 * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack
252 * @returns {jQuery.Deferred} new view loading promise
254 on_mode_switch: function(view_type, no_store) {
256 view = this.views[view_type],
259 return $.Deferred().reject();
262 this.views_history.push(view_type);
264 this.active_view = view_type;
266 if (!view.controller) {
267 // Lazy loading of views
268 var controllerclass = this.registry.get_object(view_type);
269 var controller = new controllerclass(this, this.dataset, view.view_id, view.options);
270 if (view.embedded_view) {
271 controller.set_embedded_view(view.embedded_view);
273 controller.do_switch_view.add_last(this.on_mode_switch);
274 controller.do_prev_view.add_last(this.on_prev_view);
275 var container = $("#" + this.element_id + '_view_' + view_type);
276 view_promise = controller.appendTo(container);
277 this.views[view_type].controller = controller;
278 this.views[view_type].deferred.resolve(view_type);
279 $.when(view_promise).then(function() {
280 self.on_controller_inited(view_type, controller);
281 if (self.searchview && view.controller.searchable !== false) {
282 self.searchview.ready.then(self.searchview.do_search);
285 } else if (this.searchview && view.controller.searchable !== false) {
286 this.searchview.ready.then(this.searchview.do_search);
289 if (this.searchview) {
290 this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
294 .find('.oe_vm_switch button').removeAttr('disabled')
295 .filter('[data-view-type="' + view_type + '"]')
296 .attr('disabled', true);
298 for (var view_name in this.views) {
299 if (!this.views.hasOwnProperty(view_name)) { continue; }
300 if (this.views[view_name].controller) {
301 if (view_name === view_type) {
302 $.when(view_promise).then(this.views[view_name].controller.do_show);
304 this.views[view_name].controller.do_hide();
308 $.when(view_promise).then(function () {
309 self.$element.find('.oe_view_title_text:first').text(
310 self.display_title());
317 * Returns to the view preceding the caller view in this manager's
318 * navigation history (the navigation history is appended to via
321 * @param {Boolean} [created=false] returning from a creation
322 * @returns {$.Deferred} switching end signal
324 on_prev_view: function (created) {
325 var current_view = this.views_history.pop();
326 var previous_view = this.views_history[this.views_history.length - 1];
327 // APR special case: "If creation mode from list (and only from a list),
328 // after saving, go to page view (don't come back in list)"
329 if (created && current_view === 'form' && previous_view === 'list') {
330 return this.on_mode_switch('page');
332 return this.on_mode_switch(previous_view, true);
335 * Sets up the current viewmanager's search view.
337 * @param {Number|false} view_id the view to use or false for a default one
338 * @returns {jQuery.Deferred} search view startup deferred
340 setup_search_view: function(view_id, search_defaults) {
342 if (this.searchview) {
343 this.searchview.stop();
345 this.searchview = new session.web.SearchView(
347 view_id, search_defaults, this.flags.search_view === false);
349 this.searchview.on_search.add(this.do_searchview_search);
350 return this.searchview.appendTo($("#" + this.element_id + "_search"));
352 do_searchview_search: function(domains, contexts, groupbys) {
354 controller = this.views[this.active_view].controller;
355 this.rpc('/web/session/eval_domain_and_context', {
356 domains: [this.action.domain || []].concat(domains || []),
357 contexts: [this.action.context || {}].concat(contexts || []),
358 group_by_seq: groupbys || []
359 }, function (results) {
360 self.dataset.context = results.context;
361 self.dataset.domain = results.domain;
362 controller.do_search(results.domain, results.context, results.group_by);
366 * Event launched when a controller has been inited.
368 * @param {String} view_type type of view
369 * @param {String} view the inited controller
371 on_controller_inited: function(view_type, view) {
374 * Called when one of the view want to execute an action
376 on_action: function(action) {
378 on_create: function() {
380 on_remove: function() {
382 on_edit: function() {
385 * Called by children view after executing an action
387 on_action_executed: function () {},
388 display_title: function () {
389 var view = this.views[this.active_view];
392 return view.controller.fields_view.arch.attrs.string;
398 session.web.ViewManagerAction = session.web.ViewManager.extend(/** @lends oepnerp.web.ViewManagerAction# */{
399 template:"ViewManagerAction",
401 * @constructs session.web.ViewManagerAction
402 * @extends session.web.ViewManager
404 * @param {session.web.ActionManager} parent parent object/widget
405 * @param {Object} action descriptor for the action this viewmanager needs to manage its views.
407 init: function(parent, action) {
408 // dataset initialization will take the session from ``this``, so if we
409 // do not have it yet (and we don't, because we've not called our own
410 // ``_super()``) rpc requests will blow up.
411 this._super(parent, null, action.views);
412 this.session = parent.session;
413 this.action = action;
414 var dataset = new session.web.DataSetSearch(this, action.res_model, action.context, action.domain);
416 dataset.ids.push(action.res_id);
419 this.dataset = dataset;
420 this.flags = this.action.flags || {};
421 if (action.res_model == 'board.board' && action.view_mode === 'form') {
422 // Special case for Dashboards
423 _.extend(this.flags, {
424 views_switcher : false,
425 display_title : false,
429 action_buttons : false
433 // setup storage for session-wise menu hiding
434 if (this.session.hidden_menutips) {
437 this.session.hidden_menutips = {}
440 * Initializes the ViewManagerAction: sets up the searchview (if the
441 * searchview is enabled in the manager's action flags), calls into the
442 * parent to initialize the primary view and (if the VMA has a searchview)
443 * launches an initial search after both views are done rendering.
448 search_defaults = {};
449 _.each(this.action.context, function (value, key) {
450 var match = /^search_default_(.*)$/.exec(key);
452 search_defaults[match[1]] = value;
456 var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
458 searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
460 var main_view_loaded = this._super();
462 _.each(_.keys(this.views), function(view_type) {
463 $.when(self.views[view_type].deferred).done(function(view_type) {
464 self.views[view_type].controller.do_push_state.add(self.do_push_state);
468 var manager_ready = $.when(searchview_loaded, main_view_loaded);
470 this.$element.find('.oe_get_xml_view').click(function () {
471 var view = self.views[self.active_view].controller,
472 view_id = view.fields_view.view_id;
474 view.on_sidebar_edit_resource('ir.ui.view', view_id);
477 if (this.action.help && !this.flags.low_profile) {
478 var Users = new session.web.DataSet(self, 'res.users'),
479 $tips = this.$element.find('.oe_view_manager_menu_tips');
480 $tips.delegate('blockquote button', 'click', function() {
482 //noinspection FallthroughInSwitchStatementJS
483 switch ($this.attr('name')) {
485 Users.write(self.session.uid, {menu_tips:false});
487 $this.closest('blockquote').hide();
488 self.session.hidden_menutips[self.action.id] = true;
491 if (!(self.action.id in self.session.hidden_menutips)) {
492 Users.read_ids([this.session.uid], ['menu_tips'], function(users) {
494 if (!(user && user.id === self.session.uid)) {
497 $tips.find('blockquote').toggle(user.menu_tips);
502 var $res_logs = this.$element.find('.oe-view-manager-logs:first');
503 $res_logs.delegate('a.oe-more-logs', 'click', function () {
504 $res_logs.removeClass('oe-folded');
506 }).delegate('a.oe-remove-everything', 'click', function () {
507 $res_logs.removeClass('oe-has-more')
512 return manager_ready;
514 on_mode_switch: function (view_type, no_store) {
517 return $.when(this._super(view_type, no_store)).then(function () {
518 self.shortcut_check(self.views[view_type]);
520 self.$element.find('.oe-view-manager-logs:first')
521 .addClass('oe-folded').removeClass('oe-has-more')
524 var controller = self.views[self.active_view].controller,
525 fvg = controller.fields_view,
526 view_id = (fvg && fvg.view_id) || '--';
527 self.$element.find('.oe_get_xml_view span').text(view_id);
528 if (!self.action.name && fvg) {
529 self.$element.find('.oe_view_title_text').text(fvg.arch.attrs.string || fvg.name);
532 var $title = self.$element.find('.oe_view_title_text'),
533 $search_prefix = $title.find('span.oe_searchable_view');
534 if (controller.searchable !== false) {
535 if (!$search_prefix.length) {
536 $title.prepend('<span class="oe_searchable_view">' + _t("Search: ") + '</span>');
539 $search_prefix.remove();
542 self.do_push_state({view_type: self.active_view});
546 do_push_state: function(state, overwrite) {
549 do_load_state: function(state) {
551 $.when(this.on_mode_switch(state.view_type, true)).done(function() {
552 self.views[self.active_view].controller.do_load_state(state);
556 shortcut_check : function(view) {
558 var grandparent = this.widget_parent && this.widget_parent.widget_parent;
559 // display shortcuts if on the first view for the action
560 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
561 if (!(grandparent instanceof session.web.WebClient) ||
562 !(view.view_type === this.views_src[0].view_type
563 && view.view_id === this.views_src[0].view_id)) {
564 $shortcut_toggle.hide();
567 $shortcut_toggle.removeClass('oe-shortcut-remove').show();
568 if (_(this.session.shortcuts).detect(function (shortcut) {
569 return shortcut.res_id === self.session.active_id; })) {
570 $shortcut_toggle.addClass("oe-shortcut-remove");
572 this.shortcut_add_remove();
574 shortcut_add_remove: function() {
576 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
580 if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
581 $(self.session.shortcuts.binding).trigger('remove-current');
582 $shortcut_toggle.removeClass("oe-shortcut-remove");
584 $(self.session.shortcuts.binding).trigger('add', {
585 'user_id': self.session.uid,
586 'res_id': self.session.active_id,
587 'resource': 'ir.ui.menu',
588 'name': self.action.name
590 $shortcut_toggle.addClass("oe-shortcut-remove");
595 * Intercept do_action resolution from children views
597 on_action_executed: function () {
598 return new session.web.DataSet(this, 'res.log')
599 .call('get', [], this.do_display_log);
602 * @param {Array<Object>} log_records
604 do_display_log: function (log_records) {
607 $logs = this.$element.find('.oe-view-manager-logs:first')
608 .addClass('oe-folded'),
609 $logs_list = $logs.find('ul').empty();
610 $logs.toggleClass('oe-has-more', log_records.length > cutoff);
611 _(log_records.reverse()).each(function (record) {
612 $(_.str.sprintf('<li><a href="#">%s</a></li>', record.name))
613 .appendTo($logs_list)
614 .delegate('a', 'click', function (e) {
616 type: 'ir.actions.act_window',
617 res_model: record.res_model,
618 res_id: record.res_id,
619 // TODO: need to have an evaluated context here somehow
620 //context: record.context,
621 views: [[false, 'form']]
627 display_title: function () {
628 return this.action.name;
632 session.web.Sidebar = session.web.Widget.extend({
633 init: function(parent, element_id) {
634 this._super(parent, element_id);
641 this.$element.html(session.web.qweb.render('Sidebar'));
642 this.$element.find(".toggle-sidebar").click(function(e) {
646 add_default_sections: function() {
648 view = this.widget_parent,
649 view_manager = view.widget_parent,
650 action = view_manager.action;
651 if (this.session.uid === 1) {
652 this.add_section(_t('Customize'), 'customize');
653 this.add_items('customize', [
655 label: _t("Manage Views"),
656 callback: view.on_sidebar_manage_views,
657 title: _t("Manage views of the current object")
659 label: _t("Edit Workflow"),
660 callback: view.on_sidebar_edit_workflow,
661 title: _t("Manage views of the current object"),
662 classname: 'oe_hide oe_sidebar_edit_workflow'
664 label: _t("Customize Object"),
665 callback: view.on_sidebar_customize_object,
666 title: _t("Manage views of the current object")
668 label: _t("Translate"),
669 callback: view.on_sidebar_translate,
670 title: _t("Technical translation")
675 this.add_section(_t('Other Options'), 'other');
676 this.add_items('other', [
679 callback: view.on_sidebar_import
682 callback: view.on_sidebar_export
684 label: _t("View Log"),
685 callback: view.on_sidebar_view_log,
686 classname: 'oe_hide oe_sidebar_view_log'
690 if (session.connection.debug) {
691 this.add_section("Debug", 'debug');
692 if (action && action.id) {
693 this.add_items('debug', [{
694 label: "Edit Action",
695 callback: function() {
696 view.on_sidebar_edit_resource(action.type, action.id);
700 if (view_manager.searchview && view_manager.searchview.view_id) {
701 this.add_items('debug', [{
702 label: "Edit SearchView",
703 callback: function() {
704 view.on_sidebar_edit_resource('ir.ui.view', view_manager.searchview.view_id);
711 add_toolbar: function(toolbar) {
713 _.each([['print', _t("Reports")], ['action', _t("Actions")], ['relate', _t("Links")]], function(type) {
714 var items = toolbar[type[0]];
716 for (var i = 0; i < items.length; i++) {
718 label: items[i]['name'],
720 classname: 'oe_sidebar_' + type[0]
723 self.add_section(type[1], type[0]);
724 self.add_items(type[0], items);
729 add_section: function(name, code) {
730 if(!code) code = _.str.underscored(name);
731 var $section = this.sections[code];
734 var section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
735 $section = $(session.web.qweb.render("Sidebar.section", {
736 section_id: section_id,
738 classname: 'oe_sidebar_' + code
740 $section.appendTo(this.$element.find('div.sidebar-actions'));
741 this.sections[code] = $section;
746 add_items: function(section_code, items) {
747 // An item is a dictonary : {
748 // label: label to be displayed for the link,
749 // action: action to be launch when the link is clicked,
750 // callback: a function to be executed when the link is clicked,
751 // classname: optional dom class name for the line,
752 // title: optional title for the link
754 // Note: The item should have one action or/and a callback
758 $section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
759 section_id = $section.attr('id');
762 for (var i = 0; i < items.length; i++) {
763 items[i].element_id = _.uniqueId(section_id + '_item_');
764 this.items[items[i].element_id] = items[i];
767 var $items = $(session.web.qweb.render("Sidebar.section.items", {items: items}));
769 $items.find('a.oe_sidebar_action_a').click(function() {
770 var item = self.items[$(this).attr('id')];
772 item.callback.apply(self, [item]);
775 self.on_item_action_clicked(item);
780 var $ul = $section.find('ul');
782 $ul = $('<ul/>').appendTo($section);
784 $items.appendTo($ul);
787 on_item_action_clicked: function(item) {
789 self.widget_parent.sidebar_context().then(function (context) {
790 var ids = self.widget_parent.get_selected_ids();
791 if (ids.length == 0) {
792 //TODO: make prettier warning?
793 $("<div />").text(_t("You must choose at least one record.")).dialog({
794 title: _t("Warning"),
799 var additional_context = _.extend({
802 active_model: self.widget_parent.dataset.model
804 self.rpc("/web/action/load", {
805 action_id: item.action.id,
806 context: additional_context
807 }, function(result) {
808 result.result.context = _.extend(result.result.context || {},
810 result.result.flags = result.result.flags || {};
811 result.result.flags.new_window = true;
812 self.do_action(result.result);
816 do_fold: function() {
817 this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
819 do_unfold: function() {
820 this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
822 do_toggle: function() {
823 this.$element.toggleClass('open-sidebar closed-sidebar');
827 session.web.TranslateDialog = session.web.Dialog.extend({
828 dialog_title: _t("Translations"),
829 init: function(view) {
830 // TODO fme: should add the language to fields_view_get because between the fields view get
831 // and the moment the user opens the translation dialog, the user language could have been changed
832 this.view_language = view.session.user_context.lang;
833 this['on_button' + _t("Save")] = this.on_button_Save;
834 this['on_button' + _t("Close")] = this.on_button_Close;
840 this.view_type = view.fields_view.type || '';
841 this.$fields_form = null;
842 this.$view_form = null;
843 this.$sidebar_form = null;
844 this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
845 this.languages = null;
846 this.languages_loaded = $.Deferred();
847 (new session.web.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
848 [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }, this.on_languages_loaded);
853 $.when(this.languages_loaded).then(function() {
854 self.$element.html(session.web.qweb.render('TranslateDialog', { widget: self }));
855 self.$fields_form = self.$element.find('.oe_translation_form');
856 self.$fields_form.find('.oe_trad_field').change(function() {
857 $(this).toggleClass('touched', ($(this).val() != $(this).attr('data-value')));
862 on_languages_loaded: function(langs) {
863 this.languages = langs;
864 this.languages_loaded.resolve();
866 do_load_fields_values: function(callback) {
869 this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
870 _.each(self.languages, function(lg) {
871 var deff = $.Deferred();
873 var callback = function(values) {
874 _.each(self.translatable_fields_keys, function(f) {
875 self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '').attr('data-value', values[0][f] || '');
879 if (lg.code === self.view_language) {
881 _.each(self.translatable_fields_keys, function(field) {
882 values[field] = self.view.fields[field].get_value();
886 self.rpc('/web/dataset/get', {
887 model: self.view.dataset.model,
888 ids: [self.view.datarecord.id],
889 fields: self.translatable_fields_keys,
890 context: self.view.dataset.get_context({
895 $.when.apply(null, deffered).then(callback);
897 open: function(field) {
900 $.when(this.languages_loaded).then(function() {
901 if (self.view.translatable_fields && self.view.translatable_fields.length) {
902 self.do_load_fields_values(function() {
905 var $field_input = self.$element.find('tr[data-field="' + field.name + '"] td:nth-child(2) *:first-child');
906 self.$element.scrollTo($field_input);
907 $field_input.focus();
915 on_button_Save: function() {
918 self.$fields_form.find('.oe_trad_field.touched').each(function() {
919 var field = $(this).attr('name').split('-');
920 if (!trads[field[0]]) {
921 trads[field[0]] = {};
923 trads[field[0]][field[1]] = $(this).val();
925 _.each(trads, function(data, code) {
926 if (code === self.view_language) {
927 _.each(data, function(value, field) {
928 self.view.fields[field].set_value(value);
931 self.view.dataset.write(self.view.datarecord.id, data, { 'lang': code });
936 on_button_Close: function() {
941 session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
942 template: "EmptyComponent",
943 set_default_options: function(options) {
944 this.options = options || {};
945 _.defaults(this.options, {
946 // All possible views options should be defaulted here
953 open_translate_dialog: function(field) {
954 if (!this.translate_dialog) {
955 this.translate_dialog = new session.web.TranslateDialog(this).start();
957 this.translate_dialog.open(field);
960 * Fetches and executes the action identified by ``action_data``.
962 * @param {Object} action_data the action descriptor data
963 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
964 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
965 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
966 * @param {Object} [action_data.context=null] additional action context, to add to the current context
967 * @param {session.web.DataSet} dataset a dataset object used to communicate with the server
968 * @param {Object} [record_id] the identifier of the object on which the action is to be applied
969 * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
971 do_execute_action: function (action_data, dataset, record_id, on_closed) {
973 var result_handler = function () {
974 if (on_closed) { on_closed.apply(null, arguments); }
975 if (self.widget_parent && self.widget_parent.on_action_executed) {
976 return self.widget_parent.on_action_executed.apply(null, arguments);
979 var context = new session.web.CompoundContext(dataset.get_context(), action_data.context || {});
981 var handler = function (r) {
982 var action = r.result;
983 if (action && action.constructor == Object) {
984 var ncontext = new session.web.CompoundContext(context);
987 active_id: record_id,
988 active_ids: [record_id],
989 active_model: dataset.model
992 ncontext.add(action.context || {});
993 return self.rpc('/web/session/eval_domain_and_context', {
994 contexts: [ncontext],
996 }).pipe(function (results) {
997 action.context = results.context;
998 /* niv: previously we were overriding once more with action_data.context,
999 * I assumed this was not a correct behavior and removed it
1001 return self.do_action(action, result_handler);
1004 return result_handler();
1008 if (action_data.special) {
1009 return handler({result: {"type":"ir.actions.act_window_close"}});
1010 } else if (action_data.type=="object") {
1011 var args = [[record_id]], additional_args = [];
1012 if (action_data.args) {
1014 // Warning: quotes and double quotes problem due to json and xml clash
1015 // Maybe we should force escaping in xml or do a better parse of the args array
1016 additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
1017 args = args.concat(additional_args);
1019 console.error("Could not JSON.parse arguments", action_data.args);
1023 return dataset.call_button(action_data.name, args, handler);
1024 } else if (action_data.type=="action") {
1025 return this.rpc('/web/action/load', { action_id: parseInt(action_data.name, 10), context: context, do_not_eval: true}, handler);
1027 return dataset.exec_workflow(record_id, action_data.name, handler);
1031 * Directly set a view to use instead of calling fields_view_get. This method must
1032 * be called before start(). When an embedded view is set, underlying implementations
1033 * of session.web.View must use the provided view instead of any other one.
1035 * @param embedded_view A view.
1037 set_embedded_view: function(embedded_view) {
1038 this.embedded_view = embedded_view;
1039 this.options.sidebar = false;
1042 * Switches to a specific view type
1044 * @param {String} view view type to switch to
1046 do_switch_view: function(view) { },
1048 * Cancels the switch to the current view, switches to the previous one
1050 do_prev_view: function () { },
1051 do_search: function(view) {
1054 set_common_sidebar_sections: function(sidebar) {
1055 sidebar.add_default_sections();
1057 on_sidebar_manage_views: function() {
1058 if (this.fields_view && this.fields_view.arch) {
1059 var view_editor = new session.web.ViewEditor(this, this.$element, this.dataset, this.fields_view.arch);
1060 view_editor.start();
1062 this.do_warn("Manage Views", "Could not find current view declaration");
1065 on_sidebar_edit_workflow: function() {
1066 console.log('Todo');
1068 on_sidebar_customize_object: function() {
1070 this.rpc('/web/dataset/search_read', {
1073 domain: [['model', '=', self.dataset.model]]
1074 }, function (result) {
1075 self.on_sidebar_edit_resource('ir.model', result.ids[0]);
1078 on_sidebar_import: function() {
1079 var import_view = new session.web.DataImport(this, this.dataset);
1080 import_view.start();
1082 on_sidebar_export: function() {
1083 var export_view = new session.web.DataExport(this, this.dataset);
1084 export_view.start();
1086 on_sidebar_translate: function() {
1087 return this.do_action({
1088 res_model : 'ir.translation',
1089 domain : [['type', '!=', 'object'], '|', ['name', '=', this.dataset.model], ['name', 'ilike', this.dataset.model + ',']],
1090 views: [[false, 'list'], [false, 'form']],
1091 type : 'ir.actions.act_window',
1096 on_sidebar_edit_resource: function(model, id, domain) {
1099 type : 'ir.actions.act_window',
1104 action_buttons : true
1109 action.views = [[false, 'form']];
1110 } else if (domain) {
1111 action.views = [[false, 'list'], [false, 'form']];
1112 action.domain = domain;
1113 action.flags.views_switcher = true;
1115 this.do_action(action);
1117 on_sidebar_view_log: function() {
1119 sidebar_context: function () {
1120 return $.Deferred().resolve({}).promise();
1123 do_push_state: function(state, overwrite) {
1126 do_load_state: function(state) {
1130 session.web.json_node_to_xml = function(node, human_readable, indent) {
1131 // For debugging purpose, this function will convert a json node back to xml
1132 // Maybe usefull for xml view editor
1133 indent = indent || 0;
1134 var sindent = (human_readable ? (new Array(indent + 1).join('\t')) : ''),
1135 r = sindent + '<' + node.tag,
1136 cr = human_readable ? '\n' : '';
1138 if (typeof(node) === 'string') {
1139 return sindent + node;
1140 } else if (typeof(node.tag) !== 'string' || !node.children instanceof Array || !node.attrs instanceof Object) {
1141 throw("Node a json node");
1143 for (var attr in node.attrs) {
1144 var vattr = node.attrs[attr];
1145 if (typeof(vattr) !== 'string') {
1147 vattr = JSON.stringify(vattr);
1149 vattr = vattr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
1150 if (human_readable) {
1151 vattr = vattr.replace(/"/g, "'");
1153 r += ' ' + attr + '="' + vattr + '"';
1155 if (node.children && node.children.length) {
1158 for (var i = 0, ii = node.children.length; i < ii; i++) {
1159 childs.push(session.web.json_node_to_xml(node.children[i], human_readable, indent + 1));
1161 r += childs.join(cr);
1162 r += cr + sindent + '</' + node.tag + '>';
1171 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: