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) {
23 this.inner_action = null;
24 this.inner_viewmanager = null;
26 this.dialog_viewmanager = null;
27 this.client_widget = null;
30 return '<div id="' + this.element_id + '" style="height: 100%;"></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;
50 do_push_state: function(state) {
51 if (this.widget_parent && this.widget_parent.do_push_state) {
52 if (this.inner_action) {
53 if (this.inner_action.id) {
54 state['action_id'] = this.inner_action.id;
56 state['model'] = this.inner_action.res_model;
59 this.widget_parent.do_push_state(state);
62 do_load_state: function(state) {
65 if (state.action_id) {
66 var run_action = (!this.inner_viewmanager) || this.inner_viewmanager.action.id !== state.action_id;
69 action_loaded = this.do_action(state.action_id);
72 else if (state.model && state.id) {
73 // TODO handle context & domain ?
76 res_model: state.model,
78 type: 'ir.actions.act_window',
79 views: [[false, 'page'], [false, 'form']]
81 action_loaded = this.do_action(action);
83 else if (state.client_action) {
85 this.ir_actions_client(state.client_action);
88 $.when(action_loaded || null).then(function() {
89 if (self.inner_viewmanager) {
90 self.inner_viewmanager.do_load_state(state);
94 do_action: function(action, on_close) {
95 if (_.isNumber(action)) {
97 return self.rpc("/web/action/load", { action_id: action }, function(result) {
98 self.do_action(result.result, on_close);
102 console.error("No type for action", action);
105 var type = action.type.replace(/\./g,'_');
106 var popup = action.target === 'new';
107 action.flags = _.extend({
108 views_switcher : !popup,
109 search_view : !popup,
110 action_buttons : !popup,
113 display_title : !popup
114 }, action.flags || {});
115 if (!(type in this)) {
116 console.error("Action manager can't handle action of type " + action.type, action);
119 return this[type](action, on_close);
121 null_action: function() {
125 ir_actions_act_window: function (action, on_close) {
127 if (_(['base.module.upgrade', 'base.setup.installer'])
128 .contains(action.res_model)) {
129 var old_close = on_close;
130 on_close = function () {
131 session.webclient.do_reload();
132 if (old_close) { old_close(); }
135 if (action.target === 'new') {
136 if (this.dialog == null) {
137 this.dialog = new session.web.Dialog(this, { width: '80%' });
139 this.dialog.on_close.add(on_close);
141 this.dialog_viewmanager.stop();
143 this.dialog.dialog_title = action.name;
144 this.dialog_viewmanager = new session.web.ViewManagerAction(this, action);
145 this.dialog_viewmanager.appendTo(this.dialog.$element);
149 return this.widget_parent.do_action(action, function () {
150 session.webclient.menu.open_menu(action.menu_id);
155 this.inner_action = action;
156 this.inner_viewmanager = new session.web.ViewManagerAction(this, action);
157 this.inner_viewmanager.appendTo(this.$element);
160 ir_actions_act_window_close: function (action, on_closed) {
161 if (!this.dialog && on_closed) {
166 ir_actions_server: function (action, on_closed) {
168 this.rpc('/web/action/run', {
169 action_id: action.id,
170 context: action.context || {}
171 }).then(function (action) {
172 self.do_action(action, on_closed)
175 ir_actions_client: function (action) {
178 var ClientWidget = session.web.client_actions.get_object(action.tag);
179 (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
181 ir_actions_report_xml: function(action, on_closed) {
184 self.rpc("/web/session/eval_domain_and_context", {
185 contexts: [action.context],
187 }).then(function(res) {
188 action = _.clone(action);
189 action.context = res.context;
190 self.session.get_file({
192 data: {action: JSON.stringify(action)},
193 complete: $.unblockUI,
195 if (!self.dialog && on_closed) {
203 ir_actions_act_url: function (action) {
204 window.open(action.url, action.target === 'self' ? '_self' : '_blank');
206 ir_ui_menu: function (action) {
207 this.widget_parent.do_action(action);
211 session.web.ViewManager = session.web.Widget.extend(/** @lends session.web.ViewManager# */{
212 identifier_prefix: "viewmanager",
213 template: "ViewManager",
215 * @constructs session.web.ViewManager
216 * @extends session.web.Widget
222 init: function(parent, dataset, views, flags) {
224 this.model = dataset ? dataset.model : undefined;
225 this.dataset = dataset;
226 this.searchview = null;
227 this.active_view = null;
228 this.views_src = _.map(views, function(x) {
229 if (x instanceof Array) {
230 var View = session.web.views.get_object(x[1], true);
234 label: View ? View.prototype.display_name : (void 'nope')
241 this.flags = flags || {};
242 this.registry = session.web.views;
243 this.views_history = [];
246 return session.web.qweb.render(this.template, {
248 prefix: this.element_id,
249 views: this.views_src});
252 * @returns {jQuery.Deferred} initial view loading promise
257 this.$element.find('.oe_vm_switch button').click(function() {
258 self.on_mode_switch($(this).data('view-type'));
261 _.each(this.views_src, function(view) {
262 self.views[view.view_type] = $.extend({}, view, {
263 deferred : $.Deferred(),
266 sidebar_id : self.element_id + '_sidebar_' + view.view_type,
267 action : self.action,
268 action_views_ids : views_ids
269 }, self.flags, self.flags[view.view_type] || {}, view.options || {})
271 views_ids[view.view_type] = view.view_id;
273 if (this.flags.views_switcher === false) {
274 this.$element.find('.oe_vm_switch').hide();
276 // If no default view defined, switch to the first one in sequence
277 var default_view = this.flags.default_view || this.views_src[0].view_type;
278 return this.on_mode_switch(default_view);
281 * Asks the view manager to switch visualization mode.
283 * @param {String} view_type type of view to display
284 * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack
285 * @returns {jQuery.Deferred} new view loading promise
287 on_mode_switch: function(view_type, no_store) {
289 view = this.views[view_type],
292 return $.Deferred().reject();
295 this.views_history.push(view_type);
297 this.active_view = view_type;
299 if (!view.controller) {
300 // Lazy loading of views
301 var controllerclass = this.registry.get_object(view_type);
302 var controller = new controllerclass(this, this.dataset, view.view_id, view.options);
303 if (view.embedded_view) {
304 controller.set_embedded_view(view.embedded_view);
306 controller.do_switch_view.add_last(this.on_mode_switch);
307 controller.do_prev_view.add_last(this.on_prev_view);
308 var container = $("#" + this.element_id + '_view_' + view_type);
309 view_promise = controller.appendTo(container);
310 this.views[view_type].controller = controller;
311 this.views[view_type].deferred.resolve(view_type);
312 $.when(view_promise).then(function() {
313 self.on_controller_inited(view_type, controller);
314 if (self.searchview && view.controller.searchable !== false) {
315 self.searchview.ready.then(self.searchview.do_search);
318 } else if (this.searchview && view.controller.searchable !== false) {
319 this.searchview.ready.then(this.searchview.do_search);
322 if (this.searchview) {
323 this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
327 .find('.oe_vm_switch button').removeAttr('disabled')
328 .filter('[data-view-type="' + view_type + '"]')
329 .attr('disabled', true);
331 $.when(view_promise).then(function () {
332 _.each(_.keys(self.views), function(view_name) {
333 var controller = self.views[view_name].controller;
335 if (view_name === view_type) {
336 controller.do_show();
338 controller.do_hide();
343 self.$element.find('.oe_view_title_text:first').text(
344 self.display_title());
349 * Returns to the view preceding the caller view in this manager's
350 * navigation history (the navigation history is appended to via
353 * @param {Boolean} [created=false] returning from a creation
354 * @returns {$.Deferred} switching end signal
356 on_prev_view: function (created) {
357 var current_view = this.views_history.pop();
358 var previous_view = this.views_history[this.views_history.length - 1];
359 if (created && current_view === 'form' && previous_view === 'list') {
360 // APR special case: "If creation mode from list (and only from a list),
361 // after saving, go to page view (don't come back in list)"
362 return this.on_mode_switch('page');
363 } else if (created && !previous_view && this.action && this.action.flags.default_view === 'form') {
364 // APR special case: "If creation from dashboard, we have no previous view
365 return this.on_mode_switch('page');
367 return this.on_mode_switch(previous_view, true);
370 * Sets up the current viewmanager's search view.
372 * @param {Number|false} view_id the view to use or false for a default one
373 * @returns {jQuery.Deferred} search view startup deferred
375 setup_search_view: function(view_id, search_defaults) {
377 if (this.searchview) {
378 this.searchview.stop();
380 this.searchview = new session.web.SearchView(
382 view_id, search_defaults, this.flags.search_view === false);
384 this.searchview.on_search.add(this.do_searchview_search);
385 return this.searchview.appendTo($("#" + this.element_id + "_search"));
387 do_searchview_search: function(domains, contexts, groupbys) {
389 controller = this.views[this.active_view].controller,
390 action_context = this.action.context || {};
391 this.rpc('/web/session/eval_domain_and_context', {
392 domains: [this.action.domain || []].concat(domains || []),
393 contexts: [action_context].concat(contexts || []),
394 group_by_seq: groupbys || []
395 }, function (results) {
396 self.dataset.context = results.context;
397 self.dataset.domain = results.domain;
398 var groupby = results.group_by.length
400 : action_context.group_by;
401 controller.do_search(results.domain, results.context, groupby || []);
405 * Event launched when a controller has been inited.
407 * @param {String} view_type type of view
408 * @param {String} view the inited controller
410 on_controller_inited: function(view_type, view) {
413 * Called when one of the view want to execute an action
415 on_action: function(action) {
417 on_create: function() {
419 on_remove: function() {
421 on_edit: function() {
424 * Called by children view after executing an action
426 on_action_executed: function () {
428 display_title: function () {
429 var view = this.views[this.active_view];
432 return view.controller.fields_view.arch.attrs.string;
438 session.web.ViewManagerAction = session.web.ViewManager.extend(/** @lends oepnerp.web.ViewManagerAction# */{
439 template:"ViewManagerAction",
441 * @constructs session.web.ViewManagerAction
442 * @extends session.web.ViewManager
444 * @param {session.web.ActionManager} parent parent object/widget
445 * @param {Object} action descriptor for the action this viewmanager needs to manage its views.
447 init: function(parent, action) {
448 // dataset initialization will take the session from ``this``, so if we
449 // do not have it yet (and we don't, because we've not called our own
450 // ``_super()``) rpc requests will blow up.
451 var flags = action.flags || {};
452 if (action.res_model == 'board.board' && action.view_mode === 'form') {
453 // Special case for Dashboards
455 views_switcher : false,
456 display_title : false,
460 action_buttons : false
463 this._super(parent, null, action.views, flags);
464 this.session = parent.session;
465 this.action = action;
466 var dataset = new session.web.DataSetSearch(this, action.res_model, action.context, action.domain);
468 dataset.ids.push(action.res_id);
471 this.dataset = dataset;
473 // setup storage for session-wise menu hiding
474 if (this.session.hidden_menutips) {
477 this.session.hidden_menutips = {}
480 * Initializes the ViewManagerAction: sets up the searchview (if the
481 * searchview is enabled in the manager's action flags), calls into the
482 * parent to initialize the primary view and (if the VMA has a searchview)
483 * launches an initial search after both views are done rendering.
488 search_defaults = {};
489 _.each(this.action.context, function (value, key) {
490 var match = /^search_default_(.*)$/.exec(key);
492 search_defaults[match[1]] = value;
496 var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
498 searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
500 var main_view_loaded = this._super();
502 var manager_ready = $.when(searchview_loaded, main_view_loaded);
504 this.$element.find('.oe_debug_view').change(this.on_debug_changed);
506 if (this.action.help && !this.flags.low_profile) {
507 var Users = new session.web.DataSet(self, 'res.users'),
508 $tips = this.$element.find('.oe_view_manager_menu_tips');
509 $tips.delegate('blockquote button', 'click', function() {
511 //noinspection FallthroughInSwitchStatementJS
512 switch ($this.attr('name')) {
514 Users.write(self.session.uid, {menu_tips:false});
516 $this.closest('blockquote').hide();
517 self.session.hidden_menutips[self.action.id] = true;
520 if (!(self.action.id in self.session.hidden_menutips)) {
521 Users.read_ids([this.session.uid], ['menu_tips']).then(function(users) {
523 if (!(user && user.id === self.session.uid)) {
526 $tips.find('blockquote').toggle(user.menu_tips);
531 var $res_logs = this.$element.find('.oe-view-manager-logs:first');
532 $res_logs.delegate('a.oe-more-logs', 'click', function () {
533 $res_logs.removeClass('oe-folded');
535 }).delegate('a.oe-remove-everything', 'click', function () {
536 $res_logs.removeClass('oe-has-more').find('ul').empty();
537 $res_logs.css('display','none');
540 $res_logs.css('display','none');
542 return manager_ready;
544 on_debug_changed: function (evt) {
546 $sel = $(evt.currentTarget),
547 $option = $sel.find('option:selected'),
549 current_view = this.views[this.active_view].controller;
552 var dialog = new session.web.Dialog(this, { title: "Fields View Get", width: '95%' }).open();
553 $('<pre>').text(session.web.json_node_to_xml(current_view.fields_view.arch, true)).appendTo(dialog.$element);
555 case 'customize_object':
556 this.rpc('/web/dataset/search_read', {
559 domain: [['model', '=', this.dataset.model]]
560 }, function (result) {
561 self.do_edit_resource('ir.model', result.ids[0], { name : "Customize Object" });
565 if (current_view.fields_view && current_view.fields_view.arch) {
566 var view_editor = new session.web.ViewEditor(current_view, current_view.$element, this.dataset, current_view.fields_view.arch);
569 this.do_warn("Manage Views", "Could not find current view declaration");
572 case 'edit_workflow':
573 return this.do_action({
574 res_model : 'workflow',
575 domain : [['osv', '=', this.dataset.model]],
576 views: [[false, 'list'], [false, 'form'], [false, 'diagram']],
577 type : 'ir.actions.act_window',
583 this.do_edit_resource($option.data('model'), $option.data('id'), { name : $option.text() });
587 console.log("No debug handler for ", val);
590 evt.currentTarget.selectedIndex = 0;
592 do_edit_resource: function(model, id, action) {
593 var action = _.extend({
596 type : 'ir.actions.act_window',
599 views : [[false, 'form']],
602 action_buttons : true,
604 resize_textareas : true
608 this.do_action(action);
610 on_mode_switch: function (view_type, no_store) {
613 return $.when(this._super(view_type, no_store)).then(function () {
614 self.shortcut_check(self.views[view_type]);
616 self.$element.find('.oe-view-manager-logs:first').addClass('oe-folded').removeClass('oe-has-more').css('display','none').find('ul').empty();
618 var controller = self.views[self.active_view].controller,
619 fvg = controller.fields_view,
620 view_id = (fvg && fvg.view_id) || '--';
621 self.$element.find('.oe_debug_view').html(QWeb.render('ViewManagerDebug', {
625 if (!self.action.name && fvg) {
626 self.$element.find('.oe_view_title_text').text(fvg.arch.attrs.string || fvg.name);
629 var $title = self.$element.find('.oe_view_title_text'),
630 $search_prefix = $title.find('span.oe_searchable_view');
631 if (controller.searchable !== false && self.flags.search_view !== false) {
632 if (!$search_prefix.length) {
633 $title.prepend('<span class="oe_searchable_view">' + _t("Search: ") + '</span>');
636 $search_prefix.remove();
640 do_push_state: function(state) {
641 if (this.widget_parent && this.widget_parent.do_push_state) {
642 state["view_type"] = this.active_view;
643 this.widget_parent.do_push_state(state);
646 do_load_state: function(state) {
649 if (state.view_type && state.view_type !== this.active_view) {
651 this.views[this.active_view].deferred.pipe(function() {
652 return self.on_mode_switch(state.view_type, true);
657 $.when(defs).then(function() {
658 self.views[self.active_view].controller.do_load_state(state);
661 shortcut_check : function(view) {
663 var grandparent = this.widget_parent && this.widget_parent.widget_parent;
664 // display shortcuts if on the first view for the action
665 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
666 if (!this.action.name ||
667 !(view.view_type === this.views_src[0].view_type
668 && view.view_id === this.views_src[0].view_id)) {
669 $shortcut_toggle.hide();
672 $shortcut_toggle.removeClass('oe-shortcut-remove').show();
673 if (_(this.session.shortcuts).detect(function (shortcut) {
674 return shortcut.res_id === self.session.active_id; })) {
675 $shortcut_toggle.addClass("oe-shortcut-remove");
677 this.shortcut_add_remove();
679 shortcut_add_remove: function() {
681 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
685 if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
686 $(self.session.shortcuts.binding).trigger('remove-current');
687 $shortcut_toggle.removeClass("oe-shortcut-remove");
689 $(self.session.shortcuts.binding).trigger('add', {
690 'user_id': self.session.uid,
691 'res_id': self.session.active_id,
692 'resource': 'ir.ui.menu',
693 'name': self.action.name
695 $shortcut_toggle.addClass("oe-shortcut-remove");
700 * Intercept do_action resolution from children views
702 on_action_executed: function () {
703 return new session.web.DataSet(this, 'res.log')
704 .call('get', [], this.do_display_log);
707 * @param {Array<Object>} log_records
709 do_display_log: function (log_records) {
712 var $logs = this.$element.find('.oe-view-manager-logs:first').addClass('oe-folded').css('display', 'block');
713 var $logs_list = $logs.find('ul').empty();
714 $logs.toggleClass('oe-has-more', log_records.length > cutoff);
715 _(log_records.reverse()).each(function (record) {
716 $(_.str.sprintf('<li><a href="#">%s</a></li>', record.name))
717 .appendTo($logs_list)
718 .delegate('a', 'click', function (e) {
720 type: 'ir.actions.act_window',
721 res_model: record.res_model,
722 res_id: record.res_id,
723 // TODO: need to have an evaluated context here somehow
724 //context: record.context,
725 views: [[false, 'form']]
731 display_title: function () {
732 return this.action.name;
736 session.web.Sidebar = session.web.Widget.extend({
737 init: function(parent, element_id) {
738 this._super(parent, element_id);
745 this.$element.html(session.web.qweb.render('Sidebar'));
746 this.$element.find(".toggle-sidebar").click(function(e) {
750 add_default_sections: function() {
752 view = this.widget_parent,
753 view_manager = view.widget_parent,
754 action = view_manager.action;
755 if (this.session.uid === 1) {
756 this.add_section(_t('Customize'), 'customize');
757 this.add_items('customize', [{
758 label: _t("Translate"),
759 callback: view.on_sidebar_translate,
760 title: _t("Technical translation")
764 this.add_section(_t('Other Options'), 'other');
765 this.add_items('other', [
768 callback: view.on_sidebar_import
771 callback: view.on_sidebar_export
773 label: _t("View Log"),
774 callback: view.on_sidebar_view_log,
775 classname: 'oe_hide oe_sidebar_view_log'
780 add_toolbar: function(toolbar) {
782 _.each([['print', _t("Reports")], ['action', _t("Actions")], ['relate', _t("Links")]], function(type) {
783 var items = toolbar[type[0]];
785 for (var i = 0; i < items.length; i++) {
787 label: items[i]['name'],
789 classname: 'oe_sidebar_' + type[0]
792 self.add_section(type[1], type[0]);
793 self.add_items(type[0], items);
798 add_section: function(name, code) {
799 if(!code) code = _.str.underscored(name);
800 var $section = this.sections[code];
803 var section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
804 $section = $(session.web.qweb.render("Sidebar.section", {
805 section_id: section_id,
807 classname: 'oe_sidebar_' + code
809 $section.appendTo(this.$element.find('div.sidebar-actions'));
810 this.sections[code] = $section;
815 add_items: function(section_code, items) {
816 // An item is a dictonary : {
817 // label: label to be displayed for the link,
818 // action: action to be launch when the link is clicked,
819 // callback: a function to be executed when the link is clicked,
820 // classname: optional dom class name for the line,
821 // title: optional title for the link
823 // Note: The item should have one action or/and a callback
827 $section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
828 section_id = $section.attr('id');
831 for (var i = 0; i < items.length; i++) {
832 items[i].element_id = _.uniqueId(section_id + '_item_');
833 this.items[items[i].element_id] = items[i];
836 var $items = $(session.web.qweb.render("Sidebar.section.items", {items: items}));
838 $items.find('a.oe_sidebar_action_a').click(function() {
839 var item = self.items[$(this).attr('id')];
841 item.callback.apply(self, [item]);
844 self.on_item_action_clicked(item);
849 var $ul = $section.find('ul');
851 $ul = $('<ul/>').appendTo($section);
853 $items.appendTo($ul);
856 on_item_action_clicked: function(item) {
858 self.widget_parent.sidebar_context().then(function (context) {
859 var ids = self.widget_parent.get_selected_ids();
860 if (ids.length == 0) {
861 //TODO: make prettier warning?
862 $("<div />").text(_t("You must choose at least one record.")).dialog({
863 title: _t("Warning"),
868 var additional_context = _.extend({
871 active_model: self.widget_parent.dataset.model
873 self.rpc("/web/action/load", {
874 action_id: item.action.id,
875 context: additional_context
876 }, function(result) {
877 result.result.context = _.extend(result.result.context || {},
879 result.result.flags = result.result.flags || {};
880 result.result.flags.new_window = true;
881 self.do_action(result.result, function () {
883 self.widget_parent.reload();
888 do_fold: function() {
889 this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
891 do_unfold: function() {
892 this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
894 do_toggle: function() {
895 this.$element.toggleClass('open-sidebar closed-sidebar');
899 session.web.TranslateDialog = session.web.Dialog.extend({
900 dialog_title: {toString: function () { return _t("Translations"); }},
901 init: function(view) {
902 // TODO fme: should add the language to fields_view_get because between the fields view get
903 // and the moment the user opens the translation dialog, the user language could have been changed
904 this.view_language = view.session.user_context.lang;
905 this['on_button' + _t("Save")] = this.on_button_Save;
906 this['on_button' + _t("Close")] = this.on_button_Close;
912 this.view_type = view.fields_view.type || '';
913 this.$fields_form = null;
914 this.$view_form = null;
915 this.$sidebar_form = null;
916 this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
917 this.languages = null;
918 this.languages_loaded = $.Deferred();
919 (new session.web.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
920 [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }).then(this.on_languages_loaded);
925 $.when(this.languages_loaded).then(function() {
926 self.$element.html(session.web.qweb.render('TranslateDialog', { widget: self }));
927 self.$fields_form = self.$element.find('.oe_translation_form');
928 self.$fields_form.find('.oe_trad_field').change(function() {
929 $(this).toggleClass('touched', ($(this).val() != $(this).attr('data-value')));
934 on_languages_loaded: function(langs) {
935 this.languages = langs;
936 this.languages_loaded.resolve();
938 do_load_fields_values: function(callback) {
941 this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
942 _.each(self.languages, function(lg) {
943 var deff = $.Deferred();
945 var callback = function(values) {
946 _.each(self.translatable_fields_keys, function(f) {
947 self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '').attr('data-value', values[0][f] || '');
951 if (lg.code === self.view_language) {
953 _.each(self.translatable_fields_keys, function(field) {
954 values[field] = self.view.fields[field].get_value();
958 self.rpc('/web/dataset/get', {
959 model: self.view.dataset.model,
960 ids: [self.view.datarecord.id],
961 fields: self.translatable_fields_keys,
962 context: self.view.dataset.get_context({
967 $.when.apply(null, deffered).then(callback);
969 open: function(field) {
972 $.when(this.languages_loaded).then(function() {
973 if (self.view.translatable_fields && self.view.translatable_fields.length) {
974 self.do_load_fields_values(function() {
977 var $field_input = self.$element.find('tr[data-field="' + field.name + '"] td:nth-child(2) *:first-child');
978 self.$element.scrollTo($field_input);
979 $field_input.focus();
987 on_button_Save: function() {
990 self.$fields_form.find('.oe_trad_field.touched').each(function() {
991 var field = $(this).attr('name').split('-');
992 if (!trads[field[0]]) {
993 trads[field[0]] = {};
995 trads[field[0]][field[1]] = $(this).val();
997 _.each(trads, function(data, code) {
998 if (code === self.view_language) {
999 _.each(data, function(value, field) {
1000 self.view.fields[field].set_value(value);
1003 self.view.dataset.write(self.view.datarecord.id, data, { 'lang': code });
1008 on_button_Close: function() {
1013 session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
1014 template: "EmptyComponent",
1015 // name displayed in view switchers
1017 set_default_options: function(options) {
1018 this.options = options || {};
1019 _.defaults(this.options, {
1020 // All possible views options should be defaulted here
1024 action_views_ids: {}
1027 open_translate_dialog: function(field) {
1028 if (!this.translate_dialog) {
1029 this.translate_dialog = new session.web.TranslateDialog(this).start();
1031 this.translate_dialog.open(field);
1034 * Fetches and executes the action identified by ``action_data``.
1036 * @param {Object} action_data the action descriptor data
1037 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
1038 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
1039 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
1040 * @param {Object} [action_data.context=null] additional action context, to add to the current context
1041 * @param {session.web.DataSet} dataset a dataset object used to communicate with the server
1042 * @param {Object} [record_id] the identifier of the object on which the action is to be applied
1043 * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
1045 do_execute_action: function (action_data, dataset, record_id, on_closed) {
1047 var result_handler = function () {
1048 if (on_closed) { on_closed.apply(null, arguments); }
1049 if (self.widget_parent && self.widget_parent.on_action_executed) {
1050 return self.widget_parent.on_action_executed.apply(null, arguments);
1053 var context = new session.web.CompoundContext(dataset.get_context(), action_data.context || {});
1055 var handler = function (r) {
1056 var action = r.result;
1057 if (action && action.constructor == Object) {
1058 var ncontext = new session.web.CompoundContext(context);
1061 active_id: record_id,
1062 active_ids: [record_id],
1063 active_model: dataset.model
1066 ncontext.add(action.context || {});
1067 return self.rpc('/web/session/eval_domain_and_context', {
1068 contexts: [ncontext],
1070 }).pipe(function (results) {
1071 action.context = results.context;
1072 /* niv: previously we were overriding once more with action_data.context,
1073 * I assumed this was not a correct behavior and removed it
1075 return self.do_action(action, result_handler);
1078 return result_handler();
1082 if (action_data.special) {
1083 return handler({result: {"type":"ir.actions.act_window_close"}});
1084 } else if (action_data.type=="object") {
1085 var args = [[record_id]], additional_args = [];
1086 if (action_data.args) {
1088 // Warning: quotes and double quotes problem due to json and xml clash
1089 // Maybe we should force escaping in xml or do a better parse of the args array
1090 additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
1091 args = args.concat(additional_args);
1093 console.error("Could not JSON.parse arguments", action_data.args);
1097 return dataset.call_button(action_data.name, args, handler);
1098 } else if (action_data.type=="action") {
1099 return this.rpc('/web/action/load', { action_id: parseInt(action_data.name, 10), context: context, do_not_eval: true}, handler);
1101 return dataset.exec_workflow(record_id, action_data.name, handler);
1105 * Directly set a view to use instead of calling fields_view_get. This method must
1106 * be called before start(). When an embedded view is set, underlying implementations
1107 * of session.web.View must use the provided view instead of any other one.
1109 * @param embedded_view A view.
1111 set_embedded_view: function(embedded_view) {
1112 this.embedded_view = embedded_view;
1113 this.options.sidebar = false;
1115 do_show: function () {
1116 this.$element.show();
1118 do_hide: function () {
1119 this.$element.hide();
1121 do_push_state: function(state) {
1122 if (this.widget_parent && this.widget_parent.do_push_state) {
1123 this.widget_parent.do_push_state(state);
1126 do_load_state: function(state) {
1129 * Switches to a specific view type
1131 * @param {String} view view type to switch to
1133 do_switch_view: function(view) {
1136 * Cancels the switch to the current view, switches to the previous one
1138 do_prev_view: function () {
1140 do_search: function(view) {
1142 set_common_sidebar_sections: function(sidebar) {
1143 sidebar.add_default_sections();
1145 on_sidebar_import: function() {
1146 var import_view = new session.web.DataImport(this, this.dataset);
1147 import_view.start();
1149 on_sidebar_export: function() {
1150 var export_view = new session.web.DataExport(this, this.dataset);
1151 export_view.start();
1153 on_sidebar_translate: function() {
1154 return this.do_action({
1155 res_model : 'ir.translation',
1156 domain : [['type', '!=', 'object'], '|', ['name', '=', this.dataset.model], ['name', 'ilike', this.dataset.model + ',']],
1157 views: [[false, 'list'], [false, 'form']],
1158 type : 'ir.actions.act_window',
1163 on_sidebar_view_log: function() {
1165 sidebar_context: function () {
1169 * Asks the view to reload itself, if the reloading is asynchronous should
1170 * return a {$.Deferred} indicating when the reloading is done.
1172 reload: function () {
1177 session.web.json_node_to_xml = function(node, human_readable, indent) {
1178 // For debugging purpose, this function will convert a json node back to xml
1179 // Maybe usefull for xml view editor
1180 indent = indent || 0;
1181 var sindent = (human_readable ? (new Array(indent + 1).join('\t')) : ''),
1182 r = sindent + '<' + node.tag,
1183 cr = human_readable ? '\n' : '';
1185 if (typeof(node) === 'string') {
1186 return sindent + node;
1187 } else if (typeof(node.tag) !== 'string' || !node.children instanceof Array || !node.attrs instanceof Object) {
1188 throw("Node a json node");
1190 for (var attr in node.attrs) {
1191 var vattr = node.attrs[attr];
1192 if (typeof(vattr) !== 'string') {
1194 vattr = JSON.stringify(vattr);
1196 vattr = vattr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
1197 if (human_readable) {
1198 vattr = vattr.replace(/"/g, "'");
1200 r += ' ' + attr + '="' + vattr + '"';
1202 if (node.children && node.children.length) {
1205 for (var i = 0, ii = node.children.length; i < ii; i++) {
1206 childs.push(session.web.json_node_to_xml(node.children[i], human_readable, indent + 1));
1208 r += childs.join(cr);
1209 r += cr + sindent + '</' + node.tag + '>';
1218 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: