1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.base.views = function(openerp) {
7 var _t = openerp.base._t;
10 * Registry for all the client actions key: tag value: widget
12 openerp.base.client_actions = new openerp.base.Registry();
14 openerp.base.ActionManager = openerp.base.Widget.extend({
15 identifier_prefix: "actionmanager",
16 init: function(parent) {
18 this.inner_viewmanager = null;
20 this.dialog_viewmanager = null;
21 this.client_widget = null;
25 return "<div id='"+this.element_id+"'></div>";
27 dialog_stop: function () {
29 this.dialog_viewmanager.stop();
30 this.dialog_viewmanager = null;
35 content_stop: function () {
36 if (this.inner_viewmanager) {
37 this.inner_viewmanager.stop();
38 this.inner_viewmanager = null;
40 if (this.client_widget) {
41 this.client_widget.stop();
42 this.client_widget = null;
45 url_update: function(action) {
47 // "model": action.model,
48 // "domain": action.domain,
59 url_stringify: function(action) {
61 url_parse: function(action) {
63 on_url_update: function(url) {
65 do_url_action: function(url) {
67 do_action: function(action, on_close) {
68 var type = action.type.replace(/\./g,'_');
69 var popup = action.target === 'new';
70 action.flags = _.extend({
71 views_switcher : !popup,
73 action_buttons : !popup,
76 }, action.flags || {});
77 if (!(type in this)) {
78 this.log("Action manager can't handle action of type " + action.type, action);
81 this[type](action, on_close);
83 ir_actions_act_window: function (action, on_close) {
84 if (action.target === 'new') {
85 if (this.dialog == null) {
86 this.dialog = new openerp.base.Dialog(this, { title: action.name, width: '80%' });
88 this.dialog.on_close.add(on_close);
91 this.dialog_viewmanager.stop();
93 this.dialog_viewmanager = new openerp.base.ViewManagerAction(this, action);
94 this.dialog_viewmanager.appendTo(this.dialog.$element);
99 this.inner_viewmanager = new openerp.base.ViewManagerAction(this, action);
100 this.inner_viewmanager.appendTo(this.$element);
101 this.url_update(action);
104 this.rpc("/base/session/save_session_action", { the_action : action}, function(key) {
105 var url = window.location.protocol + "//" + window.location.host + window.location.pathname + "?" + jQuery.param({ s_action : "" + key });
106 window.open(url,'_blank');
110 ir_actions_act_window_close: function (action, on_closed) {
113 ir_actions_server: function (action, on_closed) {
115 this.rpc('/base/action/run', {
116 action_id: action.id,
117 context: {active_id: 66, active_ids: [66], active_model: 'ir.ui.menu'}
118 }).then(function (action) {
119 self.do_action(action, on_closed)
122 ir_actions_client: function (action) {
124 var ClientWidget = openerp.base.client_actions.get_object(action.tag);
125 (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
129 openerp.base.ViewManager = openerp.base.Widget.extend({
130 identifier_prefix: "viewmanager",
131 init: function(parent, dataset, views) {
133 this.model = dataset.model;
134 this.dataset = dataset;
135 this.searchview = null;
136 this.active_view = null;
137 this.views_src = _.map(views, function(x) {return x instanceof Array? {view_id: x[0], view_type: x[1]} : x;});
139 this.flags = this.flags || {};
140 this.registry = openerp.base.views;
143 return QWeb.render("ViewManager", {"prefix": this.element_id, views: this.views_src})
146 * @returns {jQuery.Deferred} initial view loading promise
151 this.dataset.start();
152 this.$element.find('.oe_vm_switch button').click(function() {
153 self.on_mode_switch($(this).data('view-type'));
156 _.each(this.views_src, function(view) {
157 self.views[view.view_type] = $.extend({}, view, {
160 sidebar_id : self.element_id + '_sidebar_' + view.view_type,
161 action : self.action,
162 action_views_ids : views_ids
163 }, self.flags, view.options || {})
165 views_ids[view.view_type] = view.view_id;
167 if (this.flags.views_switcher === false) {
168 this.$element.find('.oe_vm_switch').hide();
170 // switch to the first one in sequence
171 return this.on_mode_switch(this.views_src[0].view_type);
174 * Asks the view manager to switch visualization mode.
176 * @param {String} view_type type of view to display
177 * @returns {jQuery.Deferred} new view loading promise
179 on_mode_switch: function(view_type) {
182 this.active_view = view_type;
183 var view = this.views[view_type];
184 if (!view.controller) {
185 // Lazy loading of views
186 var controllerclass = this.registry.get_object(view_type);
187 var controller = new controllerclass(this, this.element_id + '_view_' + view_type,
188 this.dataset, view.view_id, view.options);
189 if (view.embedded_view) {
190 controller.set_embedded_view(view.embedded_view);
192 controller.do_switch_view.add_last(this.on_mode_switch);
193 if (view_type === 'list' && this.flags.search_view === false && this.action && this.action['auto_search']) {
194 // In case the search view is not instantiated: manually call ListView#search
195 var domains = !_(self.action.domain).isEmpty()
196 ? [self.action.domain] : [],
197 contexts = !_(self.action.context).isEmpty()
198 ? [self.action.context] : [];
199 controller.on_loaded.add({
200 callback: function () {
201 controller.do_search(domains, contexts, []);
207 view_promise = controller.start();
208 $.when(view_promise).then(function() {
209 self.on_controller_inited(view_type, controller);
211 this.views[view_type].controller = controller;
215 if (this.searchview) {
216 if (view.controller.searchable === false) {
217 this.searchview.hide();
219 this.searchview.show();
224 .find('.views-switchers button').removeAttr('disabled')
225 .filter('[data-view-type="' + view_type + '"]')
226 .attr('disabled', true);
228 for (var view_name in this.views) {
229 if (!this.views.hasOwnProperty(view_name)) { continue; }
230 if (this.views[view_name].controller) {
231 if (view_name === view_type) {
232 $.when(view_promise).then(this.views[view_name].controller.do_show);
234 this.views[view_name].controller.do_hide();
241 * Event launched when a controller has been inited.
243 * @param {String} view_type type of view
244 * @param {String} view the inited controller
246 on_controller_inited: function(view_type, view) {
249 * Sets up the current viewmanager's search view.
251 * @param view_id the view to use or false for a default one
252 * @returns {jQuery.Deferred} search view startup deferred
254 setup_search_view: function(view_id, search_defaults) {
256 if (this.searchview) {
257 this.searchview.stop();
259 this.searchview = new openerp.base.SearchView(this, this.element_id + "_search", this.dataset, view_id, search_defaults);
260 if (this.flags.search_view === false) {
261 this.searchview.hide();
263 this.searchview.on_search.add(function(domains, contexts, groupbys) {
264 var controller = self.views[self.active_view].controller;
265 controller.do_search.call(controller, domains, contexts, groupbys);
267 return this.searchview.start();
270 * Called when one of the view want to execute an action
272 on_action: function(action) {
274 on_create: function() {
276 on_remove: function() {
278 on_edit: function() {
282 openerp.base.ViewManagerAction = openerp.base.ViewManager.extend({
283 init: function(parent, action) {
284 this.session = parent.session;
285 this.action = action;
287 if (!action.res_id) {
288 dataset = new openerp.base.DataSetSearch(this, action.res_model, action.context, action.domain);
290 this.action.flags.search_view = false;
291 dataset = new openerp.base.DataSetStatic(this, action.res_model, action.context, [action.res_id]);
294 this._super(parent, dataset, action.views);
295 this.action = action;
296 this.flags = this.action.flags || {};
297 if (action.res_model == 'board.board' && action.views.length == 1 && action.views) {
298 // Not elegant but allows to avoid flickering of SearchView#do_hide
299 this.flags.search_view = this.flags.pager = this.flags.sidebar = this.flags.action_buttons = false;
303 var inital_view_loaded = this._super();
305 var search_defaults = {};
306 _.each(this.action.context, function (value, key) {
307 var match = /^search_default_(.*)$/.exec(key);
309 search_defaults[match[1]] = value;
313 if (this.flags.search_view !== false) {
315 var searchview_id = this.action.search_view_id && this.action.search_view_id[0];
317 var searchview_loaded = this.setup_search_view(
318 searchview_id || false, search_defaults);
320 // schedule auto_search
321 if (searchview_loaded != null && this.action['auto_search']) {
322 $.when(searchview_loaded, inital_view_loaded)
323 .then(this.searchview.do_search);
327 on_mode_switch: function (view_type) {
328 this._super(view_type);
329 this.shortcut_check(this.views[view_type]);
331 shortcut_check : function(view) {
333 var grandparent = this.widget_parent && this.widget_parent.widget_parent;
334 // display shortcuts if on the first view for the action
335 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
336 if (!(grandparent instanceof openerp.base.WebClient) ||
337 !(view.view_type === this.views_src[0].view_type
338 && view.view_id === this.views_src[0].view_id)) {
339 $shortcut_toggle.hide();
342 $shortcut_toggle.removeClass('oe-shortcut-remove').show();
343 if (_(this.session.shortcuts).detect(function (shortcut) {
344 return shortcut.res_id === self.session.active_id; })) {
345 $shortcut_toggle.addClass("oe-shortcut-remove");
347 this.shortcut_add_remove();
349 shortcut_add_remove: function() {
351 var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
352 $shortcut_toggle.click(function() {
353 if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
354 $(self.session.shortcuts.binding).trigger('remove-current');
355 $shortcut_toggle.removeClass("oe-shortcut-remove");
357 $(self.session.shortcuts.binding).trigger('add', {
358 'user_id': self.session.uid,
359 'res_id': self.session.active_id,
360 'resource': 'ir.ui.menu',
361 'name': self.action.name
363 $shortcut_toggle.addClass("oe-shortcut-remove");
369 openerp.base.Sidebar = openerp.base.Widget.extend({
370 init: function(parent, element_id) {
371 this._super(parent, element_id);
377 this._super(this, arguments);
378 this.$element.html(QWeb.render('Sidebar'));
379 this.$element.find(".toggle-sidebar").click(function(e) {
383 add_toolbar: function(toolbar) {
385 _.each([['print', "Reports"], ['action', "Actions"], ['relate', "Links"]], function(type) {
386 var items = toolbar[type[0]];
388 for (var i = 0; i < items.length; i++) {
390 label: items[i]['name'],
392 classname: 'oe_sidebar_' + type[0]
395 self.add_section(type[0], type[1], items);
399 add_section: function(code, name, items) {
400 // For each section, we pass a name/label and optionally an array of items.
401 // If no items are passed, then the section will be created as a custom section
402 // returning back an element_id to be used by a custom controller.
403 // Else, the section is a standard section with items displayed as links.
404 // An item is a dictonary : {
405 // label: label to be displayed for the link,
406 // action: action to be launch when the link is clicked,
407 // callback: a function to be executed when the link is clicked,
408 // classname: optional dom class name for the line,
409 // title: optional title for the link
411 // Note: The item should have one action or/and a callback
413 section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
415 for (var i = 0; i < items.length; i++) {
416 items[i].element_id = _.uniqueId(section_id + '_item_');
417 this.items[items[i].element_id] = items[i];
420 var $section = $(QWeb.render("Sidebar.section", {
421 section_id: section_id,
423 classname: 'oe_sidebar_' + code,
427 $section.find('a.oe_sidebar_action_a').click(function() {
428 var item = self.items[$(this).attr('id')];
433 item.action.flags = item.action.flags || {};
434 item.action.flags.new_window = true;
435 self.do_action(item.action);
440 $section.appendTo(this.$element.find('div.sidebar-actions'));
441 this.sections[code] = $section;
444 do_fold: function() {
445 this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
447 do_unfold: function() {
448 this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
450 do_toggle: function() {
451 this.$element.toggleClass('open-sidebar closed-sidebar');
455 openerp.base.TranslateDialog = openerp.base.Dialog.extend({
456 dialog_title: _t("Translations"),
457 init: function(view) {
458 this['on_button' + _t("Save")] = this.on_button_Save;
459 this['on_button' + _t("Close")] = this.on_button_Close;
465 this.view_type = view.fields_view.type || '';
466 this.$fields_form = null;
467 this.$view_form = null;
468 this.$sidebar_form = null;
469 this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
470 this.languages = null;
471 this.languages_loaded = $.Deferred();
472 (new openerp.base.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
473 [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }, this.on_languages_loaded);
478 $.when(this.languages_loaded).then(function() {
479 self.$element.html(QWeb.render('TranslateDialog', { widget: self }));
480 self.$element.tabs();
481 if (!(self.view.translatable_fields && self.view.translatable_fields.length)) {
482 self.hide_tabs('fields');
483 self.select_tab('view');
485 self.$fields_form = self.$element.find('.oe_translation_form');
489 on_languages_loaded: function(langs) {
490 this.languages = langs;
491 this.languages_loaded.resolve();
493 do_load_fields_values: function(callback) {
496 this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
497 _.each(self.languages, function(lg) {
498 var deff = $.Deferred();
500 self.rpc('/base/dataset/get', {
501 model: self.view.dataset.model,
502 ids: [self.view.datarecord.id],
503 fields: self.translatable_fields_keys,
504 context: self.view.dataset.get_context({
507 }, function(values) {
508 _.each(self.translatable_fields_keys, function(f) {
509 self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '');
514 $.when.apply(null, deffered).then(callback);
516 show_tabs: function() {
517 for (var i = 0; i < arguments.length; i++) {
518 this.$element.find('ul.oe_translate_tabs li a[href$="' + arguments[i] + '"]').parent().show();
521 hide_tabs: function() {
522 for (var i = 0; i < arguments.length; i++) {
523 this.$element.find('ul.oe_translate_tabs li a[href$="' + arguments[i] + '"]').parent().hide();
526 select_tab: function(name) {
527 this.show_tabs(name);
528 var index = this.$element.find('ul.oe_translate_tabs li a[href$="' + arguments[i] + '"]').parent().index() - 1;
529 this.$element.tabs('select', index);
531 open: function(field) {
534 $.when(this.languages_loaded).then(function() {
535 if (self.view.translatable_fields && self.view.translatable_fields.length) {
536 self.do_load_fields_values(function() {
539 // TODO: focus and scroll to field
547 on_button_Save: function() {
550 on_button_Close: function() {
555 openerp.base.View = openerp.base.Widget.extend({
556 set_default_options: function(options) {
557 this.options = options || {};
558 _.defaults(this.options, {
559 // All possible views options should be defaulted here
566 open_translate_dialog: function(field) {
567 if (!this.translate_dialog) {
568 this.translate_dialog = new openerp.base.TranslateDialog(this).start();
570 this.translate_dialog.open(field);
573 * Fetches and executes the action identified by ``action_data``.
575 * @param {Object} action_data the action descriptor data
576 * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
577 * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
578 * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
579 * @param {Object} [action_data.context=null] additional action context, to add to the current context
580 * @param {openerp.base.DataSet} dataset a dataset object used to communicate with the server
581 * @param {Object} [record_id] the identifier of the object on which the action is to be applied
582 * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
584 execute_action: function (action_data, dataset, record_id, on_closed) {
586 var handler = function (r) {
587 var action = r.result;
588 if (action && action.constructor == Object) {
589 action.context = action.context || {};
590 _.extend(action.context, {
591 active_id: record_id || false,
592 active_ids: [record_id || false],
593 active_model: dataset.model
595 self.do_action(action, on_closed);
596 } else if (on_closed) {
601 var context = new openerp.base.CompoundContext(dataset.get_context(), action_data.context || {});
603 if (action_data.special) {
604 handler({result: {"type":"ir.actions.act_window_close"}});
605 } else if (action_data.type=="object") {
606 return dataset.call_button(action_data.name, [[record_id], context], handler);
607 } else if (action_data.type=="action") {
608 return this.rpc('/base/action/load', { action_id: parseInt(action_data.name, 10), context: context }, handler);
610 return dataset.exec_workflow(record_id, action_data.name, handler);
614 * Directly set a view to use instead of calling fields_view_get. This method must
615 * be called before start(). When an embedded view is set, underlying implementations
616 * of openerp.base.View must use the provided view instead of any other one.
618 * @param embedded_view A view.
620 set_embedded_view: function(embedded_view) {
621 this.embedded_view = embedded_view;
622 this.options.sidebar = false;
624 do_switch_view: function(view) {
626 set_common_sidebar_sections: function(sidebar) {
627 sidebar.add_section('customize', "Customize", [
629 label: "Manage Views",
630 callback: this.on_sidebar_manage_view,
631 title: "Manage views of the current object"
633 label: "Edit Workflow",
634 callback: this.on_sidebar_edit_workflow,
635 title: "Manage views of the current object",
636 classname: 'oe_hide oe_sidebar_edit_workflow'
638 label: "Customize Object",
639 callback: this.on_sidebar_customize_object,
640 title: "Manage views of the current object"
643 sidebar.add_section('other', "Other Options", [
646 callback: this.on_sidebar_import
649 callback: this.on_sidebar_export
652 callback: this.on_sidebar_translate,
653 classname: 'oe_sidebar_translate'
656 callback: this.on_sidebar_view_log,
657 classname: 'oe_hide oe_sidebar_view_log'
661 on_sidebar_manage_view: function() {
662 if (this.fields_view && this.fields_view.arch) {
663 $('<xmp>' + openerp.base.json_node_to_xml(this.fields_view.arch, true) + '</xmp>').dialog({ width: '95%', height: 600});
665 this.notification.warn("Manage Views", "Could not find current view declaration");
668 on_sidebar_edit_workflow: function() {
671 on_sidebar_customize_object: function() {
674 on_sidebar_import: function() {
676 on_sidebar_export: function() {
677 var export_view = new openerp.base.DataExport(this, this.dataset);
680 on_sidebar_translate: function() {
681 this.open_translate_dialog();
683 on_sidebar_view_log: function() {
688 * Registry for all the main views
690 openerp.base.views = new openerp.base.Registry();
692 openerp.base.json_node_to_xml = function(node, single_quote, indent) {
693 // For debugging purpose, this function will convert a json node back to xml
694 // Maybe usefull for xml view editor
695 if (typeof(node.tag) !== 'string' || !node.children instanceof Array || !node.attrs instanceof Object) {
696 throw("Node a json node");
698 indent = indent || 0;
699 var sindent = new Array(indent + 1).join('\t'),
700 r = sindent + '<' + node.tag;
701 for (var attr in node.attrs) {
702 var vattr = node.attrs[attr];
703 if (typeof(vattr) !== 'string') {
705 vattr = JSON.stringify(vattr);
707 vattr = vattr.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
709 vattr = vattr.replace(/"/g, "'");
711 r += ' ' + attr + '="' + vattr + '"';
713 if (node.children.length) {
716 for (var i = 0, ii = node.children.length; i < ii; i++) {
717 childs.push(openerp.base.json_node_to_xml(node.children[i], single_quote, indent + 1));
719 r += childs.join('\n');
720 r += '\n' + sindent + '</' + node.tag + '>';
729 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: