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) {
79 console.error("No type for action", action);
82 var type = action.type.replace(/\./g,'_');
83 var popup = action.target === 'new';
84 action.flags = _.extend({
85 views_switcher : !popup,
87 action_buttons : !popup,
90 }, action.flags || {});
91 if (!(type in this)) {
92 console.error("Action manager can't handle action of type " + action.type, action);
95 return this[type](action, on_close);
97 null_action: function() {
101 ir_actions_act_window: function (action, on_close) {
102 if (_(['base.module.upgrade', 'base.setup.installer'])
103 .contains(action.res_model)) {
104 var old_close = on_close;
105 on_close = function () {
106 session.webclient.do_reload();
107 if (old_close) { old_close(); }
110 if (action.target === 'new') {
111 if (this.dialog == null) {
112 this.dialog = new session.web.Dialog(this, { title: action.name, width: '80%' });
114 this.dialog.on_close.add(on_close);
117 this.dialog_viewmanager.stop();
119 this.dialog_viewmanager = new session.web.ViewManagerAction(this, action);
120 this.dialog_viewmanager.appendTo(this.dialog.$element);
125 this.inner_viewmanager = new session.web.ViewManagerAction(this, action);
126 this.inner_viewmanager.appendTo(this.$element);
127 this.url_update(action);
130 this.rpc("/web/session/save_session_action", { the_action : action}, function(key) {
131 var url = window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + jQuery.param({ s_action : "" + key });
132 window.open(url,'_blank');
136 ir_actions_act_window_close: function (action, on_closed) {
137 if (!this.dialog && on_closed) {
142 ir_actions_server: function (action, on_closed) {
144 this.rpc('/web/action/run', {
145 action_id: action.id,
146 context: action.context || {}
147 }).then(function (action) {
148 self.do_action(action, on_closed)
151 ir_actions_client: function (action) {
153 var ClientWidget = session.web.client_actions.get_object(action.tag);
154 (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
156 ir_actions_report_xml: function(action, on_closed) {
159 self.rpc("/web/session/eval_domain_and_context", {
160 contexts: [action.context],
162 }).then(function(res) {
163 action = _.clone(action);
164 action.context = res.context;
165 self.session.get_file({
167 data: {action: JSON.stringify(action)},
168 complete: $.unblockUI,
170 if (!self.dialog && on_closed) {
178 ir_actions_act_url: function (action) {
179 window.open(action.url, action.target === 'self' ? '_self' : '_blank');
181 ir_ui_menu: function (action) {
182 this.widget_parent.do_action(action);
186 session.web.ViewManager = session.web.Widget.extend(/** @lends session.web.ViewManager# */{
187 identifier_prefix: "viewmanager",
188 template: "ViewManager",
190 * @constructs session.web.ViewManager
191 * @extends session.web.Widget
197 init: function(parent, dataset, views) {
199 this.model = dataset ? dataset.model : undefined;
200 this.dataset = dataset;
201 this.searchview = null;
202 this.active_view = null;
203 this.views_src = _.map(views, function(x) {return x instanceof Array? {view_id: x[0], view_type: x[1]} : x;});
205 this.flags = this.flags || {};
206 this.registry = session.web.views;
207 this.views_history = [];
210 return session.web.qweb.render(this.template, {
212 prefix: this.element_id,
213 views: this.views_src});
216 * @returns {jQuery.Deferred} initial view loading promise
221 this.$element.find('.oe_vm_switch button').click(function() {
222 self.on_mode_switch($(this).data('view-type'));
225 _.each(this.views_src, function(view) {
226 self.views[view.view_type] = $.extend({}, view, {
227 deferred : $.Deferred(),
230 sidebar_id : self.element_id + '_sidebar_' + view.view_type,
231 action : self.action,
232 action_views_ids : views_ids
233 }, self.flags, self.flags[view.view_type] || {}, view.options || {})
235 views_ids[view.view_type] = view.view_id;
237 if (this.flags.views_switcher === false) {
238 this.$element.find('.oe_vm_switch').hide();
240 // If no default view defined, switch to the first one in sequence
241 var default_view = this.flags.default_view || this.views_src[0].view_type;
242 return this.on_mode_switch(default_view);
245 * Asks the view manager to switch visualization mode.
247 * @param {String} view_type type of view to display
248 * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack
249 * @returns {jQuery.Deferred} new view loading promise
251 on_mode_switch: function(view_type, no_store) {
253 view = this.views[view_type],
256 return $.Deferred().reject();
259 this.views_history.push(view_type);
261 this.active_view = view_type;
263 if (!view.controller) {
264 // Lazy loading of views
265 var controllerclass = this.registry.get_object(view_type);
266 var controller = new controllerclass(this, this.dataset, view.view_id, view.options);
267 if (view.embedded_view) {
268 controller.set_embedded_view(view.embedded_view);
270 controller.do_switch_view.add_last(this.on_mode_switch);
271 controller.do_prev_view.add_last(this.on_prev_view);
272 var container = $("#" + this.element_id + '_view_' + view_type);
273 view_promise = controller.appendTo(container);
274 this.views[view_type].controller = controller;
275 this.views[view_type].deferred.resolve();
276 $.when(view_promise).then(function() {
277 self.on_controller_inited(view_type, controller);
278 if (self.searchview && view.controller.searchable !== false) {
279 self.searchview.ready.then(self.searchview.do_search);
282 } else if (this.searchview && view.controller.searchable !== false) {
283 this.searchview.ready.then(this.searchview.do_search);
286 if (this.searchview) {
287 this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
291 .find('.oe_vm_switch button').removeAttr('disabled')
292 .filter('[data-view-type="' + view_type + '"]')
293 .attr('disabled', true);
295 for (var view_name in this.views) {
296 if (!this.views.hasOwnProperty(view_name)) { continue; }
297 if (this.views[view_name].controller) {
298 if (view_name === view_type) {
299 $.when(view_promise).then(this.views[view_name].controller.do_show);
301 this.views[view_name].controller.do_hide();
305 $.when(view_promise).then(function () {
306 self.$element.find('.oe_view_title_text:first').text(
307 self.display_title());
312 * Returns to the view preceding the caller view in this manager's
313 * navigation history (the navigation history is appended to via
316 * @param {Boolean} [created=false] returning from a creation
317 * @returns {$.Deferred} switching end signal
319 on_prev_view: function (created) {
320 var current_view = this.views_history.pop();
321 var previous_view = this.views_history[this.views_history.length - 1];
322 // APR special case: "If creation mode from list (and only from a list),
323 // after saving, go to page view (don't come back in list)"
324 if (created && current_view === 'form' && previous_view === 'list') {
325 return this.on_mode_switch('page');
327 return this.on_mode_switch(previous_view, true);
330 * Sets up the current viewmanager's search view.
332 * @param {Number|false} view_id the view to use or false for a default one
333 * @returns {jQuery.Deferred} search view startup deferred
335 setup_search_view: function(view_id, search_defaults) {
337 if (this.searchview) {
338 this.searchview.stop();
340 this.searchview = new session.web.SearchView(
342 view_id, search_defaults, this.flags.search_view === false);
344 this.searchview.on_search.add(this.do_searchview_search);
345 return this.searchview.appendTo($("#" + this.element_id + "_search"));
347 do_searchview_search: function(domains, contexts, groupbys) {
349 controller = this.views[this.active_view].controller;
350 this.rpc('/web/session/eval_domain_and_context', {
351 domains: [this.action.domain || []].concat(domains || []),
352 contexts: [this.action.context || {}].concat(contexts || []),
353 group_by_seq: groupbys || []
354 }, function (results) {
355 self.dataset.context = results.context;
356 self.dataset.domain = results.domain;
357 controller.do_search(results.domain, results.context, results.group_by);
361 * Event launched when a controller has been inited.
363 * @param {String} view_type type of view
364 * @param {String} view the inited controller
366 on_controller_inited: function(view_type, view) {
369 * Called when one of the view want to execute an action
371 on_action: function(action) {
373 on_create: function() {
375 on_remove: function() {
377 on_edit: function() {
380 * Called by children view after executing an action
382 on_action_executed: function () {},
383 display_title: function () {
384 var view = this.views[this.active_view];
387 return view.controller.fields_view.arch.attrs.string;
393 session.web.ViewManagerAction = session.web.ViewManager.extend(/** @lends oepnerp.web.ViewManagerAction# */{
394 template:"ViewManagerAction",
396 * @constructs session.web.ViewManagerAction
397 * @extends session.web.ViewManager
399 * @param {session.web.ActionManager} parent parent object/widget
400 * @param {Object} action descriptor for the action this viewmanager needs to manage its views.
402 init: function(parent, action) {
403 // dataset initialization will take the session from ``this``, so if we
404 // do not have it yet (and we don't, because we've not called our own
405 // ``_super()``) rpc requests will blow up.
406 this._super(parent, null, action.views);
407 this.session = parent.session;
408 this.action = action;
409 var dataset = new session.web.DataSetSearch(this, action.res_model, action.context, action.domain);
411 dataset.ids.push(action.res_id);
414 this.dataset = dataset;
415 this.flags = this.action.flags || {};
416 if (action.res_model == 'board.board' && action.view_mode === 'form') {
417 // Special case for Dashboards
418 _.extend(this.flags, {
419 views_switcher : false,
420 display_title : false,
424 action_buttons : false
428 // setup storage for session-wise menu hiding
429 if (this.session.hidden_menutips) {
432 this.session.hidden_menutips = {}
435 * Initializes the ViewManagerAction: sets up the searchview (if the
436 * searchview is enabled in the manager's action flags), calls into the
437 * parent to initialize the primary view and (if the VMA has a searchview)
438 * launches an initial search after both views are done rendering.
443 search_defaults = {};
444 _.each(this.action.context, function (value, key) {
445 var match = /^search_default_(.*)$/.exec(key);
447 search_defaults[match[1]] = value;
451 var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
453 searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
455 var main_view_loaded = this._super();
457 var manager_ready = $.when(searchview_loaded, main_view_loaded);
459 this.$element.find('.oe_get_xml_view').click(function () {
460 var view = self.views[self.active_view].controller,
461 view_id = view.fields_view.view_id;
463 view.on_sidebar_edit_resource('ir.ui.view', view_id);
466 if (this.action.help && !this.flags.low_profile) {
467 var Users = new session.web.DataSet(self, 'res.users'),
468 $tips = this.$element.find('.oe_view_manager_menu_tips');
469 $tips.delegate('blockquote button', 'click', function() {
471 //noinspection FallthroughInSwitchStatementJS
472 switch ($this.attr('name')) {
474 Users.write(self.session.uid, {menu_tips:false});
476 $this.closest('blockquote').hide();
477 self.session.hidden_menutips[self.action.id] = true;
480 if (!(self.action.id in self.session.hidden_menutips)) {
481 Users.read_ids([this.session.uid], ['menu_tips'], function(users) {
483 if (!(user && user.id === self.session.uid)) {
486 $tips.find('blockquote').toggle(user.menu_tips);
491 var $res_logs = this.$element.find('.oe-view-manager-logs:first');
492 $res_logs.delegate('a.oe-more-logs', 'click', function () {
493 $res_logs.removeClass('oe-folded');
495 }).delegate('a.oe-remove-everything', 'click', function () {
496 $res_logs.removeClass('oe-has-more')
501 return manager_ready;
503 on_mode_switch: function (view_type, no_store) {
506 return $.when(this._super(view_type, no_store)).then(function () {
507 self.shortcut_check(self.views[view_type]);
509 self.$element.find('.oe-view-manager-logs:first')
510 .addClass('oe-folded').removeClass('oe-has-more')
513 var controller = self.views[self.active_view].controller,
514 fvg = controller.fields_view,
515 view_id = (fvg && fvg.view_id) || '--';
516 self.$element.find('.oe_get_xml_view span').text(view_id);
517 if (!self.action.name && fvg) {
518 self.$element.find('.oe_view_title_text').text(fvg.arch.attrs.string || fvg.name);
521 var $title = self.$element.find('.oe_view_title_text'),
522 $search_prefix = $title.find('span.oe_searchable_view');
523 if (controller.searchable !== false) {
524 if (!$search_prefix.length) {
525 $title.prepend('<span class="oe_searchable_view">' + _t("Search: ") + '</span>');
528 $search_prefix.remove();
532 shortcut_check : function(view) {
534 var grandparent = this.widget_parent && this.widget_parent.widget_parent;
535 // display shortcuts if on the first view for the action
536 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
537 if (!(grandparent instanceof session.web.WebClient) ||
538 !(view.view_type === this.views_src[0].view_type
539 && view.view_id === this.views_src[0].view_id)) {
540 $shortcut_toggle.hide();
543 $shortcut_toggle.removeClass('oe-shortcut-remove').show();
544 if (_(this.session.shortcuts).detect(function (shortcut) {
545 return shortcut.res_id === self.session.active_id; })) {
546 $shortcut_toggle.addClass("oe-shortcut-remove");
548 this.shortcut_add_remove();
550 shortcut_add_remove: function() {
552 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
556 if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
557 $(self.session.shortcuts.binding).trigger('remove-current');
558 $shortcut_toggle.removeClass("oe-shortcut-remove");
560 $(self.session.shortcuts.binding).trigger('add', {
561 'user_id': self.session.uid,
562 'res_id': self.session.active_id,
563 'resource': 'ir.ui.menu',
564 'name': self.action.name
566 $shortcut_toggle.addClass("oe-shortcut-remove");
571 * Intercept do_action resolution from children views
573 on_action_executed: function () {
574 return new session.web.DataSet(this, 'res.log')
575 .call('get', [], this.do_display_log);
578 * @param {Array<Object>} log_records
580 do_display_log: function (log_records) {
583 $logs = this.$element.find('.oe-view-manager-logs:first')
584 .addClass('oe-folded'),
585 $logs_list = $logs.find('ul').empty();
586 $logs.toggleClass('oe-has-more', log_records.length > cutoff);
587 _(log_records.reverse()).each(function (record) {
588 $(_.str.sprintf('<li><a href="#">%s</a></li>', record.name))
589 .appendTo($logs_list)
590 .delegate('a', 'click', function (e) {
592 type: 'ir.actions.act_window',
593 res_model: record.res_model,
594 res_id: record.res_id,
595 // TODO: need to have an evaluated context here somehow
596 //context: record.context,
597 views: [[false, 'form']]
603 display_title: function () {
604 return this.action.name;
608 session.web.Sidebar = session.web.Widget.extend({
609 init: function(parent, element_id) {
610 this._super(parent, element_id);
617 this.$element.html(session.web.qweb.render('Sidebar'));
618 this.$element.find(".toggle-sidebar").click(function(e) {
622 add_default_sections: function() {
624 view = this.widget_parent,
625 view_manager = view.widget_parent,
626 action = view_manager.action;
627 if (this.session.uid === 1) {
628 this.add_section(_t('Customize'), 'customize');
629 this.add_items('customize', [
631 label: _t("Manage Views"),
632 callback: view.on_sidebar_manage_views,
633 title: _t("Manage views of the current object")
635 label: _t("Edit Workflow"),
636 callback: view.on_sidebar_edit_workflow,
637 title: _t("Manage views of the current object"),
638 classname: 'oe_hide oe_sidebar_edit_workflow'
640 label: _t("Customize Object"),
641 callback: view.on_sidebar_customize_object,
642 title: _t("Manage views of the current object")
644 label: _t("Translate"),
645 callback: view.on_sidebar_translate,
646 title: _t("Technical translation")
651 this.add_section(_t('Other Options'), 'other');
652 this.add_items('other', [
655 callback: view.on_sidebar_import
658 callback: view.on_sidebar_export
660 label: _t("View Log"),
661 callback: view.on_sidebar_view_log,
662 classname: 'oe_hide oe_sidebar_view_log'
666 if (session.connection.debug) {
667 this.add_section("Debug", 'debug');
668 if (action && action.id) {
669 this.add_items('debug', [{
670 label: "Edit Action",
671 callback: function() {
672 view.on_sidebar_edit_resource(action.type, action.id);
676 if (view_manager.searchview && view_manager.searchview.view_id) {
677 this.add_items('debug', [{
678 label: "Edit SearchView",
679 callback: function() {
680 view.on_sidebar_edit_resource('ir.ui.view', view_manager.searchview.view_id);
687 add_toolbar: function(toolbar) {
689 _.each([['print', _t("Reports")], ['action', _t("Actions")], ['relate', _t("Links")]], function(type) {
690 var items = toolbar[type[0]];
692 for (var i = 0; i < items.length; i++) {
694 label: items[i]['name'],
696 classname: 'oe_sidebar_' + type[0]
699 self.add_section(type[1], type[0]);
700 self.add_items(type[0], items);
705 add_section: function(name, code) {
706 if(!code) code = _.str.underscored(name);
707 var $section = this.sections[code];
710 var section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
711 $section = $(session.web.qweb.render("Sidebar.section", {
712 section_id: section_id,
714 classname: 'oe_sidebar_' + code
716 $section.appendTo(this.$element.find('div.sidebar-actions'));
717 this.sections[code] = $section;
722 add_items: function(section_code, items) {
723 // An item is a dictonary : {
724 // label: label to be displayed for the link,
725 // action: action to be launch when the link is clicked,
726 // callback: a function to be executed when the link is clicked,
727 // classname: optional dom class name for the line,
728 // title: optional title for the link
730 // Note: The item should have one action or/and a callback
734 $section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
735 section_id = $section.attr('id');
738 for (var i = 0; i < items.length; i++) {
739 items[i].element_id = _.uniqueId(section_id + '_item_');
740 this.items[items[i].element_id] = items[i];
743 var $items = $(session.web.qweb.render("Sidebar.section.items", {items: items}));
745 $items.find('a.oe_sidebar_action_a').click(function() {
746 var item = self.items[$(this).attr('id')];
748 item.callback.apply(self, [item]);
751 self.on_item_action_clicked(item);
756 var $ul = $section.find('ul');
758 $ul = $('<ul/>').appendTo($section);
760 $items.appendTo($ul);
763 on_item_action_clicked: function(item) {
765 self.widget_parent.sidebar_context().then(function (context) {
766 var ids = self.widget_parent.get_selected_ids();
767 if (ids.length == 0) {
768 //TODO: make prettier warning?
769 $("<div />").text(_t("You must choose at least one record.")).dialog({
770 title: _t("Warning"),
775 var additional_context = _.extend({
778 active_model: self.widget_parent.dataset.model
780 self.rpc("/web/action/load", {
781 action_id: item.action.id,
782 context: additional_context
783 }, function(result) {
784 result.result.context = _.extend(result.result.context || {},
786 result.result.flags = result.result.flags || {};
787 result.result.flags.new_window = true;
788 self.do_action(result.result);
792 do_fold: function() {
793 this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
795 do_unfold: function() {
796 this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
798 do_toggle: function() {
799 this.$element.toggleClass('open-sidebar closed-sidebar');
803 session.web.TranslateDialog = session.web.Dialog.extend({
804 dialog_title: _t("Translations"),
805 init: function(view) {
806 // TODO fme: should add the language to fields_view_get because between the fields view get
807 // and the moment the user opens the translation dialog, the user language could have been changed
808 this.view_language = view.session.user_context.lang;
809 this['on_button' + _t("Save")] = this.on_button_Save;
810 this['on_button' + _t("Close")] = this.on_button_Close;
816 this.view_type = view.fields_view.type || '';
817 this.$fields_form = null;
818 this.$view_form = null;
819 this.$sidebar_form = null;
820 this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
821 this.languages = null;
822 this.languages_loaded = $.Deferred();
823 (new session.web.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
824 [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }, this.on_languages_loaded);
829 $.when(this.languages_loaded).then(function() {
830 self.$element.html(session.web.qweb.render('TranslateDialog', { widget: self }));
831 self.$fields_form = self.$element.find('.oe_translation_form');
832 self.$fields_form.find('.oe_trad_field').change(function() {
833 $(this).toggleClass('touched', ($(this).val() != $(this).attr('data-value')));
838 on_languages_loaded: function(langs) {
839 this.languages = langs;
840 this.languages_loaded.resolve();
842 do_load_fields_values: function(callback) {
845 this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
846 _.each(self.languages, function(lg) {
847 var deff = $.Deferred();
849 var callback = function(values) {
850 _.each(self.translatable_fields_keys, function(f) {
851 self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '').attr('data-value', values[0][f] || '');
855 if (lg.code === self.view_language) {
857 _.each(self.translatable_fields_keys, function(field) {
858 values[field] = self.view.fields[field].get_value();
862 self.rpc('/web/dataset/get', {
863 model: self.view.dataset.model,
864 ids: [self.view.datarecord.id],
865 fields: self.translatable_fields_keys,
866 context: self.view.dataset.get_context({
871 $.when.apply(null, deffered).then(callback);
873 open: function(field) {
876 $.when(this.languages_loaded).then(function() {
877 if (self.view.translatable_fields && self.view.translatable_fields.length) {
878 self.do_load_fields_values(function() {
881 var $field_input = self.$element.find('tr[data-field="' + field.name + '"] td:nth-child(2) *:first-child');
882 self.$element.scrollTo($field_input);
883 $field_input.focus();
891 on_button_Save: function() {
894 self.$fields_form.find('.oe_trad_field.touched').each(function() {
895 var field = $(this).attr('name').split('-');
896 if (!trads[field[0]]) {
897 trads[field[0]] = {};
899 trads[field[0]][field[1]] = $(this).val();
901 _.each(trads, function(data, code) {
902 if (code === self.view_language) {
903 _.each(data, function(value, field) {
904 self.view.fields[field].set_value(value);
907 self.view.dataset.write(self.view.datarecord.id, data, { 'lang': code });
912 on_button_Close: function() {
917 session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
918 template: "EmptyComponent",
919 set_default_options: function(options) {
920 this.options = options || {};
921 _.defaults(this.options, {
922 // All possible views options should be defaulted here
929 open_translate_dialog: function(field) {
930 if (!this.translate_dialog) {
931 this.translate_dialog = new session.web.TranslateDialog(this).start();
933 this.translate_dialog.open(field);
936 * Fetches and executes the action identified by ``action_data``.
938 * @param {Object} action_data the action descriptor data
939 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
940 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
941 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
942 * @param {Object} [action_data.context=null] additional action context, to add to the current context
943 * @param {session.web.DataSet} dataset a dataset object used to communicate with the server
944 * @param {Object} [record_id] the identifier of the object on which the action is to be applied
945 * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
947 do_execute_action: function (action_data, dataset, record_id, on_closed) {
949 var result_handler = function () {
950 if (on_closed) { on_closed.apply(null, arguments); }
951 if (self.widget_parent && self.widget_parent.on_action_executed) {
952 return self.widget_parent.on_action_executed.apply(null, arguments);
955 var context = new session.web.CompoundContext(dataset.get_context(), action_data.context || {});
957 var handler = function (r) {
958 var action = r.result;
959 if (action && action.constructor == Object) {
960 var ncontext = new session.web.CompoundContext(context);
963 active_id: record_id,
964 active_ids: [record_id],
965 active_model: dataset.model
968 ncontext.add(action.context || {});
969 return self.rpc('/web/session/eval_domain_and_context', {
970 contexts: [ncontext],
972 }).pipe(function (results) {
973 action.context = results.context;
974 /* niv: previously we were overriding once more with action_data.context,
975 * I assumed this was not a correct behavior and removed it
977 return self.do_action(action, result_handler);
980 return result_handler();
984 if (action_data.special) {
985 return handler({result: {"type":"ir.actions.act_window_close"}});
986 } else if (action_data.type=="object") {
987 var args = [[record_id]], additional_args = [];
988 if (action_data.args) {
990 // Warning: quotes and double quotes problem due to json and xml clash
991 // Maybe we should force escaping in xml or do a better parse of the args array
992 additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
993 args = args.concat(additional_args);
995 console.error("Could not JSON.parse arguments", action_data.args);
999 return dataset.call_button(action_data.name, args, handler);
1000 } else if (action_data.type=="action") {
1001 return this.rpc('/web/action/load', { action_id: parseInt(action_data.name, 10), context: context, do_not_eval: true}, handler);
1003 return dataset.exec_workflow(record_id, action_data.name, handler);
1007 * Directly set a view to use instead of calling fields_view_get. This method must
1008 * be called before start(). When an embedded view is set, underlying implementations
1009 * of session.web.View must use the provided view instead of any other one.
1011 * @param embedded_view A view.
1013 set_embedded_view: function(embedded_view) {
1014 this.embedded_view = embedded_view;
1015 this.options.sidebar = false;
1018 * Switches to a specific view type
1020 * @param {String} view view type to switch to
1022 do_switch_view: function(view) { },
1024 * Cancels the switch to the current view, switches to the previous one
1026 do_prev_view: function () { },
1027 do_search: function(view) {
1030 set_common_sidebar_sections: function(sidebar) {
1031 sidebar.add_default_sections();
1033 on_sidebar_manage_views: function() {
1034 if (this.fields_view && this.fields_view.arch) {
1035 var view_editor = new session.web.ViewEditor(this, this.$element, this.dataset, this.fields_view.arch);
1036 view_editor.start();
1038 this.do_warn("Manage Views", "Could not find current view declaration");
1041 on_sidebar_edit_workflow: function() {
1042 console.log('Todo');
1044 on_sidebar_customize_object: function() {
1046 this.rpc('/web/dataset/search_read', {
1049 domain: [['model', '=', self.dataset.model]]
1050 }, function (result) {
1051 self.on_sidebar_edit_resource('ir.model', result.ids[0]);
1054 on_sidebar_import: function() {
1055 var import_view = new session.web.DataImport(this, this.dataset);
1056 import_view.start();
1058 on_sidebar_export: function() {
1059 var export_view = new session.web.DataExport(this, this.dataset);
1060 export_view.start();
1062 on_sidebar_translate: function() {
1063 return this.do_action({
1064 res_model : 'ir.translation',
1065 domain : [['type', '!=', 'object'], '|', ['name', '=', this.dataset.model], ['name', 'ilike', this.dataset.model + ',']],
1066 views: [[false, 'list'], [false, 'form']],
1067 type : 'ir.actions.act_window',
1072 on_sidebar_edit_resource: function(model, id, domain) {
1075 type : 'ir.actions.act_window',
1080 action_buttons : true
1085 action.views = [[false, 'form']];
1086 } else if (domain) {
1087 action.views = [[false, 'list'], [false, 'form']];
1088 action.domain = domain;
1089 action.flags.views_switcher = true;
1091 this.do_action(action);
1093 on_sidebar_view_log: function() {
1095 sidebar_context: function () {
1096 return $.Deferred().resolve({}).promise();
1100 session.web.json_node_to_xml = function(node, human_readable, indent) {
1101 // For debugging purpose, this function will convert a json node back to xml
1102 // Maybe usefull for xml view editor
1103 indent = indent || 0;
1104 var sindent = (human_readable ? (new Array(indent + 1).join('\t')) : ''),
1105 r = sindent + '<' + node.tag,
1106 cr = human_readable ? '\n' : '';
1108 if (typeof(node) === 'string') {
1109 return sindent + node;
1110 } else if (typeof(node.tag) !== 'string' || !node.children instanceof Array || !node.attrs instanceof Object) {
1111 throw("Node a json node");
1113 for (var attr in node.attrs) {
1114 var vattr = node.attrs[attr];
1115 if (typeof(vattr) !== 'string') {
1117 vattr = JSON.stringify(vattr);
1119 vattr = vattr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
1120 if (human_readable) {
1121 vattr = vattr.replace(/"/g, "'");
1123 r += ' ' + attr + '="' + vattr + '"';
1125 if (node.children && node.children.length) {
1128 for (var i = 0, ii = node.children.length; i < ii; i++) {
1129 childs.push(session.web.json_node_to_xml(node.children[i], human_readable, indent + 1));
1131 r += childs.join(cr);
1132 r += cr + sindent + '</' + node.tag + '>';
1141 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: