1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.web.views = function(session) {
6 var QWeb = session.web.qweb,
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) {
24 this.inner_viewmanager = null;
26 this.dialog_viewmanager = null;
27 this.client_widget = null;
30 return "<div id='"+this.element_id+"'></div>";
32 dialog_stop: function () {
34 this.dialog_viewmanager.stop();
35 this.dialog_viewmanager = null;
40 content_stop: function () {
41 if (this.inner_viewmanager) {
42 this.inner_viewmanager.stop();
43 this.inner_viewmanager = null;
45 if (this.client_widget) {
46 this.client_widget.stop();
47 this.client_widget = null;
51 do_push_state: function(state) {
52 if (this.wiget_parent && this.widget_parent.do_push_state) {
54 state = _.extend({}, state || {}, {
55 action_id: this.action.id,
58 this.widget_parent.do_push_state(state);
62 do_load_state: function(state) {
63 if (state.action_id) {
65 this.do_action(state.action_id);
67 else if (state.model && state.id) {
72 else if (state.client_action) {
74 this.ir_actions_client(state.client_action);
77 if (this.inner_viewmanager) {
78 this.inner_viewmanager.do_load_state(state);
82 do_action: function(action, on_close) {
83 if (_.isNumber(action)) {
85 self.rpc("/web/action/load", { action_id: action }, function(result) {
86 self.do_action(result.result, on_close);
91 console.error("No type for action", action);
94 var type = action.type.replace(/\./g,'_');
95 var popup = action.target === 'new';
96 action.flags = _.extend({
97 views_switcher : !popup,
99 action_buttons : !popup,
102 }, action.flags || {});
103 if (!(type in this)) {
104 console.error("Action manager can't handle action of type " + action.type, action);
107 this.action = action;
108 return this[type](action, on_close);
110 null_action: function() {
114 ir_actions_act_window: function (action, on_close) {
116 if (_(['base.module.upgrade', 'base.setup.installer'])
117 .contains(action.res_model)) {
118 var old_close = on_close;
119 on_close = function () {
120 session.webclient.do_reload();
121 if (old_close) { old_close(); }
124 if (action.target === 'new') {
125 if (this.dialog == null) {
126 this.dialog = new session.web.Dialog(this, { title: action.name, width: '80%' });
128 this.dialog.on_close.add(on_close);
131 this.dialog_viewmanager.stop();
133 this.dialog_viewmanager = new session.web.ViewManagerAction(this, action);
134 this.dialog_viewmanager.appendTo(this.dialog.$element);
139 this.inner_action = action;
140 this.inner_viewmanager = new session.web.ViewManagerAction(this, action);
141 this.inner_viewmanager.appendTo(this.$element);
144 ir_actions_act_window_close: function (action, on_closed) {
145 if (!this.dialog && on_closed) {
150 ir_actions_server: function (action, on_closed) {
152 this.rpc('/web/action/run', {
153 action_id: action.id,
154 context: action.context || {}
155 }).then(function (action) {
156 self.do_action(action, on_closed)
159 ir_actions_client: function (action) {
162 var ClientWidget = session.web.client_actions.get_object(action.tag);
163 (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
165 ir_actions_report_xml: function(action, on_closed) {
168 self.rpc("/web/session/eval_domain_and_context", {
169 contexts: [action.context],
171 }).then(function(res) {
172 action = _.clone(action);
173 action.context = res.context;
174 self.session.get_file({
176 data: {action: JSON.stringify(action)},
177 complete: $.unblockUI,
179 if (!self.dialog && on_closed) {
187 ir_actions_act_url: function (action) {
188 window.open(action.url, action.target === 'self' ? '_self' : '_blank');
190 ir_ui_menu: function (action) {
191 this.widget_parent.do_action(action);
195 session.web.ViewManager = session.web.Widget.extend(/** @lends session.web.ViewManager# */{
196 identifier_prefix: "viewmanager",
197 template: "ViewManager",
199 * @constructs session.web.ViewManager
200 * @extends session.web.Widget
206 init: function(parent, dataset, views, flags) {
208 this.model = dataset ? dataset.model : undefined;
209 this.dataset = dataset;
210 this.searchview = null;
211 this.active_view = null;
212 this.views_src = _.map(views, function(x) {return x instanceof Array? {view_id: x[0], view_type: x[1]} : x;});
214 this.flags = flags || {};
215 this.registry = session.web.views;
216 this.views_history = [];
219 return session.web.qweb.render(this.template, {
221 prefix: this.element_id,
222 views: this.views_src});
225 * @returns {jQuery.Deferred} initial view loading promise
230 this.$element.find('.oe_vm_switch button').click(function() {
231 self.on_mode_switch($(this).data('view-type'));
234 _.each(this.views_src, function(view) {
235 self.views[view.view_type] = $.extend({}, view, {
236 deferred : $.Deferred(),
239 sidebar_id : self.element_id + '_sidebar_' + view.view_type,
240 action : self.action,
241 action_views_ids : views_ids
242 }, self.flags, self.flags[view.view_type] || {}, view.options || {})
244 views_ids[view.view_type] = view.view_id;
246 if (this.flags.views_switcher === false) {
247 this.$element.find('.oe_vm_switch').hide();
249 // If no default view defined, switch to the first one in sequence
250 var default_view = this.flags.default_view || this.views_src[0].view_type;
251 return this.on_mode_switch(default_view);
254 * Asks the view manager to switch visualization mode.
256 * @param {String} view_type type of view to display
257 * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack
258 * @returns {jQuery.Deferred} new view loading promise
260 on_mode_switch: function(view_type, no_store) {
262 view = this.views[view_type],
265 return $.Deferred().reject();
268 this.views_history.push(view_type);
270 this.active_view = view_type;
272 if (!view.controller) {
273 // Lazy loading of views
274 var controllerclass = this.registry.get_object(view_type);
275 var controller = new controllerclass(this, this.dataset, view.view_id, view.options);
276 if (view.embedded_view) {
277 controller.set_embedded_view(view.embedded_view);
279 controller.do_switch_view.add_last(this.on_mode_switch);
280 controller.do_prev_view.add_last(this.on_prev_view);
281 var container = $("#" + this.element_id + '_view_' + view_type);
282 view_promise = controller.appendTo(container);
283 this.views[view_type].controller = controller;
284 this.views[view_type].deferred.resolve(view_type);
285 $.when(view_promise).then(function() {
286 self.on_controller_inited(view_type, controller);
287 if (self.searchview && view.controller.searchable !== false) {
288 self.searchview.ready.then(self.searchview.do_search);
291 } else if (this.searchview && view.controller.searchable !== false) {
292 this.searchview.ready.then(this.searchview.do_search);
295 if (this.searchview) {
296 this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
300 .find('.oe_vm_switch button').removeAttr('disabled')
301 .filter('[data-view-type="' + view_type + '"]')
302 .attr('disabled', true);
304 for (var view_name in this.views) {
305 if (!this.views.hasOwnProperty(view_name)) { continue; }
306 if (this.views[view_name].controller) {
307 if (view_name === view_type) {
308 $.when(view_promise).then(this.views[view_name].controller.do_show);
310 this.views[view_name].controller.do_hide();
314 $.when(view_promise).then(function () {
315 self.$element.find('.oe_view_title_text:first').text(
316 self.display_title());
323 * Returns to the view preceding the caller view in this manager's
324 * navigation history (the navigation history is appended to via
327 * @param {Boolean} [created=false] returning from a creation
328 * @returns {$.Deferred} switching end signal
330 on_prev_view: function (created) {
331 var current_view = this.views_history.pop();
332 var previous_view = this.views_history[this.views_history.length - 1];
333 // APR special case: "If creation mode from list (and only from a list),
334 // after saving, go to page view (don't come back in list)"
335 if (created && current_view === 'form' && previous_view === 'list') {
336 return this.on_mode_switch('page');
338 return this.on_mode_switch(previous_view, true);
341 * Sets up the current viewmanager's search view.
343 * @param {Number|false} view_id the view to use or false for a default one
344 * @returns {jQuery.Deferred} search view startup deferred
346 setup_search_view: function(view_id, search_defaults) {
348 if (this.searchview) {
349 this.searchview.stop();
351 this.searchview = new session.web.SearchView(
353 view_id, search_defaults, this.flags.search_view === false);
355 this.searchview.on_search.add(this.do_searchview_search);
356 return this.searchview.appendTo($("#" + this.element_id + "_search"));
358 do_searchview_search: function(domains, contexts, groupbys) {
360 controller = this.views[this.active_view].controller,
361 action_context = this.action.context || {};
362 this.rpc('/web/session/eval_domain_and_context', {
363 domains: [this.action.domain || []].concat(domains || []),
364 contexts: [action_context].concat(contexts || []),
365 group_by_seq: groupbys || []
366 }, function (results) {
367 self.dataset.context = results.context;
368 self.dataset.domain = results.domain;
369 var groupby = results.group_by.length
371 : action_context.group_by;
372 controller.do_search(results.domain, results.context, groupby || []);
376 * Event launched when a controller has been inited.
378 * @param {String} view_type type of view
379 * @param {String} view the inited controller
381 on_controller_inited: function(view_type, view) {
384 * Called when one of the view want to execute an action
386 on_action: function(action) {
388 on_create: function() {
390 on_remove: function() {
392 on_edit: function() {
395 * Called by children view after executing an action
397 on_action_executed: function () {},
398 display_title: function () {
399 var view = this.views[this.active_view];
402 return view.controller.fields_view.arch.attrs.string;
408 session.web.ViewManagerAction = session.web.ViewManager.extend(/** @lends oepnerp.web.ViewManagerAction# */{
409 template:"ViewManagerAction",
411 * @constructs session.web.ViewManagerAction
412 * @extends session.web.ViewManager
414 * @param {session.web.ActionManager} parent parent object/widget
415 * @param {Object} action descriptor for the action this viewmanager needs to manage its views.
417 init: function(parent, action) {
418 // dataset initialization will take the session from ``this``, so if we
419 // do not have it yet (and we don't, because we've not called our own
420 // ``_super()``) rpc requests will blow up.
421 var flags = action.flags || {};
422 if (action.res_model == 'board.board' && action.view_mode === 'form') {
423 // Special case for Dashboards
425 views_switcher : false,
426 display_title : false,
430 action_buttons : false
433 this._super(parent, null, action.views, flags);
434 this.session = parent.session;
435 this.action = action;
436 var dataset = new session.web.DataSetSearch(this, action.res_model, action.context, action.domain);
438 dataset.ids.push(action.res_id);
441 this.dataset = dataset;
443 // setup storage for session-wise menu hiding
444 if (this.session.hidden_menutips) {
447 this.session.hidden_menutips = {}
450 * Initializes the ViewManagerAction: sets up the searchview (if the
451 * searchview is enabled in the manager's action flags), calls into the
452 * parent to initialize the primary view and (if the VMA has a searchview)
453 * launches an initial search after both views are done rendering.
458 search_defaults = {};
459 _.each(this.action.context, function (value, key) {
460 var match = /^search_default_(.*)$/.exec(key);
462 search_defaults[match[1]] = value;
466 var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
468 searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
470 var main_view_loaded = this._super();
472 var manager_ready = $.when(searchview_loaded, main_view_loaded);
474 this.$element.find('.oe_debug_view').change(this.on_debug_changed);
476 if (this.action.help && !this.flags.low_profile) {
477 var Users = new session.web.DataSet(self, 'res.users'),
478 $tips = this.$element.find('.oe_view_manager_menu_tips');
479 $tips.delegate('blockquote button', 'click', function() {
481 //noinspection FallthroughInSwitchStatementJS
482 switch ($this.attr('name')) {
484 Users.write(self.session.uid, {menu_tips:false});
486 $this.closest('blockquote').hide();
487 self.session.hidden_menutips[self.action.id] = true;
490 if (!(self.action.id in self.session.hidden_menutips)) {
491 Users.read_ids([this.session.uid], ['menu_tips'], function(users) {
493 if (!(user && user.id === self.session.uid)) {
496 $tips.find('blockquote').toggle(user.menu_tips);
501 var $res_logs = this.$element.find('.oe-view-manager-logs:first');
502 $res_logs.delegate('a.oe-more-logs', 'click', function () {
503 $res_logs.removeClass('oe-folded');
505 }).delegate('a.oe-remove-everything', 'click', function () {
506 $res_logs.removeClass('oe-has-more')
511 return manager_ready;
513 on_debug_changed: function (evt) {
514 var $sel = $(evt.currentTarget),
515 $option = $sel.find('option:selected'),
519 $('<pre>').text(session.web.json_node_to_xml(
520 this.views[this.active_view].controller.fields_view.arch, true)
521 ).dialog({ width: '95%'});
524 var model = $option.data('model'),
525 id = $option.data('id'),
526 domain = $option.data('domain'),
529 type : 'ir.actions.act_window',
534 action_buttons : true
539 action.views = [[false, 'form']];
541 action.views = [[false, 'list'], [false, 'form']];
542 action.domain = domain;
543 action.flags.views_switcher = true;
545 this.do_action(action);
549 console.log("No debug handler for ", val);
552 evt.currentTarget.selectedIndex = 0;
554 on_mode_switch: function (view_type, no_store) {
557 return $.when(this._super(view_type, no_store)).then(function () {
558 self.shortcut_check(self.views[view_type]);
560 self.$element.find('.oe-view-manager-logs:first')
561 .addClass('oe-folded').removeClass('oe-has-more')
564 var controller = self.views[self.active_view].controller,
565 fvg = controller.fields_view,
566 view_id = (fvg && fvg.view_id) || '--';
567 self.$element.find('.oe_debug_view').html(QWeb.render('ViewManagerDebug', {
571 if (!self.action.name && fvg) {
572 self.$element.find('.oe_view_title_text').text(fvg.arch.attrs.string || fvg.name);
575 var $title = self.$element.find('.oe_view_title_text'),
576 $search_prefix = $title.find('span.oe_searchable_view');
577 if (controller.searchable !== false) {
578 if (!$search_prefix.length) {
579 $title.prepend('<span class="oe_searchable_view">' + _t("Search: ") + '</span>');
582 $search_prefix.remove();
587 do_push_state: function(state) {
588 if (this.wiget_parent && this.widget_parent.do_push_state) {
589 state = _.extend({}, state || {}, {
590 view_type: this.active_view
592 this.widget_parent.do_push_state(state);
596 do_load_state: function(state) {
598 $.when(this.on_mode_switch(state.view_type, true)).done(function() {
599 self.views[self.active_view].controller.do_load_state(state);
603 shortcut_check : function(view) {
605 var grandparent = this.widget_parent && this.widget_parent.widget_parent;
606 // display shortcuts if on the first view for the action
607 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
608 if (!(grandparent instanceof session.web.WebClient) ||
609 !(view.view_type === this.views_src[0].view_type
610 && view.view_id === this.views_src[0].view_id)) {
611 $shortcut_toggle.hide();
614 $shortcut_toggle.removeClass('oe-shortcut-remove').show();
615 if (_(this.session.shortcuts).detect(function (shortcut) {
616 return shortcut.res_id === self.session.active_id; })) {
617 $shortcut_toggle.addClass("oe-shortcut-remove");
619 this.shortcut_add_remove();
621 shortcut_add_remove: function() {
623 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
627 if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
628 $(self.session.shortcuts.binding).trigger('remove-current');
629 $shortcut_toggle.removeClass("oe-shortcut-remove");
631 $(self.session.shortcuts.binding).trigger('add', {
632 'user_id': self.session.uid,
633 'res_id': self.session.active_id,
634 'resource': 'ir.ui.menu',
635 'name': self.action.name
637 $shortcut_toggle.addClass("oe-shortcut-remove");
642 * Intercept do_action resolution from children views
644 on_action_executed: function () {
645 return new session.web.DataSet(this, 'res.log')
646 .call('get', [], this.do_display_log);
649 * @param {Array<Object>} log_records
651 do_display_log: function (log_records) {
654 $logs = this.$element.find('.oe-view-manager-logs:first')
655 .addClass('oe-folded'),
656 $logs_list = $logs.find('ul').empty();
657 $logs.toggleClass('oe-has-more', log_records.length > cutoff);
658 _(log_records.reverse()).each(function (record) {
659 $(_.str.sprintf('<li><a href="#">%s</a></li>', record.name))
660 .appendTo($logs_list)
661 .delegate('a', 'click', function (e) {
663 type: 'ir.actions.act_window',
664 res_model: record.res_model,
665 res_id: record.res_id,
666 // TODO: need to have an evaluated context here somehow
667 //context: record.context,
668 views: [[false, 'form']]
674 display_title: function () {
675 return this.action.name;
679 session.web.Sidebar = session.web.Widget.extend({
680 init: function(parent, element_id) {
681 this._super(parent, element_id);
688 this.$element.html(session.web.qweb.render('Sidebar'));
689 this.$element.find(".toggle-sidebar").click(function(e) {
693 add_default_sections: function() {
695 view = this.widget_parent,
696 view_manager = view.widget_parent,
697 action = view_manager.action;
698 if (this.session.uid === 1) {
699 this.add_section(_t('Customize'), 'customize');
700 this.add_items('customize', [
702 label: _t("Manage Views"),
703 callback: view.on_sidebar_manage_views,
704 title: _t("Manage views of the current object")
706 label: _t("Edit Workflow"),
707 callback: view.on_sidebar_edit_workflow,
708 title: _t("Manage views of the current object"),
709 classname: 'oe_hide oe_sidebar_edit_workflow'
711 label: _t("Customize Object"),
712 callback: view.on_sidebar_customize_object,
713 title: _t("Manage views of the current object")
715 label: _t("Translate"),
716 callback: view.on_sidebar_translate,
717 title: _t("Technical translation")
722 this.add_section(_t('Other Options'), 'other');
723 this.add_items('other', [
726 callback: view.on_sidebar_import
729 callback: view.on_sidebar_export
731 label: _t("View Log"),
732 callback: view.on_sidebar_view_log,
733 classname: 'oe_hide oe_sidebar_view_log'
738 add_toolbar: function(toolbar) {
740 _.each([['print', _t("Reports")], ['action', _t("Actions")], ['relate', _t("Links")]], function(type) {
741 var items = toolbar[type[0]];
743 for (var i = 0; i < items.length; i++) {
745 label: items[i]['name'],
747 classname: 'oe_sidebar_' + type[0]
750 self.add_section(type[1], type[0]);
751 self.add_items(type[0], items);
756 add_section: function(name, code) {
757 if(!code) code = _.str.underscored(name);
758 var $section = this.sections[code];
761 var section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
762 $section = $(session.web.qweb.render("Sidebar.section", {
763 section_id: section_id,
765 classname: 'oe_sidebar_' + code
767 $section.appendTo(this.$element.find('div.sidebar-actions'));
768 this.sections[code] = $section;
773 add_items: function(section_code, items) {
774 // An item is a dictonary : {
775 // label: label to be displayed for the link,
776 // action: action to be launch when the link is clicked,
777 // callback: a function to be executed when the link is clicked,
778 // classname: optional dom class name for the line,
779 // title: optional title for the link
781 // Note: The item should have one action or/and a callback
785 $section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
786 section_id = $section.attr('id');
789 for (var i = 0; i < items.length; i++) {
790 items[i].element_id = _.uniqueId(section_id + '_item_');
791 this.items[items[i].element_id] = items[i];
794 var $items = $(session.web.qweb.render("Sidebar.section.items", {items: items}));
796 $items.find('a.oe_sidebar_action_a').click(function() {
797 var item = self.items[$(this).attr('id')];
799 item.callback.apply(self, [item]);
802 self.on_item_action_clicked(item);
807 var $ul = $section.find('ul');
809 $ul = $('<ul/>').appendTo($section);
811 $items.appendTo($ul);
814 on_item_action_clicked: function(item) {
816 self.widget_parent.sidebar_context().then(function (context) {
817 var ids = self.widget_parent.get_selected_ids();
818 if (ids.length == 0) {
819 //TODO: make prettier warning?
820 $("<div />").text(_t("You must choose at least one record.")).dialog({
821 title: _t("Warning"),
826 var additional_context = _.extend({
829 active_model: self.widget_parent.dataset.model
831 self.rpc("/web/action/load", {
832 action_id: item.action.id,
833 context: additional_context
834 }, function(result) {
835 result.result.context = _.extend(result.result.context || {},
837 result.result.flags = result.result.flags || {};
838 result.result.flags.new_window = true;
839 self.do_action(result.result);
843 do_fold: function() {
844 this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
846 do_unfold: function() {
847 this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
849 do_toggle: function() {
850 this.$element.toggleClass('open-sidebar closed-sidebar');
854 session.web.TranslateDialog = session.web.Dialog.extend({
855 dialog_title: _t("Translations"),
856 init: function(view) {
857 // TODO fme: should add the language to fields_view_get because between the fields view get
858 // and the moment the user opens the translation dialog, the user language could have been changed
859 this.view_language = view.session.user_context.lang;
860 this['on_button' + _t("Save")] = this.on_button_Save;
861 this['on_button' + _t("Close")] = this.on_button_Close;
867 this.view_type = view.fields_view.type || '';
868 this.$fields_form = null;
869 this.$view_form = null;
870 this.$sidebar_form = null;
871 this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
872 this.languages = null;
873 this.languages_loaded = $.Deferred();
874 (new session.web.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
875 [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }, this.on_languages_loaded);
880 $.when(this.languages_loaded).then(function() {
881 self.$element.html(session.web.qweb.render('TranslateDialog', { widget: self }));
882 self.$fields_form = self.$element.find('.oe_translation_form');
883 self.$fields_form.find('.oe_trad_field').change(function() {
884 $(this).toggleClass('touched', ($(this).val() != $(this).attr('data-value')));
889 on_languages_loaded: function(langs) {
890 this.languages = langs;
891 this.languages_loaded.resolve();
893 do_load_fields_values: function(callback) {
896 this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
897 _.each(self.languages, function(lg) {
898 var deff = $.Deferred();
900 var callback = function(values) {
901 _.each(self.translatable_fields_keys, function(f) {
902 self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '').attr('data-value', values[0][f] || '');
906 if (lg.code === self.view_language) {
908 _.each(self.translatable_fields_keys, function(field) {
909 values[field] = self.view.fields[field].get_value();
913 self.rpc('/web/dataset/get', {
914 model: self.view.dataset.model,
915 ids: [self.view.datarecord.id],
916 fields: self.translatable_fields_keys,
917 context: self.view.dataset.get_context({
922 $.when.apply(null, deffered).then(callback);
924 open: function(field) {
927 $.when(this.languages_loaded).then(function() {
928 if (self.view.translatable_fields && self.view.translatable_fields.length) {
929 self.do_load_fields_values(function() {
932 var $field_input = self.$element.find('tr[data-field="' + field.name + '"] td:nth-child(2) *:first-child');
933 self.$element.scrollTo($field_input);
934 $field_input.focus();
942 on_button_Save: function() {
945 self.$fields_form.find('.oe_trad_field.touched').each(function() {
946 var field = $(this).attr('name').split('-');
947 if (!trads[field[0]]) {
948 trads[field[0]] = {};
950 trads[field[0]][field[1]] = $(this).val();
952 _.each(trads, function(data, code) {
953 if (code === self.view_language) {
954 _.each(data, function(value, field) {
955 self.view.fields[field].set_value(value);
958 self.view.dataset.write(self.view.datarecord.id, data, { 'lang': code });
963 on_button_Close: function() {
968 session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
969 template: "EmptyComponent",
970 set_default_options: function(options) {
971 this.options = options || {};
972 _.defaults(this.options, {
973 // All possible views options should be defaulted here
980 open_translate_dialog: function(field) {
981 if (!this.translate_dialog) {
982 this.translate_dialog = new session.web.TranslateDialog(this).start();
984 this.translate_dialog.open(field);
987 * Fetches and executes the action identified by ``action_data``.
989 * @param {Object} action_data the action descriptor data
990 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
991 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
992 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
993 * @param {Object} [action_data.context=null] additional action context, to add to the current context
994 * @param {session.web.DataSet} dataset a dataset object used to communicate with the server
995 * @param {Object} [record_id] the identifier of the object on which the action is to be applied
996 * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
998 do_execute_action: function (action_data, dataset, record_id, on_closed) {
1000 var result_handler = function () {
1001 if (on_closed) { on_closed.apply(null, arguments); }
1002 if (self.widget_parent && self.widget_parent.on_action_executed) {
1003 return self.widget_parent.on_action_executed.apply(null, arguments);
1006 var context = new session.web.CompoundContext(dataset.get_context(), action_data.context || {});
1008 var handler = function (r) {
1009 var action = r.result;
1010 if (action && action.constructor == Object) {
1011 var ncontext = new session.web.CompoundContext(context);
1014 active_id: record_id,
1015 active_ids: [record_id],
1016 active_model: dataset.model
1019 ncontext.add(action.context || {});
1020 return self.rpc('/web/session/eval_domain_and_context', {
1021 contexts: [ncontext],
1023 }).pipe(function (results) {
1024 action.context = results.context;
1025 /* niv: previously we were overriding once more with action_data.context,
1026 * I assumed this was not a correct behavior and removed it
1028 return self.do_action(action, result_handler);
1031 return result_handler();
1035 if (action_data.special) {
1036 return handler({result: {"type":"ir.actions.act_window_close"}});
1037 } else if (action_data.type=="object") {
1038 var args = [[record_id]], additional_args = [];
1039 if (action_data.args) {
1041 // Warning: quotes and double quotes problem due to json and xml clash
1042 // Maybe we should force escaping in xml or do a better parse of the args array
1043 additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
1044 args = args.concat(additional_args);
1046 console.error("Could not JSON.parse arguments", action_data.args);
1050 return dataset.call_button(action_data.name, args, handler);
1051 } else if (action_data.type=="action") {
1052 return this.rpc('/web/action/load', { action_id: parseInt(action_data.name, 10), context: context, do_not_eval: true}, handler);
1054 return dataset.exec_workflow(record_id, action_data.name, handler);
1058 * Directly set a view to use instead of calling fields_view_get. This method must
1059 * be called before start(). When an embedded view is set, underlying implementations
1060 * of session.web.View must use the provided view instead of any other one.
1062 * @param embedded_view A view.
1064 set_embedded_view: function(embedded_view) {
1065 this.embedded_view = embedded_view;
1066 this.options.sidebar = false;
1068 do_show: function () {
1069 this.$element.show();
1071 do_hide: function () {
1072 this.$element.hide();
1075 * Switches to a specific view type
1077 * @param {String} view view type to switch to
1079 do_switch_view: function(view) {
1082 * Cancels the switch to the current view, switches to the previous one
1084 do_prev_view: function () {
1086 do_search: function(view) {
1088 set_common_sidebar_sections: function(sidebar) {
1089 sidebar.add_default_sections();
1091 on_sidebar_manage_views: function() {
1092 if (this.fields_view && this.fields_view.arch) {
1093 var view_editor = new session.web.ViewEditor(this, this.$element, this.dataset, this.fields_view.arch);
1094 view_editor.start();
1096 this.do_warn("Manage Views", "Could not find current view declaration");
1099 on_sidebar_edit_workflow: function() {
1100 console.log('Todo');
1102 on_sidebar_customize_object: function() {
1104 this.rpc('/web/dataset/search_read', {
1107 domain: [['model', '=', self.dataset.model]]
1108 }, function (result) {
1109 self.on_sidebar_edit_resource('ir.model', result.ids[0]);
1112 on_sidebar_import: function() {
1113 var import_view = new session.web.DataImport(this, this.dataset);
1114 import_view.start();
1116 on_sidebar_export: function() {
1117 var export_view = new session.web.DataExport(this, this.dataset);
1118 export_view.start();
1120 on_sidebar_translate: function() {
1121 return this.do_action({
1122 res_model : 'ir.translation',
1123 domain : [['type', '!=', 'object'], '|', ['name', '=', this.dataset.model], ['name', 'ilike', this.dataset.model + ',']],
1124 views: [[false, 'list'], [false, 'form']],
1125 type : 'ir.actions.act_window',
1130 on_sidebar_edit_resource: function(model, id, domain) {
1133 type : 'ir.actions.act_window',
1138 action_buttons : true
1143 action.views = [[false, 'form']];
1144 } else if (domain) {
1145 action.views = [[false, 'list'], [false, 'form']];
1146 action.domain = domain;
1147 action.flags.views_switcher = true;
1149 this.do_action(action);
1151 on_sidebar_view_log: function() {
1153 sidebar_context: function () {
1154 return $.Deferred().resolve({}).promise();
1156 do_push_state: function(state) {
1157 if (this.wiget_parent && this.widget_parent.do_push_state) {
1158 this.widget_parent.do_push_state(state);
1161 do_load_state: function(state) {
1165 session.web.json_node_to_xml = function(node, human_readable, indent) {
1166 // For debugging purpose, this function will convert a json node back to xml
1167 // Maybe usefull for xml view editor
1168 indent = indent || 0;
1169 var sindent = (human_readable ? (new Array(indent + 1).join('\t')) : ''),
1170 r = sindent + '<' + node.tag,
1171 cr = human_readable ? '\n' : '';
1173 if (typeof(node) === 'string') {
1174 return sindent + node;
1175 } else if (typeof(node.tag) !== 'string' || !node.children instanceof Array || !node.attrs instanceof Object) {
1176 throw("Node a json node");
1178 for (var attr in node.attrs) {
1179 var vattr = node.attrs[attr];
1180 if (typeof(vattr) !== 'string') {
1182 vattr = JSON.stringify(vattr);
1184 vattr = vattr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
1185 if (human_readable) {
1186 vattr = vattr.replace(/"/g, "'");
1188 r += ' ' + attr + '="' + vattr + '"';
1190 if (node.children && node.children.length) {
1193 for (var i = 0, ii = node.children.length; i < ii; i++) {
1194 childs.push(session.web.json_node_to_xml(node.children[i], human_readable, indent + 1));
1196 r += childs.join(cr);
1197 r += cr + sindent + '</' + node.tag + '>';
1206 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: