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;
29 return "<div id='"+this.element_id+"'></div>";
31 dialog_stop: function () {
33 this.dialog_viewmanager.stop();
34 this.dialog_viewmanager = null;
39 content_stop: function () {
40 if (this.inner_viewmanager) {
41 this.inner_viewmanager.stop();
42 this.inner_viewmanager = null;
44 if (this.client_widget) {
45 this.client_widget.stop();
46 this.client_widget = null;
49 url_update: function(action) {
52 url.action_id = action.id;
54 // "model": action.res_model,
55 // "domain": action.domain,
65 this.do_url_set_hash(url);
67 do_url_set_hash: function(url) {
69 on_url_hashchange: function(url) {
71 if(url && url.action_id) {
72 self.rpc("/web/action/load", { action_id: url.action_id }, function(result) {
73 self.do_action(result.result);
77 do_action: function(action, on_close) {
78 var type = action.type.replace(/\./g,'_');
79 var popup = action.target === 'new';
80 action.flags = _.extend({
81 views_switcher : !popup,
83 action_buttons : !popup,
86 }, action.flags || {});
87 if (!(type in this)) {
88 console.log("Action manager can't handle action of type " + action.type, action);
91 return this[type](action, on_close);
93 null_action: function() {
97 ir_actions_act_window: function (action, on_close) {
98 if (_(['base.module.upgrade', 'base.setup.installer'])
99 .contains(action.res_model)) {
100 var old_close = on_close;
101 on_close = function () {
102 session.webclient.do_reload();
103 if (old_close) { old_close(); }
106 if (action.target === 'new') {
107 if (this.dialog == null) {
108 this.dialog = new session.web.Dialog(this, { title: action.name, width: '80%' });
110 this.dialog.on_close.add(on_close);
113 this.dialog_viewmanager.stop();
115 this.dialog_viewmanager = new session.web.ViewManagerAction(this, action);
116 this.dialog_viewmanager.appendTo(this.dialog.$element);
121 this.inner_viewmanager = new session.web.ViewManagerAction(this, action);
122 this.inner_viewmanager.appendTo(this.$element);
123 this.url_update(action);
126 this.rpc("/web/session/save_session_action", { the_action : action}, function(key) {
127 var url = window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + jQuery.param({ s_action : "" + key });
128 window.open(url,'_blank');
132 ir_actions_act_window_close: function (action, on_closed) {
133 if (!this.dialog && on_closed) {
138 ir_actions_server: function (action, on_closed) {
140 this.rpc('/web/action/run', {
141 action_id: action.id,
142 context: action.context || {}
143 }).then(function (action) {
144 self.do_action(action, on_closed)
147 ir_actions_client: function (action) {
149 var ClientWidget = session.web.client_actions.get_object(action.tag);
150 (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
152 ir_actions_report_xml: function(action, on_closed) {
155 self.rpc("/web/session/eval_domain_and_context", {
156 contexts: [action.context],
158 }).then(function(res) {
159 action = _.clone(action);
160 action.context = res.context;
161 self.session.get_file({
163 data: {action: JSON.stringify(action)},
164 complete: $.unblockUI,
166 if (!self.dialog && on_closed) {
174 ir_actions_act_url: function (action) {
175 window.open(action.url, action.target === 'self' ? '_self' : '_blank');
179 session.web.ViewManager = session.web.Widget.extend(/** @lends session.web.ViewManager# */{
180 identifier_prefix: "viewmanager",
181 template: "ViewManager",
183 * @constructs session.web.ViewManager
184 * @extends session.web.Widget
190 init: function(parent, dataset, views) {
192 this.model = dataset ? dataset.model : undefined;
193 this.dataset = dataset;
194 this.searchview = null;
195 this.active_view = null;
196 this.views_src = _.map(views, function(x) {return x instanceof Array? {view_id: x[0], view_type: x[1]} : x;});
198 this.flags = this.flags || {};
199 this.registry = session.web.views;
201 this.views_history = [];
204 return session.web.qweb.render(this.template, {
206 prefix: this.element_id,
207 views: this.views_src});
210 * @returns {jQuery.Deferred} initial view loading promise
215 this.$element.find('.oe_vm_switch button').click(function() {
216 self.on_mode_switch($(this).data('view-type'));
219 _.each(this.views_src, function(view) {
220 self.views[view.view_type] = $.extend({}, view, {
221 deferred : $.Deferred(),
224 sidebar_id : self.element_id + '_sidebar_' + view.view_type,
225 action : self.action,
226 action_views_ids : views_ids
227 }, self.flags, self.flags[view.view_type] || {}, view.options || {})
229 views_ids[view.view_type] = view.view_id;
231 if (this.flags.views_switcher === false) {
232 this.$element.find('.oe_vm_switch').hide();
234 // switch to the first one in sequence
235 return this.on_mode_switch(this.views_src[0].view_type);
238 * Asks the view manager to switch visualization mode.
240 * @param {String} view_type type of view to display
241 * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack
242 * @returns {jQuery.Deferred} new view loading promise
244 on_mode_switch: function(view_type, no_store) {
246 view = this.views[view_type],
249 return $.Deferred().reject();
252 this.views_history.push(view_type);
254 this.active_view = view_type;
256 if (!view.controller) {
257 // Lazy loading of views
258 var controllerclass = this.registry.get_object(view_type);
259 var controller = new controllerclass(this, this.dataset, view.view_id, view.options);
260 if (view.embedded_view) {
261 controller.set_embedded_view(view.embedded_view);
263 controller.do_switch_view.add_last(this.on_mode_switch);
264 controller.do_prev_view.add_last(this.on_prev_view);
265 var container = $("#" + this.element_id + '_view_' + view_type);
266 view_promise = controller.appendTo(container);
267 this.views[view_type].controller = controller;
268 this.views[view_type].deferred.resolve();
269 $.when(view_promise).then(function() {
270 self.on_controller_inited(view_type, controller);
271 if (self.searchview && view.controller.searchable !== false) {
272 self.searchview.ready.then(self.searchview.do_search);
275 } else if (this.searchview && view.controller.searchable !== false) {
276 this.searchview.ready.then(this.searchview.do_search);
279 if (this.searchview) {
280 this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
284 .find('.oe_vm_switch button').removeAttr('disabled')
285 .filter('[data-view-type="' + view_type + '"]')
286 .attr('disabled', true);
288 for (var view_name in this.views) {
289 if (!this.views.hasOwnProperty(view_name)) { continue; }
290 if (this.views[view_name].controller) {
291 if (view_name === view_type) {
292 $.when(view_promise).then(this.views[view_name].controller.do_show);
294 this.views[view_name].controller.do_hide();
298 $.when(view_promise).then(function () {
299 self.$element.find('.oe_view_title_text:first').text(
300 self.display_title());
304 on_prev_view: function () {
305 this.views_history.pop();
306 var previous_view = this.views_history[this.views_history.length - 1];
307 this.on_mode_switch(previous_view, true);
310 * Sets up the current viewmanager's search view.
312 * @param {Number|false} view_id the view to use or false for a default one
313 * @returns {jQuery.Deferred} search view startup deferred
315 setup_search_view: function(view_id, search_defaults) {
317 if (this.searchview) {
318 this.searchview.stop();
320 this.searchview = new session.web.SearchView(
322 view_id, search_defaults, this.flags.search_view === false);
324 this.searchview.on_search.add(this.do_searchview_search);
325 return this.searchview.appendTo($("#" + this.element_id + "_search"));
327 do_searchview_search: function(domains, contexts, groupbys) {
329 controller = this.views[this.active_view].controller;
330 this.rpc('/web/session/eval_domain_and_context', {
331 domains: [this.action.domain || []].concat(domains || []),
332 contexts: [this.action.context || {}].concat(contexts || []),
333 group_by_seq: groupbys || []
334 }, function (results) {
335 self.dataset.context = results.context;
336 self.dataset.domain = results.domain;
337 controller.do_search(results.domain, results.context, results.group_by);
341 * Event launched when a controller has been inited.
343 * @param {String} view_type type of view
344 * @param {String} view the inited controller
346 on_controller_inited: function(view_type, view) {
349 * Called when one of the view want to execute an action
351 on_action: function(action) {
353 on_create: function() {
355 on_remove: function() {
357 on_edit: function() {
360 * Called by children view after executing an action
362 on_action_executed: function () {},
363 display_title: function () {
364 var view = this.views[this.active_view];
367 return view.controller.fields_view.arch.attrs.string;
373 session.web.ViewManagerAction = session.web.ViewManager.extend(/** @lends oepnerp.web.ViewManagerAction# */{
374 template:"ViewManagerAction",
376 * @constructs session.web.ViewManagerAction
377 * @extends session.web.ViewManager
379 * @param {session.web.ActionManager} parent parent object/widget
380 * @param {Object} action descriptor for the action this viewmanager needs to manage its views.
382 init: function(parent, action) {
383 // dataset initialization will take the session from ``this``, so if we
384 // do not have it yet (and we don't, because we've not called our own
385 // ``_super()``) rpc requests will blow up.
386 this._super(parent, null, action.views);
387 this.session = parent.session;
388 this.action = action;
389 var dataset = new session.web.DataSetSearch(this, action.res_model, action.context, action.domain);
391 dataset.ids.push(action.res_id);
394 this.dataset = dataset;
395 this.flags = this.action.flags || {};
396 if (action.res_model == 'board.board' && action.views.length == 1 && action.views) {
397 // Not elegant but allows to avoid form chrome (pager, save/new
398 // buttons, sidebar, ...) displaying
399 this.flags.display_title = this.flags.search_view = this.flags.pager = this.flags.sidebar = this.flags.action_buttons = false;
402 // setup storage for session-wise menu hiding
403 if (this.session.hidden_menutips) {
406 this.session.hidden_menutips = {}
409 * Initializes the ViewManagerAction: sets up the searchview (if the
410 * searchview is enabled in the manager's action flags), calls into the
411 * parent to initialize the primary view and (if the VMA has a searchview)
412 * launches an initial search after both views are done rendering.
417 search_defaults = {};
418 _.each(this.action.context, function (value, key) {
419 var match = /^search_default_(.*)$/.exec(key);
421 search_defaults[match[1]] = value;
425 var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
427 searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
429 var main_view_loaded = this._super();
431 var manager_ready = $.when(searchview_loaded, main_view_loaded);
433 this.$element.find('.oe_get_xml_view').click(function () {
434 // TODO: add search view?
435 $('<pre>').text(session.web.json_node_to_xml(
436 self.views[self.active_view].controller.fields_view.arch, true))
437 .dialog({ width: '95%'});
439 if (this.action.help && !this.flags.low_profile) {
440 var Users = new session.web.DataSet(self, 'res.users'),
441 $tips = this.$element.find('.oe_view_manager_menu_tips');
442 $tips.delegate('blockquote button', 'click', function() {
444 //noinspection FallthroughInSwitchStatementJS
445 switch ($this.attr('name')) {
447 Users.write(self.session.uid, {menu_tips:false});
449 $this.closest('blockquote').hide();
450 self.session.hidden_menutips[self.action.id] = true;
453 if (!(self.action.id in self.session.hidden_menutips)) {
454 Users.read_ids([this.session.uid], ['menu_tips'], function(users) {
456 if (!(user && user.id === self.session.uid)) {
459 $tips.find('blockquote').toggle(user.menu_tips);
464 var $res_logs = this.$element.find('.oe-view-manager-logs:first');
465 $res_logs.delegate('a.oe-more-logs', 'click', function () {
466 $res_logs.removeClass('oe-folded');
468 }).delegate('a.oe-remove-everything', 'click', function () {
469 $res_logs.removeClass('oe-has-more')
474 return manager_ready;
476 on_mode_switch: function (view_type, no_store) {
479 var switched = $.when(this._super(view_type, no_store)).then(function () {
480 self.$element.find('.oe-view-manager-logs:first')
481 .addClass('oe-folded').removeClass('oe-has-more')
483 self.shortcut_check(self.views[view_type])
488 var controller = self.views[self.active_view].controller,
489 fvg = controller.fields_view,
490 view_id = (fvg && fvg.view_id) || '--';
491 self.$element.find('.oe_get_xml_view span').text(view_id);
492 if (!self.action.name && fvg) {
493 self.$element.find('.oe_view_title_text').text(fvg.arch.attrs.string || fvg.name);
496 var $title = self.$element.find('.oe_view_title_text'),
497 $search_prefix = $title.find('span.oe_searchable_view');
498 if (controller.searchable !== false) {
499 if (!$search_prefix.length) {
500 $title.prepend('<span class="oe_searchable_view">' + _t("Search: ") + '</span>');
503 $search_prefix.remove();
507 shortcut_check : function(view) {
509 var grandparent = this.widget_parent && this.widget_parent.widget_parent;
510 // display shortcuts if on the first view for the action
511 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
512 if (!(grandparent instanceof session.web.WebClient) ||
513 !(view.view_type === this.views_src[0].view_type
514 && view.view_id === this.views_src[0].view_id)) {
515 $shortcut_toggle.hide();
518 $shortcut_toggle.removeClass('oe-shortcut-remove').show();
519 if (_(this.session.shortcuts).detect(function (shortcut) {
520 return shortcut.res_id === self.session.active_id; })) {
521 $shortcut_toggle.addClass("oe-shortcut-remove");
523 this.shortcut_add_remove();
525 shortcut_add_remove: function() {
527 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
531 if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
532 $(self.session.shortcuts.binding).trigger('remove-current');
533 $shortcut_toggle.removeClass("oe-shortcut-remove");
535 $(self.session.shortcuts.binding).trigger('add', {
536 'user_id': self.session.uid,
537 'res_id': self.session.active_id,
538 'resource': 'ir.ui.menu',
539 'name': self.action.name
541 $shortcut_toggle.addClass("oe-shortcut-remove");
546 * Intercept do_action resolution from children views
548 on_action_executed: function () {
549 return new session.web.DataSet(this, 'res.log')
550 .call('get', [], this.do_display_log);
553 * @param {Array<Object>} log_records
555 do_display_log: function (log_records) {
558 $logs = this.$element.find('.oe-view-manager-logs:first')
559 .addClass('oe-folded'),
560 $logs_list = $logs.find('ul').empty();
561 $logs.toggleClass('oe-has-more', log_records.length > cutoff);
562 _(log_records.reverse()).each(function (record) {
563 $(_.str.sprintf('<li><a href="#">%s</a></li>', record.name))
564 .appendTo($logs_list)
565 .delegate('a', 'click', function (e) {
567 type: 'ir.actions.act_window',
568 res_model: record.res_model,
569 res_id: record.res_id,
570 // TODO: need to have an evaluated context here somehow
571 //context: record.context,
572 views: [[false, 'form']]
578 display_title: function () {
579 return this.action.name;
583 session.web.Sidebar = session.web.Widget.extend({
584 init: function(parent, element_id) {
585 this._super(parent, element_id);
592 this.$element.html(session.web.qweb.render('Sidebar'));
593 this.$element.find(".toggle-sidebar").click(function(e) {
597 add_default_sections: function() {
598 if (this.session.uid === 1) {
599 this.add_section(_t('Customize'), 'customize');
600 this.add_items('customize', [
602 label: _t("Manage Views"),
603 callback: this.widget_parent.on_sidebar_manage_views,
604 title: _t("Manage views of the current object")
606 label: _t("Edit Workflow"),
607 callback: this.widget_parent.on_sidebar_edit_workflow,
608 title: _t("Manage views of the current object"),
609 classname: 'oe_hide oe_sidebar_edit_workflow'
611 label: _t("Customize Object"),
612 callback: this.widget_parent.on_sidebar_customize_object,
613 title: _t("Manage views of the current object")
615 label: _t("Translate"),
616 callback: this.widget_parent.on_sidebar_translate,
617 title: _t("Technical translation")
622 this.add_section(_t('Other Options'), 'other');
623 this.add_items('other', [
626 callback: this.widget_parent.on_sidebar_import
629 callback: this.widget_parent.on_sidebar_export
631 label: _t("View Log"),
632 callback: this.widget_parent.on_sidebar_view_log,
633 classname: 'oe_hide oe_sidebar_view_log'
638 add_toolbar: function(toolbar) {
640 _.each([['print', _t("Reports")], ['action', _t("Actions")], ['relate', _t("Links")]], function(type) {
641 var items = toolbar[type[0]];
643 for (var i = 0; i < items.length; i++) {
645 label: items[i]['name'],
647 classname: 'oe_sidebar_' + type[0]
650 self.add_section(type[1], type[0]);
651 self.add_items(type[0], items);
656 add_section: function(name, code) {
657 if(!code) code = _.str.underscored(name);
658 var $section = this.sections[code];
661 var section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
662 $section = $(session.web.qweb.render("Sidebar.section", {
663 section_id: section_id,
665 classname: 'oe_sidebar_' + code
667 $section.appendTo(this.$element.find('div.sidebar-actions'));
668 this.sections[code] = $section;
673 add_items: function(section_code, items) {
674 // An item is a dictonary : {
675 // label: label to be displayed for the link,
676 // action: action to be launch when the link is clicked,
677 // callback: a function to be executed when the link is clicked,
678 // classname: optional dom class name for the line,
679 // title: optional title for the link
681 // Note: The item should have one action or/and a callback
685 $section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
686 section_id = $section.attr('id');
689 for (var i = 0; i < items.length; i++) {
690 items[i].element_id = _.uniqueId(section_id + '_item_');
691 this.items[items[i].element_id] = items[i];
694 var $items = $(session.web.qweb.render("Sidebar.section.items", {items: items}));
696 $items.find('a.oe_sidebar_action_a').click(function() {
697 var item = self.items[$(this).attr('id')];
699 item.callback.apply(self, [item]);
702 self.on_item_action_clicked(item);
707 var $ul = $section.find('ul');
709 $ul = $('<ul/>').appendTo($section);
711 $items.appendTo($ul);
714 on_item_action_clicked: function(item) {
716 self.widget_parent.sidebar_context().then(function (context) {
717 var ids = self.widget_parent.get_selected_ids();
718 if (ids.length == 0) {
719 //TODO: make prettier warning?
720 $("<div />").text(_t("You must choose at least one record.")).dialog({
721 title: _t("Warning"),
726 var additional_context = _.extend({
729 active_model: self.widget_parent.dataset.model
731 self.rpc("/web/action/load", {
732 action_id: item.action.id,
733 context: additional_context
734 }, function(result) {
735 result.result.context = _.extend(result.result.context || {},
737 result.result.flags = result.result.flags || {};
738 result.result.flags.new_window = true;
739 self.do_action(result.result);
743 do_fold: function() {
744 this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
746 do_unfold: function() {
747 this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
749 do_toggle: function() {
750 this.$element.toggleClass('open-sidebar closed-sidebar');
754 session.web.TranslateDialog = session.web.Dialog.extend({
755 dialog_title: _t("Translations"),
756 init: function(view) {
757 // TODO fme: should add the language to fields_view_get because between the fields view get
758 // and the moment the user opens the translation dialog, the user language could have been changed
759 this.view_language = view.session.user_context.lang;
760 this['on_button' + _t("Save")] = this.on_button_Save;
761 this['on_button' + _t("Close")] = this.on_button_Close;
767 this.view_type = view.fields_view.type || '';
768 this.$fields_form = null;
769 this.$view_form = null;
770 this.$sidebar_form = null;
771 this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
772 this.languages = null;
773 this.languages_loaded = $.Deferred();
774 (new session.web.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
775 [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }, this.on_languages_loaded);
780 $.when(this.languages_loaded).then(function() {
781 self.$element.html(session.web.qweb.render('TranslateDialog', { widget: self }));
782 self.$fields_form = self.$element.find('.oe_translation_form');
783 self.$fields_form.find('.oe_trad_field').change(function() {
784 $(this).toggleClass('touched', ($(this).val() != $(this).attr('data-value')));
789 on_languages_loaded: function(langs) {
790 this.languages = langs;
791 this.languages_loaded.resolve();
793 do_load_fields_values: function(callback) {
796 this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
797 _.each(self.languages, function(lg) {
798 var deff = $.Deferred();
800 var callback = function(values) {
801 _.each(self.translatable_fields_keys, function(f) {
802 self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '').attr('data-value', values[0][f] || '');
806 if (lg.code === self.view_language) {
808 _.each(self.translatable_fields_keys, function(field) {
809 values[field] = self.view.fields[field].get_value();
813 self.rpc('/web/dataset/get', {
814 model: self.view.dataset.model,
815 ids: [self.view.datarecord.id],
816 fields: self.translatable_fields_keys,
817 context: self.view.dataset.get_context({
822 $.when.apply(null, deffered).then(callback);
824 open: function(field) {
827 $.when(this.languages_loaded).then(function() {
828 if (self.view.translatable_fields && self.view.translatable_fields.length) {
829 self.do_load_fields_values(function() {
832 var $field_input = self.$element.find('tr[data-field="' + field.name + '"] td:nth-child(2) *:first-child');
833 self.$element.scrollTo($field_input);
834 $field_input.focus();
842 on_button_Save: function() {
845 self.$fields_form.find('.oe_trad_field.touched').each(function() {
846 var field = $(this).attr('name').split('-');
847 if (!trads[field[0]]) {
848 trads[field[0]] = {};
850 trads[field[0]][field[1]] = $(this).val();
852 _.each(trads, function(data, code) {
853 if (code === self.view_language) {
854 _.each(data, function(value, field) {
855 self.view.fields[field].set_value(value);
858 self.view.dataset.write(self.view.datarecord.id, data, { 'lang': code });
863 on_button_Close: function() {
868 session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
869 template: "EmptyComponent",
870 set_default_options: function(options) {
871 this.options = options || {};
872 _.defaults(this.options, {
873 // All possible views options should be defaulted here
880 open_translate_dialog: function(field) {
881 if (!this.translate_dialog) {
882 this.translate_dialog = new session.web.TranslateDialog(this).start();
884 this.translate_dialog.open(field);
887 * Fetches and executes the action identified by ``action_data``.
889 * @param {Object} action_data the action descriptor data
890 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
891 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
892 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
893 * @param {Object} [action_data.context=null] additional action context, to add to the current context
894 * @param {session.web.DataSet} dataset a dataset object used to communicate with the server
895 * @param {Object} [record_id] the identifier of the object on which the action is to be applied
896 * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
898 do_execute_action: function (action_data, dataset, record_id, on_closed) {
900 var result_handler = function () {
901 if (on_closed) { on_closed.apply(null, arguments); }
902 if (self.widget_parent && self.widget_parent.on_action_executed) {
903 return self.widget_parent.on_action_executed.apply(null, arguments);
906 var context = new session.web.CompoundContext(dataset.get_context(), action_data.context || {});
908 var handler = function (r) {
909 var action = r.result;
910 if (action && action.constructor == Object) {
911 var ncontext = new session.web.CompoundContext(context);
914 active_id: record_id,
915 active_ids: [record_id],
916 active_model: dataset.model
919 ncontext.add(action.context || {});
920 return self.rpc('/web/session/eval_domain_and_context', {
921 contexts: [ncontext],
923 }).pipe(function (results) {
924 action.context = results.context;
925 /* niv: previously we were overriding once more with action_data.context,
926 * I assumed this was not a correct behavior and removed it
928 return self.do_action(action, result_handler);
931 return result_handler();
935 if (action_data.special) {
936 return handler({result: {"type":"ir.actions.act_window_close"}});
937 } else if (action_data.type=="object") {
938 var args = [[record_id]], additional_args = [];
939 if (action_data.args) {
941 // Warning: quotes and double quotes problem due to json and xml clash
942 // Maybe we should force escaping in xml or do a better parse of the args array
943 additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
944 args = args.concat(additional_args);
946 console.error("Could not JSON.parse arguments", action_data.args);
950 return dataset.call_button(action_data.name, args, handler);
951 } else if (action_data.type=="action") {
952 return this.rpc('/web/action/load', { action_id: parseInt(action_data.name, 10), context: context, do_not_eval: true}, handler);
954 return dataset.exec_workflow(record_id, action_data.name, handler);
958 * Directly set a view to use instead of calling fields_view_get. This method must
959 * be called before start(). When an embedded view is set, underlying implementations
960 * of session.web.View must use the provided view instead of any other one.
962 * @param embedded_view A view.
964 set_embedded_view: function(embedded_view) {
965 this.embedded_view = embedded_view;
966 this.options.sidebar = false;
969 * Switches to a specific view type
971 * @param {String} view view type to switch to
973 do_switch_view: function(view) { },
975 * Cancels the switch to the current view, switches to the previous one
977 do_prev_view: function () { },
978 do_search: function(view) {
981 set_common_sidebar_sections: function(sidebar) {
982 sidebar.add_default_sections();
984 on_sidebar_manage_views: function() {
985 if (this.fields_view && this.fields_view.arch) {
986 var view_editor = new session.web.ViewEditor(this, this.$element, this.dataset, this.fields_view.arch);
989 this.do_warn("Manage Views", "Could not find current view declaration");
992 on_sidebar_edit_workflow: function() {
995 on_sidebar_customize_object: function() {
998 on_sidebar_import: function() {
999 var import_view = new session.web.DataImport(this, this.dataset);
1000 import_view.start();
1002 on_sidebar_export: function() {
1003 var export_view = new session.web.DataExport(this, this.dataset);
1004 export_view.start();
1006 on_sidebar_translate: function() {
1007 return this.do_action({
1008 res_model : 'ir.translation',
1009 domain : [['type', '!=', 'object'], '|', ['name', '=', this.dataset.model], ['name', 'ilike', this.dataset.model + ',']],
1010 views: [[false, 'list'], [false, 'form']],
1011 type : 'ir.actions.act_window',
1016 on_sidebar_view_log: function() {
1018 sidebar_context: function () {
1019 return $.Deferred().resolve({}).promise();
1023 session.web.json_node_to_xml = function(node, human_readable, indent) {
1024 // For debugging purpose, this function will convert a json node back to xml
1025 // Maybe usefull for xml view editor
1026 indent = indent || 0;
1027 var sindent = (human_readable ? (new Array(indent + 1).join('\t')) : ''),
1028 r = sindent + '<' + node.tag,
1029 cr = human_readable ? '\n' : '';
1031 if (typeof(node) === 'string') {
1032 return sindent + node;
1033 } else if (typeof(node.tag) !== 'string' || !node.children instanceof Array || !node.attrs instanceof Object) {
1034 throw("Node a json node");
1036 for (var attr in node.attrs) {
1037 var vattr = node.attrs[attr];
1038 if (typeof(vattr) !== 'string') {
1040 vattr = JSON.stringify(vattr);
1042 vattr = vattr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
1043 if (human_readable) {
1044 vattr = vattr.replace(/"/g, "'");
1046 r += ' ' + attr + '="' + vattr + '"';
1048 if (node.children && node.children.length) {
1051 for (var i = 0, ii = node.children.length; i < ii; i++) {
1052 childs.push(session.web.json_node_to_xml(node.children[i], human_readable, indent + 1));
1054 r += childs.join(cr);
1055 r += cr + sindent + '</' + node.tag + '>';
1064 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: