1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.base.views = function(openerp) {
7 openerp.base.ActionManager = openerp.base.Controller.extend({
8 // process all kind of actions
9 init: function(session, element_id) {
10 this._super(session, element_id);
11 this.viewmanager = null;
15 * Supported actions: act_window
17 do_action: function(action) {
18 // instantiate the right controllers by understanding the action
19 if(action.type == "ir.actions.act_window") {
20 if (this.viewmanager) {
21 this.viewmanager.stop();
23 this.viewmanager = new openerp.base.ViewManager(this.session,this.element_id);
24 this.viewmanager.do_action_window(action);
25 this.viewmanager.start();
31 * Registry for all the main views
33 openerp.base.views = new openerp.base.Registry();
35 openerp.base.ViewManager = openerp.base.Controller.extend({
36 init: function(session, element_id) {
37 this._super(session, element_id);
40 this.searchview = null;
41 this.active_view = null;
46 do_action_window: function(action) {
50 // switch to the first one in sequence
51 var inital_view_loaded = this.setup_initial_view(action.res_model,action.views);
53 var searchview_loaded = this.setup_search_view(action);
55 if (action['auto_search']) {
56 $.when(searchview_loaded, inital_view_loaded)
57 .then(this.searchview.do_search);
61 * @returns {jQuery.Deferred} initial view loading promise
63 setup_initial_view: function(model,views) {
65 this.dataset = new openerp.base.DataSet(this.session, model);
68 this.$element.html(QWeb.render("ViewManager", {"prefix": this.element_id, views: views}));
70 this.$element.find('.oe_vm_switch button').click(function() {
71 self.on_mode_switch($(this).data('view-type'));
73 _.each(views, function(view) {
74 self.views[view[1]] = { view_id: view[0], controller: null };
77 return this.on_mode_switch(views[0][1]);
80 * Asks the view manager to switch visualization mode.
82 * @param {String} view_type type of view to display
83 * @returns {jQuery.Deferred} new view loading promise
85 on_mode_switch: function(view_type) {
87 this.active_view = view_type;
88 var view = this.views[view_type];
89 if (!view.controller) {
90 // Lazy loading of views
91 var controller = new (openerp.base.views.get_object(view_type))(
92 this.session, this.element_id + "_view_" + view_type, this.dataset, view.view_id);
93 view_promise = controller.start();
94 this.views[view_type].controller = controller;
98 if (view.controller.searchable === false) {
99 this.searchview.hide();
101 this.searchview.show();
106 .find('.views-switchers button').removeAttr('disabled')
107 .filter('[data-view-type="' + view_type + '"]')
108 .attr('disabled', true);
110 for (var i in this.views) {
111 if (this.views[i].controller) {
112 if (i === view_type) {
113 this.views[i].controller.do_show();
115 this.views[i].controller.do_hide();
122 * Extract search view defaults from the current action's context.
124 * These defaults are of the form {search_default_*: value}
126 * @returns {Object} a clean defaults mapping of {field_name: value}
128 search_defaults: function () {
130 _.each(this.action.context, function (value, key) {
131 var match = /^search_default_(.*)$/.exec(key);
133 defaults[match[1]] = value;
139 * Sets up the current viewmanager's search view.
141 * @param action the action being executed
142 * @returns {jQuery.Deferred} search view startup deferred
144 setup_search_view:function (action) {
146 if (this.searchview) {
147 this.searchview.stop();
149 var view_id = action.search_view_id ? action.search_view_id[0] || false : false;
151 this.searchview = new openerp.base.SearchView(this.session, this.element_id + "_search", this.dataset, view_id, this.search_defaults());
152 this.searchview.on_search.add(function() {
153 self.views[self.active_view].controller.do_search.apply(self, arguments);
155 return this.searchview.start();
158 * Called when one of the view want to execute an action
160 on_action: function(action) {
162 on_create: function() {
164 on_remove: function() {
166 on_edit: function() {
170 openerp.base.ViewManagerRoot = openerp.base.ViewManager.extend({
171 // Extends view manager
174 openerp.base.ViewManagerUsedAsAMany2One = openerp.base.ViewManager.extend({
175 // Extends view manager
178 openerp.base.BaseWidget = openerp.base.Controller.extend({
180 * The name of the QWeb template that will be used for rendering. Must be redifined
181 * in subclasses or the render() method can not be used.
187 * The prefix used to generate an id automatically. Should be redifined in subclasses.
188 * If it is not defined, a default identifier will be used.
192 identifier_prefix: 'generic-identifier',
194 * Base class for widgets. Handle rendering (based on a QWeb template), identifier
195 * generation, parenting and destruction of the widget.
196 * Contructor. Also initialize the identifier.
198 * @params {openerp.base.search.BaseWidget} parent The parent widget.
200 init: function (parent) {
203 this.set_parent(parent);
204 this.make_id(this.identifier_prefix);
207 * Sets and returns a globally unique identifier for the widget.
209 * If a prefix is appended, the identifier will be appended to it.
211 * @params sections prefix sections, empty/falsy sections will be removed
213 make_id: function () {
214 this.element_id = _.uniqueId(_.toArray(arguments).join('_'));
215 return this.element_id;
218 * "Starts" the widgets. Called at the end of the rendering, this allows
219 * to get a jQuery object referring to the DOM ($element attribute).
223 var tmp = document.getElementById(this.element_id);
224 this.$element = tmp ? $(tmp) : null;
227 * "Stops" the widgets. Called when the view destroys itself, this
228 * lets the widgets clean up after themselves.
231 var tmp_children = this.children;
233 _.each(tmp_children, function(x) {
236 if(this.$element != null) {
237 this.$element.remove();
239 this.set_parent(null);
243 * Set the parent of this component, also unregister the previous parent if there
246 * @param {openerp.base.BaseWidget} parent The new parent.
248 set_parent: function(parent) {
250 this.parent.children = _.without(this.parent.children, this);
252 this.parent = parent;
254 parent.children.push(this);
258 * Render the widget. This.template must be defined.
259 * The content of the current object is passed as context to the template.
261 * @param {object} additional Additional context arguments to pass to the template.
263 render: function (additional) {
264 return QWeb.render(this.template, _.extend({}, this, additional != null ? additional : {}));
268 openerp.base.views.add('calendar', 'openerp.base.CalendarView');
269 openerp.base.CalendarView = openerp.base.Controller.extend({
272 this.$element.append('Calendar view');
274 do_show: function () {
275 this.$element.show();
277 do_hide: function () {
278 this.$element.hide();
282 openerp.base.views.add('gantt', 'openerp.base.GanttView');
283 openerp.base.GanttView = openerp.base.Controller.extend({
286 this.$element.append('Gantt view');
288 do_show: function () {
289 this.$element.show();
291 do_hide: function () {
292 this.$element.hide();
296 openerp.base.views.add('tree', 'openerp.base.TreeView');
297 openerp.base.TreeView = openerp.base.Controller.extend({
299 * Genuine tree view (the one displayed as a tree, not the list)
303 this.$element.append('Tree view');
305 do_show: function () {
306 this.$element.show();
308 do_hide: function () {
309 this.$element.hide();
313 openerp.base.views.add('graph', 'openerp.base.GraphView');
314 openerp.base.GraphView = openerp.base.Controller.extend({
317 this.$element.append('Graph view');
319 do_show: function () {
320 this.$element.show();
322 do_hide: function () {
323 this.$element.hide();
327 openerp.base.ProcessView = openerp.base.Controller.extend({
330 openerp.base.HelpView = openerp.base.Controller.extend({
335 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: