1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
4 openerp.web.chrome = function(openerp) {
5 var QWeb = openerp.web.qweb,
8 openerp.web.Notification = openerp.web.Widget.extend(/** @lends openerp.web.Notification# */{
9 template: 'Notification',
10 identifier_prefix: 'notification-',
13 this._super.apply(this, arguments);
14 openerp.notification = this;
18 this._super.apply(this, arguments);
19 this.$element.notify({
24 notify: function(title, text) {
25 this.$element.notify('create', {
30 warn: function(title, text) {
31 this.$element.notify('create', 'oe_notification_alert', {
39 openerp.web.Dialog = openerp.web.Widget.extend(/** @lends openerp.web.Dialog# */{
41 identifier_prefix: 'dialog',
43 * @constructs openerp.web.Dialog
44 * @extends openerp.web.OldWidget
49 init: function (parent, options, content) {
53 this.$element = content instanceof $ ? content : $(content);
55 this.dialog_options = {
57 destroy_on_close: true,
58 width: $(window).width() * (($(window).width() > 1024) ? 0.5 : 0.75),
63 max_height: this.get_height('100%') - 140,
65 position: [false, 50],
67 beforeClose: function () { self.on_close(); },
68 resizeStop: this.on_resized
71 if (f.substr(0, 10) == 'on_button_') {
72 this.dialog_options.buttons[f.substr(10)] = this[f];
76 _.extend(this.dialog_options, options);
78 if (this.dialog_options.autoOpen) {
81 this.$element.dialog(this.get_options());
84 get_options: function(options) {
86 o = _.extend({}, this.dialog_options, options || {});
87 _.each(['width', 'height'], function(unit) {
88 o[unit] = self['get_' + unit](o[unit]);
89 o['min_' + unit] = self['get_' + unit](o['min_' + unit] || 0);
90 o['max_' + unit] = self['get_' + unit](o['max_' + unit] || 0);
91 if (o[unit] !== 'auto' && o['min_' + unit] && o[unit] < o['min_' + unit]) o[unit] = o['min_' + unit];
92 if (o[unit] !== 'auto' && o['max_' + unit] && o[unit] > o['max_' + unit]) o[unit] = o['max_' + unit];
94 if (!o.title && this.dialog_title) {
95 o.title = this.dialog_title;
99 get_width: function(val) {
100 return this.get_size(val.toString(), $(window.top).width());
102 get_height: function(val) {
103 return this.get_size(val.toString(), $(window.top).height());
105 get_size: function(val, available_size) {
106 if (val === 'auto') {
108 } else if (val.slice(-1) == "%") {
109 return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
111 return parseInt(val, 10);
114 open: function(options) {
115 // TODO fme: bind window on resize
117 this.$element.html(this.render());
119 var o = this.get_options(options);
120 this.$element.dialog(o).dialog('open');
121 if (o.height === 'auto' && o.max_height) {
122 this.$element.css({ 'max-height': o.max_height, 'overflow-y': 'auto' });
127 this.$element.dialog('close');
129 on_close: function() {
130 if (this.dialog_options.destroy_on_close) {
131 this.$element.dialog('destroy');
134 on_resized: function() {
135 //openerp.log("Dialog resized to %d x %d", this.$element.width(), this.$element.height());
140 this.$element.dialog('destroy');
145 openerp.web.CrashManager = openerp.web.CallbackEnabled.extend({
148 openerp.connection.on_rpc_error.add(this.on_rpc_error);
150 on_rpc_error: function(error) {
152 if (error.data.fault_code) {
153 var split = ("" + error.data.fault_code).split('\n')[0].split(' -- ');
154 if (split.length > 1) {
155 error.type = split.shift();
156 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
159 if (error.code === 200 && error.type) {
160 this.on_managed_error(error);
162 this.on_traceback(error);
165 on_managed_error: function(error) {
166 $('<div>' + QWeb.render('CrashManagerWarning', {error: error}) + '</div>').dialog({
167 title: "OpenERP " + _.str.capitalize(error.type),
169 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
173 on_traceback: function(error) {
176 if (openerp.connection.openerp_entreprise) {
177 buttons[_t("Send OpenERP Enterprise Report")] = function() {
179 var issuename = $('#issuename').val();
180 var explanation = $('#explanation').val();
181 var remark = $('#remark').val();
182 // Call the send method from server to send mail with details
183 new openerp.web.DataSet(self, 'publisher_warranty.contract').call_and_eval('send', [error.data,explanation,remark,issuename]).then(function(result){
184 if (result === false) {
185 alert('There was a communication error.')
187 $this.dialog('close');
191 buttons[_t("Dont send")] = function() {
192 $(this).dialog("close");
195 buttons[_t("Ok")] = function() {
196 $(this).dialog("close");
199 var dialog = new openerp.web.Dialog(this, {
200 title: "OpenERP " + _.str.capitalize(this.error.type),
207 dialog.$element.html(QWeb.render('CrashManagerError', {session: openerp.connection, error: error}));
211 openerp.web.Loading = openerp.web.Widget.extend(/** @lends openerp.web.Loading# */{
214 * @constructs openerp.web.Loading
215 * @extends openerp.web.Widget
220 init: function(parent) {
223 this.blocked_ui = false;
225 this.request_call = function() {
226 self.on_rpc_event(1);
228 this.response_call = function() {
229 self.on_rpc_event(-1);
231 this.session.on_rpc_request.add_first(this.request_call);
232 this.session.on_rpc_response.add_last(this.response_call);
235 this.session.on_rpc_request.remove(this.request_call);
236 this.session.on_rpc_response.remove(this.response_call);
237 this.on_rpc_event(-this.count);
240 on_rpc_event : function(increment) {
242 if (!this.count && increment === 1) {
244 this.long_running_timer = setTimeout(function () {
245 self.blocked_ui = true;
250 this.count += increment;
251 if (this.count > 0) {
252 //this.$element.html(QWeb.render("Loading", {}));
253 $(".loading",this.$element).html("Loading ("+this.count+")");
254 $(".loading",this.$element).show();
255 this.widget_parent.$element.addClass('loading');
258 clearTimeout(this.long_running_timer);
259 // Don't unblock if blocked by somebody else
260 if (self.blocked_ui) {
261 this.blocked_ui = false;
264 $(".loading",this.$element).fadeOut();
265 this.widget_parent.$element.removeClass('loading');
270 openerp.web.Database = openerp.web.Widget.extend(/** @lends openerp.web.Database# */{
271 template: "DatabaseManager",
273 * @constructs openerp.web.Database
274 * @extends openerp.web.Widget
280 init: function(parent, element_id, option_id) {
281 this._super(parent, element_id);
282 this.unblockUIFunction = $.unblockUI;
283 $.validator.addMethod('matches', function (s, _, re) {
284 return new RegExp(re).test(s);
285 }, _t("Invalid database name"));
288 this.$option_id = $("#oe_db_options");
291 var fetch_db = this.rpc("/web/database/get_list", {}, function(result) {
292 self.db_list = result.db_list;
294 var fetch_langs = this.rpc("/web/session/get_lang_list", {}, function(result) {
296 self.display_error(result);
299 self.lang_list = result.lang_list;
301 $.when(fetch_db, fetch_langs).then(function () {self.do_create();});
303 this.$element.find('#db-create').click(this.do_create);
304 this.$element.find('#db-drop').click(this.do_drop);
305 this.$element.find('#db-backup').click(this.do_backup);
306 this.$element.find('#db-restore').click(this.do_restore);
307 this.$element.find('#db-change-password').click(this.do_change_password);
308 this.$element.find('#back-to-login').click(function() {
314 this.$option_id.empty();
317 .find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login')
324 this.$element.closest(".login")
325 .addClass("database_block");
328 this.$element.closest(".login")
329 .removeClass("database_block")
332 * Converts a .serializeArray() result into a dict. Does not bother folding
333 * multiple identical keys into an array, last key wins.
335 * @param {Array} array
337 to_object: function (array) {
339 _(array).each(function (record) {
340 result[record.name] = record.value;
345 * Waits until the new database is done creating, then unblocks the UI and
346 * logs the user in as admin
348 * @param {Number} db_creation_id identifier for the db-creation operation, used to fetch the current installation progress
349 * @param {Object} info info fields for this database creation
350 * @param {String} info.db name of the database being created
351 * @param {String} info.password super-admin password for the database
353 wait_for_newdb: function (db_creation_id, info) {
355 self.rpc('/web/database/progress', {
357 password: info.password
358 }, function (result) {
359 var progress = result[0];
360 // I'd display a progress bar, but turns out the progress status
361 // the server report kind-of blows goats: it's at 0 for ~75% of
362 // the installation, then jumps to 75%, then jumps down to either
363 // 0 or ~40%, then back up to 75%, then terminates. Let's keep that
364 // mess hidden behind a not-very-useful but not overly weird
367 setTimeout(function () {
368 self.wait_for_newdb(db_creation_id, info);
373 var admin = result[1][0];
374 setTimeout(function () {
375 self.widget_parent.do_login(
376 info.db, admin.login, admin.password);
383 * Blocks UI and replaces $.unblockUI by a noop to prevent third parties
384 * from unblocking the UI
386 blockUI: function () {
388 $.unblockUI = function () {};
391 * Reinstates $.unblockUI so third parties can play with blockUI, and
394 unblockUI: function () {
395 $.unblockUI = this.unblockUIFunction;
399 * Displays an error dialog resulting from the various RPC communications
400 * failing over themselves
402 * @param {Object} error error description
403 * @param {String} error.title title of the error dialog
404 * @param {String} error.error message of the error dialog
406 display_error: function (error) {
407 return $('<div>').dialog({
411 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
413 }).html(error.error);
415 do_create: function() {
417 self.$option_id.html(QWeb.render("Database.CreateDB", self));
418 self.$option_id.find("form[name=create_db_form]").validate({
419 submitHandler: function (form) {
420 var fields = $(form).serializeArray();
422 self.rpc("/web/database/create", {'fields': fields}, function(result) {
425 self.display_error(result);
428 self.db_list.push(self.to_object(fields)['db_name']);
430 self.widget_parent.set_db_list(self.db_list);
431 var form_obj = self.to_object(fields);
432 self.wait_for_newdb(result, {
433 password: form_obj['super_admin_pwd'],
434 db: form_obj['db_name']
440 do_drop: function() {
442 self.$option_id.html(QWeb.render("DropDB", self));
443 self.$option_id.find("form[name=drop_db_form]").validate({
444 submitHandler: function (form) {
446 fields = $form.serializeArray(),
447 $db_list = $form.find('select[name=drop_db]'),
450 if (!confirm("Do you really want to delete the database: " + db + " ?")) {
453 self.rpc("/web/database/drop", {'fields': fields}, function(result) {
455 self.display_error(result);
458 $db_list.find(':selected').remove();
459 self.db_list.splice(_.indexOf(self.db_list, db, true), 1);
460 self.widget_parent.set_db_list(self.db_list);
461 self.do_notify("Dropping database", "The database '" + db + "' has been dropped");
466 do_backup: function() {
469 .html(QWeb.render("BackupDB", self))
470 .find("form[name=backup_db_form]").validate({
471 submitHandler: function (form) {
473 self.session.get_file({
475 success: function () {
476 self.do_notify(_t("Backed"),
477 _t("Database backed up successfully"));
479 error: openerp.webclient.crashmanager.on_rpc_error,
480 complete: function() {
487 do_restore: function() {
489 self.$option_id.html(QWeb.render("RestoreDB", self));
491 self.$option_id.find("form[name=restore_db_form]").validate({
492 submitHandler: function (form) {
495 url: '/web/database/restore',
498 success: function (body) {
499 // TODO: ui manipulations
500 // note: response objects don't work, but we have the
501 // HTTP body of the response~~
503 // If empty body, everything went fine
504 if (!body) { return; }
506 if (body.indexOf('403 Forbidden') !== -1) {
508 title: 'Access Denied',
509 error: 'Incorrect super-administrator password'
513 title: 'Restore Database',
514 error: 'Could not restore the database'
518 complete: function() {
520 self.do_notify(_t("Restored"), _t("Database restored successfully"));
526 do_change_password: function() {
528 self.$option_id.html(QWeb.render("Change_DB_Pwd", self));
530 self.$option_id.find("form[name=change_pwd_form]").validate({
532 old_pwd: "Please enter your previous password",
533 new_pwd: "Please enter your new password",
535 required: "Please confirm your new password",
536 equalTo: "The confirmation does not match the password"
539 submitHandler: function (form) {
540 self.rpc("/web/database/change_password", {
541 'fields': $(form).serializeArray()
542 }, function(result) {
544 self.display_error(result);
547 self.do_notify("Changed Password", "Password has been changed successfully");
554 openerp.web.Login = openerp.web.Widget.extend(/** @lends openerp.web.Login# */{
555 remember_credentials: true,
558 identifier_prefix: 'oe-app-login-',
560 * @constructs openerp.web.Login
561 * @extends openerp.web.Widget
567 init: function(parent) {
569 this.has_local_storage = typeof(localStorage) != 'undefined';
570 this.selected_db = null;
571 this.selected_login = null;
573 if (this.has_local_storage && this.remember_credentials) {
574 this.selected_db = localStorage.getItem('last_db_login_success');
575 this.selected_login = localStorage.getItem('last_login_login_success');
576 if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
577 this.selected_password = localStorage.getItem('last_password_login_success');
583 this.database = new openerp.web.Database(this);
584 this.database.appendTo(this.$element);
586 this.$element.find('#oe-db-config').click(function() {
587 self.database.show();
590 this.$element.find("form").submit(this.on_submit);
592 this.rpc("/web/database/get_list", {}, function(result) {
593 self.set_db_list(result.db_list);
595 function(error, event) {
596 if (error.data.fault_code === 'AccessDenied') {
597 event.preventDefault();
602 set_db_list: function (list) {
603 this.$element.find("[name=db]").replaceWith(
604 openerp.web.qweb.render('Login_dblist', {
605 db_list: list, selected_db: this.selected_db}))
607 on_submit: function(ev) {
611 var $e = this.$element;
612 var db = $e.find("form [name=db]").val();
613 var login = $e.find("form input[name=login]").val();
614 var password = $e.find("form input[name=password]").val();
616 this.do_login(db, login, password);
619 * Performs actual login operation, and UI-related stuff
621 * @param {String} db database to log in
622 * @param {String} login user login
623 * @param {String} password user password
625 do_login: function (db, login, password) {
627 this.session.on_session_invalid.add({
628 callback: function () {
629 self.$element.addClass("login_invalid");
633 this.session.session_authenticate(db, login, password).then(function() {
634 self.$element.removeClass("login_invalid");
635 if (self.has_local_storage) {
636 if(self.remember_credentials) {
637 localStorage.setItem('last_db_login_success', db);
638 localStorage.setItem('last_login_login_success', login);
639 if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
640 localStorage.setItem('last_password_login_success', password);
643 localStorage.setItem('last_db_login_success', '');
644 localStorage.setItem('last_login_login_success', '');
645 localStorage.setItem('last_password_login_success', '');
652 openerp.web.Header = openerp.web.Widget.extend(/** @lends openerp.web.Header# */{
654 identifier_prefix: 'oe-app-header-',
656 * @constructs openerp.web.Header
657 * @extends openerp.web.Widget
661 init: function(parent) {
663 this.qs = "?" + jQuery.param.querystring();
665 this.update_promise = $.Deferred().resolve();
670 do_update: function () {
672 var fct = function() {
673 self.$content.remove();
674 if (!self.session.uid)
676 var func = new openerp.web.Model("res.users").get_func("read");
677 return func(self.session.uid, ["name", "company_id"]).pipe(function(res) {
678 self.$content = $(QWeb.render("Header-content", {widget: self, user: res}));
679 self.$content.appendTo(self.$element);
680 self.$element.find(".logout").click(self.on_logout);
681 self.$element.find("a.preferences").click(self.on_preferences);
682 self.$element.find(".about").click(self.on_about);
683 return self.shortcut_load();
686 this.update_promise = this.update_promise.pipe(fct, fct);
688 on_about: function() {
690 self.rpc("/web/webclient/version_info", {}).then(function(res) {
691 var $help = $(QWeb.render("About-Page", {version_info: res}));
692 $help.find('a.oe_activate_debug_mode').click(function (e) {
694 window.location = $.param.querystring(
695 window.location.href, 'debug');
697 $help.dialog({autoOpen: true,
698 modal: true, width: 960, title: _t("About")});
701 shortcut_load :function(){
703 sc = self.session.shortcuts,
704 shortcuts_ds = new openerp.web.DataSet(this, 'ir.ui.view_sc');
705 // TODO: better way to communicate between sections.
706 // sc.bindings, because jquery does not bind/trigger on arrays...
710 'add': function (e, attrs) {
711 shortcuts_ds.create(attrs, function (out) {
713 'data-shortcut-id':out.result,
714 'data-id': attrs.res_id
716 .appendTo(self.$element.find('.oe-shortcuts ul'));
717 attrs.id = out.result;
721 'remove-current': function () {
722 var menu_id = self.session.active_id;
723 var $shortcut = self.$element
724 .find('.oe-shortcuts li[data-id=' + menu_id + ']');
725 var shortcut_id = $shortcut.data('shortcut-id');
727 shortcuts_ds.unlink([shortcut_id]);
728 var sc_new = _.reject(sc, function(shortcut){ return shortcut_id === shortcut.id});
729 sc.splice(0, sc.length);
730 sc.push.apply(sc, sc_new);
734 return this.rpc('/web/session/sc_list', {}, function(shortcuts) {
735 sc.splice(0, sc.length);
736 sc.push.apply(sc, shortcuts);
738 self.$element.find('.oe-shortcuts')
739 .html(QWeb.render('Shortcuts', {'shortcuts': shortcuts}))
740 .undelegate('li', 'click')
742 .delegate('li', 'click', function(e) {
744 var id = $(this).data('id');
745 self.session.active_id = id;
746 self.rpc('/web/menu/action', {'menu_id':id}, function(ir_menu_data) {
747 if (ir_menu_data.action.length){
748 self.on_action(ir_menu_data.action[0][2]);
755 on_action: function(action) {
757 on_preferences: function(){
759 var action_manager = new openerp.web.ActionManager(this);
760 var dataset = new openerp.web.DataSet (this,'res.users',this.context);
761 dataset.call ('action_get','',function (result){
762 self.rpc('/web/action/load', {action_id:result}, function(result){
763 action_manager.do_action(_.extend(result['result'], {
764 res_id: self.session.uid,
765 res_model: 'res.users',
767 action_buttons: false,
770 views_switcher: false,
776 this.dialog = new openerp.web.Dialog(this,{
777 title: _t("Preferences"),
780 {text: _t("Cancel"), click: function(){ $(this).dialog('destroy'); }},
781 {text: _t("Change password"), click: function(){ self.change_password(); }},
782 {text: _t("Save"), click: function(){
783 var inner_viewmanager = action_manager.inner_viewmanager;
784 inner_viewmanager.views[inner_viewmanager.active_view].controller.do_save()
787 // needs to refresh interface in case language changed
788 window.location.reload();
794 action_manager.appendTo(this.dialog);
795 action_manager.render(this.dialog);
798 change_password :function() {
800 this.dialog = new openerp.web.Dialog(this, {
801 title: _t("Change Password"),
804 this.dialog.$element.html(QWeb.render("Change_Pwd", self));
805 this.dialog.$element.find("form[name=change_password_form]").validate({
806 submitHandler: function (form) {
807 self.rpc("/web/session/change_password",{
808 'fields': $(form).serializeArray()
809 }, function(result) {
811 self.display_error(result);
814 openerp.webclient.on_logout();
820 display_error: function (error) {
821 return $('<div>').dialog({
825 {text: _("Ok"), click: function() { $(this).dialog("close"); }}
827 }).html(error.error);
829 on_logout: function() {
833 openerp.web.Menu = openerp.web.Widget.extend(/** @lends openerp.web.Menu# */{
835 * @constructs openerp.web.Menu
836 * @extends openerp.web.Widget
840 * @param secondary_menu_id
842 init: function(parent, element_id, secondary_menu_id) {
843 this._super(parent, element_id);
844 this.secondary_menu_id = secondary_menu_id;
845 this.$secondary_menu = $("#" + secondary_menu_id);
848 if (window.localStorage) {
849 this.folded = localStorage.getItem('oe_menu_folded') === 'true';
851 this.float_timeout = 700;
854 this.$secondary_menu.addClass(this.folded ? 'oe_folded' : 'oe_unfolded');
856 do_reload: function() {
858 return this.rpc("/web/menu/load", {}, this.on_loaded).then(function () {
859 if (self.current_menu) {
860 self.open_menu(self.current_menu);
864 on_loaded: function(data) {
866 this.$element.html(QWeb.render("Menu", { widget : this }));
867 this.$secondary_menu.html(QWeb.render("Menu.secondary", { widget : this }));
868 this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
869 this.$secondary_menu.find('.oe_toggle_secondary_menu').click(this.on_toggle_fold);
871 on_toggle_fold: function() {
872 this.$secondary_menu.toggleClass('oe_folded').toggleClass('oe_unfolded');
874 this.$secondary_menu.find('.oe_secondary_menu.active').show();
876 this.$secondary_menu.find('.oe_secondary_menu').hide();
878 this.folded = !this.folded;
879 if (window.localStorage) {
880 localStorage.setItem('oe_menu_folded', this.folded.toString());
884 * Opens a given menu by id, as if a user had browsed to that menu by hand
885 * except does not trigger any event on the way
887 * @param {Number} menu_id database id of the terminal menu to select
889 open_menu: function (menu_id) {
890 this.$element.add(this.$secondary_menu).find('.active')
891 .removeClass('active');
892 this.$secondary_menu.find('> .oe_secondary_menu').hide();
895 var $secondary_submenu = this.$secondary_menu.find(
896 'a[data-menu=' + menu_id +']');
897 if ($secondary_submenu.length) {
899 if ($secondary_submenu.hasClass('leaf')) {
900 $secondary_submenu.addClass('active');
901 } else if ($secondary_submenu.hasClass('submenu')) {
902 $secondary_submenu.addClass('opened')
904 var $parent = $secondary_submenu.parent().show();
905 if ($parent.hasClass('oe_secondary_menu')) {
906 var primary_id = $parent.data('menu-parent');
907 $primary_menu = this.$element.find(
908 'a[data-menu=' + primary_id + ']');
911 $secondary_submenu = $parent.prev();
914 $primary_menu = this.$element.find('a[data-menu=' + menu_id + ']');
916 if (!$primary_menu.length) {
919 $primary_menu.addClass('active');
920 this.$secondary_menu.find(
921 'div[data-menu-parent=' + $primary_menu.data('menu') + ']').show();
923 on_menu_click: function(ev, id) {
925 var $clicked_menu, manual = false;
928 // We can manually activate a menu with it's id (for hash url mapping)
930 $clicked_menu = this.$element.find('a[data-menu=' + id + ']');
931 if (!$clicked_menu.length) {
932 $clicked_menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
935 $clicked_menu = $(ev.currentTarget);
936 id = $clicked_menu.data('menu');
939 if (this.do_menu_click($clicked_menu, manual) && id) {
940 this.current_menu = id;
941 this.session.active_id = id;
942 this.rpc('/web/menu/action', {'menu_id': id}, this.on_menu_action_loaded);
945 ev.stopPropagation();
949 do_menu_click: function($clicked_menu, manual) {
950 var $sub_menu, $main_menu,
951 active = $clicked_menu.is('.active'),
952 sub_menu_visible = false;
954 if (this.$secondary_menu.has($clicked_menu).length) {
955 $sub_menu = $clicked_menu.parents('.oe_secondary_menu');
956 $main_menu = this.$element.find('a[data-menu=' + $sub_menu.data('menu-parent') + ']');
958 $sub_menu = this.$secondary_menu.find('.oe_secondary_menu[data-menu-parent=' + $clicked_menu.attr('data-menu') + ']');
959 $main_menu = $clicked_menu;
962 sub_menu_visible = $sub_menu.is(':visible');
963 this.$secondary_menu.find('.oe_secondary_menu').hide();
965 $('.active', this.$element.add(this.$secondary_menu)).removeClass('active');
966 $main_menu.add($clicked_menu).add($sub_menu).addClass('active');
968 if (!(this.folded && manual)) {
969 this.do_show_secondary($sub_menu, $main_menu);
971 this.do_show_secondary();
974 if ($main_menu != $clicked_menu) {
975 if ($clicked_menu.is('.submenu')) {
976 $sub_menu.find('.submenu.opened').each(function() {
977 if (!$(this).next().has($clicked_menu).length && !$(this).is($clicked_menu)) {
978 $(this).removeClass('opened').next().hide();
981 $clicked_menu.toggleClass('opened').next().toggle();
982 } else if ($clicked_menu.is('.leaf')) {
983 $sub_menu.toggle(!this.folded);
986 } else if (this.folded) {
987 if (active && sub_menu_visible) {
997 do_hide_secondary: function() {
998 this.$secondary_menu.hide();
1000 do_show_secondary: function($sub_menu, $main_menu) {
1002 this.$secondary_menu.show();
1003 if (!arguments.length) {
1007 var css = $main_menu.position(),
1008 fold_width = this.$secondary_menu.width() + 2,
1009 window_width = $(window).width();
1011 css.left -= Math.round(($sub_menu.width() - $main_menu.width()) / 2);
1012 css.left = css.left < fold_width ? fold_width : css.left;
1013 if ((css.left + $sub_menu.width()) > window_width) {
1018 $sub_menu.mouseenter(function() {
1019 clearTimeout($sub_menu.data('timeoutId'));
1020 $sub_menu.data('timeoutId', null);
1022 }).mouseleave(function(evt) {
1023 var timeoutId = setTimeout(function() {
1024 if (self.folded && $sub_menu.data('timeoutId')) {
1025 $sub_menu.hide().unbind('mouseenter').unbind('mouseleave');
1027 }, self.float_timeout);
1028 $sub_menu.data('timeoutId', timeoutId);
1034 on_menu_action_loaded: function(data) {
1036 if (data.action.length) {
1037 var action = data.action[0][2];
1038 action.from_menu = true;
1039 self.on_action(action);
1041 self.on_action({type: 'null_action'});
1044 on_action: function(action) {
1048 openerp.web.WebClient = openerp.web.Widget.extend(/** @lends openerp.web.WebClient */{
1050 * @constructs openerp.web.WebClient
1051 * @extends openerp.web.Widget
1055 init: function(parent) {
1057 this._super(parent);
1058 openerp.webclient = this;
1060 this._current_state = null;
1064 if (jQuery.param != undefined && jQuery.deparam(jQuery.param.querystring()).kitten != undefined) {
1065 this.$element.addClass("kitten-mode-activated");
1066 this.$element.delegate('img.oe-record-edit-link-img', 'hover', function(e) {
1067 self.$element.toggleClass('clark-gable');
1070 this.session.bind().then(function() {
1071 if (!self.session.session_is_valid()) {
1075 this.session.on_session_valid.add(function() {
1076 self.show_application();
1078 self.header.do_update();
1079 self.menu.do_reload();
1080 if(self.action_manager)
1081 self.action_manager.stop();
1082 self.action_manager = new openerp.web.ActionManager(self);
1083 self.action_manager.appendTo($("#oe_app"));
1084 self.bind_hashchange();
1085 if (!self.session.openerp_entreprise) {
1086 self.$element.find('.oe_footer_powered').append('<span> - <a href="http://www.openerp.com/support-or-publisher-warranty-contract" target="_blank">Unsupported/Community Version</a></span>');
1087 document.title = _t("OpenERP - Unsupported/Community Version");
1091 show_login: function() {
1093 this.destroy_content();
1095 self.login = new openerp.web.Login(self);
1096 self.login.appendTo(self.$element);
1098 show_application: function() {
1100 this.destroy_content();
1102 self.$table = $(QWeb.render("Interface", {}));
1103 self.$element.append(self.$table);
1104 self.header = new openerp.web.Header(self);
1105 self.header.on_logout.add(this.proxy('on_logout'));
1106 self.header.on_action.add(this.proxy('on_menu_action'));
1107 self.header.appendTo($("#oe_header"));
1108 self.menu = new openerp.web.Menu(self, "oe_menu", "oe_secondary_menu");
1109 self.menu.on_action.add(this.proxy('on_menu_action'));
1112 show_common: function() {
1114 self.crashmanager = new openerp.web.CrashManager();
1115 self.notification = new openerp.web.Notification(self);
1116 self.notification.appendTo(self.$element);
1117 self.loading = new openerp.web.Loading(self);
1118 self.loading.appendTo(self.$element);
1120 destroy_content: function() {
1121 _.each(_.clone(this.widget_children), function(el) {
1124 this.$element.children().remove();
1126 do_reload: function() {
1128 return this.session.session_reload().pipe(function () {
1129 openerp.connection.load_modules(true).pipe(
1130 self.menu.proxy('do_reload')); });
1133 do_notify: function() {
1134 var n = this.notification;
1135 n.notify.apply(n, arguments);
1137 do_warn: function() {
1138 var n = this.notification;
1139 n.warn.apply(n, arguments);
1141 on_logout: function() {
1142 this.session.session_logout();
1143 $(window).unbind('hashchange', this.on_hashchange);
1144 this.do_push_state({});
1145 //would be cool to be able to do this, but I think it will make addons do strange things
1146 //this.show_login();
1147 window.location.reload();
1149 bind_hashchange: function() {
1150 $(window).bind('hashchange', this.on_hashchange);
1152 var state = $.bbq.getState(true);
1153 if (! _.isEmpty(state)) {
1154 $(window).trigger('hashchange');
1156 this.action_manager.do_action({type: 'ir.actions.client', tag: 'default_home'});
1159 on_hashchange: function(event) {
1160 var state = event.getState(true);
1161 if (!_.isEqual(this._current_state, state)) {
1162 this.action_manager.do_load_state(state);
1164 this._current_state = state;
1166 do_push_state: function(state) {
1167 var url = '#' + $.param(state);
1168 this._current_state = _.clone(state);
1169 $.bbq.pushState(url);
1171 on_menu_action: function(action) {
1172 this.action_manager.do_action(action);
1174 do_action: function(action) {
1176 // TODO replace by client action menuclick
1177 if(action.menu_id) {
1178 this.do_reload().then(function () {
1179 self.menu.on_menu_click(null, action.menu_id);
1185 openerp.web.EmbeddedClient = openerp.web.Widget.extend({
1186 template: 'EmptyComponent',
1187 init: function(action_id, options) {
1189 // TODO take the xmlid of a action instead of its id
1190 this.action_id = action_id;
1191 this.options = options || {};
1192 this.am = new openerp.web.ActionManager(this);
1197 this.am.appendTo(this.$element.addClass('openerp'));
1198 return this.rpc("/web/action/load", { action_id: this.action_id }, function(result) {
1199 var action = result.result;
1200 action.flags = _.extend({
1201 //views_switcher : false,
1202 search_view : false,
1203 action_buttons : false,
1206 }, self.options, action.flags || {});
1208 self.am.do_action(action);
1214 openerp.web.embed = function (origin, dbname, login, key, action, options) {
1215 $('head').append($('<link>', {
1216 'rel': 'stylesheet',
1218 'href': origin +'/web/webclient/css'
1220 var currentScript = document.currentScript;
1221 if (!currentScript) {
1222 var sc = document.getElementsByTagName('script');
1223 currentScript = sc[sc.length-1];
1225 openerp.connection.bind(origin).then(function () {
1226 openerp.connection.session_authenticate(dbname, login, key, true).then(function () {
1227 var client = new openerp.web.EmbeddedClient(action, options);
1228 client.insertAfter(currentScript);
1236 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: