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) {
61 this.$element = content instanceof $ ? content : $(content);
63 this.dialog_options = {
65 destroy_on_close: true,
71 max_height: this.get_height('100%') - 140,
73 position: [false, 50],
75 beforeClose: function () { self.on_close(); },
76 resizeStop: this.on_resized
79 if (f.substr(0, 10) == 'on_button_') {
80 this.dialog_options.buttons[f.substr(10)] = this[f];
84 _.extend(this.dialog_options, options);
86 if (this.dialog_options.autoOpen) {
89 instance.web.dialog(this.$element, this.get_options());
92 get_options: function(options) {
94 o = _.extend({}, this.dialog_options, options || {});
95 _.each(['width', 'height'], function(unit) {
96 o[unit] = self['get_' + unit](o[unit]);
97 o['min_' + unit] = self['get_' + unit](o['min_' + unit] || 0);
98 o['max_' + unit] = self['get_' + unit](o['max_' + unit] || 0);
99 if (o[unit] !== 'auto' && o['min_' + unit] && o[unit] < o['min_' + unit]) o[unit] = o['min_' + unit];
100 if (o[unit] !== 'auto' && o['max_' + unit] && o[unit] > o['max_' + unit]) o[unit] = o['max_' + unit];
102 if (!o.title && this.dialog_title) {
103 o.title = this.dialog_title;
107 get_width: function(val) {
108 return this.get_size(val.toString(), $(window.top).width());
110 get_height: function(val) {
111 return this.get_size(val.toString(), $(window.top).height());
113 get_size: function(val, available_size) {
114 if (val === 'auto') {
116 } else if (val.slice(-1) == "%") {
117 return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
119 return parseInt(val, 10);
122 open: function(options) {
123 // TODO fme: bind window on resize
125 this.$element.html(this.renderElement());
127 var o = this.get_options(options);
128 instance.web.dialog(this.$element, o).dialog('open');
129 if (o.height === 'auto' && o.max_height) {
130 this.$element.css({ 'max-height': o.max_height, 'overflow-y': 'auto' });
135 this.$element.dialog('close');
137 on_close: function() {
138 if (this.__tmp_dialog_destroying)
140 if (this.dialog_options.destroy_on_close) {
141 this.__tmp_dialog_closing = true;
143 this.__tmp_dialog_closing = undefined;
146 on_resized: function() {
148 destroy: function () {
149 _.each(this.getChildren(), function(el) {
152 if (! this.__tmp_dialog_closing) {
153 this.__tmp_dialog_destroying = true;
155 this.__tmp_dialog_destroying = undefined;
157 if (! this.isDestroyed()) {
158 this.$element.dialog('destroy');
164 instance.web.CrashManager = instance.web.CallbackEnabled.extend({
165 on_rpc_error: function(error) {
166 if (error.data.fault_code) {
167 var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
168 if (split.length > 1) {
169 error.type = split.shift();
170 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
173 if (error.code === 200 && error.type) {
174 this.on_managed_error(error);
176 this.on_traceback(error);
179 on_managed_error: function(error) {
180 instance.web.dialog($('<div>' + QWeb.render('CrashManager.warning', {error: error}) + '</div>'), {
181 title: "OpenERP " + _.str.capitalize(error.type),
183 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
187 on_traceback: function(error) {
190 if (instance.connection.openerp_entreprise) {
191 buttons[_t("Send OpenERP Enterprise Report")] = function() {
193 var issuename = $('#issuename').val();
194 var explanation = $('#explanation').val();
195 var remark = $('#remark').val();
196 // Call the send method from server to send mail with details
197 new instance.web.DataSet(self, 'publisher_warranty.contract').call_and_eval('send', [error.data,explanation,remark,issuename]).then(function(result){
198 if (result === false) {
199 alert('There was a communication error.');
201 $this.dialog('close');
205 buttons[_t("Dont send")] = function() {
206 $(this).dialog("close");
209 buttons[_t("Ok")] = function() {
210 $(this).dialog("close");
213 var dialog = new instance.web.Dialog(this, {
214 title: "OpenERP " + _.str.capitalize(error.type),
221 dialog.$element.html(QWeb.render('CrashManager.error', {session: instance.connection, error: error}));
223 on_javascript_exception: function(exception) {
225 type: _t("Client Error"),
232 instance.web.Loading = instance.web.Widget.extend({
234 init: function(parent) {
237 this.blocked_ui = false;
239 this.request_call = function() {
240 self.on_rpc_event(1);
242 this.response_call = function() {
243 self.on_rpc_event(-1);
245 this.session.on_rpc_request.add_first(this.request_call);
246 this.session.on_rpc_response.add_last(this.response_call);
248 destroy: function() {
249 this.session.on_rpc_request.remove(this.request_call);
250 this.session.on_rpc_response.remove(this.response_call);
251 this.on_rpc_event(-this.count);
254 on_rpc_event : function(increment) {
256 if (!this.count && increment === 1) {
258 this.long_running_timer = setTimeout(function () {
259 self.blocked_ui = true;
260 instance.web.blockUI();
264 this.count += increment;
265 if (this.count > 0) {
266 if (instance.connection.debug) {
267 this.$element.text(_.str.sprintf( _t("Loading (%d)"), this.count));
269 this.$element.text(_t("Loading"));
271 this.$element.show();
272 this.getParent().$element.addClass('oe_wait');
275 clearTimeout(this.long_running_timer);
276 // Don't unblock if blocked by somebody else
277 if (self.blocked_ui) {
278 this.blocked_ui = false;
279 instance.web.unblockUI();
281 this.$element.fadeOut();
282 this.getParent().$element.removeClass('oe_wait');
287 instance.web.DatabaseManager = instance.web.Widget.extend({
288 init: function(parent) {
290 this.unblockUIFunction = instance.web.unblockUI;
291 $.validator.addMethod('matches', function (s, _, re) {
292 return new RegExp(re).test(s);
293 }, _t("Invalid database name"));
297 var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
299 self.db_list = result.db_list;
305 var fetch_langs = this.rpc("/web/session/get_lang_list", {}).then(function(result) {
306 self.lang_list = result.lang_list;
308 return $.when(fetch_db, fetch_langs).then(self.do_render);
310 do_render: function() {
312 self.$element.html(QWeb.render("DatabaseManager", { widget : self }));
313 self.$element.find(".oe_database_manager_menu").tabs({
314 show: function(event, ui) {
315 $('*[autofocus]:first', ui.panel).focus();
318 self.$element.find("form[name=create_db_form]").validate({ submitHandler: self.do_create });
319 self.$element.find("form[name=drop_db_form]").validate({ submitHandler: self.do_drop });
320 self.$element.find("form[name=backup_db_form]").validate({ submitHandler: self.do_backup });
321 self.$element.find("form[name=restore_db_form]").validate({ submitHandler: self.do_restore });
322 self.$element.find("form[name=change_pwd_form]").validate({
324 old_pwd: "Please enter your previous password",
325 new_pwd: "Please enter your new password",
327 required: "Please confirm your new password",
328 equalTo: "The confirmation does not match the password"
331 submitHandler: self.do_change_password
333 self.$element.find("#back_to_login").click(self.do_exit);
335 destroy: function () {
336 this.$element.find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login').unbind('click').end().empty();
340 * Converts a .serializeArray() result into a dict. Does not bother folding
341 * multiple identical keys into an array, last key wins.
343 * @param {Array} array
345 to_object: function (array) {
347 _(array).each(function (record) {
348 result[record.name] = record.value;
353 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
354 * from unblocking the UI
356 blockUI: function () {
357 instance.web.blockUI();
358 instance.web.unblockUI = function () {};
361 * Reinstates $.unblockUI so third parties can play with blockUI, and
364 unblockUI: function () {
365 instance.web.unblockUI = this.unblockUIFunction;
366 instance.web.unblockUI();
369 * Displays an error dialog resulting from the various RPC communications
370 * failing over themselves
372 * @param {Object} error error description
373 * @param {String} error.title title of the error dialog
374 * @param {String} error.error message of the error dialog
376 display_error: function (error) {
377 return instance.web.dialog($('<div>'), {
381 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
383 }).html(error.error);
385 do_create: function(form) {
387 var fields = $(form).serializeArray();
388 self.rpc("/web/database/create", {'fields': fields}, function(result) {
389 var form_obj = self.to_object(fields);
390 self.getParent().do_login( form_obj['db_name'], 'admin', form_obj['create_admin_pwd']);
395 do_drop: function(form) {
398 fields = $form.serializeArray(),
399 $db_list = $form.find('[name=drop_db]'),
401 if (!db || !confirm("Do you really want to delete the database: " + db + " ?")) {
404 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
406 self.display_error(result);
409 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
413 do_backup: function(form) {
416 self.session.get_file({
418 success: function () {
419 self.do_notify(_t("Backed"), _t("Database backed up successfully"));
421 error: function(error){
424 title: 'Backup Database',
425 error: 'AccessDenied'
429 complete: function() {
434 do_restore: function(form) {
438 url: '/web/database/restore',
441 success: function (body) {
442 // If empty body, everything went fine
443 if (!body) { return; }
445 if (body.indexOf('403 Forbidden') !== -1) {
447 title: 'Access Denied',
448 error: 'Incorrect super-administrator password'
452 title: 'Restore Database',
453 error: 'Could not restore the database'
457 complete: function() {
459 self.do_notify(_t("Restored"), _t("Database restored successfully"));
463 do_change_password: function(form) {
465 self.rpc("/web/database/change_password", {
466 'fields': $(form).serializeArray()
467 }, function(result) {
469 self.display_error(result);
472 self.do_notify("Changed Password", "Password has been changed successfully");
475 do_exit: function () {
479 instance.web.Login = instance.web.Widget.extend({
481 remember_credentials: true,
484 init: function(parent) {
486 this.has_local_storage = typeof(localStorage) != 'undefined';
487 this.selected_db = null;
488 this.selected_login = null;
490 if (this.has_local_storage && this.remember_credentials) {
491 this.selected_db = localStorage.getItem('last_db_login_success');
492 this.selected_login = localStorage.getItem('last_login_login_success');
493 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
494 this.selected_password = localStorage.getItem('last_password_login_success');
498 open_db_manager: function(){
500 self.$element.find('.oe_login_bottom').hide();
501 self.$element.find('.oe_login_pane').hide();
502 self.databasemanager = new instance.web.DatabaseManager(self);
503 self.databasemanager.appendTo(self.$element);
504 self.databasemanager.do_exit.add_last(function() {
505 self.databasemanager.destroy();
506 self.$element.find('.oe_login_bottom').show();
507 self.$element.find('.oe_login_pane').show();
508 self.load_db_list(true).then(self.on_db_list_loaded);
513 self.$element.find("form").submit(self.on_submit);
514 self.$element.find('.oe_login_manage_db').click(function() {
515 self.open_db_manager();
517 return self.load_db_list().then(self.on_db_list_loaded);
519 load_db_list: function (force) {
520 var d = $.when(), self = this;
521 if (_.isNull(this._db_list) || force) {
522 d = self.rpc("/web/database/get_list", {}, function(result) {
523 self._db_list = _.clone(result.db_list);
524 }, function(error, event) {
525 if (error.data.fault_code === 'AccessDenied') {
526 event.preventDefault();
532 on_db_list_loaded: function () {
534 var list = this._db_list;
535 var dbdiv = this.$element.find('div.oe_login_dbpane');
536 this.$element.find("[name=db]").replaceWith(instance.web.qweb.render('Login.dblist', { db_list: list, selected_db: this.selected_db}));
537 if(list.length === 0) {
538 self.open_db_manager();
539 } else if(list && list.length === 1) {
545 on_submit: function(ev) {
549 var $e = this.$element;
550 var db = $e.find("form [name=db]").val();
552 this.do_warn("Login", "No database selected !");
555 var login = $e.find("form input[name=login]").val();
556 var password = $e.find("form input[name=password]").val();
558 this.do_login(db, login, password);
561 * Performs actual login operation, and UI-related stuff
563 * @param {String} db database to log in
564 * @param {String} login user login
565 * @param {String} password user password
567 do_login: function (db, login, password) {
569 this.$element.removeClass('oe_login_invalid');
570 self.$(".oe_login_pane").fadeOut("slow");
571 return this.session.session_authenticate(db, login, password).pipe(function() {
572 if (self.has_local_storage) {
573 if(self.remember_credentials) {
574 localStorage.setItem('last_db_login_success', db);
575 localStorage.setItem('last_login_login_success', login);
576 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
577 localStorage.setItem('last_password_login_success', password);
580 localStorage.setItem('last_db_login_success', '');
581 localStorage.setItem('last_login_login_success', '');
582 localStorage.setItem('last_password_login_success', '');
585 self.trigger("login");
587 self.$(".oe_login_pane").fadeIn("fast");
588 self.$element.addClass("oe_login_invalid");
593 instance.web.Menu = instance.web.Widget.extend({
596 this._super.apply(this, arguments);
597 this.has_been_loaded = $.Deferred();
598 this.maximum_visible_links = 'auto'; // # of menu to show. 0 = do not crop, 'auto' = algo
599 this.data = {data:{children:[]}};
602 this._super.apply(this, arguments);
603 this.$secondary_menus = this.getParent().$element.find('.oe_secondary_menus_container');
604 this.$secondary_menus.on('click', 'a[data-menu]', this.on_menu_click);
605 return this.do_reload();
607 do_reload: function() {
608 return this.rpc("/web/menu/load", {}).then(this.on_loaded);
610 on_loaded: function(data) {
613 this.renderElement();
614 this.limit_entries();
615 this.$secondary_menus.html(QWeb.render("Menu.secondary", { widget : this }));
616 this.$element.on('click', 'a[data-menu]', this.on_menu_click);
617 // Hide second level submenus
618 this.$secondary_menus.find('.oe_menu_toggler').siblings('.oe_secondary_submenu').hide();
619 if (self.current_menu) {
620 self.open_menu(self.current_menu);
622 this.has_been_loaded.resolve();
624 limit_entries: function() {
625 var maximum_visible_links = this.maximum_visible_links;
626 if (maximum_visible_links === 'auto') {
627 maximum_visible_links = this.auto_limit_entries();
629 if (maximum_visible_links < this.data.data.children.length) {
630 var $more = $(QWeb.render('Menu.more')),
631 $index = this.$element.find('li').eq(maximum_visible_links - 1);
633 $more.find('.oe_menu_more').append($index.next().nextAll());
636 auto_limit_entries: function() {
637 // TODO: auto detect overflow and bind window on resize
638 var width = $(window).width();
639 return Math.floor(width / 125);
642 * Opens a given menu by id, as if a user had browsed to that menu by hand
643 * except does not trigger any event on the way
645 * @param {Number} id database id of the terminal menu to select
647 open_menu: function (id) {
648 var $clicked_menu, $sub_menu, $main_menu;
649 $clicked_menu = this.$element.add(this.$secondary_menus).find('a[data-menu=' + id + ']');
650 this.trigger('open_menu', id, $clicked_menu);
652 if (this.$secondary_menus.has($clicked_menu).length) {
653 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
654 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
656 $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
657 $main_menu = $clicked_menu;
660 // Activate current main menu
661 this.$element.find('.oe_active').removeClass('oe_active');
662 $main_menu.addClass('oe_active');
664 // Show current sub menu
665 this.$secondary_menus.find('.oe_secondary_menu').hide();
668 // Hide/Show the leftbar menu depending of the presence of sub-items
669 this.$secondary_menus.parent('.oe_leftbar').toggle(!!$sub_menu.children().length);
671 // Activate current menu item and show parents
672 this.$secondary_menus.find('.oe_active').removeClass('oe_active');
673 if ($main_menu !== $clicked_menu) {
674 $clicked_menu.parents().show();
675 if ($clicked_menu.is('.oe_menu_toggler')) {
676 $clicked_menu.toggleClass('oe_menu_opened').siblings('.oe_secondary_submenu:first').toggle();
678 $clicked_menu.parent().addClass('oe_active');
683 * Call open_menu with the first menu_item matching an action_id
685 * @param {Number} id the action_id to match
687 open_action: function (id) {
688 var $menu = this.$element.add(this.$secondary_menus).find('a[data-action-id="' + id + '"]');
689 var menu_id = $menu.data('menu');
691 this.open_menu(menu_id);
695 * Process a click on a menu item
697 * @param {Number} id the menu_id
698 * @param {Boolean} [needaction=false] whether the triggered action should execute in a `needs action` context
700 menu_click: function(id, needaction) {
703 // find back the menuitem in dom to get the action
704 var $item = this.$element.find('a[data-menu=' + id + ']');
706 $item = this.$secondary_menus.find('a[data-menu=' + id + ']');
708 var action_id = $item.data('action-id');
709 // If first level menu doesnt have action trigger first leaf
711 if(this.$element.has($item).length) {
712 var $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + id + ']');
713 var $items = $sub_menu.find('a[data-action-id]').filter('[data-action-id!=""]');
715 action_id = $items.data('action-id');
716 id = $items.data('menu');
721 this.current_menu = id;
722 this.session.active_id = id;
724 this.trigger('menu_click', {
725 action_id: action_id,
726 needaction: needaction,
732 * Jquery event handler for menu click
734 * @param {Event} ev the jquery event
736 on_menu_click: function(ev) {
738 var needaction = $(ev.target).is('div.oe_menu_counter');
739 this.menu_click($(ev.currentTarget).data('menu'), needaction);
743 instance.web.UserMenu = instance.web.Widget.extend({
744 template: "UserMenu",
745 init: function(parent) {
747 this.update_promise = $.Deferred().resolve();
751 this._super.apply(this, arguments);
752 this.$element.on('click', '.oe_dropdown_menu li a[data-menu]', function(ev) {
754 var f = self['on_menu_' + $(this).data('menu')];
760 change_password :function() {
762 this.dialog = new instance.web.Dialog(this, {
763 title: _t("Change Password"),
766 this.dialog.$element.html(QWeb.render("UserMenu.password", self));
767 this.dialog.$element.find("form[name=change_password_form]").validate({
768 submitHandler: function (form) {
769 self.rpc("/web/session/change_password",{
770 'fields': $(form).serializeArray()
771 }, function(result) {
773 self.display_error(result);
776 instance.webclient.on_logout();
782 display_error: function (error) {
783 return instance.web.dialog($('<div>'), {
787 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
789 }).html(error.error);
791 do_update: function () {
793 var fct = function() {
794 var $avatar = self.$element.find('.oe_topbar_avatar');
795 $avatar.attr('src', $avatar.data('default-src'));
796 if (!self.session.uid)
798 var func = new instance.web.Model("res.users").get_func("read");
799 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
800 var topbar_name = res.name;
801 if(instance.connection.debug)
802 topbar_name = _.str.sprintf("%s (%s)", topbar_name, instance.connection.db);
803 if(res.company_id[0] > 1)
804 topbar_name = _.str.sprintf("%s (%s)", topbar_name, res.company_id[1]);
805 self.$element.find('.oe_topbar_name').text(topbar_name);
806 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);
807 $avatar.attr('src', avatar_src);
810 this.update_promise = this.update_promise.pipe(fct, fct);
812 on_action: function() {
814 on_menu_logout: function() {
816 on_menu_settings: function() {
818 var action_manager = new instance.web.ActionManager(this);
819 var dataset = new instance.web.DataSet (this,'res.users',this.context);
820 dataset.call ('action_get','',function (result){
821 self.rpc('/web/action/load', {action_id:result}, function(result){
822 action_manager.do_action(_.extend(result['result'], {
824 res_id: self.session.uid,
825 res_model: 'res.users',
827 action_buttons: false,
830 views_switcher: false,
836 this.dialog = new instance.web.Dialog(this,{
837 title: _t("Preferences"),
840 {text: _t("Change password"), click: function(){ self.change_password(); }},
841 {text: _t("Cancel"), click: function(){ $(this).dialog('destroy'); }},
842 {text: _t("Save"), click: function(){
843 var inner_widget = action_manager.inner_widget;
844 inner_widget.views[inner_widget.active_view].controller.do_save()
846 self.dialog.destroy();
847 // needs to refresh interface in case language changed
848 window.location.reload();
854 action_manager.appendTo(this.dialog.$element);
855 action_manager.renderElement(this.dialog);
857 on_menu_about: function() {
859 self.rpc("/web/webclient/version_info", {}).then(function(res) {
860 var $help = $(QWeb.render("UserMenu.about", {version_info: res}));
861 $help.find('a.oe_activate_debug_mode').click(function (e) {
863 window.location = $.param.querystring(
864 window.location.href, 'debug');
866 instance.web.dialog($help, {autoOpen: true,
867 modal: true, width: 580, height: 290, resizable: false, title: _t("About")});
872 instance.web.Client = instance.web.Widget.extend({
873 init: function(parent, origin) {
874 instance.client = instance.webclient = this;
876 this.origin = origin;
880 return instance.connection.session_bind(this.origin).then(function() {
881 var $e = $(QWeb.render(self._template, {}));
882 self.$element.replaceWith($e);
888 bind_events: function() {
890 this.$element.on('mouseenter', '.oe_systray > div:not([data-tipsy=true])', function() {
891 $(this).attr('data-tipsy', 'true').tipsy().trigger('mouseenter');
893 this.$element.on('click', '.oe_dropdown_toggle', function(ev) {
895 var $toggle = $(this);
896 var $menu = $toggle.find('.oe_dropdown_menu');
897 var state = $menu.is('.oe_opened');
898 setTimeout(function() {
899 // Do not alter propagation
900 $toggle.add($menu).toggleClass('oe_opened', !state);
902 // Move $menu if outside window's edge
903 var doc_width = $(document).width();
904 var offset = $menu.offset();
905 var menu_width = $menu.width();
906 var x = doc_width - offset.left - menu_width - 2;
908 $menu.offset({ left: offset.left + x }).width(menu_width);
913 instance.web.bus.on('click', this, function() {
914 self.$element.find('.oe_dropdown_menu.oe_opened').removeClass('oe_opened');
917 show_common: function() {
919 this.crashmanager = new instance.web.CrashManager();
920 instance.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
921 self.notification = new instance.web.Notification(this);
922 self.notification.appendTo(self.$element);
923 self.loading = new instance.web.Loading(self);
924 self.loading.appendTo(self.$element);
925 self.action_manager = new instance.web.ActionManager(self);
926 self.action_manager.appendTo(self.$('.oe_application'));
930 instance.web.WebClient = instance.web.Client.extend({
931 _template: 'WebClient',
932 init: function(parent) {
934 this._current_state = null;
938 return $.when(this._super()).pipe(function() {
939 if (jQuery.param !== undefined && jQuery.deparam(jQuery.param.querystring()).kitten !== undefined) {
940 $("body").addClass("kitten-mode-activated");
942 $.blockUI.defaults.message = '<img src="http://www.amigrave.com/kitten.gif">';
945 if (!self.session.session_is_valid()) {
948 self.show_application();
952 set_title: function(title) {
953 title = _.str.clean(title);
954 var sep = _.isEmpty(title) ? '' : ' - ';
955 document.title = title + sep + 'OpenERP';
957 show_common: function() {
960 window.onerror = function (message, file, line) {
961 self.crashmanager.on_traceback({
962 type: _t("Client Error"),
964 data: {debug: file + ':' + line}
967 // TODO: deprecate and use login client action
968 self.login = new instance.web.Login(self);
969 self.login.on("login",self,self.show_application);
971 show_login: function() {
973 self.$('.oe_topbar').hide();
974 self.login.appendTo(self.$element);
976 show_application: function() {
978 self.$('.oe_topbar').show();
979 self.login.$element.hide();
980 self.menu = new instance.web.Menu(self);
981 self.menu.replace(this.$element.find('.oe_menu_placeholder'));
982 self.menu.on('menu_click', this, this.on_menu_action);
983 self.user_menu = new instance.web.UserMenu(self);
984 self.user_menu.replace(this.$element.find('.oe_user_menu_placeholder'));
985 self.user_menu.on_menu_logout.add(this.proxy('on_logout'));
986 self.user_menu.on_action.add(this.proxy('on_menu_action'));
987 self.user_menu.do_update();
988 self.bind_hashchange();
989 if (!self.session.openerp_entreprise) {
990 var version_label = _t("OpenERP - Unsupported/Community Version");
991 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));
995 destroy_content: function() {
996 _.each(_.clone(this.getChildren()), function(el) {
999 this.$element.children().remove();
1001 do_reload: function() {
1003 return this.session.session_reload().pipe(function () {
1004 instance.connection.load_modules(true).pipe(
1005 self.menu.proxy('do_reload')); });
1008 do_notify: function() {
1009 var n = this.notification;
1010 n.notify.apply(n, arguments);
1012 do_warn: function() {
1013 var n = this.notification;
1014 n.warn.apply(n, arguments);
1016 on_logout: function() {
1018 this.session.session_logout().then(function () {
1019 $(window).unbind('hashchange', self.on_hashchange);
1020 self.do_push_state({});
1021 //would be cool to be able to do this, but I think it will make addons do strange things
1022 //this.show_login();
1023 window.location.reload();
1026 bind_hashchange: function() {
1028 $(window).bind('hashchange', this.on_hashchange);
1030 var state = $.bbq.getState(true);
1031 if (! _.isEmpty(state)) {
1032 $(window).trigger('hashchange');
1034 self.menu.has_been_loaded.then(function() {
1035 var first_menu_id = self.menu.$element.find("a:first").data("menu");
1037 self.menu.menu_click(first_menu_id);
1042 on_hashchange: function(event) {
1044 var state = event.getState(true);
1045 if (!_.isEqual(this._current_state, state)) {
1046 if(state.action_id === undefined && state.menu_id) {
1047 self.menu.has_been_loaded.then(function() {
1048 self.menu.do_reload().then(function() {
1049 self.menu.menu_click(state.menu_id)
1053 this.action_manager.do_load_state(state, !!this._current_state);
1056 this._current_state = state;
1058 do_push_state: function(state) {
1059 this.set_title(state.title);
1060 var url = '#' + $.param(state);
1061 this._current_state = _.clone(state);
1062 $.bbq.pushState(url);
1064 on_menu_action: function(options) {
1066 this.rpc("/web/action/load", { action_id: options.action_id })
1067 .then(function (result) {
1068 var action = result.result;
1069 if (options.needaction) {
1070 action.context.search_default_needaction_pending = true;
1072 self.action_manager.clear_breadcrumbs();
1073 self.action_manager.do_action(action);
1076 do_action: function(action) {
1078 // TODO replace by client action menuclick
1079 if(action.menu_id) {
1080 this.do_reload().then(function () {
1081 self.menu.menu_click(action.menu_id);
1085 set_content_full_screen: function(fullscreen) {
1087 $(".oe_webclient", this.$element).addClass("oe_content_full_screen");
1089 $(".oe_webclient", this.$element).removeClass("oe_content_full_screen");
1093 instance.web.EmbeddedClient = instance.web.Client.extend({
1094 _template: 'EmbedClient',
1095 init: function(parent, origin, dbname, login, key, action_id, options) {
1096 this._super(parent, origin);
1098 this.dbname = dbname;
1101 this.action_id = action_id;
1102 this.options = options || {};
1106 return $.when(this._super()).pipe(function() {
1107 return instance.connection.session_authenticate(self.dbname, self.login, self.key, true).pipe(function() {
1108 return self.rpc("/web/action/load", { action_id: self.action_id }, function(result) {
1109 var action = result.result;
1110 action.flags = _.extend({
1111 //views_switcher : false,
1112 search_view : false,
1113 action_buttons : false,
1116 }, self.options, action.flags || {});
1118 self.action_manager.do_action(action);
1125 instance.web.embed = function (origin, dbname, login, key, action, options) {
1126 $('head').append($('<link>', {
1127 'rel': 'stylesheet',
1129 'href': origin +'/web/webclient/css'
1131 var currentScript = document.currentScript;
1132 if (!currentScript) {
1133 var sc = document.getElementsByTagName('script');
1134 currentScript = sc[sc.length-1];
1136 var client = new instance.web.EmbeddedClient(null, origin, dbname, login, key, action, options);
1137 client.insertAfter(currentScript);
1142 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: