1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
4 openerp.web.chrome = function(openerp) {
5 var QWeb = openerp.web.qweb,
8 openerp.web.Notification = openerp.web.OldWidget.extend(/** @lends openerp.web.Notification# */{
9 template: 'Notification',
12 this._super.apply(this, arguments);
13 openerp.notification = this;
17 this._super.apply(this, arguments);
18 this.$element.notify({
23 notify: function(title, text, sticky) {
29 this.$element.notify('create', {
34 warn: function(title, text, sticky) {
40 this.$element.notify('create', 'oe_notification_alert', {
48 openerp.web.Dialog = openerp.web.OldWidget.extend(/** @lends openerp.web.Dialog# */{
51 * @constructs openerp.web.Dialog
52 * @extends openerp.web.OldWidget
57 init: function (parent, options, content) {
61 this.$element = content instanceof $ ? content : $(content);
63 this.dialog_options = {
65 destroy_on_close: true,
66 width: $(window).width() * (($(window).width() > 1024) ? 0.5 : 0.75),
71 max_height: this.get_height('100%') - 140,
73 position: [false, 50],
75 beforeClose: function () { self.on_close(); },
76 resizeStop: this.on_resized
79 if (f.substr(0, 10) == 'on_button_') {
80 this.dialog_options.buttons[f.substr(10)] = this[f];
84 _.extend(this.dialog_options, options);
86 if (this.dialog_options.autoOpen) {
89 this.$element.dialog(this.get_options());
92 get_options: function(options) {
94 o = _.extend({}, this.dialog_options, options || {});
95 _.each(['width', 'height'], function(unit) {
96 o[unit] = self['get_' + unit](o[unit]);
97 o['min_' + unit] = self['get_' + unit](o['min_' + unit] || 0);
98 o['max_' + unit] = self['get_' + unit](o['max_' + unit] || 0);
99 if (o[unit] !== 'auto' && o['min_' + unit] && o[unit] < o['min_' + unit]) o[unit] = o['min_' + unit];
100 if (o[unit] !== 'auto' && o['max_' + unit] && o[unit] > o['max_' + unit]) o[unit] = o['max_' + unit];
102 if (!o.title && this.dialog_title) {
103 o.title = this.dialog_title;
107 get_width: function(val) {
108 return this.get_size(val.toString(), $(window.top).width());
110 get_height: function(val) {
111 return this.get_size(val.toString(), $(window.top).height());
113 get_size: function(val, available_size) {
114 if (val === 'auto') {
116 } else if (val.slice(-1) == "%") {
117 return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
119 return parseInt(val, 10);
122 open: function(options) {
123 // TODO fme: bind window on resize
125 this.$element.html(this.render());
127 var o = this.get_options(options);
128 this.$element.dialog(o).dialog('open');
129 if (o.height === 'auto' && o.max_height) {
130 this.$element.css({ 'max-height': o.max_height, 'overflow-y': 'auto' });
135 this.$element.dialog('close');
137 on_close: function() {
138 if (this.dialog_options.destroy_on_close) {
139 this.$element.dialog('destroy');
142 on_resized: function() {
143 //openerp.log("Dialog resized to %d x %d", this.$element.width(), this.$element.height());
148 this.$element.dialog('destroy');
153 openerp.web.CrashManager = openerp.web.CallbackEnabled.extend({
154 on_rpc_error: function(error) {
155 if (error.data.fault_code) {
156 var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
157 if (split.length > 1) {
158 error.type = split.shift();
159 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
162 if (error.code === 200 && error.type) {
163 this.on_managed_error(error);
165 this.on_traceback(error);
168 on_managed_error: function(error) {
169 $('<div>' + QWeb.render('CrashManagerWarning', {error: error}) + '</div>').dialog({
170 title: "OpenERP " + _.str.capitalize(error.type),
172 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
176 on_traceback: function(error) {
179 if (openerp.connection.openerp_entreprise) {
180 buttons[_t("Send OpenERP Enterprise Report")] = function() {
182 var issuename = $('#issuename').val();
183 var explanation = $('#explanation').val();
184 var remark = $('#remark').val();
185 // Call the send method from server to send mail with details
186 new openerp.web.DataSet(self, 'publisher_warranty.contract').call_and_eval('send', [error.data,explanation,remark,issuename]).then(function(result){
187 if (result === false) {
188 alert('There was a communication error.')
190 $this.dialog('close');
194 buttons[_t("Dont send")] = function() {
195 $(this).dialog("close");
198 buttons[_t("Ok")] = function() {
199 $(this).dialog("close");
202 var dialog = new openerp.web.Dialog(this, {
203 title: "OpenERP " + _.str.capitalize(error.type),
210 dialog.$element.html(QWeb.render('CrashManagerError', {session: openerp.connection, error: error}));
214 openerp.web.Loading = openerp.web.OldWidget.extend(/** @lends openerp.web.Loading# */{
217 * @constructs openerp.web.Loading
218 * @extends openerp.web.OldWidget
223 init: function(parent) {
226 this.blocked_ui = false;
228 this.request_call = function() {
229 self.on_rpc_event(1);
231 this.response_call = function() {
232 self.on_rpc_event(-1);
234 this.session.on_rpc_request.add_first(this.request_call);
235 this.session.on_rpc_response.add_last(this.response_call);
238 this.session.on_rpc_request.remove(this.request_call);
239 this.session.on_rpc_response.remove(this.response_call);
240 this.on_rpc_event(-this.count);
243 on_rpc_event : function(increment) {
245 if (!this.count && increment === 1) {
247 this.long_running_timer = setTimeout(function () {
248 self.blocked_ui = true;
253 this.count += increment;
254 if (this.count > 0) {
255 $(".loading",this.$element).text(_.str.sprintf(
256 _t("Loading (%d)"), this.count));
257 $(".loading",this.$element).show();
258 this.widget_parent.$element.addClass('loading');
261 clearTimeout(this.long_running_timer);
262 // Don't unblock if blocked by somebody else
263 if (self.blocked_ui) {
264 this.blocked_ui = false;
267 $(".loading",this.$element).fadeOut();
268 this.widget_parent.$element.removeClass('loading');
273 openerp.web.Database = openerp.web.OldWidget.extend(/** @lends openerp.web.Database# */{
274 template: "DatabaseManager",
276 * @constructs openerp.web.Database
277 * @extends openerp.web.OldWidget
283 init: function(parent, element_id, option_id) {
284 this._super(parent, element_id);
285 this.unblockUIFunction = $.unblockUI;
286 $.validator.addMethod('matches', function (s, _, re) {
287 return new RegExp(re).test(s);
288 }, _t("Invalid database name"));
291 this.$option_id = $("#oe_db_options");
294 var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
295 function(result) { self.db_list = result.db_list; },
296 function (_, ev) { ev.preventDefault(); self.db_list = null; });
297 var fetch_langs = this.rpc("/web/session/get_lang_list", {}, function(result) {
299 self.display_error(result);
302 self.lang_list = result.lang_list;
304 $.when(fetch_db, fetch_langs).then(function () {self.do_create();});
306 this.$element.find('#db-create').click(this.do_create);
307 this.$element.find('#db-drop').click(this.do_drop);
308 this.$element.find('#db-backup').click(this.do_backup);
309 this.$element.find('#db-restore').click(this.do_restore);
310 this.$element.find('#db-change-password').click(this.do_change_password);
311 this.$element.find('#back-to-login').click(function() {
317 this.$option_id.empty();
320 .find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login')
327 this.$element.closest(".login")
328 .addClass("database_block");
331 this.$element.closest(".login")
332 .removeClass("database_block")
335 * Converts a .serializeArray() result into a dict. Does not bother folding
336 * multiple identical keys into an array, last key wins.
338 * @param {Array} array
340 to_object: function (array) {
342 _(array).each(function (record) {
343 result[record.name] = record.value;
348 * Waits until the new database is done creating, then unblocks the UI and
349 * logs the user in as admin
351 * @param {Number} db_creation_id identifier for the db-creation operation, used to fetch the current installation progress
352 * @param {Object} info info fields for this database creation
353 * @param {String} info.db name of the database being created
354 * @param {String} info.password super-admin password for the database
356 wait_for_newdb: function (db_creation_id, info) {
358 self.rpc('/web/database/progress', {
360 password: info.password
361 }, function (result) {
362 var progress = result[0];
363 // I'd display a progress bar, but turns out the progress status
364 // the server report kind-of blows goats: it's at 0 for ~75% of
365 // the installation, then jumps to 75%, then jumps down to either
366 // 0 or ~40%, then back up to 75%, then terminates. Let's keep that
367 // mess hidden behind a not-very-useful but not overly weird
370 setTimeout(function () {
371 self.wait_for_newdb(db_creation_id, info);
376 var admin = result[1][0];
377 setTimeout(function () {
378 self.widget_parent.do_login(
379 info.db, admin.login, admin.password);
386 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
387 * from unblocking the UI
389 blockUI: function () {
391 $.unblockUI = function () {};
394 * Reinstates $.unblockUI so third parties can play with blockUI, and
397 unblockUI: function () {
398 $.unblockUI = this.unblockUIFunction;
402 * Displays an error dialog resulting from the various RPC communications
403 * failing over themselves
405 * @param {Object} error error description
406 * @param {String} error.title title of the error dialog
407 * @param {String} error.error message of the error dialog
409 display_error: function (error) {
410 return $('<div>').dialog({
414 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
416 }).html(error.error);
418 do_create: function() {
420 self.$option_id.html(QWeb.render("Database.CreateDB", self));
421 self.$option_id.find("form[name=create_db_form]").validate({
422 submitHandler: function (form) {
423 var fields = $(form).serializeArray();
425 self.rpc("/web/database/create", {'fields': fields}, function(result) {
428 self.display_error(result);
432 self.db_list.push(self.to_object(fields)['db_name']);
434 self.widget_parent.set_db_list(self.db_list);
436 var form_obj = self.to_object(fields);
437 self.wait_for_newdb(result, {
438 password: form_obj['super_admin_pwd'],
439 db: form_obj['db_name']
445 do_drop: function() {
447 self.$option_id.html(QWeb.render("DropDB", self));
448 self.$option_id.find("form[name=drop_db_form]").validate({
449 submitHandler: function (form) {
451 fields = $form.serializeArray(),
452 $db_list = $form.find('select[name=drop_db]'),
455 if (!confirm("Do you really want to delete the database: " + db + " ?")) {
458 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
460 self.display_error(result);
463 $db_list.find(':selected').remove();
465 self.db_list.splice(_.indexOf(self.db_list, db, true), 1);
466 self.widget_parent.set_db_list(self.db_list);
468 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
473 do_backup: function() {
476 .html(QWeb.render("BackupDB", self))
477 .find("form[name=backup_db_form]").validate({
478 submitHandler: function (form) {
480 self.session.get_file({
482 success: function () {
483 self.do_notify(_t("Backed"),
484 _t("Database backed up successfully"));
486 error: openerp.webclient.crashmanager.on_rpc_error,
487 complete: function() {
494 do_restore: function() {
496 self.$option_id.html(QWeb.render("RestoreDB", self));
498 self.$option_id.find("form[name=restore_db_form]").validate({
499 submitHandler: function (form) {
502 url: '/web/database/restore',
505 success: function (body) {
506 // TODO: ui manipulations
507 // note: response objects don't work, but we have the
508 // HTTP body of the response~~
510 // If empty body, everything went fine
511 if (!body) { return; }
513 if (body.indexOf('403 Forbidden') !== -1) {
515 title: 'Access Denied',
516 error: 'Incorrect super-administrator password'
520 title: 'Restore Database',
521 error: 'Could not restore the database'
525 complete: function() {
527 self.do_notify(_t("Restored"), _t("Database restored successfully"));
533 do_change_password: function() {
535 self.$option_id.html(QWeb.render("Change_DB_Pwd", self));
537 self.$option_id.find("form[name=change_pwd_form]").validate({
539 old_pwd: "Please enter your previous password",
540 new_pwd: "Please enter your new password",
542 required: "Please confirm your new password",
543 equalTo: "The confirmation does not match the password"
546 submitHandler: function (form) {
547 self.rpc("/web/database/change_password", {
548 'fields': $(form).serializeArray()
549 }, function(result) {
551 self.display_error(result);
554 self.do_notify("Changed Password", "Password has been changed successfully");
561 openerp.web.Login = openerp.web.OldWidget.extend(/** @lends openerp.web.Login# */{
562 remember_credentials: true,
566 * @constructs openerp.web.Login
567 * @extends openerp.web.OldWidget
573 init: function(parent) {
575 this.has_local_storage = typeof(localStorage) != 'undefined';
576 this.selected_db = null;
577 this.selected_login = null;
579 if (this.has_local_storage && this.remember_credentials) {
580 this.selected_db = localStorage.getItem('last_db_login_success');
581 this.selected_login = localStorage.getItem('last_login_login_success');
582 if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
583 this.selected_password = localStorage.getItem('last_password_login_success');
589 this.database = new openerp.web.Database(this);
590 this.database.appendTo(this.$element);
592 this.$element.find('#oe-db-config').click(function() {
593 self.database.show();
596 this.$element.find("form").submit(this.on_submit);
598 this.rpc("/web/database/get_list", {}, function(result) {
599 self.set_db_list(result.db_list);
601 function(error, event) {
602 if (error.data.fault_code === 'AccessDenied') {
603 event.preventDefault();
608 set_db_list: function (list) {
609 this.$element.find("[name=db]").replaceWith(
610 openerp.web.qweb.render('Login_dblist', {
611 db_list: list, selected_db: this.selected_db}))
613 on_submit: function(ev) {
617 var $e = this.$element;
618 var db = $e.find("form [name=db]").val();
620 this.do_warn("Login", "No database selected !");
623 var login = $e.find("form input[name=login]").val();
624 var password = $e.find("form input[name=password]").val();
626 this.do_login(db, login, password);
629 * Performs actual login operation, and UI-related stuff
631 * @param {String} db database to log in
632 * @param {String} login user login
633 * @param {String} password user password
635 do_login: function (db, login, password) {
637 this.$element.removeClass('login_invalid');
638 this.session.on_session_invalid.add({
639 callback: function () {
640 self.$element.addClass("login_invalid");
644 this.session.session_authenticate(db, login, password).then(function() {
645 self.$element.removeClass("login_invalid");
646 if (self.has_local_storage) {
647 if(self.remember_credentials) {
648 localStorage.setItem('last_db_login_success', db);
649 localStorage.setItem('last_login_login_success', login);
650 if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
651 localStorage.setItem('last_password_login_success', password);
654 localStorage.setItem('last_db_login_success', '');
655 localStorage.setItem('last_login_login_success', '');
656 localStorage.setItem('last_password_login_success', '');
663 openerp.web.Header = openerp.web.OldWidget.extend(/** @lends openerp.web.Header# */{
666 * @constructs openerp.web.Header
667 * @extends openerp.web.OldWidget
671 init: function(parent) {
673 this.qs = "?" + jQuery.param.querystring();
675 this.update_promise = $.Deferred().resolve();
680 do_update: function () {
682 var fct = function() {
683 self.$content.remove();
684 if (!self.session.uid)
686 var func = new openerp.web.Model("res.users").get_func("read");
687 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
688 self.$content = $(QWeb.render("Header-content", {widget: self, user: res}));
689 self.$content.appendTo(self.$element);
690 self.$element.find(".logout").click(self.on_logout);
691 self.$element.find("a.preferences").click(self.on_preferences);
692 self.$element.find(".about").click(self.on_about);
693 return self.shortcut_load();
696 this.update_promise = this.update_promise.pipe(fct, fct);
698 on_about: function() {
700 self.rpc("/web/webclient/version_info", {}).then(function(res) {
701 var $help = $(QWeb.render("About-Page", {version_info: res}));
702 $help.find('a.oe_activate_debug_mode').click(function (e) {
704 window.location = $.param.querystring(
705 window.location.href, 'debug');
707 $help.dialog({autoOpen: true,
708 modal: true, width: 960, title: _t("About")});
711 shortcut_load :function(){
713 sc = self.session.shortcuts,
714 shortcuts_ds = new openerp.web.DataSet(this, 'ir.ui.view_sc');
715 // TODO: better way to communicate between sections.
716 // sc.bindings, because jquery does not bind/trigger on arrays...
720 'add': function (e, attrs) {
721 shortcuts_ds.create(attrs, function (out) {
723 'data-shortcut-id':out.result,
724 'data-id': attrs.res_id
726 .appendTo(self.$element.find('.oe-shortcuts ul'));
727 attrs.id = out.result;
731 'remove-current': function () {
732 var menu_id = self.session.active_id;
733 var $shortcut = self.$element
734 .find('.oe-shortcuts li[data-id=' + menu_id + ']');
735 var shortcut_id = $shortcut.data('shortcut-id');
737 shortcuts_ds.unlink([shortcut_id]);
738 var sc_new = _.reject(sc, function(shortcut){ return shortcut_id === shortcut.id});
739 sc.splice(0, sc.length);
740 sc.push.apply(sc, sc_new);
744 return this.rpc('/web/session/sc_list', {}, function(shortcuts) {
745 sc.splice(0, sc.length);
746 sc.push.apply(sc, shortcuts);
748 self.$element.find('.oe-shortcuts')
749 .html(QWeb.render('Shortcuts', {'shortcuts': shortcuts}))
750 .undelegate('li', 'click')
752 .delegate('li', 'click', function(e) {
754 var id = $(this).data('id');
755 self.session.active_id = id;
756 self.rpc('/web/menu/action', {'menu_id':id}, function(ir_menu_data) {
757 if (ir_menu_data.action.length){
758 self.on_action(ir_menu_data.action[0][2]);
765 on_action: function(action) {
767 on_preferences: function(){
769 var action_manager = new openerp.web.ActionManager(this);
770 var dataset = new openerp.web.DataSet (this,'res.users',this.context);
771 dataset.call ('action_get','',function (result){
772 self.rpc('/web/action/load', {action_id:result}, function(result){
773 action_manager.do_action(_.extend(result['result'], {
774 res_id: self.session.uid,
775 res_model: 'res.users',
777 action_buttons: false,
780 views_switcher: false,
786 this.dialog = new openerp.web.Dialog(this,{
787 title: _t("Preferences"),
790 {text: _t("Cancel"), click: function(){ $(this).dialog('destroy'); }},
791 {text: _t("Change password"), click: function(){ self.change_password(); }},
792 {text: _t("Save"), click: function(){
793 var inner_viewmanager = action_manager.inner_viewmanager;
794 inner_viewmanager.views[inner_viewmanager.active_view].controller.do_save()
797 // needs to refresh interface in case language changed
798 window.location.reload();
804 action_manager.appendTo(this.dialog);
805 action_manager.render(this.dialog);
808 change_password :function() {
810 this.dialog = new openerp.web.Dialog(this, {
811 title: _t("Change Password"),
814 this.dialog.$element.html(QWeb.render("Change_Pwd", self));
815 this.dialog.$element.find("form[name=change_password_form]").validate({
816 submitHandler: function (form) {
817 self.rpc("/web/session/change_password",{
818 'fields': $(form).serializeArray()
819 }, function(result) {
821 self.display_error(result);
824 openerp.webclient.on_logout();
830 display_error: function (error) {
831 return $('<div>').dialog({
835 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
837 }).html(error.error);
839 on_logout: function() {
843 openerp.web.Menu = openerp.web.OldWidget.extend(/** @lends openerp.web.Menu# */{
845 * @constructs openerp.web.Menu
846 * @extends openerp.web.OldWidget
850 * @param secondary_menu_id
852 init: function(parent, element_id, secondary_menu_id) {
853 this._super(parent, element_id);
854 this.secondary_menu_id = secondary_menu_id;
855 this.$secondary_menu = $("#" + secondary_menu_id);
858 if (window.localStorage) {
859 this.folded = localStorage.getItem('oe_menu_folded') === 'true';
861 this.float_timeout = 700;
864 this.$secondary_menu.addClass(this.folded ? 'oe_folded' : 'oe_unfolded');
866 do_reload: function() {
868 return this.rpc("/web/menu/load", {}, this.on_loaded).then(function () {
869 if (self.current_menu) {
870 self.open_menu(self.current_menu);
874 on_loaded: function(data) {
876 this.$element.html(QWeb.render("Menu", { widget : this }));
877 this.$secondary_menu.html(QWeb.render("Menu.secondary", { widget : this }));
878 this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
879 this.$secondary_menu.find('.oe_toggle_secondary_menu').click(this.on_toggle_fold);
881 on_toggle_fold: function() {
882 this.$secondary_menu.toggleClass('oe_folded').toggleClass('oe_unfolded');
884 this.$secondary_menu.find('.oe_secondary_menu.active').show();
886 this.$secondary_menu.find('.oe_secondary_menu').hide();
888 this.folded = !this.folded;
889 if (window.localStorage) {
890 localStorage.setItem('oe_menu_folded', this.folded.toString());
894 * Opens a given menu by id, as if a user had browsed to that menu by hand
895 * except does not trigger any event on the way
897 * @param {Number} menu_id database id of the terminal menu to select
899 open_menu: function (menu_id) {
900 this.$element.add(this.$secondary_menu).find('.active')
901 .removeClass('active');
902 this.$secondary_menu.find('> .oe_secondary_menu').hide();
905 var $secondary_submenu = this.$secondary_menu.find(
906 'a[data-menu=' + menu_id +']');
907 if ($secondary_submenu.length) {
909 if ($secondary_submenu.hasClass('leaf')) {
910 $secondary_submenu.addClass('active');
911 } else if ($secondary_submenu.hasClass('submenu')) {
912 $secondary_submenu.addClass('opened')
914 var $parent = $secondary_submenu.parent().show();
915 if ($parent.hasClass('oe_secondary_menu')) {
916 var primary_id = $parent.data('menu-parent');
917 $primary_menu = this.$element.find(
918 'a[data-menu=' + primary_id + ']');
921 $secondary_submenu = $parent.prev();
924 $primary_menu = this.$element.find('a[data-menu=' + menu_id + ']');
926 if (!$primary_menu.length) {
929 $primary_menu.addClass('active');
930 this.$secondary_menu.find(
931 'div[data-menu-parent=' + $primary_menu.data('menu') + ']').addClass('active').toggle(!this.folded);
933 on_menu_click: function(ev, id) {
935 var $clicked_menu, manual = false;
938 // We can manually activate a menu with it's id (for hash url mapping)
940 $clicked_menu = this.$element.find('a[data-menu=' + id + ']');
941 if (!$clicked_menu.length) {
942 $clicked_menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
945 $clicked_menu = $(ev.currentTarget);
946 id = $clicked_menu.data('menu');
949 if (this.do_menu_click($clicked_menu, manual) && id) {
950 this.current_menu = id;
951 this.session.active_id = id;
952 this.rpc('/web/menu/action', {'menu_id': id}, this.on_menu_action_loaded);
955 ev.stopPropagation();
959 do_menu_click: function($clicked_menu, manual) {
960 var $sub_menu, $main_menu,
961 active = $clicked_menu.is('.active'),
962 sub_menu_visible = false,
963 has_submenu_items = false;
965 if (this.$secondary_menu.has($clicked_menu).length) {
966 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
967 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
969 $sub_menu = this.$secondary_menu.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
970 $main_menu = $clicked_menu;
973 sub_menu_visible = $sub_menu.is(':visible');
974 has_submenu_items = !!$sub_menu.children().length;
975 this.$secondary_menu.find('.oe_secondary_menu').hide();
977 $('.active', this.$element.add(this.$secondary_menu)).removeClass('active');
978 $main_menu.add($clicked_menu).add($sub_menu).addClass('active');
980 if (has_submenu_items) {
981 if (!(this.folded && manual)) {
982 this.do_show_secondary($sub_menu, $main_menu);
984 this.do_show_secondary();
988 if ($main_menu != $clicked_menu) {
989 if ($clicked_menu.is('.submenu')) {
990 $sub_menu.find('.submenu.opened').each(function() {
991 if (!$(this).next().has($clicked_menu).length && !$(this).is($clicked_menu)) {
992 $(this).removeClass('opened').next().hide();
995 $clicked_menu.toggleClass('opened').next().toggle();
996 } else if ($clicked_menu.is('.leaf')) {
997 $sub_menu.toggle(!this.folded);
1000 } else if (this.folded) {
1001 if ((active && sub_menu_visible) || !has_submenu_items) {
1011 do_hide_secondary: function() {
1012 this.$secondary_menu.hide();
1014 do_show_secondary: function($sub_menu, $main_menu) {
1016 this.$secondary_menu.show();
1017 if (!arguments.length) {
1021 var css = $main_menu.position(),
1022 fold_width = this.$secondary_menu.width() + 2,
1023 window_width = $(window).width();
1025 css.left -= Math.round(($sub_menu.width() - $main_menu.width()) / 2);
1026 css.left = css.left < fold_width ? fold_width : css.left;
1027 if ((css.left + $sub_menu.width()) > window_width) {
1032 $sub_menu.mouseenter(function() {
1033 clearTimeout($sub_menu.data('timeoutId'));
1034 $sub_menu.data('timeoutId', null);
1036 }).mouseleave(function(evt) {
1037 var timeoutId = setTimeout(function() {
1038 if (self.folded && $sub_menu.data('timeoutId')) {
1039 $sub_menu.hide().unbind('mouseenter').unbind('mouseleave');
1041 }, self.float_timeout);
1042 $sub_menu.data('timeoutId', timeoutId);
1048 on_menu_action_loaded: function(data) {
1050 if (data.action.length) {
1051 var action = data.action[0][2];
1052 action.from_menu = true;
1053 self.on_action(action);
1055 self.on_action({type: 'null_action'});
1058 on_action: function(action) {
1062 openerp.web.WebClient = openerp.web.OldWidget.extend(/** @lends openerp.web.WebClient */{
1064 * @constructs openerp.web.WebClient
1065 * @extends openerp.web.OldWidget
1069 init: function(parent) {
1071 this._super(parent);
1072 openerp.webclient = this;
1074 this._current_state = null;
1078 this.$element = $(document.body);
1079 if (jQuery.param != undefined && jQuery.deparam(jQuery.param.querystring()).kitten != undefined) {
1080 this.$element.addClass("kitten-mode-activated");
1081 this.$element.delegate('img.oe-record-edit-link-img', 'hover', function(e) {
1082 self.$element.toggleClass('clark-gable');
1085 this.session.bind().then(function() {
1086 if (!self.session.session_is_valid()) {
1090 this.session.on_session_valid.add(function() {
1091 self.show_application();
1093 self.header.do_update();
1094 self.menu.do_reload();
1095 if(self.action_manager)
1096 self.action_manager.stop();
1097 self.action_manager = new openerp.web.ActionManager(self);
1098 self.action_manager.appendTo($("#oe_app"));
1099 self.bind_hashchange();
1100 var version_label = _t("OpenERP - Unsupported/Community Version");
1101 if (!self.session.openerp_entreprise) {
1102 self.$element.find('.oe_footer_powered').append(_.str.sprintf('<span> - <a href="http://www.openerp.com/support-or-publisher-warranty-contract" target="_blank">%s</a></span>', version_label));
1103 document.title = version_label;
1107 show_login: function() {
1109 this.destroy_content();
1111 self.login = new openerp.web.Login(self);
1112 self.login.appendTo(self.$element);
1114 show_application: function() {
1116 this.destroy_content();
1118 self.$table = $(QWeb.render("Interface", {}));
1119 self.$element.append(self.$table);
1120 self.header = new openerp.web.Header(self);
1121 self.header.on_logout.add(this.proxy('on_logout'));
1122 self.header.on_action.add(this.proxy('on_menu_action'));
1123 self.header.appendTo($("#oe_header"));
1124 self.menu = new openerp.web.Menu(self, "oe_menu", "oe_secondary_menu");
1125 self.menu.on_action.add(this.proxy('on_menu_action'));
1128 show_common: function() {
1130 if (!this.crashmanager) {
1131 this.crashmanager = new openerp.web.CrashManager();
1132 openerp.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
1133 window.onerror = function (message, file, line) {
1134 self.crashmanager.on_traceback({
1135 type: _t("Client Error"),
1137 data: {debug: file + ':' + line}
1141 this.notification = new openerp.web.Notification(this);
1142 this.notification.appendTo(this.$element);
1143 this.loading = new openerp.web.Loading(this);
1144 this.loading.appendTo(this.$element);
1146 destroy_content: function() {
1147 _.each(_.clone(this.widget_children), function(el) {
1150 this.$element.children().remove();
1152 do_reload: function() {
1154 return this.session.session_reload().pipe(function () {
1155 openerp.connection.load_modules(true).pipe(
1156 self.menu.proxy('do_reload')); });
1159 do_notify: function() {
1160 var n = this.notification;
1161 n.notify.apply(n, arguments);
1163 do_warn: function() {
1164 var n = this.notification;
1165 n.warn.apply(n, arguments);
1167 on_logout: function() {
1169 this.session.session_logout().then(function () {
1170 $(window).unbind('hashchange', self.on_hashchange);
1171 self.do_push_state({});
1172 //would be cool to be able to do this, but I think it will make addons do strange things
1173 //this.show_login();
1174 window.location.reload();
1177 bind_hashchange: function() {
1178 $(window).bind('hashchange', this.on_hashchange);
1180 var state = $.bbq.getState(true);
1181 if (! _.isEmpty(state)) {
1182 $(window).trigger('hashchange');
1184 this.action_manager.do_action({type: 'ir.actions.client', tag: 'default_home'});
1187 on_hashchange: function(event) {
1188 var state = event.getState(true);
1189 if (!_.isEqual(this._current_state, state)) {
1190 this.action_manager.do_load_state(state, !!this._current_state);
1192 this._current_state = state;
1194 do_push_state: function(state) {
1195 var url = '#' + $.param(state);
1196 this._current_state = _.clone(state);
1197 $.bbq.pushState(url);
1199 on_menu_action: function(action) {
1200 this.action_manager.do_action(action);
1202 do_action: function(action) {
1204 // TODO replace by client action menuclick
1205 if(action.menu_id) {
1206 this.do_reload().then(function () {
1207 self.menu.on_menu_click(null, action.menu_id);
1213 openerp.web.EmbeddedClient = openerp.web.OldWidget.extend({
1214 template: 'EmptyComponent',
1215 init: function(action_id, options) {
1217 // TODO take the xmlid of a action instead of its id
1218 this.action_id = action_id;
1219 this.options = options || {};
1220 this.am = new openerp.web.ActionManager(this);
1225 this.am.appendTo(this.$element.addClass('openerp'));
1226 return this.rpc("/web/action/load", { action_id: this.action_id }, function(result) {
1227 var action = result.result;
1228 action.flags = _.extend({
1229 //views_switcher : false,
1230 search_view : false,
1231 action_buttons : false,
1234 }, self.options, action.flags || {});
1236 self.am.do_action(action);
1242 openerp.web.embed = function (origin, dbname, login, key, action, options) {
1243 $('head').append($('<link>', {
1244 'rel': 'stylesheet',
1246 'href': origin +'/web/webclient/css'
1248 var currentScript = document.currentScript;
1249 if (!currentScript) {
1250 var sc = document.getElementsByTagName('script');
1251 currentScript = sc[sc.length-1];
1253 openerp.connection.bind(origin).then(function () {
1254 openerp.connection.session_authenticate(dbname, login, key, true).then(function () {
1255 var client = new openerp.web.EmbeddedClient(action, options);
1256 client.insertAfter(currentScript);
1264 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: