d6d2e5b6064210ea7a961ed7e51f701e39f39bf9
[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
50         // switch to the first one in sequence
51         var inital_view_loaded = this.setup_initial_view(action.res_model,action.views);
52
53         var searchview_loaded = this.setup_search_view(action);
54
55         if (action['auto_search']) {
56             $.when(searchview_loaded, inital_view_loaded)
57                 .then(this.searchview.do_search);
58         }
59     },
60     /**
61      * @returns {jQuery.Deferred} initial view loading promise
62      */
63     setup_initial_view: function(model,views) {
64         var self = this;
65         this.dataset = new openerp.base.DataSet(this.session, model);
66         this.dataset.start();
67
68         this.$element.html(QWeb.render("ViewManager", {"prefix": this.element_id, views: views}));
69
70         this.$element.find('.oe_vm_switch button').click(function() {
71             self.on_mode_switch($(this).data('view-type'));
72         });
73         _.each(views, function(view) {
74             self.views[view[1]] = { view_id: view[0], controller: null };
75         });
76
77         return this.on_mode_switch(views[0][1]);
78     },
79     /**
80      * Asks the view manager to switch visualization mode.
81      *
82      * @param {String} view_type type of view to display
83      * @returns {jQuery.Deferred} new view loading promise
84      */
85     on_mode_switch: function(view_type) {
86         var view_promise;
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;
95         }
96
97         if(this.searchview) {
98             if (view.controller.searchable === false) {
99                 this.searchview.hide();
100             } else {
101                 this.searchview.show();
102             }
103         }
104
105         this.$element
106             .find('.views-switchers button').removeAttr('disabled')
107             .filter('[data-view-type="' + view_type + '"]')
108             .attr('disabled', true);
109
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();
114                 } else {
115                     this.views[i].controller.do_hide();
116                 }
117             }
118         }
119         return view_promise;
120     },
121     /**
122      * Extract search view defaults from the current action's context.
123      *
124      * These defaults are of the form {search_default_*: value}
125      *
126      * @returns {Object} a clean defaults mapping of {field_name: value}
127      */
128     search_defaults: function () {
129         var defaults = {};
130         _.each(this.action.context, function (value, key) {
131             var match = /^search_default_(.*)$/.exec(key);
132             if (match) {
133                 defaults[match[1]] = value;
134             }
135         });
136         return defaults;
137     },
138     /**
139      * Sets up the current viewmanager's search view.
140      *
141      * @param action the action being executed
142      * @returns {jQuery.Deferred} search view startup deferred
143      */
144     setup_search_view:function (action) {
145         var self = this;
146         if (this.searchview) {
147             this.searchview.stop();
148         }
149         var view_id = action.search_view_id ? action.search_view_id[0] || false : false;
150
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);
154         });
155         return this.searchview.start();
156     },
157     /**
158      * Called when one of the view want to execute an action
159      */
160     on_action: function(action) {
161     },
162     on_create: function() {
163     },
164     on_remove: function() {
165     },
166     on_edit: function() {
167     }
168 });
169
170 openerp.base.ViewManagerRoot = openerp.base.ViewManager.extend({
171 // Extends view manager
172 });
173
174 openerp.base.ViewManagerUsedAsAMany2One = openerp.base.ViewManager.extend({
175 // Extends view manager
176 });
177
178 openerp.base.BaseWidget = openerp.base.Controller.extend({
179     /**
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.
182      * 
183      * @type string
184      */
185     template: null,
186     /**
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.
189      * 
190      * @type string
191      */
192     identifier_prefix: 'generic-identifier',
193     /**
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.
197      * 
198      * @params {openerp.base.search.BaseWidget} parent The parent widget.
199      */
200     init: function (parent) {
201         this.children = [];
202         this.parent = null;
203         this.set_parent(parent);
204         this.make_id(this.identifier_prefix);
205     },
206     /**
207      * Sets and returns a globally unique identifier for the widget.
208      *
209      * If a prefix is appended, the identifier will be appended to it.
210      *
211      * @params sections prefix sections, empty/falsy sections will be removed
212      */
213     make_id: function () {
214         this.element_id = _.uniqueId(_.toArray(arguments).join('_'));
215         return this.element_id;
216     },
217     /**
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).
220      */
221     start: function () {
222         this._super();
223         var tmp = document.getElementById(this.element_id);
224         this.$element = tmp ? $(tmp) : null;
225     },
226     /**
227      * "Stops" the widgets. Called when the view destroys itself, this
228      * lets the widgets clean up after themselves.
229      */
230     stop: function () {
231         var tmp_children = this.children;
232         this.children = [];
233         _.each(tmp_children, function(x) {
234             x.stop();
235         });
236         if(this.$element != null) {
237             this.$element.remove();
238         }
239         this.set_parent(null);
240         this._super();
241     },
242     /**
243      * Set the parent of this component, also unregister the previous parent if there
244      * was one.
245      * 
246      * @param {openerp.base.BaseWidget} parent The new parent.
247      */
248     set_parent: function(parent) {
249         if(this.parent) {
250             this.parent.children = _.without(this.parent.children, this);
251         }
252         this.parent = parent;
253         if(this.parent) {
254             parent.children.push(this);
255         }
256     },
257     /**
258      * Render the widget. This.template must be defined.
259      * The content of the current object is passed as context to the template.
260      * 
261      * @param {object} additional Additional context arguments to pass to the template.
262      */
263     render: function (additional) {
264         return QWeb.render(this.template, _.extend({}, this, additional != null ? additional : {}));
265     }
266 });
267
268 openerp.base.views.add('calendar', 'openerp.base.CalendarView');
269 openerp.base.CalendarView = openerp.base.Controller.extend({
270     start: function () {
271         this._super();
272         this.$element.append('Calendar view');
273     },
274     do_show: function () {
275         this.$element.show();
276     },
277     do_hide: function () {
278         this.$element.hide();
279     }
280 });
281
282 openerp.base.views.add('gantt', 'openerp.base.GanttView');
283 openerp.base.GanttView = openerp.base.Controller.extend({
284     start: function () {
285         this._super();
286         this.$element.append('Gantt view');
287     },
288     do_show: function () {
289         this.$element.show();
290     },
291     do_hide: function () {
292         this.$element.hide();
293     }
294 });
295
296 openerp.base.views.add('tree', 'openerp.base.TreeView');
297 openerp.base.TreeView = openerp.base.Controller.extend({
298 /**
299  * Genuine tree view (the one displayed as a tree, not the list)
300  */
301     start: function () {
302         this._super();
303         this.$element.append('Tree view');
304     },
305     do_show: function () {
306         this.$element.show();
307     },
308     do_hide: function () {
309         this.$element.hide();
310     }
311 });
312
313 openerp.base.views.add('graph', 'openerp.base.GraphView');
314 openerp.base.GraphView = openerp.base.Controller.extend({
315     start: function () {
316         this._super();
317         this.$element.append('Graph view');
318     },
319     do_show: function () {
320         this.$element.show();
321     },
322     do_hide: function () {
323         this.$element.hide();
324     }
325 });
326
327 openerp.base.ProcessView = openerp.base.Controller.extend({
328 });
329
330 openerp.base.HelpView = openerp.base.Controller.extend({
331 });
332
333 };
334
335 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: