[FIX] embed client do not save the session to a cookie
[odoo/odoo.git] / addons / web / static / src / js / chrome.js
1 /*---------------------------------------------------------
2  * OpenERP Web chrome
3  *---------------------------------------------------------*/
4 openerp.web.chrome = function(openerp) {
5 var QWeb = openerp.web.qweb,
6     _t = openerp.web._t;
7
8 openerp.web.Notification =  openerp.web.Widget.extend(/** @lends openerp.web.Notification# */{
9     template: 'Notification',
10     identifier_prefix: 'notification-',
11
12     init: function() {
13         this._super.apply(this, arguments);
14         openerp.notification = this;
15     },
16
17     start: function() {
18         this._super.apply(this, arguments);
19         this.$element.notify({
20             speed: 500,
21             expires: 1500
22         });
23     },
24     notify: function(title, text) {
25         this.$element.notify('create', {
26             title: title,
27             text: text
28         });
29     },
30     warn: function(title, text) {
31         this.$element.notify('create', 'oe_notification_alert', {
32             title: title,
33             text: text
34         });
35     }
36
37 });
38
39 openerp.web.Dialog = openerp.web.OldWidget.extend(/** @lends openerp.web.Dialog# */{
40     dialog_title: "",
41     identifier_prefix: 'dialog',
42     /**
43      * @constructs openerp.web.Dialog
44      * @extends openerp.web.OldWidget
45      *
46      * @param parent
47      * @param dialog_options
48      */
49     init: function (parent, dialog_options) {
50         var self = this;
51         this._super(parent);
52         this.dialog_options = {
53             modal: true,
54             width: 'auto',
55             min_width: 0,
56             max_width: '100%',
57             height: 'auto',
58             min_height: 0,
59             max_height: '100%',
60             autoOpen: false,
61             buttons: {},
62             beforeClose: function () { self.on_close(); }
63         };
64         for (var f in this) {
65             if (f.substr(0, 10) == 'on_button_') {
66                 this.dialog_options.buttons[f.substr(10)] = this[f];
67             }
68         }
69         if (dialog_options) {
70             this.set_options(dialog_options);
71         }
72     },
73     set_options: function(options) {
74         options = options || {};
75         options.width = this.get_width(options.width || this.dialog_options.width);
76         options.min_width = this.get_width(options.min_width || this.dialog_options.min_width);
77         options.max_width = this.get_width(options.max_width || this.dialog_options.max_width);
78         options.height = this.get_height(options.height || this.dialog_options.height);
79         options.min_height = this.get_height(options.min_height || this.dialog_options.min_height);
80         options.max_height = this.get_height(options.max_height || this.dialog_options.max_width);
81
82         if (options.width !== 'auto') {
83             if (options.width > options.max_width) options.width = options.max_width;
84             if (options.width < options.min_width) options.width = options.min_width;
85         }
86         if (options.height !== 'auto') {
87             if (options.height > options.max_height) options.height = options.max_height;
88             if (options.height < options.min_height) options.height = options.min_height;
89         }
90         if (!options.title && this.dialog_title) {
91             options.title = this.dialog_title;
92         }
93         _.extend(this.dialog_options, options);
94     },
95     get_width: function(val) {
96         return this.get_size(val.toString(), $(window.top).width());
97     },
98     get_height: function(val) {
99         return this.get_size(val.toString(), $(window.top).height());
100     },
101     get_size: function(val, available_size) {
102         if (val === 'auto') {
103             return val;
104         } else if (val.slice(-1) == "%") {
105             return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
106         } else {
107             return parseInt(val, 10);
108         }
109     },
110     start: function () {
111         this.$dialog = $(this.$element).dialog(this.dialog_options);
112         this._super();
113         return this;
114     },
115     open: function(dialog_options) {
116         // TODO fme: bind window on resize
117         if (this.template) {
118             this.$element.html(this.render());
119         }
120         this.set_options(dialog_options);
121         this.$dialog.dialog(this.dialog_options).dialog('open');
122         return this;
123     },
124     close: function() {
125         // Closes the dialog but leave it in a state where it could be opened again.
126         this.$dialog.dialog('close');
127     },
128     on_close: function() {
129     },
130     stop: function () {
131         // Destroy widget
132         this.close();
133         this.$dialog.dialog('destroy');
134         this._super();
135     }
136 });
137
138 openerp.web.CrashManager = openerp.web.CallbackEnabled.extend({
139     init: function() {
140         this._super();
141         openerp.connection.on_rpc_error.add(this.on_rpc_error);
142     },
143     on_rpc_error: function(error) {
144         this.error = error;
145         if (error.data.fault_code) {
146             var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
147             if (split.length > 1) {
148                 error.type = split.shift();
149                 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
150             }
151         }
152         if (error.code === 200 && error.type) {
153             this.on_managed_error(error);
154         } else {
155             this.on_traceback(error);
156         }
157     },
158     on_managed_error: function(error) {
159         $('<div>' + QWeb.render('DialogWarning', {error: error}) + '</div>').dialog({
160             title: "OpenERP " + _.str.capitalize(error.type),
161             buttons: [
162                 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
163             ]
164         });
165     },
166     on_traceback: function(error) {
167         var dialog = new openerp.web.Dialog(this, {
168             title: "OpenERP " + _.str.capitalize(error.type),
169             autoOpen: true,
170             width: '90%',
171             height: '90%',
172             min_width: '800px',
173             min_height: '600px',
174             buttons: [
175                 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
176             ]
177         }).start();
178         dialog.$element.html(QWeb.render('DialogTraceback', {error: error}));
179     }
180 });
181
182 openerp.web.Loading = openerp.web.Widget.extend(/** @lends openerp.web.Loading# */{
183     template: 'Loading',
184     /**
185      * @constructs openerp.web.Loading
186      * @extends openerp.web.Widget
187      *
188      * @param parent
189      * @param element_id
190      */
191     init: function(parent) {
192         this._super(parent);
193         this.count = 0;
194         this.blocked_ui = false;
195         this.session.on_rpc_request.add_first(this.on_rpc_event, 1);
196         this.session.on_rpc_response.add_last(this.on_rpc_event, -1);
197     },
198     on_rpc_event : function(increment) {
199         var self = this;
200         if (!this.count && increment === 1) {
201             // Block UI after 3s
202             this.long_running_timer = setTimeout(function () {
203                 self.blocked_ui = true;
204                 $.blockUI();
205             }, 3000);
206         }
207
208         this.count += increment;
209         if (this.count > 0) {
210             //this.$element.html(QWeb.render("Loading", {}));
211             this.$element.html("Loading ("+this.count+")");
212             this.$element.show();
213             this.widget_parent.$element.addClass('loading');
214         } else {
215             this.count = 0;
216             clearTimeout(this.long_running_timer);
217             // Don't unblock if blocked by somebody else
218             if (self.blocked_ui) {
219                 this.blocked_ui = false;
220                 $.unblockUI();
221             }
222             this.$element.fadeOut();
223             this.widget_parent.$element.removeClass('loading');
224         }
225     }
226 });
227
228 openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database# */{
229     /**
230      * @constructs openerp.web.Database
231      * @extends openerp.web.Widget
232      *
233      * @param parent
234      * @param element_id
235      * @param option_id
236      */
237     init: function(parent, element_id, option_id) {
238         this._super(parent, element_id);
239         this.$option_id = $('#' + option_id);
240         this.unblockUIFunction = $.unblockUI;
241     },
242     start: function() {
243         this._super();
244         this.$element.html(QWeb.render("Database", this));
245
246         var self = this;
247         var fetch_db = this.rpc("/web/database/get_list", {}, function(result) {
248             self.db_list = result.db_list;
249         });
250         var fetch_langs = this.rpc("/web/session/get_lang_list", {}, function(result) {
251             if (result.error) {
252                 self.display_error(result);
253                 return;
254             }
255             self.lang_list = result.lang_list;
256         });
257         $.when(fetch_db, fetch_langs).then(function () {self.do_create();});
258
259         this.$element.find('#db-create').click(this.do_create);
260         this.$element.find('#db-drop').click(this.do_drop);
261         this.$element.find('#db-backup').click(this.do_backup);
262         this.$element.find('#db-restore').click(this.do_restore);
263         this.$element.find('#db-change-password').click(this.do_change_password);
264         this.$element.find('#back-to-login').click(function() {
265             self.hide();
266         });
267     },
268     stop: function () {
269         this.hide();
270         this.$option_id.empty();
271
272         this.$element
273             .find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login')
274                 .unbind('click')
275             .end()
276             .empty();
277         this._super();
278     },
279     show: function () {
280         this.$element.closest(".openerp")
281                 .removeClass("login-mode")
282                 .addClass("database_block");
283     },
284     hide: function () {
285         this.$element.closest(".openerp")
286                 .addClass("login-mode")
287                 .removeClass("database_block")
288     },
289     /**
290      * Converts a .serializeArray() result into a dict. Does not bother folding
291      * multiple identical keys into an array, last key wins.
292      *
293      * @param {Array} array
294      */
295     to_object: function (array) {
296         var result = {};
297         _(array).each(function (record) {
298             result[record.name] = record.value;
299         });
300         return result;
301     },
302     /**
303      * Waits until the new database is done creating, then unblocks the UI and
304      * logs the user in as admin
305      *
306      * @param {Number} db_creation_id identifier for the db-creation operation, used to fetch the current installation progress
307      * @param {Object} info info fields for this database creation
308      * @param {String} info.db name of the database being created
309      * @param {String} info.password super-admin password for the database
310      */
311     wait_for_newdb: function (db_creation_id, info) {
312         var self = this;
313         self.rpc('/web/database/progress', {
314             id: db_creation_id,
315             password: info.password
316         }, function (result) {
317             var progress = result[0];
318             // I'd display a progress bar, but turns out the progress status
319             // the server report kind-of blows goats: it's at 0 for ~75% of
320             // the installation, then jumps to 75%, then jumps down to either
321             // 0 or ~40%, then back up to 75%, then terminates. Let's keep that
322             // mess hidden behind a not-very-useful but not overly weird
323             // message instead.
324             if (progress < 1) {
325                 setTimeout(function () {
326                     self.wait_for_newdb(db_creation_id, info);
327                 }, 500);
328                 return;
329             }
330
331             var admin = result[1][0];
332             setTimeout(function () {
333                 self.widget_parent.do_login(
334                         info.db, admin.login, admin.password);
335                 self.stop();
336                 self.unblockUI();
337             });
338         });
339     },
340     /**
341      * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
342      * from unblocking the UI
343      */
344     blockUI: function () {
345         $.blockUI();
346         $.unblockUI = function () {};
347     },
348     /**
349      * Reinstates $.unblockUI so third parties can play with blockUI, and
350      * unblocks the UI
351      */
352     unblockUI: function () {
353         $.unblockUI = this.unblockUIFunction;
354         $.unblockUI();
355     },
356     /**
357      * Displays an error dialog resulting from the various RPC communications
358      * failing over themselves
359      *
360      * @param {Object} error error description
361      * @param {String} error.title title of the error dialog
362      * @param {String} error.error message of the error dialog
363      */
364     display_error: function (error) {
365         return $('<div>').dialog({
366             modal: true,
367             title: error.title,
368             buttons: [
369                 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
370             ]
371         }).html(error.error);
372     },
373     do_create: function() {
374         var self = this;
375         self.$option_id.html(QWeb.render("Database.CreateDB", self));
376         self.$option_id.find("form[name=create_db_form]").validate({
377             submitHandler: function (form) {
378                 var fields = $(form).serializeArray();
379                 self.blockUI();
380                 self.rpc("/web/database/create", {'fields': fields}, function(result) {
381                     if (result.error) {
382                         self.unblockUI();
383                         self.display_error(result);
384                         return;
385                     }
386                     self.db_list.push(self.to_object(fields)['db_name']);
387                     self.db_list.sort();
388                     self.widget_parent.set_db_list(self.db_list);
389                     var form_obj = self.to_object(fields);
390                     self.wait_for_newdb(result, {
391                         password: form_obj['super_admin_pwd'],
392                         db: form_obj['db_name']
393                     });
394                 });
395             }
396         });
397     },
398     do_drop: function() {
399         var self = this;
400         self.$option_id.html(QWeb.render("DropDB", self));
401         self.$option_id.find("form[name=drop_db_form]").validate({
402             submitHandler: function (form) {
403                 var $form = $(form),
404                     fields = $form.serializeArray(),
405                     $db_list = $form.find('select[name=drop_db]'),
406                     db = $db_list.val();
407
408                 if (!confirm("Do you really want to delete the database: " + db + " ?")) {
409                     return;
410                 }
411                 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
412                     if (result.error) {
413                         self.display_error(result);
414                         return;
415                     }
416                     $db_list.find(':selected').remove();
417                     self.db_list.splice(_.indexOf(self.db_list, db, true), 1);
418                     self.widget_parent.set_db_list(self.db_list);
419                     self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
420                 });
421             }
422         });
423     },
424     do_backup: function() {
425         var self = this;
426         self.$option_id
427             .html(QWeb.render("BackupDB", self))
428             .find("form[name=backup_db_form]").validate({
429             submitHandler: function (form) {
430                 self.blockUI();
431                 self.session.get_file({
432                     form: form,
433                     error: function (body) {
434                         var error = body.firstChild.data.split('|');
435                         self.display_error({
436                             title: error[0],
437                             error: error[1]
438                         });
439                     },
440                     complete: $.proxy(self, 'unblockUI')
441                 });
442             }
443         });
444     },
445     do_restore: function() {
446         var self = this;
447         self.$option_id.html(QWeb.render("RestoreDB", self));
448
449         self.$option_id.find("form[name=restore_db_form]").validate({
450             submitHandler: function (form) {
451                 self.blockUI();
452                 $(form).ajaxSubmit({
453                     url: '/web/database/restore',
454                     type: 'POST',
455                     resetForm: true,
456                     success: function (body) {
457                         // TODO: ui manipulations
458                         // note: response objects don't work, but we have the
459                         // HTTP body of the response~~
460
461                         // If empty body, everything went fine
462                         if (!body) { return; }
463
464                         if (body.indexOf('403 Forbidden') !== -1) {
465                             self.display_error({
466                                 title: 'Access Denied',
467                                 error: 'Incorrect super-administrator password'
468                             })
469                         } else {
470                             self.display_error({
471                                 title: 'Restore Database',
472                                 error: 'Could not restore the database'
473                             })
474                         }
475                     },
476                     complete: $.proxy(self, 'unblockUI')
477                 });
478             }
479         });
480     },
481     do_change_password: function() {
482         var self = this;
483         self.$option_id.html(QWeb.render("Change_DB_Pwd", self));
484
485         self.$option_id.find("form[name=change_pwd_form]").validate({
486             messages: {
487                 old_pwd: "Please enter your previous password",
488                 new_pwd: "Please enter your new password",
489                 confirm_pwd: {
490                     required: "Please confirm your new password",
491                     equalTo: "The confirmation does not match the password"
492                 }
493             },
494             submitHandler: function (form) {
495                 self.rpc("/web/database/change_password", {
496                     'fields': $(form).serializeArray()
497                 }, function(result) {
498                     if (result.error) {
499                         self.display_error(result);
500                         return;
501                     }
502                     self.do_notify("Changed Password", "Password has been changed successfully");
503                 });
504             }
505         });
506     }
507 });
508
509 openerp.web.Login =  openerp.web.Widget.extend(/** @lends openerp.web.Login# */{
510     remember_credentials: true,
511     
512     template: "Login",
513     identifier_prefix: 'oe-app-login-',
514     /**
515      * @constructs openerp.web.Login
516      * @extends openerp.web.Widget
517      *
518      * @param parent
519      * @param element_id
520      */
521
522     init: function(parent) {
523         this._super(parent);
524         this.has_local_storage = typeof(localStorage) != 'undefined';
525         this.selected_db = null;
526         this.selected_login = null;
527
528         if (this.has_local_storage && this.remember_credentials) {
529             this.selected_db = localStorage.getItem('last_db_login_success');
530             this.selected_login = localStorage.getItem('last_login_login_success');
531             if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
532                 this.selected_password = localStorage.getItem('last_password_login_success');
533             }
534         }
535     },
536     start: function() {
537         var self = this;
538         this.database = new openerp.web.Database(
539                 this, "oe_database", "oe_db_options");
540         this.database.start();
541
542         this.$element.find('#oe-db-config').click(function() {
543             self.database.show();
544         });
545
546         this.$element.find("form").submit(this.on_submit);
547
548         this.rpc("/web/database/get_list", {}, function(result) {
549             self.set_db_list(result.db_list);
550         }, 
551         function(error, event) {
552             if (error.data.fault_code === 'AccessDenied') {
553                 event.preventDefault();
554             }
555         });
556
557     },
558     stop: function () {
559         this.database.stop();
560         this._super();
561     },
562     set_db_list: function (list) {
563         this.$element.find("[name=db]").replaceWith(
564             openerp.web.qweb.render('Login_dblist', {
565                 db_list: list, selected_db: this.selected_db}))
566     },
567     on_login_invalid: function() {
568         this.$element.closest(".openerp").addClass("login-mode");
569     },
570     on_login_valid: function() {
571         this.$element.closest(".openerp").removeClass("login-mode");
572     },
573     on_submit: function(ev) {
574         if(ev) {
575             ev.preventDefault();
576         }
577         var $e = this.$element;
578         var db = $e.find("form [name=db]").val();
579         var login = $e.find("form input[name=login]").val();
580         var password = $e.find("form input[name=password]").val();
581
582         this.do_login(db, login, password);
583     },
584     /**
585      * Performs actual login operation, and UI-related stuff
586      *
587      * @param {String} db database to log in
588      * @param {String} login user login
589      * @param {String} password user password
590      */
591     do_login: function (db, login, password) {
592         var self = this;
593         this.session.session_authenticate(db, login, password).then(function() {
594             if(self.session.session_is_valid()) {
595                 if (self.has_local_storage) {
596                     if(self.remember_credentials) {
597                         localStorage.setItem('last_db_login_success', db);
598                         localStorage.setItem('last_login_login_success', login);
599                         if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
600                             localStorage.setItem('last_password_login_success', password);
601                         }
602                     } else {
603                         localStorage.setItem('last_db_login_success', '');
604                         localStorage.setItem('last_login_login_success', '');
605                         localStorage.setItem('last_password_login_success', '');
606                     }
607                 }
608                 self.on_login_valid();
609             } else {
610                 self.$element.addClass("login_invalid");
611                 self.on_login_invalid();
612             }
613         });
614     },
615     do_ask_login: function(continuation) {
616         this.on_login_invalid();
617         this.$element
618             .removeClass("login_invalid");
619         this.on_login_valid.add({
620             position: "last",
621             unique: true,
622             callback: continuation || function() {}
623         });
624     },
625 });
626
627 openerp.web.Header =  openerp.web.Widget.extend(/** @lends openerp.web.Header# */{
628     template: "Header",
629     identifier_prefix: 'oe-app-header-',
630     /**
631      * @constructs openerp.web.Header
632      * @extends openerp.web.Widget
633      *
634      * @param parent
635      */
636     init: function(parent) {
637         this._super(parent);
638         this.qs = "?" + jQuery.param.querystring();
639         this.$content = $();
640         this.update_promise = $.Deferred().resolve();
641     },
642     start: function() {
643         this._super();
644     },
645     do_update: function () {
646         var self = this;
647         var fct = function() {
648             self.$content.remove();
649             if (!self.session.uid)
650                 return;
651             var func = new openerp.web.Model("res.users").get_func("read");
652             return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
653                 self.$content = $(QWeb.render("Header-content", {widget: self, user: res}));
654                 self.$content.appendTo(self.$element);
655                 self.$element.find(".logout").click(self.on_logout);
656                 self.$element.find("a.preferences").click(self.on_preferences);
657                 self.$element.find(".about").click(self.on_about);
658                 return self.shortcut_load();
659             });
660         };
661         this.update_promise = this.update_promise.pipe(fct, fct);
662     },
663     on_about: function() {
664         var self = this;
665         self.rpc("/web/webclient/version_info", {}).then(function(res) {
666             var $help = $(QWeb.render("About-Page", {version_info: res}));
667             $help.dialog({autoOpen: true,
668                 modal: true, width: 960, title: _t("About")});
669         });
670     },
671     shortcut_load :function(){
672         var self = this,
673             sc = self.session.shortcuts,
674             shortcuts_ds = new openerp.web.DataSet(this, 'ir.ui.view_sc');
675         // TODO: better way to communicate between sections.
676         // sc.bindings, because jquery does not bind/trigger on arrays...
677         if (!sc.binding) {
678             sc.binding = {};
679             $(sc.binding).bind({
680                 'add': function (e, attrs) {
681                     shortcuts_ds.create(attrs, function (out) {
682                         $('<li>', {
683                             'data-shortcut-id':out.result,
684                             'data-id': attrs.res_id
685                         }).text(attrs.name)
686                           .appendTo(self.$element.find('.oe-shortcuts ul'));
687                         attrs.id = out.result;
688                         sc.push(attrs);
689                     });
690                 },
691                 'remove-current': function () {
692                     var menu_id = self.session.active_id;
693                     var $shortcut = self.$element
694                         .find('.oe-shortcuts li[data-id=' + menu_id + ']');
695                     var shortcut_id = $shortcut.data('shortcut-id');
696                     $shortcut.remove();
697                     shortcuts_ds.unlink([shortcut_id]);
698                     var sc_new = _.reject(sc, function(shortcut){ return shortcut_id === shortcut.id});
699                     sc.splice(0, sc.length);
700                     sc.push.apply(sc, sc_new);
701                     }
702             });
703         }
704         return this.rpc('/web/session/sc_list', {}, function(shortcuts) {
705             sc.splice(0, sc.length);
706             sc.push.apply(sc, shortcuts);
707
708             self.$element.find('.oe-shortcuts')
709                 .html(QWeb.render('Shortcuts', {'shortcuts': shortcuts}))
710                 .undelegate('li', 'click')
711
712                 .delegate('li', 'click', function(e) {
713                     e.stopPropagation();
714                     var id = $(this).data('id');
715                     self.session.active_id = id;
716                     self.rpc('/web/menu/action', {'menu_id':id}, function(ir_menu_data) {
717                         if (ir_menu_data.action.length){
718                             self.on_action(ir_menu_data.action[0][2]);
719                         }
720                     });
721                 });
722         });
723     },
724
725     on_action: function(action) {
726     },
727     on_preferences: function(){
728         var self = this;
729         var action_manager = new openerp.web.ActionManager(this);
730         var dataset = new openerp.web.DataSet (this,'res.users',this.context);
731         dataset.call ('action_get','',function (result){
732             self.rpc('/web/action/load', {action_id:result}, function(result){
733                 action_manager.do_action(_.extend(result['result'], {
734                     res_id: self.session.uid,
735                     res_model: 'res.users',
736                     flags: {
737                         action_buttons: false,
738                         search_view: false,
739                         sidebar: false,
740                         views_switcher: false,
741                         pager: false
742                     }
743                 }));
744             });
745         });
746         this.dialog = new openerp.web.Dialog(this,{
747             modal: true,
748             title: _t("Preferences"),
749             width: 600,
750             height: 500,
751             buttons: [
752                 {text: _t("Change password"), click: function(){ self.change_password(); }},
753                 {text: _t("Cancel"), click: function(){ $(this).dialog('destroy'); }},
754                 {text: _t("Save"), click: function(){
755                         var inner_viewmanager = action_manager.inner_viewmanager;
756                         inner_viewmanager.views[inner_viewmanager.active_view].controller.do_save()
757                         .then(function() {
758                             self.dialog.stop();
759                             window.location.reload();
760                         });
761                     }
762                 }
763             ]
764         });
765        this.dialog.start().open();
766        action_manager.appendTo(this.dialog);
767        action_manager.render(this.dialog);
768     },
769
770     change_password :function() {
771         var self = this;
772         this.dialog = new openerp.web.Dialog(this,{
773             modal : true,
774             title: _t("Change Password"),
775             width : 'auto',
776             height : 'auto'
777         });
778         this.dialog.start().open();
779         this.dialog.$element.html(QWeb.render("Change_Pwd", self));
780         this.dialog.$element.find("form[name=change_password_form]").validate({
781             submitHandler: function (form) {
782                 self.rpc("/web/session/change_password",{
783                     'fields': $(form).serializeArray()
784                 }, function(result) {
785                     if (result.error) {
786                         self.display_error(result);
787                         return;
788                     } else {
789                         self.session.logout();
790                     }
791                 });
792             }
793         });
794     },
795     display_error: function (error) {
796         return $('<div>').dialog({
797             modal: true,
798             title: error.title,
799             buttons: [
800                 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
801             ]
802         }).html(error.error);
803     },
804     on_logout: function() {
805     }
806 });
807
808 openerp.web.Menu =  openerp.web.Widget.extend(/** @lends openerp.web.Menu# */{
809     /**
810      * @constructs openerp.web.Menu
811      * @extends openerp.web.Widget
812      *
813      * @param parent
814      * @param element_id
815      * @param secondary_menu_id
816      */
817     init: function(parent, element_id, secondary_menu_id) {
818         this._super(parent, element_id);
819         this.secondary_menu_id = secondary_menu_id;
820         this.$secondary_menu = $("#" + secondary_menu_id);
821         this.menu = false;
822         this.folded = false;
823         if (window.localStorage) {
824             this.folded = localStorage.getItem('oe_menu_folded') === 'true';
825         }
826         this.float_timeout = 700;
827     },
828     start: function() {
829         this.$secondary_menu.addClass(this.folded ? 'oe_folded' : 'oe_unfolded');
830     },
831     do_reload: function() {
832         return this.rpc("/web/menu/load", {}, this.on_loaded);
833     },
834     on_loaded: function(data) {
835         this.data = data;
836         this.$element.html(QWeb.render("Menu", { widget : this }));
837         this.$secondary_menu.html(QWeb.render("Menu.secondary", { widget : this }));
838         this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
839         this.$secondary_menu.find('.oe_toggle_secondary_menu').click(this.on_toggle_fold);
840     },
841     on_toggle_fold: function() {
842         this.$secondary_menu.toggleClass('oe_folded').toggleClass('oe_unfolded');
843         if (this.folded) {
844             this.$secondary_menu.find('.oe_secondary_menu.active').show();
845         } else {
846             this.$secondary_menu.find('.oe_secondary_menu').hide();
847         }
848         this.folded = !this.folded;
849         if (window.localStorage) {
850             localStorage.setItem('oe_menu_folded', this.folded.toString());
851         }
852     },
853     on_menu_click: function(ev, id) {
854         id = id || 0;
855         var $clicked_menu, manual = false;
856
857         if (id) {
858             // We can manually activate a menu with it's id (for hash url mapping)
859             manual = true;
860             $clicked_menu = this.$element.find('a[data-menu=' + id + ']');
861             if (!$clicked_menu.length) {
862                 $clicked_menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
863             }
864         } else {
865             $clicked_menu = $(ev.currentTarget);
866             id = $clicked_menu.data('menu');
867         }
868
869         if (this.do_menu_click($clicked_menu, manual) && id) {
870             this.session.active_id = id;
871             this.rpc('/web/menu/action', {'menu_id': id}, this.on_menu_action_loaded);
872         }
873         if (ev) {
874             ev.stopPropagation();
875         }
876         return false;
877     },
878     do_menu_click: function($clicked_menu, manual) {
879         var $sub_menu, $main_menu,
880             active = $clicked_menu.is('.active'),
881             sub_menu_visible = false;
882
883         if (this.$secondary_menu.has($clicked_menu).length) {
884             $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
885             $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
886         } else {
887             $sub_menu = this.$secondary_menu.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
888             $main_menu = $clicked_menu;
889         }
890
891         sub_menu_visible = $sub_menu.is(':visible');
892         this.$secondary_menu.find('.oe_secondary_menu').hide();
893
894         $('.active', this.$element.add(this.$secondary_menu)).removeClass('active');
895         $main_menu.add($clicked_menu).add($sub_menu).addClass('active');
896
897         if (!(this.folded && manual)) {
898             this.do_show_secondary($sub_menu, $main_menu);
899         } else {
900             this.do_show_secondary();
901         }
902
903         if ($main_menu != $clicked_menu) {
904             if ($clicked_menu.is('.submenu')) {
905                 $sub_menu.find('.submenu.opened').each(function() {
906                     if (!$(this).next().has($clicked_menu).length && !$(this).is($clicked_menu)) {
907                         $(this).removeClass('opened').next().hide();
908                     }
909                 });
910                 $clicked_menu.toggleClass('opened').next().toggle();
911             } else if ($clicked_menu.is('.leaf')) {
912                 $sub_menu.toggle(!this.folded);
913                 return true;
914             }
915         } else if (this.folded) {
916             if (active && sub_menu_visible) {
917                 $sub_menu.hide();
918                 return true;
919             }
920             return manual;
921         } else {
922             return true;
923         }
924         return false;
925     },
926     do_hide_secondary: function() {
927         this.$secondary_menu.hide();
928     },
929     do_show_secondary: function($sub_menu, $main_menu) {
930         var self = this;
931         this.$secondary_menu.show();
932         if (!arguments.length) {
933             return;
934         }
935         if (this.folded) {
936             var css = $main_menu.position(),
937                 fold_width = this.$secondary_menu.width() + 2,
938                 window_width = $(window).width();
939             css.top += 33;
940             css.left -= Math.round(($sub_menu.width() - $main_menu.width()) / 2);
941             css.left = css.left < fold_width ? fold_width : css.left;
942             if ((css.left + $sub_menu.width()) > window_width) {
943                 delete(css.left);
944                 css.right = 1;
945             }
946             $sub_menu.css(css);
947             $sub_menu.mouseenter(function() {
948                 clearTimeout($sub_menu.data('timeoutId'));
949                 $sub_menu.data('timeoutId', null);
950                 return false;
951             }).mouseleave(function(evt) {
952                 var timeoutId = setTimeout(function() {
953                     if (self.folded && $sub_menu.data('timeoutId')) {
954                         $sub_menu.hide().unbind('mouseenter').unbind('mouseleave');
955                     }
956                 }, self.float_timeout);
957                 $sub_menu.data('timeoutId', timeoutId);
958                 return false;
959             });
960         }
961         $sub_menu.show();
962     },
963     on_menu_action_loaded: function(data) {
964         var self = this;
965         if (data.action.length) {
966             var action = data.action[0][2];
967             action.from_menu = true;
968             self.on_action(action);
969         } else {
970             self.on_action({type: 'null_action'});
971         }
972     },
973     on_action: function(action) {
974     }
975 });
976
977 openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClient */{
978     /**
979      * @constructs openerp.web.WebClient
980      * @extends openerp.web.Widget
981      *
982      * @param element_id
983      */
984     init: function(element_id) {
985         var self = this;
986         this._super(null, element_id);
987         openerp.webclient = this;
988
989         this.notification = new openerp.web.Notification(this);
990         this.loading = new openerp.web.Loading(this);
991         this.crashmanager =  new openerp.web.CrashManager();
992
993         this.header = new openerp.web.Header(this);
994         this.login = new openerp.web.Login(this);
995         this.header.on_logout.add(this.on_logout);
996         this.header.on_action.add(this.on_menu_action);
997
998         this._current_state = null;
999     },
1000     start: function() {
1001         this._super.apply(this, arguments);
1002         var self = this;
1003         this.session.bind().then(function() {
1004             var params = {};
1005             if (jQuery.param != undefined && jQuery.deparam(jQuery.param.querystring()).kitten != undefined) {
1006                 this.$element.addClass("kitten-mode-activated");
1007                 this.$element.delegate('img.oe-record-edit-link-img', 'hover', function(e) {
1008                     self.$element.toggleClass('clark-gable');
1009                 });
1010             }
1011             self.$element.html(QWeb.render("Interface", params));
1012             self.menu = new openerp.web.Menu(self, "oe_menu", "oe_secondary_menu");
1013             self.menu.on_action.add(self.on_menu_action);
1014
1015             self.notification.prependTo(self.$element);
1016             self.loading.appendTo($('#oe_loading'));
1017             self.header.appendTo($("#oe_header"));
1018             self.login.appendTo($('#oe_login'));
1019             self.menu.start();
1020             if(self.session.session_is_valid()) {
1021                 self.login.on_login_valid();
1022             } else {
1023                 self.login.on_login_invalid();
1024             }
1025         });
1026         this.session.ready.then(function() {
1027             self.login.on_login_valid();
1028             self.header.do_update();
1029             self.menu.do_reload();
1030             if(self.action_manager)
1031                 self.action_manager.stop();
1032             self.action_manager = new openerp.web.ActionManager(self);
1033             self.action_manager.appendTo($("#oe_app"));
1034             self.bind_hashchange();
1035         });
1036     },
1037     do_reload: function() {
1038         return $.when(this.session.session_init(),this.menu.do_reload());
1039     },
1040     do_notify: function() {
1041         var n = this.notification;
1042         n.notify.apply(n, arguments);
1043     },
1044     do_warn: function() {
1045         var n = this.notification;
1046         n.warn.apply(n, arguments);
1047     },
1048     on_logout: function() {
1049         this.session.session_logout();
1050         this.login.on_login_invalid();
1051         this.header.do_update();
1052         $(window).unbind('hashchange', this.on_hashchange);
1053         this.do_push_state({});
1054         if(this.action_manager)
1055             this.action_manager.stop();
1056         this.action_manager = null;
1057     },
1058     bind_hashchange: function() {
1059         $(window).bind('hashchange', this.on_hashchange);
1060
1061         var state = $.bbq.getState(true);
1062         if (! _.isEmpty(state)) {
1063             $(window).trigger('hashchange');
1064         } else {
1065             this.action_manager.do_action({type: 'ir.actions.client', tag: 'default_home'});
1066         }
1067     },
1068     on_hashchange: function(event) {
1069         var state = event.getState(true);
1070         if (!_.isEqual(this._current_state, state)) {
1071             this.action_manager.do_load_state(state);
1072         }
1073         this._current_state = state;
1074     },
1075     do_push_state: function(state) {
1076         var url = '#' + $.param(state);
1077         this._current_state = _.clone(state);
1078         $.bbq.pushState(url);
1079     },
1080     on_menu_action: function(action) {
1081         this.action_manager.do_action(action);
1082     },
1083     do_action: function(action) {
1084         var self = this;
1085         // TODO replace by client action menuclick 
1086         if(action.type === "ir.ui.menu") {
1087             this.do_reload().then(function () {
1088                 self.menu.on_menu_click(null, action.menu_id);
1089             });
1090         }
1091     },
1092 });
1093
1094 openerp.web.EmbeddedClient = openerp.web.Widget.extend({
1095     template: 'EmptyComponent',
1096     init: function(action_id, options) {
1097         this._super();
1098         // TODO take the xmlid of a action instead of its id 
1099         this.action_id = action_id;
1100         this.options = options || {};
1101         this.am = new openerp.web.ActionManager(this);
1102     },
1103
1104     start: function() {
1105         var self = this;
1106         this.am.appendTo(this.$element.addClass('openerp'));
1107         return this.rpc("/web/action/load", { action_id: this.action_id }, function(result) {
1108             var action = result.result;
1109             action.flags = _.extend({
1110                 //views_switcher : false,
1111                 search_view : false,
1112                 action_buttons : false,
1113                 sidebar : false
1114                 //pager : false
1115             }, self.options, action.flags || {});
1116
1117             self.am.do_action(action);
1118         });
1119     },
1120
1121 });
1122
1123 openerp.web.embed = function (origin, dbname, login, key, action, options) {
1124     $('head').append($('<link>', {
1125         'rel': 'stylesheet',
1126         'type': 'text/css',
1127         'href': origin +'/web/webclient/css'
1128     }));
1129     var currentScript = document.currentScript;
1130     if (!currentScript) {
1131         var sc = document.getElementsByTagName('script');
1132         currentScript = sc[sc.length-1];
1133     }
1134     openerp.connection.bind(origin).then(function () {
1135         openerp.connection.session_authenticate(dbname, login, key, true).then(function () {
1136             var client = new openerp.web.EmbeddedClient(action, options);
1137             client.insertAfter(currentScript);
1138         });
1139     });
1140
1141 }
1142
1143 };
1144
1145 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: