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,
66 width: $(window).width() * (($(window).width() > 1024) ? 0.5 : 0.75),
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.dialog_options.destroy_on_close) {
142 on_resized: function() {
144 destroy: function () {
145 _.each(this.getChildren(), function(el) {
148 if (! this.isDestroyed()) {
149 this.$element.dialog('destroy');
155 instance.web.CrashManager = instance.web.CallbackEnabled.extend({
156 on_rpc_error: function(error) {
157 if (error.data.fault_code) {
158 var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
159 if (split.length > 1) {
160 error.type = split.shift();
161 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
164 if (error.code === 200 && error.type) {
165 this.on_managed_error(error);
167 this.on_traceback(error);
170 on_managed_error: function(error) {
171 instance.web.dialog($('<div>' + QWeb.render('CrashManager.warning', {error: error}) + '</div>'), {
172 title: "OpenERP " + _.str.capitalize(error.type),
174 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
178 on_traceback: function(error) {
181 if (instance.connection.openerp_entreprise) {
182 buttons[_t("Send OpenERP Enterprise Report")] = function() {
184 var issuename = $('#issuename').val();
185 var explanation = $('#explanation').val();
186 var remark = $('#remark').val();
187 // Call the send method from server to send mail with details
188 new instance.web.DataSet(self, 'publisher_warranty.contract').call_and_eval('send', [error.data,explanation,remark,issuename]).then(function(result){
189 if (result === false) {
190 alert('There was a communication error.');
192 $this.dialog('close');
196 buttons[_t("Dont send")] = function() {
197 $(this).dialog("close");
200 buttons[_t("Ok")] = function() {
201 $(this).dialog("close");
204 var dialog = new instance.web.Dialog(this, {
205 title: "OpenERP " + _.str.capitalize(error.type),
212 dialog.$element.html(QWeb.render('CrashManager.error', {session: instance.connection, error: error}));
214 on_javascript_exception: function(exception) {
216 type: _t("Client Error"),
223 instance.web.Loading = instance.web.Widget.extend({
225 init: function(parent) {
228 this.blocked_ui = false;
230 this.request_call = function() {
231 self.on_rpc_event(1);
233 this.response_call = function() {
234 self.on_rpc_event(-1);
236 this.session.on_rpc_request.add_first(this.request_call);
237 this.session.on_rpc_response.add_last(this.response_call);
239 destroy: function() {
240 this.session.on_rpc_request.remove(this.request_call);
241 this.session.on_rpc_response.remove(this.response_call);
242 this.on_rpc_event(-this.count);
245 on_rpc_event : function(increment) {
247 if (!this.count && increment === 1) {
249 this.long_running_timer = setTimeout(function () {
250 self.blocked_ui = true;
255 this.count += increment;
256 if (this.count > 0) {
257 this.$element.text(_.str.sprintf( _t("Loading (%d)"), this.count));
258 this.$element.show();
259 this.getParent().$element.addClass('oe_wait');
262 clearTimeout(this.long_running_timer);
263 // Don't unblock if blocked by somebody else
264 if (self.blocked_ui) {
265 this.blocked_ui = false;
268 this.$element.fadeOut();
269 this.getParent().$element.removeClass('oe_wait');
274 instance.web.DatabaseManager = instance.web.Widget.extend({
275 init: function(parent) {
277 this.unblockUIFunction = $.unblockUI;
278 $.validator.addMethod('matches', function (s, _, re) {
279 return new RegExp(re).test(s);
280 }, _t("Invalid database name"));
284 var fetch_db = this.rpc("/web/database/get_list", {}).pipe(
286 self.db_list = result.db_list;
292 var fetch_langs = this.rpc("/web/session/get_lang_list", {}).then(function(result) {
293 self.lang_list = result.lang_list;
295 return $.when(fetch_db, fetch_langs).then(self.do_render);
297 do_render: function() {
299 self.$element.html(QWeb.render("DatabaseManager", { widget : self }));
300 self.$element.find(".oe_database_manager_menu").tabs({
301 show: function(event, ui) {
302 $('*[autofocus]:first', ui.panel).focus();
305 self.$element.find("form[name=create_db_form]").validate({ submitHandler: self.do_create });
306 self.$element.find("form[name=drop_db_form]").validate({ submitHandler: self.do_drop });
307 self.$element.find("form[name=backup_db_form]").validate({ submitHandler: self.do_backup });
308 self.$element.find("form[name=restore_db_form]").validate({ submitHandler: self.do_restore });
309 self.$element.find("form[name=change_pwd_form]").validate({
311 old_pwd: "Please enter your previous password",
312 new_pwd: "Please enter your new password",
314 required: "Please confirm your new password",
315 equalTo: "The confirmation does not match the password"
318 submitHandler: self.do_change_password
320 self.$element.find("#back_to_login").click(self.do_exit);
322 destroy: function () {
323 this.$element.find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login').unbind('click').end().empty();
327 * Converts a .serializeArray() result into a dict. Does not bother folding
328 * multiple identical keys into an array, last key wins.
330 * @param {Array} array
332 to_object: function (array) {
334 _(array).each(function (record) {
335 result[record.name] = record.value;
340 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
341 * from unblocking the UI
343 blockUI: function () {
345 $.unblockUI = function () {};
348 * Reinstates $.unblockUI so third parties can play with blockUI, and
351 unblockUI: function () {
352 $.unblockUI = this.unblockUIFunction;
356 * Displays an error dialog resulting from the various RPC communications
357 * failing over themselves
359 * @param {Object} error error description
360 * @param {String} error.title title of the error dialog
361 * @param {String} error.error message of the error dialog
363 display_error: function (error) {
364 return instance.web.dialog($('<div>'), {
368 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
370 }).html(error.error);
372 do_create: function(form) {
374 var fields = $(form).serializeArray();
375 self.rpc("/web/database/create", {'fields': fields}, function(result) {
376 var form_obj = self.to_object(fields);
377 self.getParent().do_login( form_obj['db_name'], 'admin', form_obj['create_admin_pwd']);
382 do_drop: function(form) {
385 fields = $form.serializeArray(),
386 $db_list = $form.find('[name=drop_db]'),
388 if (!db || !confirm("Do you really want to delete the database: " + db + " ?")) {
391 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
393 self.display_error(result);
396 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
400 do_backup: function(form) {
403 self.session.get_file({
405 success: function () {
406 self.do_notify(_t("Backed"), _t("Database backed up successfully"));
408 error: function(error){
411 title: 'Backup Database',
412 error: 'AccessDenied'
416 complete: function() {
421 do_restore: function(form) {
425 url: '/web/database/restore',
428 success: function (body) {
429 // If empty body, everything went fine
430 if (!body) { return; }
432 if (body.indexOf('403 Forbidden') !== -1) {
434 title: 'Access Denied',
435 error: 'Incorrect super-administrator password'
439 title: 'Restore Database',
440 error: 'Could not restore the database'
444 complete: function() {
446 self.do_notify(_t("Restored"), _t("Database restored successfully"));
450 do_change_password: function(form) {
452 self.rpc("/web/database/change_password", {
453 'fields': $(form).serializeArray()
454 }, function(result) {
456 self.display_error(result);
459 self.do_notify("Changed Password", "Password has been changed successfully");
462 do_exit: function () {
466 instance.web.Login = instance.web.Widget.extend({
468 remember_credentials: true,
471 init: function(parent) {
473 this.has_local_storage = typeof(localStorage) != 'undefined';
474 this.selected_db = null;
475 this.selected_login = null;
477 if (this.has_local_storage && this.remember_credentials) {
478 this.selected_db = localStorage.getItem('last_db_login_success');
479 this.selected_login = localStorage.getItem('last_login_login_success');
480 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
481 this.selected_password = localStorage.getItem('last_password_login_success');
488 self.$element.find("form").submit(self.on_submit);
490 self.$element.find('.oe_login_manage_db').click(function() {
491 self.$element.find('.oe_login_bottom').hide();
492 self.$element.find('.oe_login_pane').hide();
493 self.databasemanager = new instance.web.DatabaseManager(self);
494 self.databasemanager.appendTo(self.$element);
495 self.databasemanager.do_exit.add_last(function() {
496 self.databasemanager.destroy();
497 self.$element.find('.oe_login_bottom').show();
498 self.$element.find('.oe_login_pane').show();
499 self.load_db_list(true).then(self.proxy('_db_list_loaded'));
502 return self.load_db_list().then(self.proxy('_db_list_loaded'));
504 load_db_list: function (force) {
505 var d = $.when(), self = this;
506 if (_.isNull(this._db_list) || force) {
507 d = self.rpc("/web/database/get_list", {}, function(result) {
508 self._db_list = _.clone(result.db_list);
509 }, function(error, event) {
510 if (error.data.fault_code === 'AccessDenied') {
511 event.preventDefault();
517 _db_list_loaded: function () {
518 var list = this._db_list,
519 dbdiv = this.$element.find('div.oe_login_dbpane');
520 this.$element.find("[name=db]").replaceWith(instance.web.qweb.render('Login.dblist', { db_list: list, selected_db: this.selected_db}));
521 if(list && list.length === 1) {
527 on_submit: function(ev) {
531 var $e = this.$element;
532 var db = $e.find("form [name=db]").val();
534 this.do_warn("Login", "No database selected !");
537 var login = $e.find("form input[name=login]").val();
538 var password = $e.find("form input[name=password]").val();
540 this.do_login(db, login, password);
543 * Performs actual login operation, and UI-related stuff
545 * @param {String} db database to log in
546 * @param {String} login user login
547 * @param {String} password user password
549 do_login: function (db, login, password) {
551 this.$element.removeClass('oe_login_invalid');
552 self.$(".oe_login_pane").fadeOut("slow");
553 return this.session.session_authenticate(db, login, password).pipe(function() {
554 if (self.has_local_storage) {
555 if(self.remember_credentials) {
556 localStorage.setItem('last_db_login_success', db);
557 localStorage.setItem('last_login_login_success', login);
558 if (jQuery.deparam(jQuery.param.querystring()).debug !== undefined) {
559 localStorage.setItem('last_password_login_success', password);
562 localStorage.setItem('last_db_login_success', '');
563 localStorage.setItem('last_login_login_success', '');
564 localStorage.setItem('last_password_login_success', '');
567 self.trigger("login");
569 self.$(".oe_login_pane").fadeIn("fast");
570 self.$element.addClass("oe_login_invalid");
575 instance.web.Menu = instance.web.Widget.extend({
578 this._super.apply(this, arguments);
579 this.has_been_loaded = $.Deferred();
580 this.maximum_visible_links = 'auto'; // # of menu to show. 0 = do not crop, 'auto' = algo
581 this.data = {data:{children:[]}};
584 this._super.apply(this, arguments);
585 this.$secondary_menus = this.getParent().$element.find('.oe_secondary_menus_container');
586 this.$secondary_menus.on('click', 'a[data-menu]', this.on_menu_click);
587 return this.do_reload();
589 do_reload: function() {
590 return this.rpc("/web/menu/load", {}).then(this.on_loaded);
592 on_loaded: function(data) {
595 this.renderElement();
596 this.limit_entries();
597 this.$secondary_menus.html(QWeb.render("Menu.secondary", { widget : this }));
598 this.$element.on('click', 'a[data-menu]', this.on_menu_click);
599 // Hide second level submenus
600 this.$secondary_menus.find('.oe_menu_toggler').siblings('.oe_secondary_submenu').hide();
601 if (self.current_menu) {
602 self.open_menu(self.current_menu);
604 this.has_been_loaded.resolve();
606 limit_entries: function() {
607 var maximum_visible_links = this.maximum_visible_links;
608 if (maximum_visible_links === 'auto') {
609 maximum_visible_links = this.auto_limit_entries();
611 if (maximum_visible_links < this.data.data.children.length) {
612 var $more = $(QWeb.render('Menu.more')),
613 $index = this.$element.find('li').eq(maximum_visible_links - 1);
615 $more.find('.oe_menu_more').append($index.next().nextAll());
618 auto_limit_entries: function() {
619 // TODO: auto detect overflow and bind window on resize
620 var width = $(window).width();
621 return Math.floor(width / 125);
624 * Opens a given menu by id, as if a user had browsed to that menu by hand
625 * except does not trigger any event on the way
627 * @param {Number} id database id of the terminal menu to select
629 open_menu: function (id) {
630 var $clicked_menu, $sub_menu, $main_menu;
631 $clicked_menu = this.$element.add(this.$secondary_menus).find('a[data-menu=' + id + ']');
632 this.trigger('open_menu', id, $clicked_menu);
634 if (this.$secondary_menus.has($clicked_menu).length) {
635 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
636 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
638 $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
639 $main_menu = $clicked_menu;
642 // Activate current main menu
643 this.$element.find('.oe_active').removeClass('oe_active');
644 $main_menu.addClass('oe_active');
646 // Show current sub menu
647 this.$secondary_menus.find('.oe_secondary_menu').hide();
650 // Hide/Show the leftbar menu depending of the presence of sub-items
651 this.$secondary_menus.parent('.oe_leftbar').toggle(!!$sub_menu.children().length);
653 // Activate current menu item and show parents
654 this.$secondary_menus.find('.oe_active').removeClass('oe_active');
655 if ($main_menu !== $clicked_menu) {
656 $clicked_menu.parents().show();
657 if ($clicked_menu.is('.oe_menu_toggler')) {
658 $clicked_menu.toggleClass('oe_menu_opened').siblings('.oe_secondary_submenu:first').toggle();
660 $clicked_menu.parent().addClass('oe_active');
665 * Call open_menu with the first menu_item matching an action_id
667 * @param {Number} id the action_id to match
669 open_action: function (id) {
670 var $menu = this.$element.add(this.$secondary_menus).find('a[data-action-id=' + id + ']');
671 var menu_id = $menu.data('menu');
673 this.open_menu(menu_id);
677 * Process a click on a menu item
679 * @param {Number} id the menu_id
680 * @param {Boolean} [needaction=false] whether the triggered action should execute in a `needs action` context
682 menu_click: function(id, needaction) {
685 // find back the menuitem in dom to get the action
686 var $item = this.$element.find('a[data-menu=' + id + ']');
688 $item = this.$secondary_menus.find('a[data-menu=' + id + ']');
690 var action_id = $item.data('action-id');
691 // If first level menu doesnt have action trigger first leaf
693 if(this.$element.has($item).length) {
694 var $sub_menu = this.$secondary_menus.find('.oe_secondary_menu[data-menu-parent=' + id + ']');
695 var $items = $sub_menu.find('a[data-action-id]').filter('[data-action-id!=""]');
697 action_id = $items.data('action-id');
698 id = $items.data('menu');
703 this.current_menu = id;
704 this.session.active_id = id;
706 this.trigger('menu_click', {
707 action_id: action_id,
708 needaction: needaction,
714 * Jquery event handler for menu click
716 * @param {Event} ev the jquery event
718 on_menu_click: function(ev) {
720 var needaction = $(ev.target).is('div.oe_menu_counter');
721 this.menu_click($(ev.currentTarget).data('menu'), needaction);
725 instance.web.UserMenu = instance.web.Widget.extend({
726 template: "UserMenu",
727 init: function(parent) {
729 this.update_promise = $.Deferred().resolve();
733 this._super.apply(this, arguments);
734 this.$element.on('click', '.oe_dropdown_menu li a[data-menu]', function(ev) {
736 var f = self['on_menu_' + $(this).data('menu')];
742 change_password :function() {
744 this.dialog = new instance.web.Dialog(this, {
745 title: _t("Change Password"),
748 this.dialog.$element.html(QWeb.render("UserMenu.password", self));
749 this.dialog.$element.find("form[name=change_password_form]").validate({
750 submitHandler: function (form) {
751 self.rpc("/web/session/change_password",{
752 'fields': $(form).serializeArray()
753 }, function(result) {
755 self.display_error(result);
758 instance.webclient.on_logout();
764 display_error: function (error) {
765 return instance.web.dialog($('<div>'), {
769 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
771 }).html(error.error);
773 do_update: function () {
775 var fct = function() {
776 var $avatar = self.$element.find('.oe_topbar_avatar');
777 $avatar.attr('src', $avatar.data('default-src'));
778 if (!self.session.uid)
780 var func = new instance.web.Model("res.users").get_func("read");
781 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
782 var topbar_name = res.name;
783 if(instance.connection.debug)
784 topbar_name = _.str.sprintf("%s (%s)", topbar_name, instance.connection.db);
785 if(res.company_id[0] > 1)
786 topbar_name = _.str.sprintf("%s (%s)", topbar_name, res.company_id[1]);
787 self.$element.find('.oe_topbar_name').text(topbar_name);
788 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);
789 $avatar.attr('src', avatar_src);
792 this.update_promise = this.update_promise.pipe(fct, fct);
794 on_action: function() {
796 on_menu_logout: function() {
798 on_menu_settings: function() {
800 var action_manager = new instance.web.ActionManager(this);
801 var dataset = new instance.web.DataSet (this,'res.users',this.context);
802 dataset.call ('action_get','',function (result){
803 self.rpc('/web/action/load', {action_id:result}, function(result){
804 action_manager.do_action(_.extend(result['result'], {
806 res_id: self.session.uid,
807 res_model: 'res.users',
809 action_buttons: false,
812 views_switcher: false,
818 this.dialog = new instance.web.Dialog(this,{
819 title: _t("Preferences"),
822 {text: _t("Change password"), click: function(){ self.change_password(); }},
823 {text: _t("Cancel"), click: function(){ $(this).dialog('destroy'); }},
824 {text: _t("Save"), click: function(){
825 var inner_viewmanager = action_manager.inner_viewmanager;
826 inner_viewmanager.views[inner_viewmanager.active_view].controller.do_save()
828 self.dialog.destroy();
829 // needs to refresh interface in case language changed
830 window.location.reload();
836 action_manager.appendTo(this.dialog.$element);
837 action_manager.renderElement(this.dialog);
839 on_menu_about: function() {
841 self.rpc("/web/webclient/version_info", {}).then(function(res) {
842 var $help = $(QWeb.render("UserMenu.about", {version_info: res}));
843 $help.find('a.oe_activate_debug_mode').click(function (e) {
845 window.location = $.param.querystring(
846 window.location.href, 'debug');
848 instance.web.dialog($help, {autoOpen: true,
849 modal: true, width: 960, title: _t("About")});
854 instance.web.WebClient = instance.web.Widget.extend({
855 init: function(parent) {
858 instance.webclient = this;
859 this.querystring = '?' + jQuery.param.querystring();
860 this._current_state = null;
862 _get_version_label: function() {
863 if (this.session.openerp_entreprise) {
866 return _t("OpenERP - Unsupported/Community Version");
869 set_title: function(title) {
870 title = _.str.clean(title);
871 var sep = _.isEmpty(title) ? '' : ' - ';
872 document.title = title + sep + 'OpenERP';
876 this.$element.addClass("openerp openerp_webclient_container");
877 if (jQuery.param !== undefined && jQuery.deparam(jQuery.param.querystring()).kitten !== undefined) {
878 $("body").addClass("kitten-mode-activated");
880 $.blockUI.defaults.message = '<img src="http://www.amigrave.com/kitten.gif">';
883 this.session.session_bind().then(function() {
884 self.destroy_content();
886 if (!self.session.session_is_valid()) {
889 self.show_application();
892 this.$element.on('mouseenter', '.oe_systray > div:not([data-tipsy=true])', function() {
893 $(this).attr('data-tipsy', 'true').tipsy().trigger('mouseenter');
895 this.$element.on('click', '.oe_dropdown_toggle', function(ev) {
897 var $menu = $(this).find('.oe_dropdown_menu');
898 var state = $menu.is('.oe_opened');
899 setTimeout(function() {
900 // Do not alter propagation
901 $menu.toggleClass('oe_opened', !state);
903 // Move $menu if outside window's edge
904 var doc_width = $(document).width();
905 var offset = $menu.offset();
906 var menu_width = $menu.width();
907 var x = doc_width - offset.left - menu_width - 15;
909 $menu.offset({ left: offset.left + x }).width(menu_width);
914 instance.web.bus.on('click', this, function() {
915 self.$element.find('.oe_dropdown_menu.oe_opened').removeClass('oe_opened');
918 show_common: function() {
920 this.crashmanager = new instance.web.CrashManager();
921 instance.connection.on_rpc_error.add(this.crashmanager.on_rpc_error);
922 window.onerror = function (message, file, line) {
923 self.crashmanager.on_traceback({
924 type: _t("Client Error"),
926 data: {debug: file + ':' + line}
929 self.notification = new instance.web.Notification(this);
930 self.notification.appendTo(self.$element);
931 self.loading = new instance.web.Loading(self);
932 self.loading.appendTo(self.$element);
933 self.login = new instance.web.Login(self);
934 self.login.on("login",self,self.show_application);
935 self.$table = $(QWeb.render("WebClient", {}));
936 self.action_manager = new instance.web.ActionManager(self);
937 self.action_manager.appendTo(self.$table.find('.oe_application'));
939 show_login: function() {
941 self.login.appendTo(self.$element);
943 show_application: function() {
945 self.$element.append(self.$table);
946 self.login.$element.hide();
947 self.menu = new instance.web.Menu(self);
948 self.menu.replace(this.$element.find('.oe_menu_placeholder'));
949 self.menu.on('menu_click', this, this.on_menu_action);
950 self.user_menu = new instance.web.UserMenu(self);
951 self.user_menu.replace(this.$element.find('.oe_user_menu_placeholder'));
952 self.user_menu.on_menu_logout.add(this.proxy('on_logout'));
953 self.user_menu.on_action.add(this.proxy('on_menu_action'));
954 self.user_menu.do_update();
955 self.bind_hashchange();
956 if (!self.session.openerp_entreprise) {
957 var version_label = self._get_version_label();
958 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));
962 destroy_content: function() {
963 _.each(_.clone(this.getChildren()), function(el) {
966 this.$element.children().remove();
968 do_reload: function() {
970 return this.session.session_reload().pipe(function () {
971 instance.connection.load_modules(true).pipe(
972 self.menu.proxy('do_reload')); });
975 do_notify: function() {
976 var n = this.notification;
977 n.notify.apply(n, arguments);
979 do_warn: function() {
980 var n = this.notification;
981 n.warn.apply(n, arguments);
983 on_logout: function() {
985 this.session.session_logout().then(function () {
986 $(window).unbind('hashchange', self.on_hashchange);
987 self.do_push_state({});
988 //would be cool to be able to do this, but I think it will make addons do strange things
990 window.location.reload();
993 bind_hashchange: function() {
995 $(window).bind('hashchange', this.on_hashchange);
997 var state = $.bbq.getState(true);
998 if (! _.isEmpty(state)) {
999 $(window).trigger('hashchange');
1001 self.menu.has_been_loaded.then(function() {
1002 var first_menu_id = self.menu.$element.find("a:first").data("menu");
1004 self.menu.menu_click(first_menu_id);
1009 on_hashchange: function(event) {
1011 var state = event.getState(true);
1012 if (!_.isEqual(this._current_state, state)) {
1013 if(state.action_id === undefined && state.menu_id) {
1014 self.menu.has_been_loaded.then(function() {
1015 self.menu.do_reload().then(function() {
1016 self.menu.menu_click(state.menu_id)
1020 this.action_manager.do_load_state(state, !!this._current_state);
1023 this._current_state = state;
1025 do_push_state: function(state) {
1026 this.set_title(state.title);
1027 var url = '#' + $.param(state);
1028 this._current_state = _.clone(state);
1029 $.bbq.pushState(url);
1031 on_menu_action: function(options) {
1033 this.rpc("/web/action/load", { action_id: options.action_id })
1034 .then(function (result) {
1035 var action = result.result;
1036 if (options.needaction) {
1037 action.context.search_default_needaction_pending = true;
1039 self.action_manager.do_action(action);
1042 do_action: function(action) {
1044 // TODO replace by client action menuclick
1045 if(action.menu_id) {
1046 this.do_reload().then(function () {
1047 self.menu.menu_click(action.menu_id);
1051 set_content_full_screen: function(fullscreen) {
1053 $(".oe_webclient", this.$element).addClass("oe_content_full_screen");
1055 $(".oe_webclient", this.$element).removeClass("oe_content_full_screen");
1059 instance.web.EmbeddedClient = instance.web.Widget.extend({
1060 template: 'EmptyComponent',
1061 init: function(parent, action_id, options) {
1062 this._super(parent);
1063 // TODO take the xmlid of a action instead of its id
1064 this.action_id = action_id;
1065 this.options = options || {};
1066 this.am = new instance.web.ActionManager(this);
1070 this.am.appendTo(this.$element.addClass('openerp'));
1071 return this.rpc("/web/action/load", { action_id: this.action_id }, function(result) {
1072 var action = result.result;
1073 action.flags = _.extend({
1074 //views_switcher : false,
1075 search_view : false,
1076 action_buttons : false,
1079 }, self.options, action.flags || {});
1081 self.am.do_action(action);
1086 instance.web.embed = function (origin, dbname, login, key, action, options) {
1087 $('head').append($('<link>', {
1088 'rel': 'stylesheet',
1090 'href': origin +'/web/webclient/css'
1092 var currentScript = document.currentScript;
1093 if (!currentScript) {
1094 var sc = document.getElementsByTagName('script');
1095 currentScript = sc[sc.length-1];
1097 instance.connection.session_bind(origin).then(function () {
1098 instance.connection.session_authenticate(dbname, login, key, true).then(function () {
1099 var client = new instance.web.EmbeddedClient(null, action, options);
1100 client.insertAfter(currentScript);
1108 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: