1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
4 openerp.web.chrome = function(instance) {
5 var QWeb = instance.web.qweb,
8 instance.web.Notification = instance.web.Widget.extend({
9 template: 'Notification',
11 this._super.apply(this, arguments);
12 instance.web.notification = this;
15 this._super.apply(this, arguments);
16 this.$element.notify({
21 notify: function(title, text, sticky) {
27 this.$element.notify('create', {
32 warn: function(title, text, sticky) {
38 this.$element.notify('create', 'oe_notification_alert', {
46 * The very minimal function everything should call to create a dialog
47 * in OpenERP Web Client.
49 instance.web.dialog = function(element) {
50 var result = element.dialog.apply(element, _.rest(_.toArray(arguments)));
51 result.dialog("widget").addClass("openerp");
55 instance.web.Dialog = instance.web.Widget.extend({
57 init: function (parent, options, content) {
60 this.content_to_set = content;
61 this.dialog_options = {
63 destroy_on_close: true,
69 max_height: this.get_height('100%') - 140,
71 position: [false, 50],
73 beforeClose: function () { self.on_close(); },
74 resizeStop: this.on_resized
77 if (f.substr(0, 10) == 'on_button_') {
78 this.dialog_options.buttons[f.substr(10)] = this[f];
82 _.extend(this.dialog_options, options);
85 get_options: function(options) {
87 o = _.extend({}, this.dialog_options, options || {});
88 _.each(['width', 'height'], function(unit) {
89 o[unit] = self['get_' + unit](o[unit]);
90 o['min_' + unit] = self['get_' + unit](o['min_' + unit] || 0);
91 o['max_' + unit] = self['get_' + unit](o['max_' + unit] || 0);
92 if (o[unit] !== 'auto' && o['min_' + unit] && o[unit] < o['min_' + unit]) o[unit] = o['min_' + unit];
93 if (o[unit] !== 'auto' && o['max_' + unit] && o[unit] > o['max_' + unit]) o[unit] = o['max_' + unit];
95 if (!o.title && this.dialog_title) {
96 o.title = this.dialog_title;
100 get_width: function(val) {
101 return this.get_size(val.toString(), $(window.top).width());
103 get_height: function(val) {
104 return this.get_size(val.toString(), $(window.top).height());
106 get_size: function(val, available_size) {
107 if (val === 'auto') {
109 } else if (val.slice(-1) == "%") {
110 return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
112 return parseInt(val, 10);
115 renderElement: function() {
116 if (this.content_to_set) {
117 this.setElement(this.content_to_set);
118 } else if (this.template) {
122 open: function(options) {
123 if (! this.dialog_inited)
125 var o = this.get_options(options);
126 instance.web.dialog(this.$element, o).dialog('open');
127 if (o.height === 'auto' && o.max_height) {
128 this.$element.css({ 'max-height': o.max_height, 'overflow-y': 'auto' });
132 init_dialog: function(options) {
133 this.renderElement();
134 var o = this.get_options(options);
135 instance.web.dialog(this.$element, o);
136 var res = this.start();
137 this.dialog_inited = true;
141 this.$element.dialog('close');
143 on_close: function() {
144 if (this.__tmp_dialog_destroying)
146 if (this.dialog_options.destroy_on_close) {
147 this.__tmp_dialog_closing = true;
149 this.__tmp_dialog_closing = undefined;
152 on_resized: function() {
154 destroy: function () {
155 _.each(this.getChildren(), function(el) {
158 if (! this.__tmp_dialog_closing) {
159 this.__tmp_dialog_destroying = true;
161 this.__tmp_dialog_destroying = undefined;
163 if (! this.isDestroyed()) {
164 this.$element.dialog('destroy');
170 instance.web.CrashManager = instance.web.CallbackEnabled.extend({
171 on_rpc_error: function(error) {
172 if (error.data.fault_code) {
173 var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
174 if (split.length > 1) {
175 error.type = split.shift();
176 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
179 if (error.code === 200 && error.type) {
180 this.on_managed_error(error);
182 this.on_traceback(error);
185 on_managed_error: function(error) {
186 instance.web.dialog($('<div>' + QWeb.render('CrashManager.warning', {error: error}) + '</div>'), {
187 title: "OpenERP " + _.str.capitalize(error.type),
189 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
193 on_traceback: function(error) {
196 if (instance.connection.openerp_entreprise) {
197 buttons[_t("Send OpenERP Enterprise Report")] = function() {
199 var issuename = $('#issuename').val();
200 var explanation = $('#explanation').val();
201 var remark = $('#remark').val();
202 // Call the send method from server to send mail with details
203 new instance.web.DataSet(self, 'publisher_warranty.contract').call_and_eval('send', [error.data,explanation,remark,issuename]).then(function(result){
204 if (result === false) {
205 alert('There was a communication error.');
207 $this.dialog('close');
211 buttons[_t("Dont send")] = function() {
212 $(this).dialog("close");
215 buttons[_t("Ok")] = function() {
216 $(this).dialog("close");
219 var dialog = new instance.web.Dialog(this, {
220 title: "OpenERP " + _.str.capitalize(error.type),
227 dialog.$element.html(QWeb.render('CrashManager.error', {session: instance.connection, error: error}));
229 on_javascript_exception: function(exception) {
231 type: _t("Client Error"),
238 instance.web.Loading = instance.web.Widget.extend({
240 init: function(parent) {
243 this.blocked_ui = false;
245 this.request_call = function() {
246 self.on_rpc_event(1);
248 this.response_call = function() {
249 self.on_rpc_event(-1);
251 this.session.on_rpc_request.add_first(this.request_call);
252 this.session.on_rpc_response.add_last(this.response_call);
254 destroy: function() {
255 this.session.on_rpc_request.remove(this.request_call);
256 this.session.on_rpc_response.remove(this.response_call);
257 this.on_rpc_event(-this.count);
260 on_rpc_event : function(increment) {
262 if (!this.count && increment === 1) {
264 this.long_running_timer = setTimeout(function () {
265 self.blocked_ui = true;
266 instance.web.blockUI();
270 this.count += increment;
271 if (this.count > 0) {
272 if (instance.connection.debug) {
273 this.$element.text(_.str.sprintf( _t("Loading (%d)"), this.count));
275 this.$element.text(_t("Loading"));
277 this.$element.show();
278 this.getParent().$element.addClass('oe_wait');
281 clearTimeout(this.long_running_timer);
282 // Don't unblock if blocked by somebody else
283 if (self.blocked_ui) {
284 this.blocked_ui = false;
285 instance.web.unblockUI();
287 this.$element.fadeOut();
288 this.getParent().$element.removeClass('oe_wait');
293 instance.web.DatabaseManager = instance.web.Widget.extend({
294 init: function(parent) {
296 this.unblockUIFunction = instance.web.unblockUI;
297 $.validator.addMethod('matches', function (s, _, re) {
298 return new RegExp(re).test(s);
299 }, _t("Invalid database name"));
303 var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
305 self.db_list = result.db_list;
311 var fetch_langs = this.rpc("/web/session/get_lang_list", {}).then(function(result) {
312 self.lang_list = result.lang_list;
314 return $.when(fetch_db, fetch_langs).then(self.do_render);
316 do_render: function() {
318 self.$element.html(QWeb.render("DatabaseManager", { widget : self }));
319 self.$element.find(".oe_database_manager_menu").tabs({
320 show: function(event, ui) {
321 $('*[autofocus]:first', ui.panel).focus();
324 self.$element.find("form[name=create_db_form]").validate({ submitHandler: self.do_create });
325 self.$element.find("form[name=drop_db_form]").validate({ submitHandler: self.do_drop });
326 self.$element.find("form[name=backup_db_form]").validate({ submitHandler: self.do_backup });
327 self.$element.find("form[name=restore_db_form]").validate({ submitHandler: self.do_restore });
328 self.$element.find("form[name=change_pwd_form]").validate({
330 old_pwd: "Please enter your previous password",
331 new_pwd: "Please enter your new password",
333 required: "Please confirm your new password",
334 equalTo: "The confirmation does not match the password"
337 submitHandler: self.do_change_password
339 self.$element.find("#back_to_login").click(self.do_exit);
341 destroy: function () {
342 this.$element.find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login').unbind('click').end().empty();
346 * Converts a .serializeArray() result into a dict. Does not bother folding
347 * multiple identical keys into an array, last key wins.
349 * @param {Array} array
351 to_object: function (array) {
353 _(array).each(function (record) {
354 result[record.name] = record.value;
359 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
360 * from unblocking the UI
362 blockUI: function () {
363 instance.web.blockUI();
364 instance.web.unblockUI = function () {};
367 * Reinstates $.unblockUI so third parties can play with blockUI, and
370 unblockUI: function () {
371 instance.web.unblockUI = this.unblockUIFunction;
372 instance.web.unblockUI();
375 * Displays an error dialog resulting from the various RPC communications
376 * failing over themselves
378 * @param {Object} error error description
379 * @param {String} error.title title of the error dialog
380 * @param {String} error.error message of the error dialog
382 display_error: function (error) {
383 return instance.web.dialog($('<div>'), {
387 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
389 }).html(error.error);
391 do_create: function(form) {
393 var fields = $(form).serializeArray();
394 self.rpc("/web/database/create", {'fields': fields}, function(result) {
395 var form_obj = self.to_object(fields);
396 var client_action = {
397 type: 'ir.actions.client',
400 'db': form_obj['db_name'],
402 'password': form_obj['create_admin_pwd'],
403 'login_successful': function() {
404 instance.webclient.show_application();
408 self.do_action(client_action);
412 do_drop: function(form) {
415 fields = $form.serializeArray(),
416 $db_list = $form.find('[name=drop_db]'),
418 if (!db || !confirm("Do you really want to delete the database: " + db + " ?")) {
421 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
423 self.display_error(result);
426 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
430 do_backup: function(form) {
433 self.session.get_file({
435 success: function () {
436 self.do_notify(_t("Backed"), _t("Database backed up successfully"));
438 error: function(error){
441 title: 'Backup Database',
442 error: 'AccessDenied'
446 complete: function() {
451 do_restore: function(form) {
455 url: '/web/database/restore',
458 success: function (body) {
459 // If empty body, everything went fine
460 if (!body) { return; }
462 if (body.indexOf('403 Forbidden') !== -1) {
464 title: 'Access Denied',
465 error: 'Incorrect super-administrator password'
469 title: 'Restore Database',
470 error: 'Could not restore the database'
474 complete: function() {
476 self.do_notify(_t("Restored"), _t("Database restored successfully"));
480 do_change_password: function(form) {
482 self.rpc("/web/database/change_password", {
483 'fields': $(form).serializeArray()
484 }, function(result) {
486 self.display_error(result);
489 self.do_notify("Changed Password", "Password has been changed successfully");
492 do_exit: function () {
493 this.do_action("login");
496 instance.web.client_actions.add("database_manager", "instance.web.DatabaseManager");
498 instance.web.Login = instance.web.Widget.extend({
500 remember_credentials: true,
503 init: function(parent, params) {
505 this.has_local_storage = typeof(localStorage) != 'undefined';
506 this.selected_db = null;
507 this.selected_login = null;
508 this.params = params || {};
510 if (this.params.login_successful) {
511 this.on('login_successful', this, this.params.login_successful);
514 if (this.has_local_storage && this.remember_credentials) {
515 this.selected_db = localStorage.getItem('last_db_login_success');
516 this.selected_login = localStorage.getItem('last_login_login_success');
517 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
518 this.selected_password = localStorage.getItem('last_password_login_success');
524 self.$element.find("form").submit(self.on_submit);
525 self.$element.find('.oe_login_manage_db').click(function() {
526 self.do_action("database_manager");
528 return self.load_db_list().then(self.on_db_list_loaded).then(function() {
529 if (self.params.db) {
530 self.do_login(self.params.db, self.params.login, self.params.password);
534 load_db_list: function (force) {
535 var d = $.when(), self = this;
536 if (_.isNull(this._db_list) || force) {
537 d = self.rpc("/web/database/get_list", {}, function(result) {
538 self._db_list = _.clone(result.db_list);
539 }, function(error, event) {
540 if (error.data.fault_code === 'AccessDenied') {
541 event.preventDefault();
547 on_db_list_loaded: function () {
549 var list = this._db_list;
550 var dbdiv = this.$element.find('div.oe_login_dbpane');
551 this.$element.find("[name=db]").replaceWith(instance.web.qweb.render('Login.dblist', { db_list: list, selected_db: this.selected_db}));
552 if(list.length === 0) {
553 this.do_action("database_manager");
554 } else if(list && list.length === 1) {
560 on_submit: function(ev) {
564 var $e = this.$element;
565 var db = $e.find("form [name=db]").val();
567 this.do_warn("Login", "No database selected !");
570 var login = $e.find("form input[name=login]").val();
571 var password = $e.find("form input[name=password]").val();
573 this.do_login(db, login, password);
576 * Performs actual login operation, and UI-related stuff
578 * @param {String} db database to log in
579 * @param {String} login user login
580 * @param {String} password user password
582 do_login: function (db, login, password) {
584 this.$element.removeClass('oe_login_invalid');
585 self.$(".oe_login_pane").fadeOut("slow");
586 return this.session.session_authenticate(db, login, password).pipe(function() {
587 if (self.has_local_storage) {
588 if(self.remember_credentials) {
589 localStorage.setItem('last_db_login_success', db);
590 localStorage.setItem('last_login_login_success', login);
591 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
592 localStorage.setItem('last_password_login_success', password);
595 localStorage.setItem('last_db_login_success', '');
596 localStorage.setItem('last_login_login_success', '');
597 localStorage.setItem('last_password_login_success', '');
600 self.trigger('login_successful');
602 self.$(".oe_login_pane").fadeIn("fast");
603 self.$element.addClass("oe_login_invalid");
607 this.$element.show();
610 this.$element.hide();
613 instance.web.client_actions.add("login", "instance.web.Login");
616 * Client action to reload the whole interface.
617 * If params has an entry 'menu_id', it opens the given menu entry.
619 instance.web.Reload = instance.web.Widget.extend({
620 init: function(parent, params) {
622 this.menu_id = (params && params.menu_id) || false;
625 var l = window.location;
626 var timestamp = new Date().getTime();
627 var search = "?ts=" + timestamp;
629 search = l.search + "&ts=" + timestamp;
633 hash = "#menu_id=" + this.menu_id;
635 var url = l.protocol + "//" + l.host + l.pathname + search + hash;
636 window.location = url;
639 instance.web.client_actions.add("reload", "instance.web.Reload");
642 * Client action to go back in breadcrumb history.
643 * If can't go back in history stack, will go back to home.
645 instance.web.HistoryBack = instance.web.Widget.extend({
646 init: function(parent, params) {
647 if (!parent.history_back()) {
648 window.location = '/' + (window.location.search || '');
652 instance.web.client_actions.add("history_back", "instance.web.HistoryBack");
655 * Client action to go back home.
657 instance.web.Home = instance.web.Widget.extend({
658 init: function(parent, params) {
659 window.location = '/' + (window.location.search || '');
662 instance.web.client_actions.add("home", "instance.web.Home");
664 instance.web.Menu = instance.web.Widget.extend({
667 this._super.apply(this, arguments);
668 this.has_been_loaded = $.Deferred();
669 this.maximum_visible_links = 'auto'; // # of menu to show. 0 = do not crop, 'auto' = algo
670 this.data = {data:{children:[]}};
673 this._super.apply(this, arguments);
674 this.$secondary_menus = this.getParent().$element.find('.oe_secondary_menus_container');
675 this.$secondary_menus.on('click', 'a[data-menu]', this.on_menu_click);
676 return this.do_reload();
678 do_reload: function() {
679 return this.rpc("/web/menu/load", {}).then(this.on_loaded);
681 on_loaded: function(data) {
684 this.renderElement();
685 this.limit_entries();
686 this.$secondary_menus.html(QWeb.render("Menu.secondary", { widget : this }));
687 this.$element.on('click', 'a[data-menu]', this.on_menu_click);
688 // Hide second level submenus
689 this.$secondary_menus.find('.oe_menu_toggler').siblings('.oe_secondary_submenu').hide();
690 if (self.current_menu) {
691 self.open_menu(self.current_menu);
693 this.has_been_loaded.resolve();
695 limit_entries: function() {
696 var maximum_visible_links = this.maximum_visible_links;
697 if (maximum_visible_links === 'auto') {
698 maximum_visible_links = this.auto_limit_entries();
700 if (maximum_visible_links < this.data.data.children.length) {
701 var $more = $(QWeb.render('Menu.more')),
702 $index = this.$element.find('li').eq(maximum_visible_links - 1);
704 $more.find('.oe_menu_more').append($index.next().nextAll());
707 auto_limit_entries: function() {
708 // TODO: auto detect overflow and bind window on resize
709 var width = $(window).width();
710 return Math.floor(width / 125);
713 * Opens a given menu by id, as if a user had browsed to that menu by hand
714 * except does not trigger any event on the way
716 * @param {Number} id database id of the terminal menu to select
718 open_menu: function (id) {
719 var $clicked_menu, $sub_menu, $main_menu;
720 $clicked_menu = this.$element.add(this.$secondary_menus).find('a[data-menu=' + id + ']');
721 this.trigger('open_menu', id, $clicked_menu);
723 if (this.$secondary_menus.has($clicked_menu).length) {
724 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
725 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
727 $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
728 $main_menu = $clicked_menu;
731 // Activate current main menu
732 this.$element.find('.oe_active').removeClass('oe_active');
733 $main_menu.addClass('oe_active');
735 // Show current sub menu
736 this.$secondary_menus.find('.oe_secondary_menu').hide();
739 // Hide/Show the leftbar menu depending of the presence of sub-items
740 this.$secondary_menus.parent('.oe_leftbar').toggle(!!$sub_menu.children().length);
742 // Activate current menu item and show parents
743 this.$secondary_menus.find('.oe_active').removeClass('oe_active');
744 if ($main_menu !== $clicked_menu) {
745 $clicked_menu.parents().show();
746 if ($clicked_menu.is('.oe_menu_toggler')) {
747 $clicked_menu.toggleClass('oe_menu_opened').siblings('.oe_secondary_submenu:first').toggle();
749 $clicked_menu.parent().addClass('oe_active');
754 * Call open_menu with the first menu_item matching an action_id
756 * @param {Number} id the action_id to match
758 open_action: function (id) {
759 var $menu = this.$element.add(this.$secondary_menus).find('a[data-action-id="' + id + '"]');
760 var menu_id = $menu.data('menu');
762 this.open_menu(menu_id);
766 * Process a click on a menu item
768 * @param {Number} id the menu_id
769 * @param {Boolean} [needaction=false] whether the triggered action should execute in a `needs action` context
771 menu_click: function(id, needaction) {
774 // find back the menuitem in dom to get the action
775 var $item = this.$element.find('a[data-menu=' + id + ']');
777 $item = this.$secondary_menus.find('a[data-menu=' + id + ']');
779 var action_id = $item.data('action-id');
780 // If first level menu doesnt have action trigger first leaf
782 if(this.$element.has($item).length) {
783 var $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + id + ']');
784 var $items = $sub_menu.find('a[data-action-id]').filter('[data-action-id!=""]');
786 action_id = $items.data('action-id');
787 id = $items.data('menu');
792 this.current_menu = id;
793 this.session.active_id = id;
795 this.trigger('menu_click', {
796 action_id: action_id,
797 needaction: needaction,
803 * Jquery event handler for menu click
805 * @param {Event} ev the jquery event
807 on_menu_click: function(ev) {
809 var needaction = $(ev.target).is('div.oe_menu_counter');
810 this.menu_click($(ev.currentTarget).data('menu'), needaction);
814 instance.web.UserMenu = instance.web.Widget.extend({
815 template: "UserMenu",
816 init: function(parent) {
818 this.update_promise = $.Deferred().resolve();
822 this._super.apply(this, arguments);
823 this.$element.on('click', '.oe_dropdown_menu li a[data-menu]', function(ev) {
825 var f = self['on_menu_' + $(this).data('menu')];
831 change_password :function() {
833 this.dialog = new instance.web.Dialog(this, {
834 title: _t("Change Password"),
837 this.dialog.$element.html(QWeb.render("UserMenu.password", self));
838 this.dialog.$element.find("form[name=change_password_form]").validate({
839 submitHandler: function (form) {
840 self.rpc("/web/session/change_password",{
841 'fields': $(form).serializeArray()
842 }, function(result) {
844 self.display_error(result);
847 instance.webclient.on_logout();
853 display_error: function (error) {
854 return instance.web.dialog($('<div>'), {
858 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
860 }).html(error.error);
862 do_update: function () {
864 var fct = function() {
865 var $avatar = self.$element.find('.oe_topbar_avatar');
866 $avatar.attr('src', $avatar.data('default-src'));
867 if (!self.session.uid)
869 var func = new instance.web.Model("res.users").get_func("read");
870 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
871 var topbar_name = res.name;
872 if(instance.connection.debug)
873 topbar_name = _.str.sprintf("%s (%s)", topbar_name, instance.connection.db);
874 if(res.company_id[0] > 1)
875 topbar_name = _.str.sprintf("%s (%s)", topbar_name, res.company_id[1]);
876 self.$element.find('.oe_topbar_name').text(topbar_name);
877 var avatar_src = _.str.sprintf('%s/web/binary/image?session_id=%s&model=res.users&field=image_small&id=%s', self.session.prefix, self.session.session_id, self.session.uid);
878 $avatar.attr('src', avatar_src);
881 this.update_promise = this.update_promise.pipe(fct, fct);
883 on_action: function() {
885 on_menu_logout: function() {
887 on_menu_settings: function() {
889 self.rpc("/web/action/load", { action_id: "base.action_res_users_my" }, function(result) {
890 result.result.res_id = instance.connection.uid;
891 self.getParent().action_manager.do_action(result.result);
894 on_menu_about: function() {
896 self.rpc("/web/webclient/version_info", {}).then(function(res) {
897 var $help = $(QWeb.render("UserMenu.about", {version_info: res}));
898 $help.find('a.oe_activate_debug_mode').click(function (e) {
900 window.location = $.param.querystring(
901 window.location.href, 'debug');
903 instance.web.dialog($help, {autoOpen: true,
904 modal: true, width: 580, height: 290, resizable: false, title: _t("About")});
909 instance.web.Client = instance.web.Widget.extend({
910 init: function(parent, origin) {
911 instance.client = instance.webclient = this;
913 this.origin = origin;
917 return instance.connection.session_bind(this.origin).pipe(function() {
918 var $e = $(QWeb.render(self._template, {}));
919 self.replaceElement($e);
921 return self.show_common();
924 bind_events: function() {
926 this.$element.on('mouseenter', '.oe_systray > div:not([data-tipsy=true])', function() {
927 $(this).attr('data-tipsy', 'true').tipsy().trigger('mouseenter');
929 this.$element.on('click', '.oe_dropdown_toggle', function(ev) {
931 var $toggle = $(this);
932 var $menu = $toggle.siblings('.oe_dropdown_menu');
933 $menu = $menu.size() >= 1 ? $menu : $toggle.find('.oe_dropdown_menu');
934 var state = $menu.is('.oe_opened');
935 setTimeout(function() {
936 // Do not alter propagation
937 $toggle.add($menu).toggleClass('oe_opened', !state);
939 // Move $menu if outside window's edge
940 var doc_width = $(document).width();
941 var offset = $menu.offset();
942 var menu_width = $menu.width();
943 var x = doc_width - offset.left - menu_width - 2;
945 $menu.offset({ left: offset.left + x }).width(menu_width);
950 instance.web.bus.on('click', this, function(ev) {
952 if (!$(ev.target).is('input[type=file]')) {
953 self.$element.find('.oe_dropdown_menu.oe_opened').removeClass('oe_opened');
957 show_common: function() {
959 this.crashmanager = new instance.web.CrashManager();
960 instance.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
961 self.notification = new instance.web.Notification(this);
962 self.notification.appendTo(self.$element);
963 self.loading = new instance.web.Loading(self);
964 self.loading.appendTo(self.$element);
965 self.action_manager = new instance.web.ActionManager(self);
966 self.action_manager.appendTo(self.$('.oe_application'));
970 instance.web.WebClient = instance.web.Client.extend({
971 _template: 'WebClient',
972 init: function(parent) {
974 this._current_state = null;
978 return $.when(this._super()).pipe(function() {
979 if (jQuery.param !== undefined && jQuery.deparam(jQuery.param.querystring()).kitten !== undefined) {
980 $("body").addClass("kitten-mode-activated");
982 $.blockUI.defaults.message = '<img src="http://www.amigrave.com/kitten.gif">';
985 if (!self.session.session_is_valid()) {
988 self.show_application();
992 set_title: function(title) {
993 title = _.str.clean(title);
994 var sep = _.isEmpty(title) ? '' : ' - ';
995 document.title = title + sep + 'OpenERP';
997 show_common: function() {
1000 window.onerror = function (message, file, line) {
1001 self.crashmanager.on_traceback({
1002 type: _t("Client Error"),
1004 data: {debug: file + ':' + line}
1008 show_login: function() {
1009 this.$('.oe_topbar').hide();
1010 this.action_manager.do_action("login");
1011 this.action_manager.inner_widget.on('login_successful', this, this.show_application);
1013 show_application: function() {
1015 self.$('.oe_topbar').show();
1016 self.menu = new instance.web.Menu(self);
1017 self.menu.replace(this.$element.find('.oe_menu_placeholder'));
1018 self.menu.on('menu_click', this, this.on_menu_action);
1019 self.user_menu = new instance.web.UserMenu(self);
1020 self.user_menu.replace(this.$element.find('.oe_user_menu_placeholder'));
1021 self.user_menu.on_menu_logout.add(this.proxy('on_logout'));
1022 self.user_menu.on_action.add(this.proxy('on_menu_action'));
1023 self.user_menu.do_update();
1024 self.bind_hashchange();
1025 if (!self.session.openerp_entreprise) {
1026 var version_label = _t("OpenERP - Unsupported/Community Version");
1027 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));
1031 destroy_content: function() {
1032 _.each(_.clone(this.getChildren()), function(el) {
1035 this.$element.children().remove();
1037 do_reload: function() {
1039 return this.session.session_reload().pipe(function () {
1040 instance.connection.load_modules(true).pipe(
1041 self.menu.proxy('do_reload')); });
1044 do_notify: function() {
1045 var n = this.notification;
1046 n.notify.apply(n, arguments);
1048 do_warn: function() {
1049 var n = this.notification;
1050 n.warn.apply(n, arguments);
1052 on_logout: function() {
1054 this.session.session_logout().then(function () {
1055 $(window).unbind('hashchange', self.on_hashchange);
1056 self.do_push_state({});
1057 //would be cool to be able to do this, but I think it will make addons do strange things
1058 //this.show_login();
1059 window.location.reload();
1062 bind_hashchange: function() {
1064 $(window).bind('hashchange', this.on_hashchange);
1066 var state = $.bbq.getState(true);
1067 if (_.isEmpty(state) || state.action == "login") {
1068 self.menu.has_been_loaded.then(function() {
1069 var first_menu_id = self.menu.$element.find("a:first").data("menu");
1071 self.menu.menu_click(first_menu_id);
1075 $(window).trigger('hashchange');
1078 on_hashchange: function(event) {
1080 var state = event.getState(true);
1081 if (!_.isEqual(this._current_state, state)) {
1082 if(state.action_id === undefined && state.menu_id) {
1083 self.menu.has_been_loaded.then(function() {
1084 self.menu.do_reload().then(function() {
1085 self.menu.menu_click(state.menu_id)
1089 this.action_manager.do_load_state(state, !!this._current_state);
1092 this._current_state = state;
1094 do_push_state: function(state) {
1095 this.set_title(state.title);
1097 var url = '#' + $.param(state);
1098 this._current_state = _.clone(state);
1099 $.bbq.pushState(url);
1101 on_menu_action: function(options) {
1103 this.rpc("/web/action/load", { action_id: options.action_id })
1104 .then(function (result) {
1105 var action = result.result;
1106 if (options.needaction) {
1107 action.context.search_default_needaction_pending = true;
1109 self.action_manager.clear_breadcrumbs();
1110 self.action_manager.do_action(action);
1113 do_action: function(action) {
1115 // TODO replace by client action menuclick
1116 if(action.menu_id) {
1117 this.do_reload().then(function () {
1118 self.menu.menu_click(action.menu_id);
1122 set_content_full_screen: function(fullscreen) {
1124 $(".oe_webclient", this.$element).addClass("oe_content_full_screen");
1125 $("body").css({'overflow-y':'hidden'});
1127 $(".oe_webclient", this.$element).removeClass("oe_content_full_screen");
1128 $("body").css({'overflow-y':'scroll'});
1133 instance.web.EmbeddedClient = instance.web.Client.extend({
1134 _template: 'EmbedClient',
1135 init: function(parent, origin, dbname, login, key, action_id, options) {
1136 this._super(parent, origin);
1138 this.dbname = dbname;
1141 this.action_id = action_id;
1142 this.options = options || {};
1146 return $.when(this._super()).pipe(function() {
1147 return instance.connection.session_authenticate(self.dbname, self.login, self.key, true).pipe(function() {
1148 return self.rpc("/web/action/load", { action_id: self.action_id }, function(result) {
1149 var action = result.result;
1150 action.flags = _.extend({
1151 //views_switcher : false,
1152 search_view : false,
1153 action_buttons : false,
1156 }, self.options, action.flags || {});
1158 self.action_manager.do_action(action);
1165 instance.web.embed = function (origin, dbname, login, key, action, options) {
1166 $('head').append($('<link>', {
1167 'rel': 'stylesheet',
1169 'href': origin +'/web/webclient/css'
1171 var currentScript = document.currentScript;
1172 if (!currentScript) {
1173 var sc = document.getElementsByTagName('script');
1174 currentScript = sc[sc.length-1];
1176 var client = new instance.web.EmbeddedClient(null, origin, dbname, login, key, action, options);
1177 client.insertAfter(currentScript);
1182 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: