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.setElement(content || this.make(this.tagName));
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);
84 if (this.dialog_options.autoOpen) {
87 instance.web.dialog(this.$element, this.get_options());
90 get_options: function(options) {
92 o = _.extend({}, this.dialog_options, options || {});
93 _.each(['width', 'height'], function(unit) {
94 o[unit] = self['get_' + unit](o[unit]);
95 o['min_' + unit] = self['get_' + unit](o['min_' + unit] || 0);
96 o['max_' + unit] = self['get_' + unit](o['max_' + unit] || 0);
97 if (o[unit] !== 'auto' && o['min_' + unit] && o[unit] < o['min_' + unit]) o[unit] = o['min_' + unit];
98 if (o[unit] !== 'auto' && o['max_' + unit] && o[unit] > o['max_' + unit]) o[unit] = o['max_' + unit];
100 if (!o.title && this.dialog_title) {
101 o.title = this.dialog_title;
105 get_width: function(val) {
106 return this.get_size(val.toString(), $(window.top).width());
108 get_height: function(val) {
109 return this.get_size(val.toString(), $(window.top).height());
111 get_size: function(val, available_size) {
112 if (val === 'auto') {
114 } else if (val.slice(-1) == "%") {
115 return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
117 return parseInt(val, 10);
120 open: function(options) {
121 // TODO fme: bind window on resize
123 this.$element.html(this.renderElement());
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' });
133 this.$element.dialog('close');
135 on_close: function() {
136 if (this.__tmp_dialog_destroying)
138 if (this.dialog_options.destroy_on_close) {
139 this.__tmp_dialog_closing = true;
141 this.__tmp_dialog_closing = undefined;
144 on_resized: function() {
146 destroy: function () {
147 _.each(this.getChildren(), function(el) {
150 if (! this.__tmp_dialog_closing) {
151 this.__tmp_dialog_destroying = true;
153 this.__tmp_dialog_destroying = undefined;
155 if (! this.isDestroyed()) {
156 this.$element.dialog('destroy');
162 instance.web.CrashManager = instance.web.CallbackEnabled.extend({
163 on_rpc_error: function(error) {
164 if (error.data.fault_code) {
165 var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
166 if (split.length > 1) {
167 error.type = split.shift();
168 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
171 if (error.code === 200 && error.type) {
172 this.on_managed_error(error);
174 this.on_traceback(error);
177 on_managed_error: function(error) {
178 instance.web.dialog($('<div>' + QWeb.render('CrashManager.warning', {error: error}) + '</div>'), {
179 title: "OpenERP " + _.str.capitalize(error.type),
181 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
185 on_traceback: function(error) {
188 if (instance.connection.openerp_entreprise) {
189 buttons[_t("Send OpenERP Enterprise Report")] = function() {
191 var issuename = $('#issuename').val();
192 var explanation = $('#explanation').val();
193 var remark = $('#remark').val();
194 // Call the send method from server to send mail with details
195 new instance.web.DataSet(self, 'publisher_warranty.contract').call_and_eval('send', [error.data,explanation,remark,issuename]).then(function(result){
196 if (result === false) {
197 alert('There was a communication error.');
199 $this.dialog('close');
203 buttons[_t("Dont send")] = function() {
204 $(this).dialog("close");
207 buttons[_t("Ok")] = function() {
208 $(this).dialog("close");
211 var dialog = new instance.web.Dialog(this, {
212 title: "OpenERP " + _.str.capitalize(error.type),
219 dialog.$element.html(QWeb.render('CrashManager.error', {session: instance.connection, error: error}));
221 on_javascript_exception: function(exception) {
223 type: _t("Client Error"),
230 instance.web.Loading = instance.web.Widget.extend({
232 init: function(parent) {
235 this.blocked_ui = false;
237 this.request_call = function() {
238 self.on_rpc_event(1);
240 this.response_call = function() {
241 self.on_rpc_event(-1);
243 this.session.on_rpc_request.add_first(this.request_call);
244 this.session.on_rpc_response.add_last(this.response_call);
246 destroy: function() {
247 this.session.on_rpc_request.remove(this.request_call);
248 this.session.on_rpc_response.remove(this.response_call);
249 this.on_rpc_event(-this.count);
252 on_rpc_event : function(increment) {
254 if (!this.count && increment === 1) {
256 this.long_running_timer = setTimeout(function () {
257 self.blocked_ui = true;
258 instance.web.blockUI();
262 this.count += increment;
263 if (this.count > 0) {
264 if (instance.connection.debug) {
265 this.$element.text(_.str.sprintf( _t("Loading (%d)"), this.count));
267 this.$element.text(_t("Loading"));
269 this.$element.show();
270 this.getParent().$element.addClass('oe_wait');
273 clearTimeout(this.long_running_timer);
274 // Don't unblock if blocked by somebody else
275 if (self.blocked_ui) {
276 this.blocked_ui = false;
277 instance.web.unblockUI();
279 this.$element.fadeOut();
280 this.getParent().$element.removeClass('oe_wait');
285 instance.web.DatabaseManager = instance.web.Widget.extend({
286 init: function(parent) {
288 this.unblockUIFunction = instance.web.unblockUI;
289 $.validator.addMethod('matches', function (s, _, re) {
290 return new RegExp(re).test(s);
291 }, _t("Invalid database name"));
295 var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
297 self.db_list = result.db_list;
303 var fetch_langs = this.rpc("/web/session/get_lang_list", {}).then(function(result) {
304 self.lang_list = result.lang_list;
306 return $.when(fetch_db, fetch_langs).then(self.do_render);
308 do_render: function() {
310 self.$element.html(QWeb.render("DatabaseManager", { widget : self }));
311 self.$element.find(".oe_database_manager_menu").tabs({
312 show: function(event, ui) {
313 $('*[autofocus]:first', ui.panel).focus();
316 self.$element.find("form[name=create_db_form]").validate({ submitHandler: self.do_create });
317 self.$element.find("form[name=drop_db_form]").validate({ submitHandler: self.do_drop });
318 self.$element.find("form[name=backup_db_form]").validate({ submitHandler: self.do_backup });
319 self.$element.find("form[name=restore_db_form]").validate({ submitHandler: self.do_restore });
320 self.$element.find("form[name=change_pwd_form]").validate({
322 old_pwd: "Please enter your previous password",
323 new_pwd: "Please enter your new password",
325 required: "Please confirm your new password",
326 equalTo: "The confirmation does not match the password"
329 submitHandler: self.do_change_password
331 self.$element.find("#back_to_login").click(self.do_exit);
333 destroy: function () {
334 this.$element.find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login').unbind('click').end().empty();
338 * Converts a .serializeArray() result into a dict. Does not bother folding
339 * multiple identical keys into an array, last key wins.
341 * @param {Array} array
343 to_object: function (array) {
345 _(array).each(function (record) {
346 result[record.name] = record.value;
351 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
352 * from unblocking the UI
354 blockUI: function () {
355 instance.web.blockUI();
356 instance.web.unblockUI = function () {};
359 * Reinstates $.unblockUI so third parties can play with blockUI, and
362 unblockUI: function () {
363 instance.web.unblockUI = this.unblockUIFunction;
364 instance.web.unblockUI();
367 * Displays an error dialog resulting from the various RPC communications
368 * failing over themselves
370 * @param {Object} error error description
371 * @param {String} error.title title of the error dialog
372 * @param {String} error.error message of the error dialog
374 display_error: function (error) {
375 return instance.web.dialog($('<div>'), {
379 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
381 }).html(error.error);
383 do_create: function(form) {
385 var fields = $(form).serializeArray();
386 self.rpc("/web/database/create", {'fields': fields}, function(result) {
387 var form_obj = self.to_object(fields);
388 var client_action = {
389 type: 'ir.actions.client',
392 'db': form_obj['db_name'],
394 'password': form_obj['create_admin_pwd'],
397 self.do_action(client_action);
401 do_drop: function(form) {
404 fields = $form.serializeArray(),
405 $db_list = $form.find('[name=drop_db]'),
407 if (!db || !confirm("Do you really want to delete the database: " + db + " ?")) {
410 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
412 self.display_error(result);
415 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
419 do_backup: function(form) {
422 self.session.get_file({
424 success: function () {
425 self.do_notify(_t("Backed"), _t("Database backed up successfully"));
427 error: function(error){
430 title: 'Backup Database',
431 error: 'AccessDenied'
435 complete: function() {
440 do_restore: function(form) {
444 url: '/web/database/restore',
447 success: function (body) {
448 // If empty body, everything went fine
449 if (!body) { return; }
451 if (body.indexOf('403 Forbidden') !== -1) {
453 title: 'Access Denied',
454 error: 'Incorrect super-administrator password'
458 title: 'Restore Database',
459 error: 'Could not restore the database'
463 complete: function() {
465 self.do_notify(_t("Restored"), _t("Database restored successfully"));
469 do_change_password: function(form) {
471 self.rpc("/web/database/change_password", {
472 'fields': $(form).serializeArray()
473 }, function(result) {
475 self.display_error(result);
478 self.do_notify("Changed Password", "Password has been changed successfully");
481 do_exit: function () {
482 this.do_action("login");
485 instance.web.client_actions.add("database_manager", "instance.web.DatabaseManager");
487 instance.web.Login = instance.web.Widget.extend({
489 remember_credentials: true,
492 init: function(parent, params) {
494 this.has_local_storage = typeof(localStorage) != 'undefined';
495 this.selected_db = null;
496 this.selected_login = null;
497 this.params = params || {};
499 if (this.params.login_successful) {
500 this.on('login_successful', this, this.params.login_successful);
503 if (this.has_local_storage && this.remember_credentials) {
504 this.selected_db = localStorage.getItem('last_db_login_success');
505 this.selected_login = localStorage.getItem('last_login_login_success');
506 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
507 this.selected_password = localStorage.getItem('last_password_login_success');
513 self.$element.find("form").submit(self.on_submit);
514 self.$element.find('.oe_login_manage_db').click(function() {
515 self.do_action("database_manager");
517 return self.load_db_list().then(self.on_db_list_loaded).then(function() {
518 if (self.params.db) {
519 self.do_login(self.params.db, self.params.login, self.params.password);
523 load_db_list: function (force) {
524 var d = $.when(), self = this;
525 if (_.isNull(this._db_list) || force) {
526 d = self.rpc("/web/database/get_list", {}, function(result) {
527 self._db_list = _.clone(result.db_list);
528 }, function(error, event) {
529 if (error.data.fault_code === 'AccessDenied') {
530 event.preventDefault();
536 on_db_list_loaded: function () {
538 var list = this._db_list;
539 var dbdiv = this.$element.find('div.oe_login_dbpane');
540 this.$element.find("[name=db]").replaceWith(instance.web.qweb.render('Login.dblist', { db_list: list, selected_db: this.selected_db}));
541 if(list.length === 0) {
542 this.do_action("database_manager");
543 } else if(list && list.length === 1) {
549 on_submit: function(ev) {
553 var $e = this.$element;
554 var db = $e.find("form [name=db]").val();
556 this.do_warn("Login", "No database selected !");
559 var login = $e.find("form input[name=login]").val();
560 var password = $e.find("form input[name=password]").val();
562 this.do_login(db, login, password);
565 * Performs actual login operation, and UI-related stuff
567 * @param {String} db database to log in
568 * @param {String} login user login
569 * @param {String} password user password
571 do_login: function (db, login, password) {
573 this.$element.removeClass('oe_login_invalid');
574 self.$(".oe_login_pane").fadeOut("slow");
575 return this.session.session_authenticate(db, login, password).pipe(function() {
576 if (self.has_local_storage) {
577 if(self.remember_credentials) {
578 localStorage.setItem('last_db_login_success', db);
579 localStorage.setItem('last_login_login_success', login);
580 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
581 localStorage.setItem('last_password_login_success', password);
584 localStorage.setItem('last_db_login_success', '');
585 localStorage.setItem('last_login_login_success', '');
586 localStorage.setItem('last_password_login_success', '');
589 self.trigger('login_successful');
591 self.$(".oe_login_pane").fadeIn("fast");
592 self.$element.addClass("oe_login_invalid");
596 this.$element.show();
599 this.$element.hide();
602 instance.web.client_actions.add("login", "instance.web.Login");
604 instance.web.Menu = instance.web.Widget.extend({
607 this._super.apply(this, arguments);
608 this.has_been_loaded = $.Deferred();
609 this.maximum_visible_links = 'auto'; // # of menu to show. 0 = do not crop, 'auto' = algo
610 this.data = {data:{children:[]}};
613 this._super.apply(this, arguments);
614 this.$secondary_menus = this.getParent().$element.find('.oe_secondary_menus_container');
615 this.$secondary_menus.on('click', 'a[data-menu]', this.on_menu_click);
616 return this.do_reload();
618 do_reload: function() {
619 return this.rpc("/web/menu/load", {}).then(this.on_loaded);
621 on_loaded: function(data) {
624 this.renderElement();
625 this.limit_entries();
626 this.$secondary_menus.html(QWeb.render("Menu.secondary", { widget : this }));
627 this.$element.on('click', 'a[data-menu]', this.on_menu_click);
628 // Hide second level submenus
629 this.$secondary_menus.find('.oe_menu_toggler').siblings('.oe_secondary_submenu').hide();
630 if (self.current_menu) {
631 self.open_menu(self.current_menu);
633 this.has_been_loaded.resolve();
635 limit_entries: function() {
636 var maximum_visible_links = this.maximum_visible_links;
637 if (maximum_visible_links === 'auto') {
638 maximum_visible_links = this.auto_limit_entries();
640 if (maximum_visible_links < this.data.data.children.length) {
641 var $more = $(QWeb.render('Menu.more')),
642 $index = this.$element.find('li').eq(maximum_visible_links - 1);
644 $more.find('.oe_menu_more').append($index.next().nextAll());
647 auto_limit_entries: function() {
648 // TODO: auto detect overflow and bind window on resize
649 var width = $(window).width();
650 return Math.floor(width / 125);
653 * Opens a given menu by id, as if a user had browsed to that menu by hand
654 * except does not trigger any event on the way
656 * @param {Number} id database id of the terminal menu to select
658 open_menu: function (id) {
659 var $clicked_menu, $sub_menu, $main_menu;
660 $clicked_menu = this.$element.add(this.$secondary_menus).find('a[data-menu=' + id + ']');
661 this.trigger('open_menu', id, $clicked_menu);
663 if (this.$secondary_menus.has($clicked_menu).length) {
664 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
665 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
667 $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
668 $main_menu = $clicked_menu;
671 // Activate current main menu
672 this.$element.find('.oe_active').removeClass('oe_active');
673 $main_menu.addClass('oe_active');
675 // Show current sub menu
676 this.$secondary_menus.find('.oe_secondary_menu').hide();
679 // Hide/Show the leftbar menu depending of the presence of sub-items
680 this.$secondary_menus.parent('.oe_leftbar').toggle(!!$sub_menu.children().length);
682 // Activate current menu item and show parents
683 this.$secondary_menus.find('.oe_active').removeClass('oe_active');
684 if ($main_menu !== $clicked_menu) {
685 $clicked_menu.parents().show();
686 if ($clicked_menu.is('.oe_menu_toggler')) {
687 $clicked_menu.toggleClass('oe_menu_opened').siblings('.oe_secondary_submenu:first').toggle();
689 $clicked_menu.parent().addClass('oe_active');
694 * Call open_menu with the first menu_item matching an action_id
696 * @param {Number} id the action_id to match
698 open_action: function (id) {
699 var $menu = this.$element.add(this.$secondary_menus).find('a[data-action-id="' + id + '"]');
700 var menu_id = $menu.data('menu');
702 this.open_menu(menu_id);
706 * Process a click on a menu item
708 * @param {Number} id the menu_id
709 * @param {Boolean} [needaction=false] whether the triggered action should execute in a `needs action` context
711 menu_click: function(id, needaction) {
714 // find back the menuitem in dom to get the action
715 var $item = this.$element.find('a[data-menu=' + id + ']');
717 $item = this.$secondary_menus.find('a[data-menu=' + id + ']');
719 var action_id = $item.data('action-id');
720 // If first level menu doesnt have action trigger first leaf
722 if(this.$element.has($item).length) {
723 var $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + id + ']');
724 var $items = $sub_menu.find('a[data-action-id]').filter('[data-action-id!=""]');
726 action_id = $items.data('action-id');
727 id = $items.data('menu');
732 this.current_menu = id;
733 this.session.active_id = id;
735 this.trigger('menu_click', {
736 action_id: action_id,
737 needaction: needaction,
743 * Jquery event handler for menu click
745 * @param {Event} ev the jquery event
747 on_menu_click: function(ev) {
749 var needaction = $(ev.target).is('div.oe_menu_counter');
750 this.menu_click($(ev.currentTarget).data('menu'), needaction);
754 instance.web.UserMenu = instance.web.Widget.extend({
755 template: "UserMenu",
756 init: function(parent) {
758 this.update_promise = $.Deferred().resolve();
762 this._super.apply(this, arguments);
763 this.$element.on('click', '.oe_dropdown_menu li a[data-menu]', function(ev) {
765 var f = self['on_menu_' + $(this).data('menu')];
771 change_password :function() {
773 this.dialog = new instance.web.Dialog(this, {
774 title: _t("Change Password"),
777 this.dialog.$element.html(QWeb.render("UserMenu.password", self));
778 this.dialog.$element.find("form[name=change_password_form]").validate({
779 submitHandler: function (form) {
780 self.rpc("/web/session/change_password",{
781 'fields': $(form).serializeArray()
782 }, function(result) {
784 self.display_error(result);
787 instance.webclient.on_logout();
793 display_error: function (error) {
794 return instance.web.dialog($('<div>'), {
798 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
800 }).html(error.error);
802 do_update: function () {
804 var fct = function() {
805 var $avatar = self.$element.find('.oe_topbar_avatar');
806 $avatar.attr('src', $avatar.data('default-src'));
807 if (!self.session.uid)
809 var func = new instance.web.Model("res.users").get_func("read");
810 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
811 var topbar_name = res.name;
812 if(instance.connection.debug)
813 topbar_name = _.str.sprintf("%s (%s)", topbar_name, instance.connection.db);
814 if(res.company_id[0] > 1)
815 topbar_name = _.str.sprintf("%s (%s)", topbar_name, res.company_id[1]);
816 self.$element.find('.oe_topbar_name').text(topbar_name);
817 var avatar_src = _.str.sprintf('%s/web/binary/image?session_id=%s&model=res.users&field=avatar&id=%s', self.session.prefix, self.session.session_id, self.session.uid);
818 $avatar.attr('src', avatar_src);
821 this.update_promise = this.update_promise.pipe(fct, fct);
823 on_action: function() {
825 on_menu_logout: function() {
827 on_menu_settings: function() {
829 var action_manager = new instance.web.ActionManager(this);
830 var dataset = new instance.web.DataSet (this,'res.users',this.context);
831 dataset.call ('action_get','',function (result){
832 self.rpc('/web/action/load', {action_id:result}, function(result){
833 action_manager.do_action(_.extend(result['result'], {
835 res_id: self.session.uid,
836 res_model: 'res.users',
838 action_buttons: false,
841 views_switcher: false,
847 this.dialog = new instance.web.Dialog(this,{
848 title: _t("Preferences"),
851 {text: _t("Change password"), click: function(){ self.change_password(); }},
852 {text: _t("Cancel"), click: function(){ $(this).dialog('destroy'); }},
853 {text: _t("Save"), click: function(){
854 var inner_widget = action_manager.inner_widget;
855 inner_widget.views[inner_widget.active_view].controller.do_save()
857 self.dialog.destroy();
858 // needs to refresh interface in case language changed
859 window.location.reload();
865 action_manager.appendTo(this.dialog.$element);
866 action_manager.renderElement(this.dialog);
868 on_menu_about: function() {
870 self.rpc("/web/webclient/version_info", {}).then(function(res) {
871 var $help = $(QWeb.render("UserMenu.about", {version_info: res}));
872 $help.find('a.oe_activate_debug_mode').click(function (e) {
874 window.location = $.param.querystring(
875 window.location.href, 'debug');
877 instance.web.dialog($help, {autoOpen: true,
878 modal: true, width: 580, height: 290, resizable: false, title: _t("About")});
883 instance.web.Client = instance.web.Widget.extend({
884 init: function(parent, origin) {
885 instance.client = instance.webclient = this;
887 this.origin = origin;
891 return instance.connection.session_bind(this.origin).pipe(function() {
892 var $e = $(QWeb.render(self._template, {}));
893 self.replaceElement($e);
895 return self.show_common();
898 bind_events: function() {
900 this.$element.on('mouseenter', '.oe_systray > div:not([data-tipsy=true])', function() {
901 $(this).attr('data-tipsy', 'true').tipsy().trigger('mouseenter');
903 this.$element.on('click', '.oe_dropdown_toggle', function(ev) {
905 var $toggle = $(this);
906 var $menu = $toggle.siblings('.oe_dropdown_menu');
907 $menu = $menu.size() >= 1 ? $menu : $toggle.find('.oe_dropdown_menu');
908 var state = $menu.is('.oe_opened');
909 setTimeout(function() {
910 // Do not alter propagation
911 $toggle.add($menu).toggleClass('oe_opened', !state);
913 // Move $menu if outside window's edge
914 var doc_width = $(document).width();
915 var offset = $menu.offset();
916 var menu_width = $menu.width();
917 var x = doc_width - offset.left - menu_width - 2;
919 $menu.offset({ left: offset.left + x }).width(menu_width);
924 instance.web.bus.on('click', this, function(ev) {
925 if (!$(ev.target).is('input[type=file]')) {
926 self.$element.find('.oe_dropdown_menu.oe_opened').removeClass('oe_opened');
930 show_common: function() {
932 this.crashmanager = new instance.web.CrashManager();
933 instance.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
934 self.notification = new instance.web.Notification(this);
935 self.notification.appendTo(self.$element);
936 self.loading = new instance.web.Loading(self);
937 self.loading.appendTo(self.$element);
938 self.action_manager = new instance.web.ActionManager(self);
939 self.action_manager.appendTo(self.$('.oe_application'));
943 instance.web.WebClient = instance.web.Client.extend({
944 _template: 'WebClient',
945 init: function(parent) {
947 this._current_state = null;
951 return $.when(this._super()).pipe(function() {
952 if (jQuery.param !== undefined && jQuery.deparam(jQuery.param.querystring()).kitten !== undefined) {
953 $("body").addClass("kitten-mode-activated");
955 $.blockUI.defaults.message = '<img src="http://www.amigrave.com/kitten.gif">';
958 if (!self.session.session_is_valid()) {
961 self.show_application();
965 set_title: function(title) {
966 title = _.str.clean(title);
967 var sep = _.isEmpty(title) ? '' : ' - ';
968 document.title = title + sep + 'OpenERP';
970 show_common: function() {
973 window.onerror = function (message, file, line) {
974 self.crashmanager.on_traceback({
975 type: _t("Client Error"),
977 data: {debug: file + ':' + line}
981 show_login: function() {
982 this.$('.oe_topbar').hide();
983 this.action_manager.do_action("login");
984 this.action_manager.inner_widget.on('login_successful', this, this.show_application);
986 show_application: function() {
988 self.$('.oe_topbar').show();
989 self.menu = new instance.web.Menu(self);
990 self.menu.replace(this.$element.find('.oe_menu_placeholder'));
991 self.menu.on('menu_click', this, this.on_menu_action);
992 self.user_menu = new instance.web.UserMenu(self);
993 self.user_menu.replace(this.$element.find('.oe_user_menu_placeholder'));
994 self.user_menu.on_menu_logout.add(this.proxy('on_logout'));
995 self.user_menu.on_action.add(this.proxy('on_menu_action'));
996 self.user_menu.do_update();
997 self.bind_hashchange();
998 if (!self.session.openerp_entreprise) {
999 var version_label = _t("OpenERP - Unsupported/Community Version");
1000 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));
1004 destroy_content: function() {
1005 _.each(_.clone(this.getChildren()), function(el) {
1008 this.$element.children().remove();
1010 do_reload: function() {
1012 return this.session.session_reload().pipe(function () {
1013 instance.connection.load_modules(true).pipe(
1014 self.menu.proxy('do_reload')); });
1017 do_notify: function() {
1018 var n = this.notification;
1019 n.notify.apply(n, arguments);
1021 do_warn: function() {
1022 var n = this.notification;
1023 n.warn.apply(n, arguments);
1025 on_logout: function() {
1027 this.session.session_logout().then(function () {
1028 $(window).unbind('hashchange', self.on_hashchange);
1029 self.do_push_state({});
1030 //would be cool to be able to do this, but I think it will make addons do strange things
1031 //this.show_login();
1032 window.location.reload();
1035 bind_hashchange: function() {
1037 $(window).bind('hashchange', this.on_hashchange);
1039 var state = $.bbq.getState(true);
1040 if (! _.isEmpty(state)) {
1041 $(window).trigger('hashchange');
1043 self.menu.has_been_loaded.then(function() {
1044 var first_menu_id = self.menu.$element.find("a:first").data("menu");
1046 self.menu.menu_click(first_menu_id);
1051 on_hashchange: function(event) {
1053 var state = event.getState(true);
1054 if (!_.isEqual(this._current_state, state)) {
1055 if(state.action_id === undefined && state.menu_id) {
1056 self.menu.has_been_loaded.then(function() {
1057 self.menu.do_reload().then(function() {
1058 self.menu.menu_click(state.menu_id)
1062 this.action_manager.do_load_state(state, !!this._current_state);
1065 this._current_state = state;
1067 do_push_state: function(state) {
1068 this.set_title(state.title);
1070 var url = '#' + $.param(state);
1071 this._current_state = _.clone(state);
1072 $.bbq.pushState(url);
1074 on_menu_action: function(options) {
1076 this.rpc("/web/action/load", { action_id: options.action_id })
1077 .then(function (result) {
1078 var action = result.result;
1079 if (options.needaction) {
1080 action.context.search_default_needaction_pending = true;
1082 self.action_manager.clear_breadcrumbs();
1083 self.action_manager.do_action(action);
1086 do_action: function(action) {
1088 // TODO replace by client action menuclick
1089 if(action.menu_id) {
1090 this.do_reload().then(function () {
1091 self.menu.menu_click(action.menu_id);
1095 set_content_full_screen: function(fullscreen) {
1097 $(".oe_webclient", this.$element).addClass("oe_content_full_screen");
1098 $("body").css({'overflow-y':'hidden'});
1100 $(".oe_webclient", this.$element).removeClass("oe_content_full_screen");
1101 $("body").css({'overflow-y':'scroll'});
1106 instance.web.EmbeddedClient = instance.web.Client.extend({
1107 _template: 'EmbedClient',
1108 init: function(parent, origin, dbname, login, key, action_id, options) {
1109 this._super(parent, origin);
1111 this.dbname = dbname;
1114 this.action_id = action_id;
1115 this.options = options || {};
1119 return $.when(this._super()).pipe(function() {
1120 return instance.connection.session_authenticate(self.dbname, self.login, self.key, true).pipe(function() {
1121 return self.rpc("/web/action/load", { action_id: self.action_id }, function(result) {
1122 var action = result.result;
1123 action.flags = _.extend({
1124 //views_switcher : false,
1125 search_view : false,
1126 action_buttons : false,
1129 }, self.options, action.flags || {});
1131 self.action_manager.do_action(action);
1138 instance.web.embed = function (origin, dbname, login, key, action, options) {
1139 $('head').append($('<link>', {
1140 'rel': 'stylesheet',
1142 'href': origin +'/web/webclient/css'
1144 var currentScript = document.currentScript;
1145 if (!currentScript) {
1146 var sc = document.getElementsByTagName('script');
1147 currentScript = sc[sc.length-1];
1149 var client = new instance.web.EmbeddedClient(null, origin, dbname, login, key, action, options);
1150 client.insertAfter(currentScript);
1155 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: