[IMP]Improve code for passing parameter.
[odoo/odoo.git] / addons / base / static / src / js / chrome.js
1 /*---------------------------------------------------------
2  * OpenERP base library
3  *---------------------------------------------------------*/
4
5 openerp.base.chrome = function(openerp) {
6
7 openerp.base.Session = openerp.base.Widget.extend( /** @lends openerp.base.Session# */{
8     /**
9      * @constructs
10      * @param element_id to use for exception reporting
11      * @param server
12      * @param port
13      */
14     init: function(parent, element_id, server, port) {
15         this._super(parent, element_id);
16         this.server = (server == undefined) ? location.hostname : server;
17         this.port = (port == undefined) ? location.port : port;
18         this.rpc_mode = (server == location.hostname) ? "ajax" : "jsonp";
19         this.debug = true;
20         this.db = "";
21         this.login = "";
22         this.password = "";
23         this.uid = false;
24         this.session_id = false;
25         this.module_list = [];
26         this.module_loaded = {"base": true};
27         this.context = {};
28     },
29     start: function() {
30         this.session_restore();
31     },
32     /**
33      * Executes an RPC call, registering the provided callbacks.
34      *
35      * Registers a default error callback if none is provided, and handles
36      * setting the correct session id and session context in the parameter
37      * objects
38      *
39      * @param {String} url RPC endpoint
40      * @param {Object} params call parameters
41      * @param {Function} success_callback function to execute on RPC call success
42      * @param {Function} error_callback function to execute on RPC call failure
43      * @returns {jQuery.Deferred} jquery-provided ajax deferred
44      */
45     rpc: function(url, params, success_callback, error_callback) {
46         var self = this;
47         // Construct a JSON-RPC2 request, method is currently unused
48         params.session_id = this.session_id;
49
50         // Call using the rpc_mode
51         var deferred = $.Deferred();
52         this.rpc_ajax(url, {
53             jsonrpc: "2.0",
54             method: "call",
55             params: params,
56             id:null
57         }).then(function () {deferred.resolve.apply(deferred, arguments);},
58                 function(error) {deferred.reject(error, $.Event());});
59         return deferred.fail(function() {
60             deferred.fail(function(error, event) {
61                 if (!event.isDefaultPrevented()) {
62                     self.on_rpc_error(error, event);
63                 }
64             });
65         }).then(success_callback, error_callback).promise();
66     },
67     /**
68      * Raw JSON-RPC call
69      *
70      * @returns {jQuery.Deferred} ajax-based deferred object
71      */
72     rpc_ajax: function(url, payload) {
73         var self = this;
74         this.on_rpc_request();
75         // url can be an $.ajax option object
76         if (_.isString(url)) {
77             url = {
78                 url: url
79             }
80         }
81         var ajax = _.extend({
82             type: "POST",
83             url: url,
84             dataType: 'json',
85             contentType: 'application/json',
86             data: JSON.stringify(payload),
87             processData: false
88         }, url);
89         var deferred = $.Deferred();
90         $.ajax(ajax).done(function(response, textStatus, jqXHR) {
91             self.on_rpc_response();
92             if (!response.error) {
93                 deferred.resolve(response["result"], textStatus, jqXHR);
94                 return;
95             }
96             if (response.error.data.type !== "session_invalid") {
97                 deferred.reject(response.error);
98                 return;
99             }
100             self.uid = false;
101             self.on_session_invalid(function() {
102                 self.rpc(url, payload.params,
103                     function() {
104                         deferred.resolve.apply(deferred, arguments);
105                     },
106                     function(error, event) {
107                         event.preventDefault();
108                         deferred.reject.apply(deferred, arguments);
109                     });
110             });
111         }).fail(function(jqXHR, textStatus, errorThrown) {
112             self.on_rpc_response();
113             var error = {
114                 code: -32098,
115                 message: "XmlHttpRequestError " + errorThrown,
116                 data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] }
117             };
118             deferred.reject(error);
119         });
120         return deferred.promise();
121     },
122     on_rpc_request: function() {
123     },
124     on_rpc_response: function() {
125     },
126     on_rpc_error: function(error) {
127     },
128     /**
129      * The session is validated either by login or by restoration of a previous session
130      */
131     on_session_valid: function() {
132         if(!openerp._modules_loaded)
133             this.load_modules();
134     },
135     on_session_invalid: function(contination) {
136     },
137     session_is_valid: function() {
138         return this.uid;
139     },
140     session_login: function(db, login, password, success_callback) {
141         var self = this;
142         this.db = db;
143         this.login = login;
144         this.password = password;
145         var params = { db: this.db, login: this.login, password: this.password };
146         this.rpc("/base/session/login", params, function(result) {
147             self.session_id = result.session_id;
148             self.uid = result.uid;
149             self.session_save();
150             self.on_session_valid();
151             if (success_callback)
152                 success_callback();
153         });
154     },
155     session_logout: function() {
156         this.uid = false;
157     },
158     /**
159      * Reloads uid and session_id from local storage, if they exist
160      */
161     session_restore: function () {
162         this.uid = this.get_cookie('uid');
163         this.session_id = this.get_cookie('session_id');
164         this.db = this.get_cookie('db');
165         this.login = this.get_cookie('login');
166         // we should do an rpc to confirm that this session_id is valid and if it is retrieve the information about db and login
167         // then call on_session_valid
168         this.on_session_valid();
169     },
170     /**
171      * Saves the session id and uid locally
172      */
173     session_save: function () {
174         this.set_cookie('uid', this.uid);
175         this.set_cookie('session_id', this.session_id);
176         this.set_cookie('db', this.db);
177         this.set_cookie('login', this.login);
178     },
179     logout: function() {
180         delete this.uid;
181         delete this.session_id;
182         delete this.db;
183         delete this.login;
184         this.set_cookie('uid', '');
185         this.set_cookie('session_id', '');
186         this.set_cookie('db', '');
187         this.set_cookie('login', '');
188         this.on_session_invalid(function() {});
189     },
190     /**
191      * Fetches a cookie stored by an openerp session
192      *
193      * @private
194      * @param name the cookie's name
195      */
196     get_cookie: function (name) {
197         var nameEQ = this.element_id + '|' + name + '=';
198         var cookies = document.cookie.split(';');
199         for(var i=0; i<cookies.length; ++i) {
200             var cookie = cookies[i].replace(/^\s*/, '');
201             if(cookie.indexOf(nameEQ) === 0) {
202                 return JSON.parse(decodeURIComponent(cookie.substring(nameEQ.length)));
203             }
204         }
205         return null;
206     },
207     /**
208      * Create a new cookie with the provided name and value
209      *
210      * @private
211      * @param name the cookie's name
212      * @param value the cookie's value
213      * @param ttl the cookie's time to live, 1 year by default, set to -1 to delete
214      */
215     set_cookie: function (name, value, ttl) {
216         ttl = ttl || 24*60*60*365;
217         document.cookie = [
218             this.element_id + '|' + name + '=' + encodeURIComponent(JSON.stringify(value)),
219             'max-age=' + ttl,
220             'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString()
221         ].join(';');
222     },
223     /**
224      * Load additional web addons of that instance and init them
225      */
226     load_modules: function() {
227         var self = this;
228         this.rpc('/base/session/modules', {}, function(result) {
229             self.module_list = result;
230             var modules = self.module_list.join(',');
231             if(self.debug || true) {
232                 self.rpc('/base/webclient/csslist', {"mods": modules}, self.do_load_css);
233                 self.rpc('/base/webclient/jslist', {"mods": modules}, self.do_load_js);
234             } else {
235                 self.do_load_css(["/base/webclient/css?mods="+modules]);
236                 self.do_load_js(["/base/webclient/js?mods="+modules]);
237             }
238             openerp._modules_loaded = true;
239         });
240     },
241     do_load_css: function (files) {
242         _.each(files, function (file) {
243             $('head').append($('<link>', {
244                 'href': file,
245                 'rel': 'stylesheet',
246                 'type': 'text/css'
247             }));
248         });
249     },
250     do_load_js: function(files) {
251         var self = this;
252         if(files.length != 0) {
253             var file = files.shift();
254             var tag = document.createElement('script');
255             tag.type = 'text/javascript';
256             tag.src = file;
257             tag.onload = tag.onreadystatechange = function() {
258                 if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done )
259                     return;
260                 tag.onload_done = true;
261                 self.do_load_js(files);
262             };
263             document.head.appendChild(tag);
264         } else {
265             this.on_modules_loaded();
266         }
267     },
268     on_modules_loaded: function() {
269         for(var j=0; j<this.module_list.length; j++) {
270             var mod = this.module_list[j];
271             if(this.module_loaded[mod])
272                 continue;
273             openerp[mod] = {};
274             // init module mod
275             if(openerp._openerp[mod] != undefined) {
276                 openerp._openerp[mod](openerp);
277                 this.module_loaded[mod] = true;
278             }
279         }
280     }
281 });
282
283 openerp.base.Notification =  openerp.base.Widget.extend({
284     init: function(parent, element_id) {
285         this._super(parent, element_id);
286         this.$element.notify({
287             speed: 500,
288             expires: 1500
289         });
290     },
291     notify: function(title, text) {
292         this.$element.notify('create', {
293             title: title,
294             text: text
295         });
296     },
297     warn: function(title, text) {
298         this.$element.notify('create', 'oe_notification_alert', {
299             title: title,
300             text: text
301         });
302     }
303 });
304
305 openerp.base.Dialog = openerp.base.OldWidget.extend({
306     dialog_title: "",
307     identifier_prefix: 'dialog',
308     init: function (parent, dialog_options) {
309         var self = this;
310         this._super(parent);
311         this.dialog_options = {
312             modal: true,
313             width: 'auto',
314             min_width: 0,
315             max_width: '100%',
316             height: 'auto',
317             min_height: 0,
318             max_height: '100%',
319             autoOpen: false,
320             buttons: {},
321             beforeClose: function () {
322                 self.on_close();
323             }
324         };
325         for (var f in this) {
326             if (f.substr(0, 10) == 'on_button_') {
327                 this.dialog_options.buttons[f.substr(10)] = this[f];
328             }
329         }
330         if (dialog_options) {
331             this.set_options(dialog_options);
332         }
333     },
334     set_options: function(options) {
335         options = options || {};
336         options.width = this.get_width(options.width || this.dialog_options.width);
337         options.min_width = this.get_width(options.min_width || this.dialog_options.min_width);
338         options.max_width = this.get_width(options.max_width || this.dialog_options.max_width);
339         options.height = this.get_height(options.height || this.dialog_options.height);
340         options.min_height = this.get_height(options.min_height || this.dialog_options.min_height);
341         options.max_height = this.get_height(options.max_height || this.dialog_options.max_width);
342
343         if (options.width !== 'auto') {
344             if (options.width > options.max_width) options.width = options.max_width;
345             if (options.width < options.min_width) options.width = options.min_width;
346         }
347         if (options.height !== 'auto') {
348             if (options.height > options.max_height) options.height = options.max_height;
349             if (options.height < options.min_height) options.height = options.min_height;
350         }
351         if (!options.title && this.dialog_title) {
352             options.title = this.dialog_title;
353         }
354         _.extend(this.dialog_options, options);
355     },
356     get_width: function(val) {
357         return this.get_size(val.toString(), $(window.top).width());
358     },
359     get_height: function(val) {
360         return this.get_size(val.toString(), $(window.top).height());
361     },
362     get_size: function(val, available_size) {
363         if (val === 'auto') {
364             return val;
365         } else if (val.slice(-1) == "%") {
366             return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
367         } else {
368             return parseInt(val, 10);
369         }
370     },
371     start: function (auto_open) {
372         this.$dialog = $('<div id="' + this.element_id + '"></div>').dialog(this.dialog_options);
373         if (auto_open !== false) {
374             this.open();
375         }
376         this._super();
377     },
378     open: function(dialog_options) {
379         // TODO fme: bind window on resize
380         if (this.template) {
381             this.$element.html(this.render());
382         }
383         this.set_options(dialog_options);
384         this.$dialog.dialog(this.dialog_options).dialog('open');
385     },
386     close: function() {
387         // Closes the dialog but leave it in a state where it could be opened again.
388         this.$dialog.dialog('close');
389     },
390     on_close: function() {
391     },
392     stop: function () {
393         // Destroy widget
394         this.close();
395         this.$dialog.dialog('destroy');
396     }
397 });
398
399 openerp.base.CrashManager = openerp.base.Dialog.extend({
400     identifier_prefix: 'dialog_crash',
401     init: function(parent) {
402         this._super(parent);
403         this.session.on_rpc_error.add(this.on_rpc_error);
404     },
405     on_button_Ok: function() {
406         this.close();
407     },
408     on_rpc_error: function(error) {
409         this.error = error;
410         if (error.data.fault_code) {
411             var split = error.data.fault_code.split('\n')[0].split(' -- ');
412             if (split.length > 1) {
413                 error.type = split.shift();
414                 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
415             }
416         }
417         if (error.code === 200 && error.type) {
418             this.dialog_title = "OpenERP " + _.capitalize(error.type);
419             this.template = 'DialogWarning';
420             this.open({
421                 width: 'auto',
422                 height: 'auto'
423             });
424         } else {
425             this.dialog_title = "OpenERP Error";
426             this.template = 'DialogTraceback';
427             this.open({
428                 width: 'auto',
429                 height: 'auto'
430             });
431         }
432     }
433 });
434
435 openerp.base.Loading =  openerp.base.Widget.extend({
436     init: function(parent, element_id) {
437         this._super(parent, element_id);
438         this.count = 0;
439         this.session.on_rpc_request.add_first(this.on_rpc_event, 1);
440         this.session.on_rpc_response.add_last(this.on_rpc_event, -1);
441     },
442     on_rpc_event : function(increment) {
443         this.count += increment;
444         if (this.count) {
445             //this.$element.html(QWeb.render("Loading", {}));
446             this.$element.html("Loading ("+this.count+")");
447             this.$element.show();
448         } else {
449             this.$element.fadeOut();
450         }
451     }
452 });
453
454 openerp.base.Database = openerp.base.Widget.extend({
455     init: function(parent, element_id, option_id) {
456         this._super(parent, element_id);
457         this.$option_id = $('#' + option_id);
458     },
459     start: function() {
460         this.$element.html(QWeb.render("Database", this));
461         this.$element.closest(".openerp")
462                 .removeClass("login-mode")
463                 .addClass("database_block");
464
465         var self = this;
466
467         var fetch_db = this.rpc("/base/database/get_list", {}, function(result) {
468             self.db_list = result.db_list;
469         });
470         var fetch_langs = this.rpc("/base/session/get_lang_list", {}, function(result) {
471             if (result.error) {
472                 self.display_error(result);
473                 return;
474             }
475             self.lang_list = result.lang_list;
476         });
477         $.when(fetch_db, fetch_langs).then(function () {self.do_create();});
478
479         this.$element.find('#db-create').click(this.do_create);
480         this.$element.find('#db-drop').click(this.do_drop);
481         this.$element.find('#db-backup').click(this.do_backup);
482         this.$element.find('#db-restore').click(this.do_restore);
483         this.$element.find('#db-change-password').click(this.do_change_password);
484         this.$element.find('#back-to-login').click(function() {
485             self.stop();
486         });
487     },
488     stop: function () {
489         this.$option_id.empty();
490
491         this.$element
492             .find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login')
493                 .unbind('click')
494             .end()
495             .closest(".openerp")
496                 .addClass("login-mode")
497                 .removeClass("database_block")
498             .end()
499             .empty();
500
501     },
502     /**
503      * Converts a .serializeArray() result into a dict. Does not bother folding
504      * multiple identical keys into an array, last key wins.
505      *
506      * @param {Array} array
507      */
508     to_object: function (array) {
509         var result = {};
510         _(array).each(function (record) {
511             result[record.name] = record.value;
512         });
513         return result;
514     },
515     /**
516      * Waits until the new database is done creating, then unblocks the UI and
517      * logs the user in as admin
518      *
519      * @param {Number} db_creation_id identifier for the db-creation operation, used to fetch the current installation progress
520      * @param {Object} info info fields for this database creation
521      * @param {String} info.db name of the database being created
522      * @param {String} info.password super-admin password for the database
523      */
524     wait_for_newdb: function (db_creation_id, info) {
525         var self = this;
526         self.rpc('/base/database/progress', {
527             id: db_creation_id,
528             password: info.password
529         }, function (result) {
530             var progress = result[0];
531             // I'd display a progress bar, but turns out the progress status
532             // the server report kind-of blows goats: it's at 0 for ~75% of
533             // the installation, then jumps to 75%, then jumps down to either
534             // 0 or ~40%, then back up to 75%, then terminates. Let's keep that
535             // mess hidden behind a not-very-useful but not overly weird
536             // message instead.
537             if (progress < 1) {
538                 setTimeout(function () {
539                     self.wait_for_newdb(db_creation_id, info);
540                 }, 500);
541                 return;
542             }
543
544             var admin = result[1][0];
545             setTimeout(function () {
546                 self.stop();
547                 self.widget_parent.do_login(
548                         info.db, admin.login, admin.password);
549                 $.unblockUI();
550             });
551         });
552     },
553     /**
554      * Displays an error dialog resulting from the various RPC communications
555      * failing over themselves
556      *
557      * @param {Object} error error description
558      * @param {String} error.title title of the error dialog
559      * @param {String} error.error message of the error dialog
560      */
561     display_error: function (error) {
562         return $('<div>').dialog({
563             modal: true,
564             title: error.title,
565             buttons: {
566                 Ok: function() {
567                     $(this).dialog("close");
568                 }
569             }
570         }).html(error.error);
571     },
572     do_create: function() {
573         var self = this;
574         self.$option_id.html(QWeb.render("CreateDB", self));
575
576         self.$option_id.find("form[name=create_db_form]").validate({
577             submitHandler: function (form) {
578                 var fields = $(form).serializeArray();
579                 $.blockUI();
580                 self.rpc("/base/database/create", {'fields': fields}, function(result) {
581                     if (result.error) {
582                         $.unblockUI();
583                         self.display_error(result);
584                         return;
585                     }
586                     self.db_list.push(self.to_object(fields)['db_name']);
587                     self.db_list.sort();
588                     var form_obj = self.to_object(fields);
589                     self.wait_for_newdb(result, {
590                         password: form_obj['super_admin_pwd'],
591                         db: form_obj['db_name']
592                     });
593                 });
594             }
595         });
596     },
597
598     do_drop: function() {
599         var self = this;
600         self.$option_id.html(QWeb.render("DropDB", self));
601
602         self.$option_id.find("form[name=drop_db_form]").validate({
603             submitHandler: function (form) {
604                 var $form = $(form),
605                     fields = $form.serializeArray(),
606                     $db_list = $form.find('select[name=drop_db]'),
607                     db = $db_list.val();
608
609                 if (!confirm("Do you really want to delete the database: " + db + " ?")) {
610                     return;
611                 }
612                 self.rpc("/base/database/drop", {'fields': fields}, function(result) {
613                     if (result.error) {
614                         self.display_error(result);
615                         return;
616                     }
617                     $db_list.find(':selected').remove();
618                     self.db_list.splice(_.indexOf(self.db_list, db, true), 1);
619                     self.notification.notify("Dropping database", "The database '" + db + "' has been dropped");
620                 });
621             }
622         });
623     },
624
625     wait_for_file: function (token, cleanup) {
626         var self = this,
627             cookie_name = 'fileToken',
628             cookie_length = cookie_name.length;
629         this.backup_timer = setInterval(function () {
630             var cookies = document.cookie.split(';');
631             for(var i=0; i<cookies.length; ++i) {
632                 var cookie = cookies[i].replace(/^\s*/, '');
633                 if(!cookie.indexOf(cookie_name) === 0) { continue; }
634                 var cookie_val = cookie.substring(cookie_length + 1);
635                 if(parseInt(cookie_val, 10) !== token) { continue; }
636
637                 // clear waiter
638                 clearInterval(self.backup_timer);
639                 // clear cookie
640                 document.cookie = _.sprintf("%s=;expires=%s;path=/",
641                     cookie_name, new Date().toGMTString());
642
643                 if (cleanup) { cleanup(); }
644             }
645         }, 100);
646     },
647     do_backup: function() {
648         var self = this;
649         self.$option_id.html(QWeb.render("BackupDB", self));
650
651         self.$option_id.find("form[name=backup_db_form]").validate({
652             submitHandler: function (form) {
653                 $.blockUI();
654                 // need to detect when the file is done downloading (not used
655                 // yet, but we'll need it to fix the UI e.g. with a throbber
656                 // while dump is being generated), iframe load event only fires
657                 // when the iframe content loads, so we need to go smarter:
658                 // http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
659                 var $target = $('#backup-target'),
660                       token = new Date().getTime();
661                 if (!$target.length) {
662                     $target = $('<iframe id="backup-target" style="display: none;">')
663                         .appendTo(document.body)
664                         .load(function () {
665                             $.unblockUI();
666                             clearInterval(self.backup_timer);
667                             var error = this.contentDocument.body
668                                     .firstChild.data
669                                     .split('|');
670                             self.display_error({
671                                 title: error[0],
672                                 error: error[1]
673                             });
674                         });
675                 }
676                 $(form).find('input[name=token]').val(token);
677                 form.submit();
678
679                 self.wait_for_file(token, function () {
680                     $.unblockUI();
681                 });
682             }
683         });
684     },
685
686     do_restore: function() {
687         var self = this;
688         self.$option_id.html(QWeb.render("RestoreDB", self));
689
690         self.$option_id.find("form[name=restore_db_form]").validate({
691             submitHandler: function (form) {
692                 $.blockUI();
693                 $(form).ajaxSubmit({
694                     url: '/base/database/restore',
695                     type: 'POST',
696                     resetForm: true,
697                     success: function (body) {
698                         // TODO: ui manipulations
699                         // note: response objects don't work, but we have the
700                         // HTTP body of the response~~
701
702                         // If empty body, everything went fine
703                         if (!body) { return; }
704
705                         if (body.indexOf('403 Forbidden') !== -1) {
706                             self.display_error({
707                                 title: 'Access Denied',
708                                 error: 'Incorrect super-administrator password'
709                             })
710                         } else {
711                             self.display_error({
712                                 title: 'Restore Database',
713                                 error: 'Could not restore the database'
714                             })
715                         }
716                     },
717                     complete: function () {
718                         $.unblockUI();
719                     }
720                 });
721             }
722         });
723     },
724
725     do_change_password: function() {
726         var self = this;
727         self.$option_id.html(QWeb.render("Change_DB_Pwd", self));
728
729         self.$option_id.find("form[name=change_pwd_form]").validate({
730             messages: {
731                 old_pwd: "Please enter your previous password",
732                 new_pwd: "Please enter your new password",
733                 confirm_pwd: {
734                     required: "Please confirm your new password",
735                     equalTo: "The confirmation does not match the password"
736                 }
737             },
738             submitHandler: function (form) {
739                 self.rpc("/base/database/change_password", {
740                     'fields': $(form).serializeArray()
741                 }, function(result) {
742                     if (result.error) {
743                         self.display_error(result);
744                         return;
745                     }
746                     self.notification.notify("Changed Password", "Password has been changed successfully");
747                 });
748             }
749         });
750     }
751 });
752
753 openerp.base.Login =  openerp.base.Widget.extend({
754     remember_creditentials: true,
755
756     init: function(parent, element_id) {
757         this._super(parent, element_id);
758         this.has_local_storage = typeof(localStorage) != 'undefined';
759         this.selected_db = null;
760         this.selected_login = null;
761
762         if (this.has_local_storage && this.remember_creditentials) {
763             this.selected_db = localStorage.getItem('last_db_login_success');
764             this.selected_login = localStorage.getItem('last_login_login_success');
765         }
766         if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
767             this.selected_db = this.selected_db || "trunk";
768             this.selected_login = this.selected_login || "admin";
769             this.selected_password = this.selected_password || "a";
770         }
771     },
772     start: function() {
773         var self = this;
774         this.rpc("/base/database/get_list", {}, function(result) {
775             self.db_list = result.db_list;
776             self.display();
777         }, function() {
778             self.display();
779         });
780     },
781     display: function() {
782         var self = this;
783
784         this.$element.html(QWeb.render("Login", this));
785         this.database = new openerp.base.Database(
786                 this, "oe_database", "oe_db_options");
787
788         this.$element.find('#oe-db-config').click(function() {
789             self.database.start();
790         });
791
792         this.$element.find("form").submit(this.on_submit);
793     },
794     on_login_invalid: function() {
795         this.$element.closest(".openerp").addClass("login-mode");
796     },
797     on_login_valid: function() {
798         this.$element.closest(".openerp").removeClass("login-mode");
799     },
800     on_submit: function(ev) {
801         ev.preventDefault();
802         var $e = this.$element;
803         var db = $e.find("form [name=db]").val();
804         var login = $e.find("form input[name=login]").val();
805         var password = $e.find("form input[name=password]").val();
806
807         this.do_login(db, login, password);
808     },
809     /**
810      * Performs actual login operation, and UI-related stuff
811      *
812      * @param {String} db database to log in
813      * @param {String} login user login
814      * @param {String} password user password
815      */
816     do_login: function (db, login, password) {
817         var self = this;
818         this.session.session_login(db, login, password, function() {
819             if(self.session.session_is_valid()) {
820                 if (self.has_local_storage) {
821                     if(self.remember_creditentials) {
822                         localStorage.setItem('last_db_login_success', db);
823                         localStorage.setItem('last_login_login_success', login);
824                     } else {
825                         localStorage.setItem('last_db_login_success', '');
826                         localStorage.setItem('last_login_login_success', '');
827                     }
828                 }
829                 self.on_login_valid();
830             } else {
831                 self.$element.addClass("login_invalid");
832                 self.on_login_invalid();
833             }
834         });
835     },
836     do_ask_login: function(continuation) {
837         this.on_login_invalid();
838         this.$element
839             .removeClass("login_invalid");
840         this.on_login_valid.add({
841             position: "last",
842             unique: true,
843             callback: continuation
844         });
845     },
846     on_logout: function() {
847         this.session.logout();
848     }
849 });
850
851 openerp.base.Header =  openerp.base.Widget.extend({
852     init: function(parent, element_id) {
853         this._super(parent, element_id);
854     },
855     start: function() {
856         this.do_update();
857     },
858     do_update: function() {
859         this.$element.html(QWeb.render("Header", this));
860         this.$element.find(".logout").click(this.on_logout);
861     },
862     on_logout: function() {}
863 });
864
865 openerp.base.Menu =  openerp.base.Widget.extend({
866     init: function(parent, element_id, secondary_menu_id) {
867         this._super(parent, element_id);
868         this.secondary_menu_id = secondary_menu_id;
869         this.$secondary_menu = $("#" + secondary_menu_id).hide();
870         this.menu = false;
871     },
872     start: function() {
873         this.rpc("/base/menu/load", {}, this.on_loaded);
874     },
875     on_loaded: function(data) {
876         this.data = data;
877         this.$element.html(QWeb.render("Menu", this.data));
878         for (var i = 0; i < this.data.data.children.length; i++) {
879             var v = { menu : this.data.data.children[i] };
880             this.$secondary_menu.append(QWeb.render("Menu.secondary", v));
881         }
882         this.$secondary_menu.find("div.menu_accordion").accordion({
883             animated : false,
884             autoHeight : false,
885             icons : false
886         });
887         this.$secondary_menu.find("div.submenu_accordion").accordion({
888             animated : false,
889             autoHeight : false,
890             active: false,
891             collapsible: true,
892             header: 'h4'
893         });
894
895         this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
896     },
897     on_menu_click: function(ev, id) {
898         id = id || 0;
899         var $menu, $parent, $secondary;
900
901         if (id) {
902             // We can manually activate a menu with it's id (for hash url mapping)
903             $menu = this.$element.find('a[data-menu=' + id + ']');
904             if (!$menu.length) {
905                 $menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
906             }
907         } else {
908             $menu = $(ev.currentTarget);
909             id = $menu.data('menu');
910         }
911         if (this.$secondary_menu.has($menu).length) {
912             $secondary = $menu.parents('.menu_accordion');
913             $parent = this.$element.find('a[data-menu=' + $secondary.data('menu-parent') + ']');
914         } else {
915             $parent = $menu;
916             $secondary = this.$secondary_menu.find('.menu_accordion[data-menu-parent=' + $menu.attr('data-menu') + ']');
917         }
918
919         this.$secondary_menu.find('.menu_accordion').hide();
920         // TODO: ui-accordion : collapse submenus and expand the good one
921         $secondary.show();
922
923         if (id) {
924             this.rpc('/base/menu/action', {'menu_id': id},
925                     this.on_menu_action_loaded);
926         }
927
928         $('.active', this.$element.add(this.$secondary_menu.show())).removeClass('active');
929         $parent.addClass('active');
930         $menu.addClass('active');
931         $menu.parent('h4').addClass('active');
932
933         return !$menu.is(".leaf");
934     },
935     on_menu_action_loaded: function(data) {
936         var self = this;
937         if (data.action.length) {
938             var action = data.action[0][2];
939             self.on_action(action);
940         }
941     },
942     on_action: function(action) {
943     }
944 });
945
946 openerp.base.Homepage = openerp.base.Widget.extend({
947 });
948
949 openerp.base.Preferences = openerp.base.Widget.extend({
950 });
951
952 openerp.base.ImportExport = openerp.base.Widget.extend({
953 });
954
955 openerp.base.WebClient = openerp.base.Widget.extend({
956     init: function(element_id) {
957         this._super(null, element_id);
958
959         QWeb.add_template("/base/static/src/xml/base.xml");
960         var params = {};
961         if(jQuery.param != undefined && jQuery.deparam(jQuery.param.querystring()).kitten != undefined) {
962             this.$element.addClass("kitten-mode-activated");
963         }
964         this.$element.html(QWeb.render("Interface", params));
965
966         this.session = new openerp.base.Session(this,"oe_errors");
967         this.loading = new openerp.base.Loading(this,"oe_loading");
968         this.crashmanager =  new openerp.base.CrashManager(this);
969         this.crashmanager.start(false);
970
971         // Do you autorize this ? will be replaced by notify() in controller
972         openerp.base.Widget.prototype.notification = new openerp.base.Notification(this, "oe_notification");
973
974         this.header = new openerp.base.Header(this, "oe_header");
975         this.login = new openerp.base.Login(this, "oe_login");
976         this.header.on_logout.add(this.login.on_logout);
977
978         this.session.on_session_invalid.add(this.login.do_ask_login);
979         this.session.on_session_valid.add_last(this.header.do_update);
980         this.session.on_session_valid.add_last(this.on_logged);
981
982         this.menu = new openerp.base.Menu(this, "oe_menu", "oe_secondary_menu");
983         this.menu.on_action.add(this.on_menu_action);
984
985     },
986     start: function() {
987         this.session.start();
988         this.header.start();
989         this.login.start();
990         this.menu.start();
991         this.notification.notify("OpenERP Client", "The openerp client has been initialized.");
992     },
993     on_logged: function() {
994         this.action_manager =  new openerp.base.ActionManager(this, "oe_app");
995         this.action_manager.start();
996
997         // if using saved actions, load the action and give it to action manager
998         var parameters = jQuery.deparam(jQuery.param.querystring());
999         if (parameters["s_action"] != undefined) {
1000             var key = parseInt(parameters["s_action"], 10);
1001             var self = this;
1002             this.rpc("/base/session/get_session_action", {key:key}, function(action) {
1003                 self.action_manager.do_action(action);
1004             });
1005         } else if (openerp._modules_loaded) { // TODO: find better option than this
1006             this.load_url_state()
1007         } else {
1008             this.session.on_modules_loaded.add({
1009                 callback: $.proxy(this, 'load_url_state'),
1010                 unique: true,
1011                 position: 'last'
1012             })
1013         }
1014     },
1015     /**
1016      * Loads state from URL if any, or checks if there is a home action and
1017      * loads that, assuming we're at the index
1018      */
1019     load_url_state: function () {
1020         var self = this;
1021         // TODO: add actual loading if there is url state to unpack, test on window.location.hash
1022
1023         // not logged in
1024         if (!this.session.uid) { return; }
1025         var ds = new openerp.base.DataSetSearch(this, 'res.users');
1026         ds.read_ids([this.session.uid], ['action_id'], function (users) {
1027             var home_action = users[0].action_id;
1028             if (!home_action) {
1029                 self.default_home();
1030                 return;
1031             }
1032             self.execute_home_action(home_action[0], ds);
1033         })
1034     },
1035     default_home: function () { },
1036     /**
1037      * Bundles the execution of the home action
1038      *
1039      * @param {Number} action action id
1040      * @param {openerp.base.DataSet} dataset action executor
1041      */
1042     execute_home_action: function (action, dataset) {
1043         var self = this;
1044         this.rpc('/base/action/load', {
1045             action_id: action,
1046             context: dataset.get_context()
1047         }, function (meh) {
1048             var action = meh.result;
1049             action.context = _.extend(action.context || {}, {
1050                 active_id: false,
1051                 active_ids: [false],
1052                 active_model: dataset.model
1053             });
1054             self.action_manager.do_action(action);
1055         });
1056     },
1057     on_menu_action: function(action) {
1058         this.action_manager.do_action(action);
1059     },
1060     do_about: function() {
1061     }
1062 });
1063
1064 openerp.base.webclient = function(element_id) {
1065     // TODO Helper to start webclient rename it openerp.base.webclient
1066     var client = new openerp.base.WebClient(element_id);
1067     client.start();
1068     return client;
1069 };
1070
1071 };
1072
1073 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: