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 template: "DatabaseManager",
295 init: function(parent) {
297 this.unblockUIFunction = instance.web.unblockUI;
298 $.validator.addMethod('matches', function (s, _, re) {
299 return new RegExp(re).test(s);
300 }, _t("Invalid database name"));
304 $('.oe_secondary_menus_container,.oe_user_menu_placeholder').empty();
305 var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
307 self.db_list = result.db_list;
313 var fetch_langs = this.rpc("/web/session/get_lang_list", {}).then(function(result) {
314 self.lang_list = result.lang_list;
316 return $.when(fetch_db, fetch_langs).then(self.do_render);
318 do_render: function() {
320 $('.oe_topbar,.oe_leftbar').show();
321 $('.oe_user_menu_placeholder').append(QWeb.render("DatabaseManager.user_menu",{ widget : self }));
322 $('.oe_secondary_menus_container').append(QWeb.render("DatabaseManager.menu",{ widget : self }));
323 $('ul.oe_secondary_submenu > li:first').addClass('oe_active')
324 $('ul.oe_secondary_submenu > li').bind('click', function (event) {
325 var menuitem = $(this);
326 menuitem.addClass('oe_active').siblings().removeClass('oe_active');
327 var form_id =menuitem.find('a').attr('href');
328 $(form_id).show().siblings().hide();
329 event.preventDefault();
331 $('#back-to-login').click(self.do_exit);
332 self.$element.find("td").addClass("oe_form_group_cell");
333 self.$element.find("tr td:first-child").addClass("oe_form_group_cell_label");
334 self.$element.find("label").addClass("oe_form_label");
335 self.$element.find("form[name=create_db_form]").validate({ submitHandler: self.do_create });
336 self.$element.find("form[name=drop_db_form]").validate({ submitHandler: self.do_drop });
337 self.$element.find("form[name=backup_db_form]").validate({ submitHandler: self.do_backup });
338 self.$element.find("form[name=restore_db_form]").validate({ submitHandler: self.do_restore });
339 self.$element.find("form[name=change_pwd_form]").validate({
341 old_pwd: "Please enter your previous password",
342 new_pwd: "Please enter your new password",
344 required: "Please confirm your new password",
345 equalTo: "The confirmation does not match the password"
348 submitHandler: self.do_change_password
351 destroy: function () {
352 this.$element.find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login').unbind('click').end().empty();
356 * Converts a .serializeArray() result into a dict. Does not bother folding
357 * multiple identical keys into an array, last key wins.
359 * @param {Array} array
361 to_object: function (array) {
363 _(array).each(function (record) {
364 result[record.name] = record.value;
369 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
370 * from unblocking the UI
372 blockUI: function () {
373 instance.web.blockUI();
374 instance.web.unblockUI = function () {};
377 * Reinstates $.unblockUI so third parties can play with blockUI, and
380 unblockUI: function () {
381 instance.web.unblockUI = this.unblockUIFunction;
382 instance.web.unblockUI();
385 * Displays an error dialog resulting from the various RPC communications
386 * failing over themselves
388 * @param {Object} error error description
389 * @param {String} error.title title of the error dialog
390 * @param {String} error.error message of the error dialog
392 display_error: function (error) {
393 return instance.web.dialog($('<div>'), {
397 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
399 }).html(error.error);
401 do_create: function(form) {
403 var fields = $(form).serializeArray();
404 self.rpc("/web/database/create", {'fields': fields}, function(result) {
405 var form_obj = self.to_object(fields);
406 var client_action = {
407 type: 'ir.actions.client',
410 'db': form_obj['db_name'],
412 'password': form_obj['create_admin_pwd'],
413 'login_successful': function() {
414 instance.webclient.show_application();
418 self.do_action(client_action);
421 do_drop: function(form) {
424 fields = $form.serializeArray(),
425 $db_list = $form.find('[name=drop_db]'),
427 if (!db || !confirm("Do you really want to delete the database: " + db + " ?")) {
430 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
432 self.display_error(result);
435 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
439 do_backup: function(form) {
442 self.session.get_file({
444 success: function () {
445 self.do_notify(_t("Backed"), _t("Database backed up successfully"));
447 error: function(error){
450 title: 'Backup Database',
451 error: 'AccessDenied'
455 complete: function() {
460 do_restore: function(form) {
464 url: '/web/database/restore',
467 success: function (body) {
468 // If empty body, everything went fine
469 if (!body) { return; }
471 if (body.indexOf('403 Forbidden') !== -1) {
473 title: 'Access Denied',
474 error: 'Incorrect super-administrator password'
478 title: 'Restore Database',
479 error: 'Could not restore the database'
483 complete: function() {
485 self.do_notify(_t("Restored"), _t("Database restored successfully"));
489 do_change_password: function(form) {
491 self.rpc("/web/database/change_password", {
492 'fields': $(form).serializeArray()
493 }, function(result) {
495 self.display_error(result);
499 self.do_notify("Changed Password", "Password has been changed successfully");
502 do_exit: function () {
503 this.$element.remove();
504 $('.oe_topbar,.oe_leftbar').hide();
505 this.do_action('login');
508 instance.web.client_actions.add("database_manager", "instance.web.DatabaseManager");
510 instance.web.Login = instance.web.Widget.extend({
512 remember_credentials: true,
515 init: function(parent, params) {
517 this.has_local_storage = typeof(localStorage) != 'undefined';
518 this.selected_db = null;
519 this.selected_login = null;
520 this.params = params || {};
522 if (this.params.login_successful) {
523 this.on('login_successful', this, this.params.login_successful);
526 if (this.has_local_storage && this.remember_credentials) {
527 this.selected_db = localStorage.getItem('last_db_login_success');
528 this.selected_login = localStorage.getItem('last_login_login_success');
529 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
530 this.selected_password = localStorage.getItem('last_password_login_success');
536 self.$element.find("form").submit(self.on_submit);
537 self.$element.find('.oe_login_manage_db').click(function() {
538 self.do_action("database_manager");
540 return self.load_db_list().then(self.on_db_list_loaded).then(function() {
541 if (self.params.db) {
542 self.do_login(self.params.db, self.params.login, self.params.password);
546 load_db_list: function (force) {
547 var d = $.when(), self = this;
548 if (_.isNull(this._db_list) || force) {
549 d = self.rpc("/web/database/get_list", {}, function(result) {
550 self._db_list = _.clone(result.db_list);
551 }, function(error, event) {
552 if (error.data.fault_code === 'AccessDenied') {
553 event.preventDefault();
559 on_db_list_loaded: function () {
561 var list = this._db_list;
562 var dbdiv = this.$element.find('div.oe_login_dbpane');
563 this.$element.find("[name=db]").replaceWith(instance.web.qweb.render('Login.dblist', { db_list: list, selected_db: this.selected_db}));
564 if(list.length === 0) {
565 this.do_action("database_manager");
566 } else if(list && list.length === 1) {
572 on_submit: function(ev) {
576 var $e = this.$element;
577 var db = $e.find("form [name=db]").val();
579 this.do_warn("Login", "No database selected !");
582 var login = $e.find("form input[name=login]").val();
583 var password = $e.find("form input[name=password]").val();
585 this.do_login(db, login, password);
588 * Performs actual login operation, and UI-related stuff
590 * @param {String} db database to log in
591 * @param {String} login user login
592 * @param {String} password user password
594 do_login: function (db, login, password) {
596 this.$element.removeClass('oe_login_invalid');
597 self.$(".oe_login_pane").fadeOut("slow");
598 return this.session.session_authenticate(db, login, password).pipe(function() {
599 if (self.has_local_storage) {
600 if(self.remember_credentials) {
601 localStorage.setItem('last_db_login_success', db);
602 localStorage.setItem('last_login_login_success', login);
603 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
604 localStorage.setItem('last_password_login_success', password);
607 localStorage.setItem('last_db_login_success', '');
608 localStorage.setItem('last_login_login_success', '');
609 localStorage.setItem('last_password_login_success', '');
612 self.trigger('login_successful');
614 self.$(".oe_login_pane").fadeIn("fast");
615 self.$element.addClass("oe_login_invalid");
619 this.$element.show();
622 this.$element.hide();
625 instance.web.client_actions.add("login", "instance.web.Login");
628 * Client action to reload the whole interface.
629 * If params has an entry 'menu_id', it opens the given menu entry.
631 instance.web.Reload = instance.web.Widget.extend({
632 init: function(parent, params) {
634 this.menu_id = (params && params.menu_id) || false;
637 var l = window.location;
638 var timestamp = new Date().getTime();
639 var search = "?ts=" + timestamp;
641 search = l.search + "&ts=" + timestamp;
645 hash = "#menu_id=" + this.menu_id;
647 var url = l.protocol + "//" + l.host + l.pathname + search + hash;
648 window.location = url;
651 instance.web.client_actions.add("reload", "instance.web.Reload");
654 * Client action to go back in breadcrumb history.
655 * If can't go back in history stack, will go back to home.
657 instance.web.HistoryBack = instance.web.Widget.extend({
658 init: function(parent, params) {
659 if (!parent.history_back()) {
660 window.location = '/' + (window.location.search || '');
664 instance.web.client_actions.add("history_back", "instance.web.HistoryBack");
667 * Client action to go back home.
669 instance.web.Home = instance.web.Widget.extend({
670 init: function(parent, params) {
671 window.location = '/' + (window.location.search || '');
674 instance.web.client_actions.add("home", "instance.web.Home");
676 instance.web.ChangePassword = instance.web.Widget.extend({
677 template: "ChangePassword",
680 self.$element.validate({
681 submitHandler: function (form) {
682 self.rpc("/web/session/change_password",{
683 'fields': $(form).serializeArray()
684 }, function(result) {
686 self.display_error(result);
689 instance.webclient.on_logout();
695 display_error: function (error) {
696 return instance.web.dialog($('<div>'), {
700 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
702 }).html(error.error);
705 instance.web.client_actions.add("change_password", "instance.web.ChangePassword");
707 instance.web.Menu = instance.web.Widget.extend({
710 this._super.apply(this, arguments);
711 this.has_been_loaded = $.Deferred();
712 this.maximum_visible_links = 'auto'; // # of menu to show. 0 = do not crop, 'auto' = algo
713 this.data = {data:{children:[]}};
716 this._super.apply(this, arguments);
717 this.$secondary_menus = this.getParent().$element.find('.oe_secondary_menus_container');
718 this.$secondary_menus.on('click', 'a[data-menu]', this.on_menu_click);
719 return this.do_reload();
721 do_reload: function() {
722 return this.rpc("/web/menu/load", {}).then(this.on_loaded);
724 on_loaded: function(data) {
727 this.renderElement();
728 this.limit_entries();
729 this.$secondary_menus.html(QWeb.render("Menu.secondary", { widget : this }));
730 this.$element.on('click', 'a[data-menu]', this.on_menu_click);
731 // Hide second level submenus
732 this.$secondary_menus.find('.oe_menu_toggler').siblings('.oe_secondary_submenu').hide();
733 if (self.current_menu) {
734 self.open_menu(self.current_menu);
736 this.has_been_loaded.resolve();
738 limit_entries: function() {
739 var maximum_visible_links = this.maximum_visible_links;
740 if (maximum_visible_links === 'auto') {
741 maximum_visible_links = this.auto_limit_entries();
743 if (maximum_visible_links < this.data.data.children.length) {
744 var $more = $(QWeb.render('Menu.more')),
745 $index = this.$element.find('li').eq(maximum_visible_links - 1);
747 $more.find('.oe_menu_more').append($index.next().nextAll());
750 auto_limit_entries: function() {
751 // TODO: auto detect overflow and bind window on resize
752 var width = $(window).width();
753 return Math.floor(width / 125);
756 * Opens a given menu by id, as if a user had browsed to that menu by hand
757 * except does not trigger any event on the way
759 * @param {Number} id database id of the terminal menu to select
761 open_menu: function (id) {
762 var $clicked_menu, $sub_menu, $main_menu;
763 $clicked_menu = this.$element.add(this.$secondary_menus).find('a[data-menu=' + id + ']');
764 this.trigger('open_menu', id, $clicked_menu);
766 if (this.$secondary_menus.has($clicked_menu).length) {
767 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
768 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
770 $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
771 $main_menu = $clicked_menu;
774 // Activate current main menu
775 this.$element.find('.oe_active').removeClass('oe_active');
776 $main_menu.addClass('oe_active');
778 // Show current sub menu
779 this.$secondary_menus.find('.oe_secondary_menu').hide();
782 // Hide/Show the leftbar menu depending of the presence of sub-items
783 this.$secondary_menus.parent('.oe_leftbar').toggle(!!$sub_menu.children().length);
785 // Activate current menu item and show parents
786 this.$secondary_menus.find('.oe_active').removeClass('oe_active');
787 if ($main_menu !== $clicked_menu) {
788 $clicked_menu.parents().show();
789 if ($clicked_menu.is('.oe_menu_toggler')) {
790 $clicked_menu.toggleClass('oe_menu_opened').siblings('.oe_secondary_submenu:first').toggle();
792 $clicked_menu.parent().addClass('oe_active');
797 * Call open_menu with the first menu_item matching an action_id
799 * @param {Number} id the action_id to match
801 open_action: function (id) {
802 var $menu = this.$element.add(this.$secondary_menus).find('a[data-action-id="' + id + '"]');
803 var menu_id = $menu.data('menu');
805 this.open_menu(menu_id);
809 * Process a click on a menu item
811 * @param {Number} id the menu_id
812 * @param {Boolean} [needaction=false] whether the triggered action should execute in a `needs action` context
814 menu_click: function(id, needaction) {
817 // find back the menuitem in dom to get the action
818 var $item = this.$element.find('a[data-menu=' + id + ']');
820 $item = this.$secondary_menus.find('a[data-menu=' + id + ']');
822 var action_id = $item.data('action-id');
823 // If first level menu doesnt have action trigger first leaf
825 if(this.$element.has($item).length) {
826 var $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + id + ']');
827 var $items = $sub_menu.find('a[data-action-id]').filter('[data-action-id!=""]');
829 action_id = $items.data('action-id');
830 id = $items.data('menu');
835 this.current_menu = id;
836 this.session.active_id = id;
838 this.trigger('menu_click', {
839 action_id: action_id,
840 needaction: needaction,
846 * Jquery event handler for menu click
848 * @param {Event} ev the jquery event
850 on_menu_click: function(ev) {
852 var needaction = $(ev.target).is('div.oe_menu_counter');
853 this.menu_click($(ev.currentTarget).data('menu'), needaction);
857 instance.web.UserMenu = instance.web.Widget.extend({
858 template: "UserMenu",
859 init: function(parent) {
861 this.update_promise = $.Deferred().resolve();
865 this._super.apply(this, arguments);
866 this.$element.on('click', '.oe_dropdown_menu li a[data-menu]', function(ev) {
868 var f = self['on_menu_' + $(this).data('menu')];
874 do_update: function () {
876 var fct = function() {
877 var $avatar = self.$element.find('.oe_topbar_avatar');
878 $avatar.attr('src', $avatar.data('default-src'));
879 if (!self.session.uid)
881 var func = new instance.web.Model("res.users").get_func("read");
882 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
883 var topbar_name = res.name;
884 if(instance.connection.debug)
885 topbar_name = _.str.sprintf("%s (%s)", topbar_name, instance.connection.db);
886 if(res.company_id[0] > 1)
887 topbar_name = _.str.sprintf("%s (%s)", topbar_name, res.company_id[1]);
888 self.$element.find('.oe_topbar_name').text(topbar_name);
889 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);
890 $avatar.attr('src', avatar_src);
893 this.update_promise = this.update_promise.pipe(fct, fct);
895 on_action: function() {
897 on_menu_logout: function() {
899 on_menu_settings: function() {
901 self.rpc("/web/action/load", { action_id: "base.action_res_users_my" }, function(result) {
902 result.result.res_id = instance.connection.uid;
903 self.getParent().action_manager.do_action(result.result);
906 on_menu_about: function() {
908 self.rpc("/web/webclient/version_info", {}).then(function(res) {
909 var $help = $(QWeb.render("UserMenu.about", {version_info: res}));
910 $help.find('a.oe_activate_debug_mode').click(function (e) {
912 window.location = $.param.querystring(
913 window.location.href, 'debug');
915 instance.web.dialog($help, {autoOpen: true,
916 modal: true, width: 507, height: 290, resizable: false, title: _t("About")});
921 instance.web.Client = instance.web.Widget.extend({
922 init: function(parent, origin) {
923 instance.client = instance.webclient = this;
925 this.origin = origin;
929 return instance.connection.session_bind(this.origin).pipe(function() {
930 var $e = $(QWeb.render(self._template, {}));
931 self.replaceElement($e);
933 return self.show_common();
936 bind_events: function() {
938 this.$element.on('mouseenter', '.oe_systray > div:not([data-tipsy=true])', function() {
939 $(this).attr('data-tipsy', 'true').tipsy().trigger('mouseenter');
941 this.$element.on('click', '.oe_dropdown_toggle', function(ev) {
943 var $toggle = $(this);
944 var $menu = $toggle.siblings('.oe_dropdown_menu');
945 $menu = $menu.size() >= 1 ? $menu : $toggle.find('.oe_dropdown_menu');
946 var state = $menu.is('.oe_opened');
947 setTimeout(function() {
948 // Do not alter propagation
949 $toggle.add($menu).toggleClass('oe_opened', !state);
951 // Move $menu if outside window's edge
952 var doc_width = $(document).width();
953 var offset = $menu.offset();
954 var menu_width = $menu.width();
955 var x = doc_width - offset.left - menu_width - 2;
957 $menu.offset({ left: offset.left + x }).width(menu_width);
962 instance.web.bus.on('click', this, function(ev) {
964 if (!$(ev.target).is('input[type=file]')) {
965 self.$element.find('.oe_dropdown_menu.oe_opened').removeClass('oe_opened');
969 show_common: function() {
971 this.crashmanager = new instance.web.CrashManager();
972 instance.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
973 self.notification = new instance.web.Notification(this);
974 self.notification.appendTo(self.$element);
975 self.loading = new instance.web.Loading(self);
976 self.loading.appendTo(self.$element);
977 self.action_manager = new instance.web.ActionManager(self);
978 self.action_manager.appendTo(self.$('.oe_application'));
982 instance.web.WebClient = instance.web.Client.extend({
983 _template: 'WebClient',
984 init: function(parent) {
986 this._current_state = null;
990 return $.when(this._super()).pipe(function() {
991 if (jQuery.param !== undefined && jQuery.deparam(jQuery.param.querystring()).kitten !== undefined) {
992 $("body").addClass("kitten-mode-activated");
994 $.blockUI.defaults.message = '<img src="http://www.amigrave.com/kitten.gif">';
997 if (!self.session.session_is_valid()) {
1000 self.show_application();
1004 set_title: function(title) {
1005 title = _.str.clean(title);
1006 var sep = _.isEmpty(title) ? '' : ' - ';
1007 document.title = title + sep + 'OpenERP';
1009 show_common: function() {
1012 window.onerror = function (message, file, line) {
1013 self.crashmanager.on_traceback({
1014 type: _t("Client Error"),
1016 data: {debug: file + ':' + line}
1020 show_login: function() {
1021 this.$('.oe_topbar').hide();
1022 this.action_manager.do_action("login");
1023 this.action_manager.inner_widget.on('login_successful', this, this.show_application);
1025 show_application: function() {
1027 self.$('.oe_topbar').show();
1028 self.menu = new instance.web.Menu(self);
1029 self.menu.replace(this.$element.find('.oe_menu_placeholder'));
1030 self.menu.on('menu_click', this, this.on_menu_action);
1031 self.user_menu = new instance.web.UserMenu(self);
1032 self.user_menu.replace(this.$element.find('.oe_user_menu_placeholder'));
1033 self.user_menu.on_menu_logout.add(this.proxy('on_logout'));
1034 self.user_menu.on_action.add(this.proxy('on_menu_action'));
1035 self.user_menu.do_update();
1036 self.bind_hashchange();
1037 if (!self.session.openerp_entreprise) {
1038 var version_label = _t("OpenERP - Unsupported/Community Version");
1039 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));
1043 destroy_content: function() {
1044 _.each(_.clone(this.getChildren()), function(el) {
1047 this.$element.children().remove();
1049 do_reload: function() {
1051 return this.session.session_reload().pipe(function () {
1052 instance.connection.load_modules(true).pipe(
1053 self.menu.proxy('do_reload')); });
1056 do_notify: function() {
1057 var n = this.notification;
1058 n.notify.apply(n, arguments);
1060 do_warn: function() {
1061 var n = this.notification;
1062 n.warn.apply(n, arguments);
1064 on_logout: function() {
1066 this.session.session_logout().then(function () {
1067 $(window).unbind('hashchange', self.on_hashchange);
1068 self.do_push_state({});
1069 //would be cool to be able to do this, but I think it will make addons do strange things
1070 //this.show_login();
1071 window.location.reload();
1074 bind_hashchange: function() {
1076 $(window).bind('hashchange', this.on_hashchange);
1078 var state = $.bbq.getState(true);
1079 if (_.isEmpty(state) || state.action == "login") {
1080 self.menu.has_been_loaded.then(function() {
1081 var first_menu_id = self.menu.$element.find("a:first").data("menu");
1083 self.menu.menu_click(first_menu_id);
1087 $(window).trigger('hashchange');
1090 on_hashchange: function(event) {
1092 var state = event.getState(true);
1093 if (!_.isEqual(this._current_state, state)) {
1094 if(state.action_id === undefined && state.menu_id) {
1095 self.menu.has_been_loaded.then(function() {
1096 self.menu.do_reload().then(function() {
1097 self.menu.menu_click(state.menu_id)
1101 this.action_manager.do_load_state(state, !!this._current_state);
1104 this._current_state = state;
1106 do_push_state: function(state) {
1107 this.set_title(state.title);
1109 var url = '#' + $.param(state);
1110 this._current_state = _.clone(state);
1111 $.bbq.pushState(url);
1113 on_menu_action: function(options) {
1115 this.rpc("/web/action/load", { action_id: options.action_id })
1116 .then(function (result) {
1117 var action = result.result;
1118 if (options.needaction) {
1119 action.context.search_default_needaction_pending = true;
1121 self.action_manager.clear_breadcrumbs();
1122 self.action_manager.do_action(action);
1125 do_action: function(action) {
1127 // TODO replace by client action menuclick
1128 if(action.menu_id) {
1129 this.do_reload().then(function () {
1130 self.menu.menu_click(action.menu_id);
1134 set_content_full_screen: function(fullscreen) {
1136 $(".oe_webclient", this.$element).addClass("oe_content_full_screen");
1137 $("body").css({'overflow-y':'hidden'});
1139 $(".oe_webclient", this.$element).removeClass("oe_content_full_screen");
1140 $("body").css({'overflow-y':'scroll'});
1145 instance.web.EmbeddedClient = instance.web.Client.extend({
1146 _template: 'EmbedClient',
1147 init: function(parent, origin, dbname, login, key, action_id, options) {
1148 this._super(parent, origin);
1150 this.dbname = dbname;
1153 this.action_id = action_id;
1154 this.options = options || {};
1158 return $.when(this._super()).pipe(function() {
1159 return instance.connection.session_authenticate(self.dbname, self.login, self.key, true).pipe(function() {
1160 return self.rpc("/web/action/load", { action_id: self.action_id }, function(result) {
1161 var action = result.result;
1162 action.flags = _.extend({
1163 //views_switcher : false,
1164 search_view : false,
1165 action_buttons : false,
1168 }, self.options, action.flags || {});
1170 self.action_manager.do_action(action);
1177 instance.web.embed = function (origin, dbname, login, key, action, options) {
1178 $('head').append($('<link>', {
1179 'rel': 'stylesheet',
1181 'href': origin +'/web/webclient/css'
1183 var currentScript = document.currentScript;
1184 if (!currentScript) {
1185 var sc = document.getElementsByTagName('script');
1186 currentScript = sc[sc.length-1];
1188 var client = new instance.web.EmbeddedClient(null, origin, dbname, login, key, action, options);
1189 client.insertAfter(currentScript);
1194 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: