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');
183 session.web.ViewManager = session.web.Widget.extend(/** @lends session.web.ViewManager# */{
184 identifier_prefix: "viewmanager",
185 template: "ViewManager",
187 * @constructs session.web.ViewManager
188 * @extends session.web.Widget
194 init: function(parent, dataset, views) {
196 this.model = dataset ? dataset.model : undefined;
197 this.dataset = dataset;
198 this.searchview = null;
199 this.active_view = null;
200 this.views_src = _.map(views, function(x) {return x instanceof Array? {view_id: x[0], view_type: x[1]} : x;});
202 this.flags = this.flags || {};
203 this.registry = session.web.views;
204 this.views_history = [];
207 return session.web.qweb.render(this.template, {
209 prefix: this.element_id,
210 views: this.views_src});
213 * @returns {jQuery.Deferred} initial view loading promise
218 this.$element.find('.oe_vm_switch button').click(function() {
219 self.on_mode_switch($(this).data('view-type'));
222 _.each(this.views_src, function(view) {
223 self.views[view.view_type] = $.extend({}, view, {
224 deferred : $.Deferred(),
227 sidebar_id : self.element_id + '_sidebar_' + view.view_type,
228 action : self.action,
229 action_views_ids : views_ids
230 }, self.flags, self.flags[view.view_type] || {}, view.options || {})
232 views_ids[view.view_type] = view.view_id;
234 if (this.flags.views_switcher === false) {
235 this.$element.find('.oe_vm_switch').hide();
237 // switch to the first one in sequence
238 return this.on_mode_switch(this.views_src[0].view_type);
241 * Asks the view manager to switch visualization mode.
243 * @param {String} view_type type of view to display
244 * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack
245 * @returns {jQuery.Deferred} new view loading promise
247 on_mode_switch: function(view_type, no_store) {
249 view = this.views[view_type],
252 return $.Deferred().reject();
255 this.views_history.push(view_type);
257 this.active_view = view_type;
259 if (!view.controller) {
260 // Lazy loading of views
261 var controllerclass = this.registry.get_object(view_type);
262 var controller = new controllerclass(this, this.dataset, view.view_id, view.options);
263 if (view.embedded_view) {
264 controller.set_embedded_view(view.embedded_view);
266 controller.do_switch_view.add_last(this.on_mode_switch);
267 controller.do_prev_view.add_last(this.on_prev_view);
268 var container = $("#" + this.element_id + '_view_' + view_type);
269 view_promise = controller.appendTo(container);
270 this.views[view_type].controller = controller;
271 this.views[view_type].deferred.resolve();
272 $.when(view_promise).then(function() {
273 self.on_controller_inited(view_type, controller);
274 if (self.searchview && view.controller.searchable !== false) {
275 self.searchview.ready.then(self.searchview.do_search);
278 } else if (this.searchview && view.controller.searchable !== false) {
279 this.searchview.ready.then(this.searchview.do_search);
282 if (this.searchview) {
283 this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
287 .find('.oe_vm_switch button').removeAttr('disabled')
288 .filter('[data-view-type="' + view_type + '"]')
289 .attr('disabled', true);
291 for (var view_name in this.views) {
292 if (!this.views.hasOwnProperty(view_name)) { continue; }
293 if (this.views[view_name].controller) {
294 if (view_name === view_type) {
295 $.when(view_promise).then(this.views[view_name].controller.do_show);
297 this.views[view_name].controller.do_hide();
301 $.when(view_promise).then(function () {
302 self.$element.find('.oe_view_title_text:first').text(
303 self.display_title());
308 * Returns to the view preceding the caller view in this manager's
309 * navigation history (the navigation history is appended to via
312 * @param {Boolean} [created=false] returning from a creation
313 * @returns {$.Deferred} switching end signal
315 on_prev_view: function (created) {
316 var current_view = this.views_history.pop();
317 var previous_view = this.views_history[this.views_history.length - 1];
318 // APR special case: "If creation mode from list (and only from a list),
319 // after saving, go to page view (don't come back in list)"
320 if (created && current_view === 'form' && previous_view === 'list') {
321 return this.on_mode_switch('page');
323 return this.on_mode_switch(previous_view, true);
326 * Sets up the current viewmanager's search view.
328 * @param {Number|false} view_id the view to use or false for a default one
329 * @returns {jQuery.Deferred} search view startup deferred
331 setup_search_view: function(view_id, search_defaults) {
333 if (this.searchview) {
334 this.searchview.stop();
336 this.searchview = new session.web.SearchView(
338 view_id, search_defaults, this.flags.search_view === false);
340 this.searchview.on_search.add(this.do_searchview_search);
341 return this.searchview.appendTo($("#" + this.element_id + "_search"));
343 do_searchview_search: function(domains, contexts, groupbys) {
345 controller = this.views[this.active_view].controller;
346 this.rpc('/web/session/eval_domain_and_context', {
347 domains: [this.action.domain || []].concat(domains || []),
348 contexts: [this.action.context || {}].concat(contexts || []),
349 group_by_seq: groupbys || []
350 }, function (results) {
351 self.dataset.context = results.context;
352 self.dataset.domain = results.domain;
353 controller.do_search(results.domain, results.context, results.group_by);
357 * Event launched when a controller has been inited.
359 * @param {String} view_type type of view
360 * @param {String} view the inited controller
362 on_controller_inited: function(view_type, view) {
365 * Called when one of the view want to execute an action
367 on_action: function(action) {
369 on_create: function() {
371 on_remove: function() {
373 on_edit: function() {
376 * Called by children view after executing an action
378 on_action_executed: function () {},
379 display_title: function () {
380 var view = this.views[this.active_view];
383 return view.controller.fields_view.arch.attrs.string;
389 session.web.ViewManagerAction = session.web.ViewManager.extend(/** @lends oepnerp.web.ViewManagerAction# */{
390 template:"ViewManagerAction",
392 * @constructs session.web.ViewManagerAction
393 * @extends session.web.ViewManager
395 * @param {session.web.ActionManager} parent parent object/widget
396 * @param {Object} action descriptor for the action this viewmanager needs to manage its views.
398 init: function(parent, action) {
399 // dataset initialization will take the session from ``this``, so if we
400 // do not have it yet (and we don't, because we've not called our own
401 // ``_super()``) rpc requests will blow up.
402 this._super(parent, null, action.views);
403 this.session = parent.session;
404 this.action = action;
405 var dataset = new session.web.DataSetSearch(this, action.res_model, action.context, action.domain);
407 dataset.ids.push(action.res_id);
410 this.dataset = dataset;
411 this.flags = this.action.flags || {};
412 if (action.res_model == 'board.board' && action.view_mode === 'form') {
413 // Special case for Dashboards
414 _.extend(this.flags, {
415 views_switcher : false,
416 display_title : false,
420 action_buttons : false
424 // setup storage for session-wise menu hiding
425 if (this.session.hidden_menutips) {
428 this.session.hidden_menutips = {}
431 * Initializes the ViewManagerAction: sets up the searchview (if the
432 * searchview is enabled in the manager's action flags), calls into the
433 * parent to initialize the primary view and (if the VMA has a searchview)
434 * launches an initial search after both views are done rendering.
439 search_defaults = {};
440 _.each(this.action.context, function (value, key) {
441 var match = /^search_default_(.*)$/.exec(key);
443 search_defaults[match[1]] = value;
447 var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
449 searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
451 var main_view_loaded = this._super();
453 var manager_ready = $.when(searchview_loaded, main_view_loaded);
455 this.$element.find('.oe_get_xml_view').click(function () {
456 var view = self.views[self.active_view].controller,
457 view_id = view.fields_view.view_id;
459 view.on_sidebar_edit_resource('ir.ui.view', view_id);
462 if (this.action.help && !this.flags.low_profile) {
463 var Users = new session.web.DataSet(self, 'res.users'),
464 $tips = this.$element.find('.oe_view_manager_menu_tips');
465 $tips.delegate('blockquote button', 'click', function() {
467 //noinspection FallthroughInSwitchStatementJS
468 switch ($this.attr('name')) {
470 Users.write(self.session.uid, {menu_tips:false});
472 $this.closest('blockquote').hide();
473 self.session.hidden_menutips[self.action.id] = true;
476 if (!(self.action.id in self.session.hidden_menutips)) {
477 Users.read_ids([this.session.uid], ['menu_tips'], function(users) {
479 if (!(user && user.id === self.session.uid)) {
482 $tips.find('blockquote').toggle(user.menu_tips);
487 var $res_logs = this.$element.find('.oe-view-manager-logs:first');
488 $res_logs.delegate('a.oe-more-logs', 'click', function () {
489 $res_logs.removeClass('oe-folded');
491 }).delegate('a.oe-remove-everything', 'click', function () {
492 $res_logs.removeClass('oe-has-more')
497 return manager_ready;
499 on_mode_switch: function (view_type, no_store) {
502 return $.when(this._super(view_type, no_store)).then(function () {
503 self.shortcut_check(self.views[view_type]);
505 self.$element.find('.oe-view-manager-logs:first')
506 .addClass('oe-folded').removeClass('oe-has-more')
509 var controller = self.views[self.active_view].controller,
510 fvg = controller.fields_view,
511 view_id = (fvg && fvg.view_id) || '--';
512 self.$element.find('.oe_get_xml_view span').text(view_id);
513 if (!self.action.name && fvg) {
514 self.$element.find('.oe_view_title_text').text(fvg.arch.attrs.string || fvg.name);
517 var $title = self.$element.find('.oe_view_title_text'),
518 $search_prefix = $title.find('span.oe_searchable_view');
519 if (controller.searchable !== false) {
520 if (!$search_prefix.length) {
521 $title.prepend('<span class="oe_searchable_view">' + _t("Search: ") + '</span>');
524 $search_prefix.remove();
528 shortcut_check : function(view) {
530 var grandparent = this.widget_parent && this.widget_parent.widget_parent;
531 // display shortcuts if on the first view for the action
532 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
533 if (!(grandparent instanceof session.web.WebClient) ||
534 !(view.view_type === this.views_src[0].view_type
535 && view.view_id === this.views_src[0].view_id)) {
536 $shortcut_toggle.hide();
539 $shortcut_toggle.removeClass('oe-shortcut-remove').show();
540 if (_(this.session.shortcuts).detect(function (shortcut) {
541 return shortcut.res_id === self.session.active_id; })) {
542 $shortcut_toggle.addClass("oe-shortcut-remove");
544 this.shortcut_add_remove();
546 shortcut_add_remove: function() {
548 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
552 if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
553 $(self.session.shortcuts.binding).trigger('remove-current');
554 $shortcut_toggle.removeClass("oe-shortcut-remove");
556 $(self.session.shortcuts.binding).trigger('add', {
557 'user_id': self.session.uid,
558 'res_id': self.session.active_id,
559 'resource': 'ir.ui.menu',
560 'name': self.action.name
562 $shortcut_toggle.addClass("oe-shortcut-remove");
567 * Intercept do_action resolution from children views
569 on_action_executed: function () {
570 return new session.web.DataSet(this, 'res.log')
571 .call('get', [], this.do_display_log);
574 * @param {Array<Object>} log_records
576 do_display_log: function (log_records) {
579 $logs = this.$element.find('.oe-view-manager-logs:first')
580 .addClass('oe-folded'),
581 $logs_list = $logs.find('ul').empty();
582 $logs.toggleClass('oe-has-more', log_records.length > cutoff);
583 _(log_records.reverse()).each(function (record) {
584 $(_.str.sprintf('<li><a href="#">%s</a></li>', record.name))
585 .appendTo($logs_list)
586 .delegate('a', 'click', function (e) {
588 type: 'ir.actions.act_window',
589 res_model: record.res_model,
590 res_id: record.res_id,
591 // TODO: need to have an evaluated context here somehow
592 //context: record.context,
593 views: [[false, 'form']]
599 display_title: function () {
600 return this.action.name;
604 session.web.Sidebar = session.web.Widget.extend({
605 init: function(parent, element_id) {
606 this._super(parent, element_id);
613 this.$element.html(session.web.qweb.render('Sidebar'));
614 this.$element.find(".toggle-sidebar").click(function(e) {
618 add_default_sections: function() {
620 view = this.widget_parent,
621 view_manager = view.widget_parent,
622 action = view_manager.action;
623 if (this.session.uid === 1) {
624 this.add_section(_t('Customize'), 'customize');
625 this.add_items('customize', [
627 label: _t("Manage Views"),
628 callback: view.on_sidebar_manage_views,
629 title: _t("Manage views of the current object")
631 label: _t("Edit Workflow"),
632 callback: view.on_sidebar_edit_workflow,
633 title: _t("Manage views of the current object"),
634 classname: 'oe_hide oe_sidebar_edit_workflow'
636 label: _t("Customize Object"),
637 callback: view.on_sidebar_customize_object,
638 title: _t("Manage views of the current object")
640 label: _t("Translate"),
641 callback: view.on_sidebar_translate,
642 title: _t("Technical translation")
647 this.add_section(_t('Other Options'), 'other');
648 this.add_items('other', [
651 callback: view.on_sidebar_import
654 callback: view.on_sidebar_export
656 label: _t("View Log"),
657 callback: view.on_sidebar_view_log,
658 classname: 'oe_hide oe_sidebar_view_log'
662 if (session.connection.debug) {
663 this.add_section("Debug", 'debug');
664 if (action && action.id) {
665 this.add_items('debug', [{
666 label: "Edit Action",
667 callback: function() {
668 view.on_sidebar_edit_resource(action.type, action.id);
672 if (view_manager.searchview && view_manager.searchview.view_id) {
673 this.add_items('debug', [{
674 label: "Edit SearchView",
675 callback: function() {
676 view.on_sidebar_edit_resource('ir.ui.view', view_manager.searchview.view_id);
683 add_toolbar: function(toolbar) {
685 _.each([['print', _t("Reports")], ['action', _t("Actions")], ['relate', _t("Links")]], function(type) {
686 var items = toolbar[type[0]];
688 for (var i = 0; i < items.length; i++) {
690 label: items[i]['name'],
692 classname: 'oe_sidebar_' + type[0]
695 self.add_section(type[1], type[0]);
696 self.add_items(type[0], items);
701 add_section: function(name, code) {
702 if(!code) code = _.str.underscored(name);
703 var $section = this.sections[code];
706 var section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
707 $section = $(session.web.qweb.render("Sidebar.section", {
708 section_id: section_id,
710 classname: 'oe_sidebar_' + code
712 $section.appendTo(this.$element.find('div.sidebar-actions'));
713 this.sections[code] = $section;
718 add_items: function(section_code, items) {
719 // An item is a dictonary : {
720 // label: label to be displayed for the link,
721 // action: action to be launch when the link is clicked,
722 // callback: a function to be executed when the link is clicked,
723 // classname: optional dom class name for the line,
724 // title: optional title for the link
726 // Note: The item should have one action or/and a callback
730 $section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
731 section_id = $section.attr('id');
734 for (var i = 0; i < items.length; i++) {
735 items[i].element_id = _.uniqueId(section_id + '_item_');
736 this.items[items[i].element_id] = items[i];
739 var $items = $(session.web.qweb.render("Sidebar.section.items", {items: items}));
741 $items.find('a.oe_sidebar_action_a').click(function() {
742 var item = self.items[$(this).attr('id')];
744 item.callback.apply(self, [item]);
747 self.on_item_action_clicked(item);
752 var $ul = $section.find('ul');
754 $ul = $('<ul/>').appendTo($section);
756 $items.appendTo($ul);
759 on_item_action_clicked: function(item) {
761 self.widget_parent.sidebar_context().then(function (context) {
762 var ids = self.widget_parent.get_selected_ids();
763 if (ids.length == 0) {
764 //TODO: make prettier warning?
765 $("<div />").text(_t("You must choose at least one record.")).dialog({
766 title: _t("Warning"),
771 var additional_context = _.extend({
774 active_model: self.widget_parent.dataset.model
776 self.rpc("/web/action/load", {
777 action_id: item.action.id,
778 context: additional_context
779 }, function(result) {
780 result.result.context = _.extend(result.result.context || {},
782 result.result.flags = result.result.flags || {};
783 result.result.flags.new_window = true;
784 self.do_action(result.result);
788 do_fold: function() {
789 this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
791 do_unfold: function() {
792 this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
794 do_toggle: function() {
795 this.$element.toggleClass('open-sidebar closed-sidebar');
799 session.web.TranslateDialog = session.web.Dialog.extend({
800 dialog_title: _t("Translations"),
801 init: function(view) {
802 // TODO fme: should add the language to fields_view_get because between the fields view get
803 // and the moment the user opens the translation dialog, the user language could have been changed
804 this.view_language = view.session.user_context.lang;
805 this['on_button' + _t("Save")] = this.on_button_Save;
806 this['on_button' + _t("Close")] = this.on_button_Close;
812 this.view_type = view.fields_view.type || '';
813 this.$fields_form = null;
814 this.$view_form = null;
815 this.$sidebar_form = null;
816 this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
817 this.languages = null;
818 this.languages_loaded = $.Deferred();
819 (new session.web.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
820 [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }, this.on_languages_loaded);
825 $.when(this.languages_loaded).then(function() {
826 self.$element.html(session.web.qweb.render('TranslateDialog', { widget: self }));
827 self.$fields_form = self.$element.find('.oe_translation_form');
828 self.$fields_form.find('.oe_trad_field').change(function() {
829 $(this).toggleClass('touched', ($(this).val() != $(this).attr('data-value')));
834 on_languages_loaded: function(langs) {
835 this.languages = langs;
836 this.languages_loaded.resolve();
838 do_load_fields_values: function(callback) {
841 this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
842 _.each(self.languages, function(lg) {
843 var deff = $.Deferred();
845 var callback = function(values) {
846 _.each(self.translatable_fields_keys, function(f) {
847 self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '').attr('data-value', values[0][f] || '');
851 if (lg.code === self.view_language) {
853 _.each(self.translatable_fields_keys, function(field) {
854 values[field] = self.view.fields[field].get_value();
858 self.rpc('/web/dataset/get', {
859 model: self.view.dataset.model,
860 ids: [self.view.datarecord.id],
861 fields: self.translatable_fields_keys,
862 context: self.view.dataset.get_context({
867 $.when.apply(null, deffered).then(callback);
869 open: function(field) {
872 $.when(this.languages_loaded).then(function() {
873 if (self.view.translatable_fields && self.view.translatable_fields.length) {
874 self.do_load_fields_values(function() {
877 var $field_input = self.$element.find('tr[data-field="' + field.name + '"] td:nth-child(2) *:first-child');
878 self.$element.scrollTo($field_input);
879 $field_input.focus();
887 on_button_Save: function() {
890 self.$fields_form.find('.oe_trad_field.touched').each(function() {
891 var field = $(this).attr('name').split('-');
892 if (!trads[field[0]]) {
893 trads[field[0]] = {};
895 trads[field[0]][field[1]] = $(this).val();
897 _.each(trads, function(data, code) {
898 if (code === self.view_language) {
899 _.each(data, function(value, field) {
900 self.view.fields[field].set_value(value);
903 self.view.dataset.write(self.view.datarecord.id, data, { 'lang': code });
908 on_button_Close: function() {
913 session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
914 template: "EmptyComponent",
915 set_default_options: function(options) {
916 this.options = options || {};
917 _.defaults(this.options, {
918 // All possible views options should be defaulted here
925 open_translate_dialog: function(field) {
926 if (!this.translate_dialog) {
927 this.translate_dialog = new session.web.TranslateDialog(this).start();
929 this.translate_dialog.open(field);
932 * Fetches and executes the action identified by ``action_data``.
934 * @param {Object} action_data the action descriptor data
935 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
936 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
937 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
938 * @param {Object} [action_data.context=null] additional action context, to add to the current context
939 * @param {session.web.DataSet} dataset a dataset object used to communicate with the server
940 * @param {Object} [record_id] the identifier of the object on which the action is to be applied
941 * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
943 do_execute_action: function (action_data, dataset, record_id, on_closed) {
945 var result_handler = function () {
946 if (on_closed) { on_closed.apply(null, arguments); }
947 if (self.widget_parent && self.widget_parent.on_action_executed) {
948 return self.widget_parent.on_action_executed.apply(null, arguments);
951 var context = new session.web.CompoundContext(dataset.get_context(), action_data.context || {});
953 var handler = function (r) {
954 var action = r.result;
955 if (action && action.constructor == Object) {
956 var ncontext = new session.web.CompoundContext(context);
959 active_id: record_id,
960 active_ids: [record_id],
961 active_model: dataset.model
964 ncontext.add(action.context || {});
965 return self.rpc('/web/session/eval_domain_and_context', {
966 contexts: [ncontext],
968 }).pipe(function (results) {
969 action.context = results.context;
970 /* niv: previously we were overriding once more with action_data.context,
971 * I assumed this was not a correct behavior and removed it
973 return self.do_action(action, result_handler);
976 return result_handler();
980 if (action_data.special) {
981 return handler({result: {"type":"ir.actions.act_window_close"}});
982 } else if (action_data.type=="object") {
983 var args = [[record_id]], additional_args = [];
984 if (action_data.args) {
986 // Warning: quotes and double quotes problem due to json and xml clash
987 // Maybe we should force escaping in xml or do a better parse of the args array
988 additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
989 args = args.concat(additional_args);
991 console.error("Could not JSON.parse arguments", action_data.args);
995 return dataset.call_button(action_data.name, args, handler);
996 } else if (action_data.type=="action") {
997 return this.rpc('/web/action/load', { action_id: parseInt(action_data.name, 10), context: context, do_not_eval: true}, handler);
999 return dataset.exec_workflow(record_id, action_data.name, handler);
1003 * Directly set a view to use instead of calling fields_view_get. This method must
1004 * be called before start(). When an embedded view is set, underlying implementations
1005 * of session.web.View must use the provided view instead of any other one.
1007 * @param embedded_view A view.
1009 set_embedded_view: function(embedded_view) {
1010 this.embedded_view = embedded_view;
1011 this.options.sidebar = false;
1014 * Switches to a specific view type
1016 * @param {String} view view type to switch to
1018 do_switch_view: function(view) { },
1020 * Cancels the switch to the current view, switches to the previous one
1022 do_prev_view: function () { },
1023 do_search: function(view) {
1026 set_common_sidebar_sections: function(sidebar) {
1027 sidebar.add_default_sections();
1029 on_sidebar_manage_views: function() {
1030 if (this.fields_view && this.fields_view.arch) {
1031 var view_editor = new session.web.ViewEditor(this, this.$element, this.dataset, this.fields_view.arch);
1032 view_editor.start();
1034 this.do_warn("Manage Views", "Could not find current view declaration");
1037 on_sidebar_edit_workflow: function() {
1038 console.log('Todo');
1040 on_sidebar_customize_object: function() {
1042 this.rpc('/web/dataset/search_read', {
1045 domain: [['model', '=', self.dataset.model]]
1046 }, function (result) {
1047 self.on_sidebar_edit_resource('ir.model', result.ids[0]);
1050 on_sidebar_import: function() {
1051 var import_view = new session.web.DataImport(this, this.dataset);
1052 import_view.start();
1054 on_sidebar_export: function() {
1055 var export_view = new session.web.DataExport(this, this.dataset);
1056 export_view.start();
1058 on_sidebar_translate: function() {
1059 return this.do_action({
1060 res_model : 'ir.translation',
1061 domain : [['type', '!=', 'object'], '|', ['name', '=', this.dataset.model], ['name', 'ilike', this.dataset.model + ',']],
1062 views: [[false, 'list'], [false, 'form']],
1063 type : 'ir.actions.act_window',
1068 on_sidebar_edit_resource: function(model, id, domain) {
1071 type : 'ir.actions.act_window',
1076 action_buttons : true
1081 action.views = [[false, 'form']];
1082 } else if (domain) {
1083 action.views = [[false, 'list'], [false, 'form']];
1084 action.domain = domain;
1085 action.flags.views_switcher = true;
1087 this.do_action(action);
1089 on_sidebar_view_log: function() {
1091 sidebar_context: function () {
1092 return $.Deferred().resolve({}).promise();
1096 session.web.json_node_to_xml = function(node, human_readable, indent) {
1097 // For debugging purpose, this function will convert a json node back to xml
1098 // Maybe usefull for xml view editor
1099 indent = indent || 0;
1100 var sindent = (human_readable ? (new Array(indent + 1).join('\t')) : ''),
1101 r = sindent + '<' + node.tag,
1102 cr = human_readable ? '\n' : '';
1104 if (typeof(node) === 'string') {
1105 return sindent + node;
1106 } else if (typeof(node.tag) !== 'string' || !node.children instanceof Array || !node.attrs instanceof Object) {
1107 throw("Node a json node");
1109 for (var attr in node.attrs) {
1110 var vattr = node.attrs[attr];
1111 if (typeof(vattr) !== 'string') {
1113 vattr = JSON.stringify(vattr);
1115 vattr = vattr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
1116 if (human_readable) {
1117 vattr = vattr.replace(/"/g, "'");
1119 r += ' ' + attr + '="' + vattr + '"';
1121 if (node.children && node.children.length) {
1124 for (var i = 0, ii = node.children.length; i < ii; i++) {
1125 childs.push(session.web.json_node_to_xml(node.children[i], human_readable, indent + 1));
1127 r += childs.join(cr);
1128 r += cr + sindent + '</' + node.tag + '>';
1137 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: