ed99210a55b066695780797f8c0d0f5101b6ba8f
[odoo/odoo.git] / addons / base / static / src / js / views.js
1 /*---------------------------------------------------------
2  * OpenERP base library
3  *---------------------------------------------------------*/
4
5 openerp.base.views = function(openerp) {
6
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;
12     },
13     /**
14      * Process an action
15      * Supported actions: act_window
16      */
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();
22             }
23             this.viewmanager = new openerp.base.ViewManager(this.session,this.element_id);
24             this.viewmanager.do_action_window(action);
25             this.viewmanager.start();
26         }
27     }
28 });
29
30 /**
31  * Registry for all the main views
32  */
33 openerp.base.views = new openerp.base.Registry();
34
35 openerp.base.ViewManager =  openerp.base.Controller.extend({
36     init: function(session, element_id) {
37         this._super(session, element_id);
38         this.action = null;
39         this.dataset = null;
40         this.searchview = null;
41         this.active_view = null;
42         this.views = {};
43     },
44     start: function() {
45     },
46     do_action_window: function(action) {
47         var self = this;
48         this.action = action;
49         this.dataset = new openerp.base.DataSet(this.session, action.res_model);
50         this.dataset.start();
51
52         this.$element.html(QWeb.render("ViewManager", {"prefix": this.element_id, views: action.views}));
53
54         var searchview_loaded = this.setup_search_view(action);
55
56         this.$element.find('.oe_vm_switch button').click(function() {
57             self.on_mode_switch($(this).data('view-type'));
58         });
59         _.each(action.views, function(view) {
60             self.views[view[1]] = { view_id: view[0], controller: null };
61         });
62
63         // switch to the first one in sequence
64         var inital_view_loaded = this.on_mode_switch(action.views[0][1]);
65
66         if (action['auto_search']) {
67             $.when(searchview_loaded, inital_view_loaded)
68                 .then(this.searchview.do_search);
69         }
70     },
71     /**
72      * Asks the view manager to switch visualization mode.
73      *
74      * @param {String} view_type type of view to display
75      * @returns {jQuery.Deferred} new view loading promise
76      */
77     on_mode_switch: function(view_type) {
78         var view_promise;
79         this.active_view = view_type;
80         var view = this.views[view_type];
81         if (!view.controller) {
82             // Lazy loading of views
83             var controller = new (openerp.base.views.get_object(view_type))(
84                 this.session, this.element_id + "_view_" + view_type, this.dataset, view.view_id);
85             view_promise = controller.start();
86             this.views[view_type].controller = controller;
87         }
88
89         if (view.controller.searchable === false) {
90             this.searchview.hide();
91         } else {
92             this.searchview.show();
93         }
94
95         this.$element
96             .find('.views-switchers button').removeAttr('disabled')
97             .filter('[data-view-type="' + view_type + '"]')
98             .attr('disabled', true);
99
100         for (var i in this.views) {
101             if (this.views[i].controller) {
102                 if (i === view_type) {
103                     this.views[i].controller.do_show();
104                 } else {
105                     this.views[i].controller.do_hide();
106                 }
107             }
108         }
109         return view_promise;
110     },
111     /**
112      * Extract search view defaults from the current action's context.
113      *
114      * These defaults are of the form {search_default_*: value}
115      *
116      * @returns {Object} a clean defaults mapping of {field_name: value}
117      */
118     search_defaults: function () {
119         var defaults = {};
120         _.each(this.action.context, function (value, key) {
121             var match = /^search_default_(.*)$/.exec(key);
122             if (match) {
123                 defaults[match[1]] = value;
124             }
125         });
126         return defaults;
127     },
128     /**
129      * Sets up the current viewmanager's search view.
130      *
131      * @param action the action being executed
132      * @returns {jQuery.Deferred} search view startup deferred
133      */
134     setup_search_view:function (action) {
135         var self = this;
136         if (this.searchview) {
137             this.searchview.stop();
138         }
139         var view_id = action.search_view_id ? action.search_view_id[0] || false : false;
140
141         this.searchview = new openerp.base.SearchView(this.session, this.element_id + "_search", this.dataset, view_id, this.search_defaults());
142         this.searchview.on_search.add(function() {
143             self.views[self.active_view].controller.do_search.apply(self, arguments);
144         });
145         return this.searchview.start();
146     },
147
148     /**
149      * Called when one of the view want to execute an action
150      */
151     on_action: function(action) {
152     },
153     on_create: function() {
154     },
155     on_remove: function() {
156     },
157     on_edit: function() {
158     },
159 });
160
161 openerp.base.ViewManagerRoot = openerp.base.ViewManager.extend({
162 // Extends view manager
163 });
164
165 openerp.base.ViewManagerUsedAsAMany2One = openerp.base.ViewManager.extend({
166 // Extends view manager
167 });
168
169 openerp.base.BaseWidget = openerp.base.Controller.extend({
170     /**
171      * The name of the QWeb template that will be used for rendering. Must be redifined
172      * in subclasses or the render() method can not be used.
173      * 
174      * @type string
175      */
176     template: null,
177     /**
178      * The prefix used to generate an id automatically. Should be redifined in subclasses.
179      * If it is not defined, a default identifier will be used.
180      * 
181      * @type string
182      */
183     identifier_prefix: 'generic-identifier',
184     /**
185  * Base class for widgets. Handle rendering (based on a QWeb template), identifier
186  * generation, parenting and destruction of the widget.
187      * Contructor. Also initialize the identifier.
188      * 
189      * @params {openerp.base.search.BaseWidget} parent The parent widget.
190      */
191     init: function (parent) {
192         this.children = [];
193         this.parent = null;
194         this.set_parent(parent);
195         this.make_id(this.identifier_prefix);
196     },
197     /**
198      * Sets and returns a globally unique identifier for the widget.
199      *
200      * If a prefix is appended, the identifier will be appended to it.
201      *
202      * @params sections prefix sections, empty/falsy sections will be removed
203      */
204     make_id: function () {
205         this.element_id = _.uniqueId(_.toArray(arguments).join('_'));
206         return this.element_id;
207     },
208     /**
209      * "Starts" the widgets. Called at the end of the rendering, this allows
210      * to get a jQuery object referring to the DOM ($element attribute).
211      */
212     start: function () {
213         this._super();
214         var tmp = document.getElementById(this.element_id);
215         this.$element = tmp ? $(tmp) : null;
216     },
217     /**
218      * "Stops" the widgets. Called when the view destroys itself, this
219      * lets the widgets clean up after themselves.
220      */
221     stop: function () {
222         var tmp_children = this.children;
223         this.children = [];
224         _.each(tmp_children, function(x) {
225             x.stop();
226         });
227         if(this.$element != null) {
228             this.$element.remove();
229         }
230         this.set_parent(null);
231         this._super();
232     },
233     /**
234      * Set the parent of this component, also unregister the previous parent if there
235      * was one.
236      * 
237      * @param {openerp.base.BaseWidget} parent The new parent.
238      */
239     set_parent: function(parent) {
240         if(this.parent) {
241             this.parent.children = _.without(this.parent.children, this);
242         }
243         this.parent = parent;
244         if(this.parent) {
245             parent.children.push(this);
246         }
247     },
248     /**
249      * Render the widget. This.template must be defined.
250      * The content of the current object is passed as context to the template.
251      * 
252      * @param {object} additional Additional context arguments to pass to the template.
253      */
254     render: function (additional) {
255         return QWeb.render(this.template, _.extend({}, this, additional != null ? additional : {}));
256     }
257 });
258
259 openerp.base.views.add('calendar', 'openerp.base.CalendarView');
260 openerp.base.CalendarView = openerp.base.Controller.extend({
261     start: function () {
262         this._super();
263         this.$element.append('Calendar view');
264     },
265     do_show: function () {
266         this.$element.show();
267     },
268     do_hide: function () {
269         this.$element.hide();
270     }
271 });
272
273 openerp.base.views.add('gantt', 'openerp.base.GanttView');
274 openerp.base.GanttView = openerp.base.Controller.extend({
275     start: function () {
276         this._super();
277         this.$element.append('Gantt view');
278     },
279     do_show: function () {
280         this.$element.show();
281     },
282     do_hide: function () {
283         this.$element.hide();
284     }
285 });
286
287 openerp.base.views.add('tree', 'openerp.base.TreeView');
288 openerp.base.TreeView = openerp.base.Controller.extend({
289 /**
290  * Genuine tree view (the one displayed as a tree, not the list)
291  */
292     start: function () {
293         this._super();
294         this.$element.append('Tree view');
295     },
296     do_show: function () {
297         this.$element.show();
298     },
299     do_hide: function () {
300         this.$element.hide();
301     }
302 });
303
304 openerp.base.views.add('graph', 'openerp.base.GraphView');
305 openerp.base.GraphView = openerp.base.Controller.extend({
306     start: function () {
307         this._super();
308         this.$element.append('Graph view');
309     },
310     do_show: function () {
311         this.$element.show();
312     },
313     do_hide: function () {
314         this.$element.hide();
315     }
316 });
317
318 openerp.base.ProcessView = openerp.base.Controller.extend({
319 });
320
321 openerp.base.HelpView = openerp.base.Controller.extend({
322 });
323
324 };
325
326 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: