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 = function(element) {
49 var result = element.dialog.apply(element, _.rest(_.toArray(arguments)));
50 result.dialog("widget").addClass("openerp");
54 openerp.web.Dialog = openerp.web.OldWidget.extend(/** @lends openerp.web.Dialog# */{
57 * @constructs openerp.web.Dialog
58 * @extends openerp.web.OldWidget
63 init: function (parent, options, content) {
67 this.$element = content instanceof $ ? content : $(content);
69 this.dialog_options = {
71 destroy_on_close: true,
72 width: $(window).width() * (($(window).width() > 1024) ? 0.5 : 0.75),
77 max_height: this.get_height('100%') - 140,
79 position: [false, 50],
81 beforeClose: function () { self.on_close(); },
82 resizeStop: this.on_resized
85 if (f.substr(0, 10) == 'on_button_') {
86 this.dialog_options.buttons[f.substr(10)] = this[f];
90 _.extend(this.dialog_options, options);
92 if (this.dialog_options.autoOpen) {
95 openerp.web.dialog(this.$element, this.get_options());
98 get_options: function(options) {
100 o = _.extend({}, this.dialog_options, options || {});
101 _.each(['width', 'height'], function(unit) {
102 o[unit] = self['get_' + unit](o[unit]);
103 o['min_' + unit] = self['get_' + unit](o['min_' + unit] || 0);
104 o['max_' + unit] = self['get_' + unit](o['max_' + unit] || 0);
105 if (o[unit] !== 'auto' && o['min_' + unit] && o[unit] < o['min_' + unit]) o[unit] = o['min_' + unit];
106 if (o[unit] !== 'auto' && o['max_' + unit] && o[unit] > o['max_' + unit]) o[unit] = o['max_' + unit];
108 if (!o.title && this.dialog_title) {
109 o.title = this.dialog_title;
113 get_width: function(val) {
114 return this.get_size(val.toString(), $(window.top).width());
116 get_height: function(val) {
117 return this.get_size(val.toString(), $(window.top).height());
119 get_size: function(val, available_size) {
120 if (val === 'auto') {
122 } else if (val.slice(-1) == "%") {
123 return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
125 return parseInt(val, 10);
128 open: function(options) {
129 // TODO fme: bind window on resize
131 this.$element.html(this.render());
133 var o = this.get_options(options);
134 openerp.web.dialog(this.$element, o).dialog('open');
135 if (o.height === 'auto' && o.max_height) {
136 this.$element.css({ 'max-height': o.max_height, 'overflow-y': 'auto' });
141 this.$element.dialog('close');
143 on_close: function() {
144 if (this.dialog_options.destroy_on_close) {
145 this.$element.dialog('destroy');
148 on_resized: function() {
149 //openerp.log("Dialog resized to %d x %d", this.$element.width(), this.$element.height());
151 destroy: function () {
154 this.$element.dialog('destroy');
159 openerp.web.CrashManager = openerp.web.CallbackEnabled.extend({
160 on_rpc_error: function(error) {
161 if (error.data.fault_code) {
162 var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
163 if (split.length > 1) {
164 error.type = split.shift();
165 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
168 if (error.code === 200 && error.type) {
169 this.on_managed_error(error);
171 this.on_traceback(error);
174 on_managed_error: function(error) {
175 openerp.web.dialog($('<div>' + QWeb.render('CrashManagerWarning', {error: error}) + '</div>'), {
176 title: "OpenERP " + _.str.capitalize(error.type),
178 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
182 on_traceback: function(error) {
185 if (openerp.connection.openerp_entreprise) {
186 buttons[_t("Send OpenERP Enterprise Report")] = function() {
188 var issuename = $('#issuename').val();
189 var explanation = $('#explanation').val();
190 var remark = $('#remark').val();
191 // Call the send method from server to send mail with details
192 new openerp.web.DataSet(self, 'publisher_warranty.contract').call_and_eval('send', [error.data,explanation,remark,issuename]).then(function(result){
193 if (result === false) {
194 alert('There was a communication error.')
196 $this.dialog('close');
200 buttons[_t("Dont send")] = function() {
201 $(this).dialog("close");
204 buttons[_t("Ok")] = function() {
205 $(this).dialog("close");
208 var dialog = new openerp.web.Dialog(this, {
209 title: "OpenERP " + _.str.capitalize(error.type),
216 dialog.$element.html(QWeb.render('CrashManagerError', {session: openerp.connection, error: error}));
220 openerp.web.Loading = openerp.web.OldWidget.extend(/** @lends openerp.web.Loading# */{
223 * @constructs openerp.web.Loading
224 * @extends openerp.web.OldWidget
229 init: function(parent) {
232 this.blocked_ui = false;
234 this.request_call = function() {
235 self.on_rpc_event(1);
237 this.response_call = function() {
238 self.on_rpc_event(-1);
240 this.session.on_rpc_request.add_first(this.request_call);
241 this.session.on_rpc_response.add_last(this.response_call);
243 destroy: function() {
244 this.session.on_rpc_request.remove(this.request_call);
245 this.session.on_rpc_response.remove(this.response_call);
246 this.on_rpc_event(-this.count);
249 on_rpc_event : function(increment) {
251 if (!this.count && increment === 1) {
253 this.long_running_timer = setTimeout(function () {
254 self.blocked_ui = true;
259 this.count += increment;
260 if (this.count > 0) {
261 $(".loading",this.$element).text(_.str.sprintf(
262 _t("Loading (%d)"), this.count));
263 $(".loading",this.$element).show();
264 this.getParent().$element.addClass('loading');
267 clearTimeout(this.long_running_timer);
268 // Don't unblock if blocked by somebody else
269 if (self.blocked_ui) {
270 this.blocked_ui = false;
273 $(".loading",this.$element).fadeOut();
274 this.getParent().$element.removeClass('loading');
279 openerp.web.Database = openerp.web.OldWidget.extend(/** @lends openerp.web.Database# */{
280 template: "DatabaseManager",
282 * @constructs openerp.web.Database
283 * @extends openerp.web.OldWidget
289 init: function(parent, element_id, option_id) {
290 this._super(parent, element_id);
291 this.unblockUIFunction = $.unblockUI;
292 $.validator.addMethod('matches', function (s, _, re) {
293 return new RegExp(re).test(s);
294 }, _t("Invalid database name"));
297 this.$option_id = $("#oe_db_options");
300 var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
301 function(result) { self.db_list = result.db_list; },
302 function (_, ev) { ev.preventDefault(); self.db_list = null; });
303 var fetch_langs = this.rpc("/web/session/get_lang_list", {}, function(result) {
305 self.display_error(result);
308 self.lang_list = result.lang_list;
310 $.when(fetch_db, fetch_langs).then(function () {self.do_create();});
312 this.$element.find('#db-create').click(this.do_create);
313 this.$element.find('#db-drop').click(this.do_drop);
314 this.$element.find('#db-backup').click(this.do_backup);
315 this.$element.find('#db-restore').click(this.do_restore);
316 this.$element.find('#db-change-password').click(this.do_change_password);
317 this.$element.find('#back-to-login').click(function() {
321 destroy: function () {
323 this.$option_id.empty();
326 .find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login')
333 this.$element.closest(".login")
334 .addClass("database_block");
337 this.$element.closest(".login")
338 .removeClass("database_block")
341 * Converts a .serializeArray() result into a dict. Does not bother folding
342 * multiple identical keys into an array, last key wins.
344 * @param {Array} array
346 to_object: function (array) {
348 _(array).each(function (record) {
349 result[record.name] = record.value;
354 * Waits until the new database is done creating, then unblocks the UI and
355 * logs the user in as admin
357 * @param {Number} db_creation_id identifier for the db-creation operation, used to fetch the current installation progress
358 * @param {Object} info info fields for this database creation
359 * @param {String} info.db name of the database being created
360 * @param {String} info.password super-admin password for the database
362 wait_for_newdb: function (db_creation_id, info) {
364 self.rpc('/web/database/progress', {
366 password: info.password
367 }, function (result) {
368 var progress = result[0];
369 // I'd display a progress bar, but turns out the progress status
370 // the server report kind-of blows goats: it's at 0 for ~75% of
371 // the installation, then jumps to 75%, then jumps down to either
372 // 0 or ~40%, then back up to 75%, then terminates. Let's keep that
373 // mess hidden behind a not-very-useful but not overly weird
376 setTimeout(function () {
377 self.wait_for_newdb(db_creation_id, info);
382 var admin = result[1][0];
383 setTimeout(function () {
384 self.getParent().do_login(
385 info.db, admin.login, admin.password);
392 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
393 * from unblocking the UI
395 blockUI: function () {
397 $.unblockUI = function () {};
400 * Reinstates $.unblockUI so third parties can play with blockUI, and
403 unblockUI: function () {
404 $.unblockUI = this.unblockUIFunction;
408 * Displays an error dialog resulting from the various RPC communications
409 * failing over themselves
411 * @param {Object} error error description
412 * @param {String} error.title title of the error dialog
413 * @param {String} error.error message of the error dialog
415 display_error: function (error) {
416 return openerp.web.dialog($('<div>'), {
420 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
422 }).html(error.error);
424 do_create: function() {
426 self.$option_id.html(QWeb.render("Database.CreateDB", self));
427 self.$option_id.find("form[name=create_db_form]").validate({
428 submitHandler: function (form) {
429 var fields = $(form).serializeArray();
431 self.rpc("/web/database/create", {'fields': fields}, function(result) {
434 self.display_error(result);
438 self.db_list.push(self.to_object(fields)['db_name']);
440 self.getParent().set_db_list(self.db_list);
442 var form_obj = self.to_object(fields);
443 self.wait_for_newdb(result, {
444 password: form_obj['super_admin_pwd'],
445 db: form_obj['db_name']
451 do_drop: function() {
453 self.$option_id.html(QWeb.render("DropDB", self));
454 self.$option_id.find("form[name=drop_db_form]").validate({
455 submitHandler: function (form) {
457 fields = $form.serializeArray(),
458 $db_list = $form.find('select[name=drop_db]'),
461 if (!confirm("Do you really want to delete the database: " + db + " ?")) {
464 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
466 self.display_error(result);
469 $db_list.find(':selected').remove();
471 self.db_list.splice(_.indexOf(self.db_list, db, true), 1);
472 self.getParent().set_db_list(self.db_list);
474 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
479 do_backup: function() {
482 .html(QWeb.render("BackupDB", self))
483 .find("form[name=backup_db_form]").validate({
484 submitHandler: function (form) {
486 self.session.get_file({
488 success: function () {
489 self.do_notify(_t("Backed"),
490 _t("Database backed up successfully"));
492 error: openerp.webclient.crashmanager.on_rpc_error,
493 complete: function() {
500 do_restore: function() {
502 self.$option_id.html(QWeb.render("RestoreDB", self));
504 self.$option_id.find("form[name=restore_db_form]").validate({
505 submitHandler: function (form) {
508 url: '/web/database/restore',
511 success: function (body) {
512 // TODO: ui manipulations
513 // note: response objects don't work, but we have the
514 // HTTP body of the response~~
516 // If empty body, everything went fine
517 if (!body) { return; }
519 if (body.indexOf('403 Forbidden') !== -1) {
521 title: 'Access Denied',
522 error: 'Incorrect super-administrator password'
526 title: 'Restore Database',
527 error: 'Could not restore the database'
531 complete: function() {
533 self.do_notify(_t("Restored"), _t("Database restored successfully"));
539 do_change_password: function() {
541 self.$option_id.html(QWeb.render("Change_DB_Pwd", self));
543 self.$option_id.find("form[name=change_pwd_form]").validate({
545 old_pwd: "Please enter your previous password",
546 new_pwd: "Please enter your new password",
548 required: "Please confirm your new password",
549 equalTo: "The confirmation does not match the password"
552 submitHandler: function (form) {
553 self.rpc("/web/database/change_password", {
554 'fields': $(form).serializeArray()
555 }, function(result) {
557 self.display_error(result);
560 self.do_notify("Changed Password", "Password has been changed successfully");
567 openerp.web.Login = openerp.web.OldWidget.extend(/** @lends openerp.web.Login# */{
568 remember_credentials: true,
572 * @constructs openerp.web.Login
573 * @extends openerp.web.OldWidget
579 init: function(parent) {
581 this.has_local_storage = typeof(localStorage) != 'undefined';
582 this.selected_db = null;
583 this.selected_login = null;
585 if (this.has_local_storage && this.remember_credentials) {
586 this.selected_db = localStorage.getItem('last_db_login_success');
587 this.selected_login = localStorage.getItem('last_login_login_success');
588 if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
589 this.selected_password = localStorage.getItem('last_password_login_success');
595 this.database = new openerp.web.Database(this);
596 this.database.appendTo(this.$element);
598 this.$element.find('#oe-db-config').click(function() {
599 self.database.show();
602 this.$element.find("form").submit(this.on_submit);
604 this.rpc("/web/database/get_list", {}, function(result) {
605 self.set_db_list(result.db_list);
607 function(error, event) {
608 if (error.data.fault_code === 'AccessDenied') {
609 event.preventDefault();
614 set_db_list: function (list) {
615 this.$element.find("[name=db]").replaceWith(
616 openerp.web.qweb.render('Login_dblist', {
617 db_list: list, selected_db: this.selected_db}))
619 on_submit: function(ev) {
623 var $e = this.$element;
624 var db = $e.find("form [name=db]").val();
626 this.do_warn("Login", "No database selected !");
629 var login = $e.find("form input[name=login]").val();
630 var password = $e.find("form input[name=password]").val();
632 this.do_login(db, login, password);
635 * Performs actual login operation, and UI-related stuff
637 * @param {String} db database to log in
638 * @param {String} login user login
639 * @param {String} password user password
641 do_login: function (db, login, password) {
643 this.$element.removeClass('login_invalid');
644 this.session.on_session_invalid.add({
645 callback: function () {
646 self.$element.addClass("login_invalid");
650 this.session.session_authenticate(db, login, password).then(function() {
651 self.$element.removeClass("login_invalid");
652 if (self.has_local_storage) {
653 if(self.remember_credentials) {
654 localStorage.setItem('last_db_login_success', db);
655 localStorage.setItem('last_login_login_success', login);
656 if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
657 localStorage.setItem('last_password_login_success', password);
660 localStorage.setItem('last_db_login_success', '');
661 localStorage.setItem('last_login_login_success', '');
662 localStorage.setItem('last_password_login_success', '');
669 openerp.web.Header = openerp.web.OldWidget.extend(/** @lends openerp.web.Header# */{
672 * @constructs openerp.web.Header
673 * @extends openerp.web.OldWidget
677 init: function(parent) {
679 this.qs = "?" + jQuery.param.querystring();
681 this.update_promise = $.Deferred().resolve();
686 do_update: function () {
688 var fct = function() {
689 self.$content.remove();
690 if (!self.session.uid)
692 var func = new openerp.web.Model("res.users").get_func("read");
693 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
694 self.$content = $(QWeb.render("Header-content", {widget: self, user: res}));
695 self.$content.appendTo(self.$element);
696 self.$element.find(".logout").click(self.on_logout);
697 self.$element.find("a.preferences").click(self.on_preferences);
698 self.$element.find(".about").click(self.on_about);
699 return self.shortcut_load();
702 this.update_promise = this.update_promise.pipe(fct, fct);
704 on_about: function() {
706 self.rpc("/web/webclient/version_info", {}).then(function(res) {
707 var $help = $(QWeb.render("About-Page", {version_info: res}));
708 $help.find('a.oe_activate_debug_mode').click(function (e) {
710 window.location = $.param.querystring(
711 window.location.href, 'debug');
713 openerp.web.dialog($help, {autoOpen: true,
714 modal: true, width: 960, title: _t("About")});
717 shortcut_load :function(){
719 sc = self.session.shortcuts,
720 shortcuts_ds = new openerp.web.DataSet(this, 'ir.ui.view_sc');
721 // TODO: better way to communicate between sections.
722 // sc.bindings, because jquery does not bind/trigger on arrays...
726 'add': function (e, attrs) {
727 shortcuts_ds.create(attrs, function (out) {
729 'data-shortcut-id':out.result,
730 'data-id': attrs.res_id
732 .appendTo(self.$element.find('.oe-shortcuts ul'));
733 attrs.id = out.result;
737 'remove-current': function () {
738 var menu_id = self.session.active_id;
739 var $shortcut = self.$element
740 .find('.oe-shortcuts li[data-id=' + menu_id + ']');
741 var shortcut_id = $shortcut.data('shortcut-id');
743 shortcuts_ds.unlink([shortcut_id]);
744 var sc_new = _.reject(sc, function(shortcut){ return shortcut_id === shortcut.id});
745 sc.splice(0, sc.length);
746 sc.push.apply(sc, sc_new);
750 return this.rpc('/web/session/sc_list', {}, function(shortcuts) {
751 sc.splice(0, sc.length);
752 sc.push.apply(sc, shortcuts);
754 self.$element.find('.oe-shortcuts')
755 .html(QWeb.render('Shortcuts', {'shortcuts': shortcuts}))
756 .undelegate('li', 'click')
758 .delegate('li', 'click', function(e) {
760 var id = $(this).data('id');
761 self.session.active_id = id;
762 self.rpc('/web/menu/action', {'menu_id':id}, function(ir_menu_data) {
763 if (ir_menu_data.action.length){
764 self.on_action(ir_menu_data.action[0][2]);
771 on_action: function(action) {
773 on_preferences: function(){
775 var action_manager = new openerp.web.ActionManager(this);
776 var dataset = new openerp.web.DataSet (this,'res.users',this.context);
777 dataset.call ('action_get','',function (result){
778 self.rpc('/web/action/load', {action_id:result}, function(result){
779 action_manager.do_action(_.extend(result['result'], {
780 res_id: self.session.uid,
781 res_model: 'res.users',
783 action_buttons: false,
786 views_switcher: false,
792 this.dialog = new openerp.web.Dialog(this,{
793 title: _t("Preferences"),
796 {text: _t("Cancel"), click: function(){ $(this).dialog('destroy'); }},
797 {text: _t("Change password"), click: function(){ self.change_password(); }},
798 {text: _t("Save"), click: function(){
799 var inner_viewmanager = action_manager.inner_viewmanager;
800 inner_viewmanager.views[inner_viewmanager.active_view].controller.do_save()
802 self.dialog.destroy();
803 // needs to refresh interface in case language changed
804 window.location.reload();
810 action_manager.appendTo(this.dialog);
811 action_manager.render(this.dialog);
814 change_password :function() {
816 this.dialog = new openerp.web.Dialog(this, {
817 title: _t("Change Password"),
820 this.dialog.$element.html(QWeb.render("Change_Pwd", self));
821 this.dialog.$element.find("form[name=change_password_form]").validate({
822 submitHandler: function (form) {
823 self.rpc("/web/session/change_password",{
824 'fields': $(form).serializeArray()
825 }, function(result) {
827 self.display_error(result);
830 openerp.webclient.on_logout();
836 display_error: function (error) {
837 return openerp.web.dialog($('<div>'), {
841 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
843 }).html(error.error);
845 on_logout: function() {
849 openerp.web.Menu = openerp.web.OldWidget.extend(/** @lends openerp.web.Menu# */{
851 * @constructs openerp.web.Menu
852 * @extends openerp.web.OldWidget
856 * @param secondary_menu_id
858 init: function(parent, element_id, secondary_menu_id) {
859 this._super(parent, element_id);
860 this.secondary_menu_id = secondary_menu_id;
861 this.$secondary_menu = $("#" + secondary_menu_id);
864 if (window.localStorage) {
865 this.folded = localStorage.getItem('oe_menu_folded') === 'true';
867 this.float_timeout = 700;
870 this.$secondary_menu.addClass(this.folded ? 'oe_folded' : 'oe_unfolded');
872 do_reload: function() {
874 return this.rpc("/web/menu/load", {}, this.on_loaded).then(function () {
875 if (self.current_menu) {
876 self.open_menu(self.current_menu);
880 on_loaded: function(data) {
882 this.$element.html(QWeb.render("Menu", { widget : this }));
883 this.$secondary_menu.html(QWeb.render("Menu.secondary", { widget : this }));
884 this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
885 this.$secondary_menu.find('.oe_toggle_secondary_menu').click(this.on_toggle_fold);
887 on_toggle_fold: function() {
888 this.$secondary_menu.toggleClass('oe_folded').toggleClass('oe_unfolded');
890 this.$secondary_menu.find('.oe_secondary_menu.active').show();
892 this.$secondary_menu.find('.oe_secondary_menu').hide();
894 this.folded = !this.folded;
895 if (window.localStorage) {
896 localStorage.setItem('oe_menu_folded', this.folded.toString());
900 * Opens a given menu by id, as if a user had browsed to that menu by hand
901 * except does not trigger any event on the way
903 * @param {Number} menu_id database id of the terminal menu to select
905 open_menu: function (menu_id) {
906 this.$element.add(this.$secondary_menu).find('.active')
907 .removeClass('active');
908 this.$secondary_menu.find('> .oe_secondary_menu').hide();
911 var $secondary_submenu = this.$secondary_menu.find(
912 'a[data-menu=' + menu_id +']');
913 if ($secondary_submenu.length) {
915 if ($secondary_submenu.hasClass('leaf')) {
916 $secondary_submenu.addClass('active');
917 } else if ($secondary_submenu.hasClass('submenu')) {
918 $secondary_submenu.addClass('opened')
920 var $parent = $secondary_submenu.parent().show();
921 if ($parent.hasClass('oe_secondary_menu')) {
922 var primary_id = $parent.data('menu-parent');
923 $primary_menu = this.$element.find(
924 'a[data-menu=' + primary_id + ']');
927 $secondary_submenu = $parent.prev();
930 $primary_menu = this.$element.find('a[data-menu=' + menu_id + ']');
932 if (!$primary_menu.length) {
935 $primary_menu.addClass('active');
936 this.$secondary_menu.find(
937 'div[data-menu-parent=' + $primary_menu.data('menu') + ']').addClass('active').toggle(!this.folded);
939 on_menu_click: function(ev, id) {
941 var $clicked_menu, manual = false;
944 // We can manually activate a menu with it's id (for hash url mapping)
946 $clicked_menu = this.$element.find('a[data-menu=' + id + ']');
947 if (!$clicked_menu.length) {
948 $clicked_menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
951 $clicked_menu = $(ev.currentTarget);
952 id = $clicked_menu.data('menu');
955 if (this.do_menu_click($clicked_menu, manual) && id) {
956 this.current_menu = id;
957 this.session.active_id = id;
958 this.rpc('/web/menu/action', {'menu_id': id}, this.on_menu_action_loaded);
961 ev.stopPropagation();
965 do_menu_click: function($clicked_menu, manual) {
966 var $sub_menu, $main_menu,
967 active = $clicked_menu.is('.active'),
968 sub_menu_visible = false,
969 has_submenu_items = false;
971 if (this.$secondary_menu.has($clicked_menu).length) {
972 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
973 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
975 $sub_menu = this.$secondary_menu.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
976 $main_menu = $clicked_menu;
979 sub_menu_visible = $sub_menu.is(':visible');
980 has_submenu_items = !!$sub_menu.children().length;
981 this.$secondary_menu.find('.oe_secondary_menu').hide();
983 $('.active', this.$element.add(this.$secondary_menu)).removeClass('active');
984 $main_menu.add($clicked_menu).add($sub_menu).addClass('active');
986 if (has_submenu_items) {
987 if (!(this.folded && manual)) {
988 this.do_show_secondary($sub_menu, $main_menu);
990 this.do_show_secondary();
994 if ($main_menu != $clicked_menu) {
995 if ($clicked_menu.is('.submenu')) {
996 $sub_menu.find('.submenu.opened').each(function() {
997 if (!$(this).next().has($clicked_menu).length && !$(this).is($clicked_menu)) {
998 $(this).removeClass('opened').next().hide();
1001 $clicked_menu.toggleClass('opened').next().toggle();
1002 } else if ($clicked_menu.is('.leaf')) {
1003 $sub_menu.toggle(!this.folded);
1006 } else if (this.folded) {
1007 if ((active && sub_menu_visible) || !has_submenu_items) {
1017 do_hide_secondary: function() {
1018 this.$secondary_menu.hide();
1020 do_show_secondary: function($sub_menu, $main_menu) {
1022 this.$secondary_menu.show();
1023 if (!arguments.length) {
1027 var css = $main_menu.position(),
1028 fold_width = this.$secondary_menu.width() + 2,
1029 window_width = $(window).width();
1031 css.left -= Math.round(($sub_menu.width() - $main_menu.width()) / 2);
1032 css.left = css.left < fold_width ? fold_width : css.left;
1033 if ((css.left + $sub_menu.width()) > window_width) {
1038 $sub_menu.mouseenter(function() {
1039 clearTimeout($sub_menu.data('timeoutId'));
1040 $sub_menu.data('timeoutId', null);
1042 }).mouseleave(function(evt) {
1043 var timeoutId = setTimeout(function() {
1044 if (self.folded && $sub_menu.data('timeoutId')) {
1045 $sub_menu.hide().unbind('mouseenter').unbind('mouseleave');
1047 }, self.float_timeout);
1048 $sub_menu.data('timeoutId', timeoutId);
1054 on_menu_action_loaded: function(data) {
1056 if (data.action.length) {
1057 var action = data.action[0][2];
1058 action.from_menu = true;
1059 self.on_action(action);
1061 self.on_action({type: 'null_action'});
1064 on_action: function(action) {
1068 openerp.web.WebClient = openerp.web.OldWidget.extend(/** @lends openerp.web.WebClient */{
1070 * @constructs openerp.web.WebClient
1071 * @extends openerp.web.OldWidget
1075 init: function(parent) {
1077 this._super(parent);
1078 openerp.webclient = this;
1080 this._current_state = null;
1082 render_element: function() {
1083 this.$element.addClass("openerp");
1087 if (jQuery.param != undefined && jQuery.deparam(jQuery.param.querystring()).kitten != undefined) {
1088 this.$element.addClass("kitten-mode-activated");
1089 this.$element.delegate('img.oe-record-edit-link-img', 'hover', function(e) {
1090 self.$element.toggleClass('clark-gable');
1093 this.session.bind_session().then(function() {
1094 if (!self.session.session_is_valid()) {
1098 this.session.on_session_valid.add(function() {
1099 self.show_application();
1101 self.header.do_update();
1102 self.menu.do_reload();
1103 if(self.action_manager)
1104 self.action_manager.destroy();
1105 self.action_manager = new openerp.web.ActionManager(self);
1106 self.action_manager.appendTo($("#oe_app"));
1107 self.bind_hashchange();
1108 var version_label = _t("OpenERP - Unsupported/Community Version");
1109 if (!self.session.openerp_entreprise) {
1110 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));
1111 document.title = version_label;
1115 show_login: function() {
1117 this.destroy_content();
1119 self.login = new openerp.web.Login(self);
1120 self.login.appendTo(self.$element);
1122 show_application: function() {
1124 this.destroy_content();
1126 self.$table = $(QWeb.render("Interface", {}));
1127 self.$element.append(self.$table);
1128 self.header = new openerp.web.Header(self);
1129 self.header.on_logout.add(this.proxy('on_logout'));
1130 self.header.on_action.add(this.proxy('on_menu_action'));
1131 self.header.appendTo($("#oe_header"));
1132 self.menu = new openerp.web.Menu(self, "oe_menu", "oe_secondary_menu");
1133 self.menu.on_action.add(this.proxy('on_menu_action'));
1136 show_common: function() {
1138 if (!this.crashmanager) {
1139 this.crashmanager = new openerp.web.CrashManager();
1140 openerp.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
1141 window.onerror = function (message, file, line) {
1142 self.crashmanager.on_traceback({
1143 type: _t("Client Error"),
1145 data: {debug: file + ':' + line}
1149 this.notification = new openerp.web.Notification(this);
1150 this.notification.appendTo(this.$element);
1151 this.loading = new openerp.web.Loading(this);
1152 this.loading.appendTo(this.$element);
1154 destroy_content: function() {
1155 _.each(_.clone(this.getChildren()), function(el) {
1158 this.$element.children().remove();
1160 do_reload: function() {
1162 return this.session.session_reload().pipe(function () {
1163 openerp.connection.load_modules(true).pipe(
1164 self.menu.proxy('do_reload')); });
1167 do_notify: function() {
1168 var n = this.notification;
1169 n.notify.apply(n, arguments);
1171 do_warn: function() {
1172 var n = this.notification;
1173 n.warn.apply(n, arguments);
1175 on_logout: function() {
1177 this.session.session_logout().then(function () {
1178 $(window).unbind('hashchange', self.on_hashchange);
1179 self.do_push_state({});
1180 //would be cool to be able to do this, but I think it will make addons do strange things
1181 //this.show_login();
1182 window.location.reload();
1185 bind_hashchange: function() {
1186 $(window).bind('hashchange', this.on_hashchange);
1188 var state = $.bbq.getState(true);
1189 if (! _.isEmpty(state)) {
1190 $(window).trigger('hashchange');
1192 this.action_manager.do_action({type: 'ir.actions.client', tag: 'default_home'});
1195 on_hashchange: function(event) {
1196 var state = event.getState(true);
1197 if (!_.isEqual(this._current_state, state)) {
1198 this.action_manager.do_load_state(state, !!this._current_state);
1200 this._current_state = state;
1202 do_push_state: function(state) {
1203 var url = '#' + $.param(state);
1204 this._current_state = _.clone(state);
1205 $.bbq.pushState(url);
1207 on_menu_action: function(action) {
1208 this.action_manager.do_action(action);
1210 do_action: function(action) {
1212 // TODO replace by client action menuclick
1213 if(action.menu_id) {
1214 this.do_reload().then(function () {
1215 self.menu.on_menu_click(null, action.menu_id);
1221 openerp.web.EmbeddedClient = openerp.web.OldWidget.extend({
1222 template: 'EmptyComponent',
1223 init: function(action_id, options) {
1225 // TODO take the xmlid of a action instead of its id
1226 this.action_id = action_id;
1227 this.options = options || {};
1228 this.am = new openerp.web.ActionManager(this);
1233 this.am.appendTo(this.$element.addClass('openerp'));
1234 return this.rpc("/web/action/load", { action_id: this.action_id }, function(result) {
1235 var action = result.result;
1236 action.flags = _.extend({
1237 //views_switcher : false,
1238 search_view : false,
1239 action_buttons : false,
1242 }, self.options, action.flags || {});
1244 self.am.do_action(action);
1250 openerp.web.embed = function (origin, dbname, login, key, action, options) {
1251 $('head').append($('<link>', {
1252 'rel': 'stylesheet',
1254 'href': origin +'/web/webclient/css'
1256 var currentScript = document.currentScript;
1257 if (!currentScript) {
1258 var sc = document.getElementsByTagName('script');
1259 currentScript = sc[sc.length-1];
1261 openerp.connection.bind_session(origin).then(function () {
1262 openerp.connection.session_authenticate(dbname, login, key, true).then(function () {
1263 var client = new openerp.web.EmbeddedClient(action, options);
1264 client.insertAfter(currentScript);
1272 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: