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