1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.base.chrome = function(openerp) {
7 openerp.base.Notification = openerp.base.Widget.extend({
8 init: function(parent, element_id) {
9 this._super(parent, element_id);
10 this.$element.notify({
15 notify: function(title, text) {
16 this.$element.notify('create', {
21 warn: function(title, text) {
22 this.$element.notify('create', 'oe_notification_alert', {
29 openerp.base.Dialog = openerp.base.OldWidget.extend({
31 identifier_prefix: 'dialog',
32 init: function (parent, dialog_options) {
35 this.dialog_options = {
45 beforeClose: function () { self.on_close(); }
48 if (f.substr(0, 10) == 'on_button_') {
49 this.dialog_options.buttons[f.substr(10)] = this[f];
53 this.set_options(dialog_options);
56 set_options: function(options) {
57 options = options || {};
58 options.width = this.get_width(options.width || this.dialog_options.width);
59 options.min_width = this.get_width(options.min_width || this.dialog_options.min_width);
60 options.max_width = this.get_width(options.max_width || this.dialog_options.max_width);
61 options.height = this.get_height(options.height || this.dialog_options.height);
62 options.min_height = this.get_height(options.min_height || this.dialog_options.min_height);
63 options.max_height = this.get_height(options.max_height || this.dialog_options.max_width);
65 if (options.width !== 'auto') {
66 if (options.width > options.max_width) options.width = options.max_width;
67 if (options.width < options.min_width) options.width = options.min_width;
69 if (options.height !== 'auto') {
70 if (options.height > options.max_height) options.height = options.max_height;
71 if (options.height < options.min_height) options.height = options.min_height;
73 if (!options.title && this.dialog_title) {
74 options.title = this.dialog_title;
76 _.extend(this.dialog_options, options);
78 get_width: function(val) {
79 return this.get_size(val.toString(), $(window.top).width());
81 get_height: function(val) {
82 return this.get_size(val.toString(), $(window.top).height());
84 get_size: function(val, available_size) {
87 } else if (val.slice(-1) == "%") {
88 return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10));
90 return parseInt(val, 10);
94 this.$dialog = $('<div id="' + this.element_id + '"></div>').dialog(this.dialog_options);
98 open: function(dialog_options) {
99 // TODO fme: bind window on resize
101 this.$element.html(this.render());
103 this.set_options(dialog_options);
104 this.$dialog.dialog(this.dialog_options).dialog('open');
107 // Closes the dialog but leave it in a state where it could be opened again.
108 this.$dialog.dialog('close');
110 on_close: function() {
115 this.$dialog.dialog('destroy');
119 openerp.base.CrashManager = openerp.base.Dialog.extend({
120 identifier_prefix: 'dialog_crash',
121 init: function(parent) {
123 this.session.on_rpc_error.add(this.on_rpc_error);
125 on_button_Ok: function() {
128 on_rpc_error: function(error) {
130 if (error.data.fault_code) {
131 var split = error.data.fault_code.split('\n')[0].split(' -- ');
132 if (split.length > 1) {
133 error.type = split.shift();
134 error.data.fault_code = error.data.fault_code.substr(error.type.length + 4);
137 if (error.code === 200 && error.type) {
138 this.dialog_title = "OpenERP " + _.capitalize(error.type);
139 this.template = 'DialogWarning';
145 this.dialog_title = "OpenERP Error";
146 this.template = 'DialogTraceback';
155 openerp.base.Loading = openerp.base.Widget.extend({
156 init: function(parent, element_id) {
157 this._super(parent, element_id);
159 this.session.on_rpc_request.add_first(this.on_rpc_event, 1);
160 this.session.on_rpc_response.add_last(this.on_rpc_event, -1);
162 on_rpc_event : function(increment) {
163 this.count += increment;
165 //this.$element.html(QWeb.render("Loading", {}));
166 this.$element.html("Loading ("+this.count+")");
167 this.$element.show();
169 this.$element.fadeOut();
174 openerp.base.Database = openerp.base.Widget.extend({
175 init: function(parent, element_id, option_id) {
176 this._super(parent, element_id);
177 this.$option_id = $('#' + option_id);
180 this.$element.html(QWeb.render("Database", this));
181 this.$element.closest(".openerp")
182 .removeClass("login-mode")
183 .addClass("database_block");
187 var fetch_db = this.rpc("/base/database/get_list", {}, function(result) {
188 self.db_list = result.db_list;
190 var fetch_langs = this.rpc("/base/session/get_lang_list", {}, function(result) {
192 self.display_error(result);
195 self.lang_list = result.lang_list;
197 $.when(fetch_db, fetch_langs).then(function () {self.do_create();});
199 this.$element.find('#db-create').click(this.do_create);
200 this.$element.find('#db-drop').click(this.do_drop);
201 this.$element.find('#db-backup').click(this.do_backup);
202 this.$element.find('#db-restore').click(this.do_restore);
203 this.$element.find('#db-change-password').click(this.do_change_password);
204 this.$element.find('#back-to-login').click(function() {
209 this.$option_id.empty();
212 .find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login')
216 .addClass("login-mode")
217 .removeClass("database_block")
223 * Converts a .serializeArray() result into a dict. Does not bother folding
224 * multiple identical keys into an array, last key wins.
226 * @param {Array} array
228 to_object: function (array) {
230 _(array).each(function (record) {
231 result[record.name] = record.value;
236 * Waits until the new database is done creating, then unblocks the UI and
237 * logs the user in as admin
239 * @param {Number} db_creation_id identifier for the db-creation operation, used to fetch the current installation progress
240 * @param {Object} info info fields for this database creation
241 * @param {String} info.db name of the database being created
242 * @param {String} info.password super-admin password for the database
244 wait_for_newdb: function (db_creation_id, info) {
246 self.rpc('/base/database/progress', {
248 password: info.password
249 }, function (result) {
250 var progress = result[0];
251 // I'd display a progress bar, but turns out the progress status
252 // the server report kind-of blows goats: it's at 0 for ~75% of
253 // the installation, then jumps to 75%, then jumps down to either
254 // 0 or ~40%, then back up to 75%, then terminates. Let's keep that
255 // mess hidden behind a not-very-useful but not overly weird
258 setTimeout(function () {
259 self.wait_for_newdb(db_creation_id, info);
264 var admin = result[1][0];
265 setTimeout(function () {
267 self.widget_parent.do_login(
268 info.db, admin.login, admin.password);
274 * Displays an error dialog resulting from the various RPC communications
275 * failing over themselves
277 * @param {Object} error error description
278 * @param {String} error.title title of the error dialog
279 * @param {String} error.error message of the error dialog
281 display_error: function (error) {
282 return $('<div>').dialog({
287 $(this).dialog("close");
290 }).html(error.error);
292 do_create: function() {
294 self.$option_id.html(QWeb.render("CreateDB", self));
296 self.$option_id.find("form[name=create_db_form]").validate({
297 submitHandler: function (form) {
298 var fields = $(form).serializeArray();
300 self.rpc("/base/database/create", {'fields': fields}, function(result) {
303 self.display_error(result);
306 self.db_list.push(self.to_object(fields)['db_name']);
308 var form_obj = self.to_object(fields);
309 self.wait_for_newdb(result, {
310 password: form_obj['super_admin_pwd'],
311 db: form_obj['db_name']
318 do_drop: function() {
320 self.$option_id.html(QWeb.render("DropDB", self));
322 self.$option_id.find("form[name=drop_db_form]").validate({
323 submitHandler: function (form) {
325 fields = $form.serializeArray(),
326 $db_list = $form.find('select[name=drop_db]'),
329 if (!confirm("Do you really want to delete the database: " + db + " ?")) {
332 self.rpc("/base/database/drop", {'fields': fields}, function(result) {
334 self.display_error(result);
337 $db_list.find(':selected').remove();
338 self.db_list.splice(_.indexOf(self.db_list, db, true), 1);
339 self.notification.notify("Dropping database", "The database '" + db + "' has been dropped");
345 wait_for_file: function (token, cleanup) {
347 cookie_name = 'fileToken',
348 cookie_length = cookie_name.length;
349 this.backup_timer = setInterval(function () {
350 var cookies = document.cookie.split(';');
351 for(var i=0; i<cookies.length; ++i) {
352 var cookie = cookies[i].replace(/^\s*/, '');
353 if(!cookie.indexOf(cookie_name) === 0) { continue; }
354 var cookie_val = cookie.substring(cookie_length + 1);
355 if(parseInt(cookie_val, 10) !== token) { continue; }
358 clearInterval(self.backup_timer);
360 document.cookie = _.sprintf("%s=;expires=%s;path=/",
361 cookie_name, new Date().toGMTString());
363 if (cleanup) { cleanup(); }
367 do_backup: function() {
369 self.$option_id.html(QWeb.render("BackupDB", self));
371 self.$option_id.find("form[name=backup_db_form]").validate({
372 submitHandler: function (form) {
374 // need to detect when the file is done downloading (not used
375 // yet, but we'll need it to fix the UI e.g. with a throbber
376 // while dump is being generated), iframe load event only fires
377 // when the iframe content loads, so we need to go smarter:
378 // http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
379 var $target = $('#backup-target'),
380 token = new Date().getTime();
381 if (!$target.length) {
382 $target = $('<iframe id="backup-target" style="display: none;">')
383 .appendTo(document.body)
386 clearInterval(self.backup_timer);
387 var error = this.contentDocument.body
396 $(form).find('input[name=token]').val(token);
399 self.wait_for_file(token, function () {
406 do_restore: function() {
408 self.$option_id.html(QWeb.render("RestoreDB", self));
410 self.$option_id.find("form[name=restore_db_form]").validate({
411 submitHandler: function (form) {
414 url: '/base/database/restore',
417 success: function (body) {
418 // TODO: ui manipulations
419 // note: response objects don't work, but we have the
420 // HTTP body of the response~~
422 // If empty body, everything went fine
423 if (!body) { return; }
425 if (body.indexOf('403 Forbidden') !== -1) {
427 title: 'Access Denied',
428 error: 'Incorrect super-administrator password'
432 title: 'Restore Database',
433 error: 'Could not restore the database'
437 complete: function () {
445 do_change_password: function() {
447 self.$option_id.html(QWeb.render("Change_DB_Pwd", self));
449 self.$option_id.find("form[name=change_pwd_form]").validate({
451 old_pwd: "Please enter your previous password",
452 new_pwd: "Please enter your new password",
454 required: "Please confirm your new password",
455 equalTo: "The confirmation does not match the password"
458 submitHandler: function (form) {
459 self.rpc("/base/database/change_password", {
460 'fields': $(form).serializeArray()
461 }, function(result) {
463 self.display_error(result);
466 self.notification.notify("Changed Password", "Password has been changed successfully");
473 openerp.base.Login = openerp.base.Widget.extend({
474 remember_creditentials: true,
476 init: function(parent, element_id) {
477 this._super(parent, element_id);
478 this.has_local_storage = typeof(localStorage) != 'undefined';
479 this.selected_db = null;
480 this.selected_login = null;
482 if (this.has_local_storage && this.remember_creditentials) {
483 this.selected_db = localStorage.getItem('last_db_login_success');
484 this.selected_login = localStorage.getItem('last_login_login_success');
486 if (jQuery.deparam(jQuery.param.querystring()).debug != undefined) {
487 this.selected_db = this.selected_db || "trunk";
488 this.selected_login = this.selected_login || "admin";
489 this.selected_password = this.selected_password || "a";
494 this.rpc("/base/database/get_list", {}, function(result) {
495 self.db_list = result.db_list;
501 display: function() {
504 this.$element.html(QWeb.render("Login", this));
505 this.database = new openerp.base.Database(
506 this, "oe_database", "oe_db_options");
508 this.$element.find('#oe-db-config').click(function() {
509 self.database.start();
512 this.$element.find("form").submit(this.on_submit);
514 on_login_invalid: function() {
515 this.$element.closest(".openerp").addClass("login-mode");
517 on_login_valid: function() {
518 this.$element.closest(".openerp").removeClass("login-mode");
520 on_submit: function(ev) {
522 var $e = this.$element;
523 var db = $e.find("form [name=db]").val();
524 var login = $e.find("form input[name=login]").val();
525 var password = $e.find("form input[name=password]").val();
527 this.do_login(db, login, password);
530 * Performs actual login operation, and UI-related stuff
532 * @param {String} db database to log in
533 * @param {String} login user login
534 * @param {String} password user password
536 do_login: function (db, login, password) {
538 this.session.session_login(db, login, password, function() {
539 if(self.session.session_is_valid()) {
540 if (self.has_local_storage) {
541 if(self.remember_creditentials) {
542 localStorage.setItem('last_db_login_success', db);
543 localStorage.setItem('last_login_login_success', login);
545 localStorage.setItem('last_db_login_success', '');
546 localStorage.setItem('last_login_login_success', '');
549 self.on_login_valid();
551 self.$element.addClass("login_invalid");
552 self.on_login_invalid();
556 do_ask_login: function(continuation) {
557 this.on_login_invalid();
559 .removeClass("login_invalid");
560 this.on_login_valid.add({
563 callback: continuation
566 on_logout: function() {
567 this.session.logout();
571 openerp.base.Header = openerp.base.Widget.extend({
572 init: function(parent, element_id) {
573 this._super(parent, element_id);
578 do_update: function() {
579 this.$element.html(QWeb.render("Header", this));
580 this.$element.find(".logout").click(this.on_logout);
582 on_logout: function() {}
585 openerp.base.Menu = openerp.base.Widget.extend({
586 init: function(parent, element_id, secondary_menu_id) {
587 this._super(parent, element_id);
588 this.secondary_menu_id = secondary_menu_id;
589 this.$secondary_menu = $("#" + secondary_menu_id).hide();
593 this.rpc("/base/menu/load", {}, this.on_loaded);
595 on_loaded: function(data) {
597 this.$element.html(QWeb.render("Menu", this.data));
598 for (var i = 0; i < this.data.data.children.length; i++) {
599 var v = { menu : this.data.data.children[i] };
600 this.$secondary_menu.append(QWeb.render("Menu.secondary", v));
602 this.$secondary_menu.find("div.menu_accordion").accordion({
607 this.$secondary_menu.find("div.submenu_accordion").accordion({
615 this.$element.add(this.$secondary_menu).find("a").click(this.on_menu_click);
617 on_menu_click: function(ev, id) {
619 var $menu, $parent, $secondary;
622 // We can manually activate a menu with it's id (for hash url mapping)
623 $menu = this.$element.find('a[data-menu=' + id + ']');
625 $menu = this.$secondary_menu.find('a[data-menu=' + id + ']');
628 $menu = $(ev.currentTarget);
629 id = $menu.data('menu');
631 if (this.$secondary_menu.has($menu).length) {
632 $secondary = $menu.parents('.menu_accordion');
633 $parent = this.$element.find('a[data-menu=' + $secondary.data('menu-parent') + ']');
636 $secondary = this.$secondary_menu.find('.menu_accordion[data-menu-parent=' + $menu.attr('data-menu') + ']');
639 this.$secondary_menu.find('.menu_accordion').hide();
640 // TODO: ui-accordion : collapse submenus and expand the good one
644 this.rpc('/base/menu/action', {'menu_id': id},
645 this.on_menu_action_loaded);
648 $('.active', this.$element.add(this.$secondary_menu.show())).removeClass('active');
649 $parent.addClass('active');
650 $menu.addClass('active');
651 $menu.parent('h4').addClass('active');
653 return !$menu.is(".leaf");
655 on_menu_action_loaded: function(data) {
657 if (data.action.length) {
658 var action = data.action[0][2];
659 self.on_action(action);
662 on_action: function(action) {
666 openerp.base.Homepage = openerp.base.Widget.extend({
669 openerp.base.Preferences = openerp.base.Widget.extend({
672 openerp.base.WebClient = openerp.base.Widget.extend({
673 init: function(element_id) {
674 this._super(null, element_id);
675 openerp.webclient = this;
677 QWeb.add_template("/base/static/src/xml/base.xml");
679 if(jQuery.param != undefined && jQuery.deparam(jQuery.param.querystring()).kitten != undefined) {
680 this.$element.addClass("kitten-mode-activated");
682 this.$element.html(QWeb.render("Interface", params));
684 this.session = new openerp.base.Session(this,"oe_errors");
685 this.loading = new openerp.base.Loading(this,"oe_loading");
686 this.crashmanager = new openerp.base.CrashManager(this);
687 this.crashmanager.start();
689 // Do you autorize this ? will be replaced by notify() in controller
690 openerp.base.Widget.prototype.notification = new openerp.base.Notification(this, "oe_notification");
692 this.header = new openerp.base.Header(this, "oe_header");
693 this.login = new openerp.base.Login(this, "oe_login");
694 this.header.on_logout.add(this.login.on_logout);
696 this.session.on_session_invalid.add(this.login.do_ask_login);
697 this.session.on_session_valid.add_last(this.header.do_update);
698 this.session.on_session_valid.add_last(this.on_logged);
700 this.menu = new openerp.base.Menu(this, "oe_menu", "oe_secondary_menu");
701 this.menu.on_action.add(this.on_menu_action);
705 this.session.start();
709 this.notification.notify("OpenERP Client", "The openerp client has been initialized.");
711 on_logged: function() {
712 if(this.action_manager)
713 this.action_manager.stop();
714 this.action_manager = new openerp.base.ActionManager(this);
715 this.action_manager.appendTo($("#oe_app"));
717 // if using saved actions, load the action and give it to action manager
718 var parameters = jQuery.deparam(jQuery.param.querystring());
719 if (parameters["s_action"] != undefined) {
720 var key = parseInt(parameters["s_action"], 10);
722 this.rpc("/base/session/get_session_action", {key:key}, function(action) {
723 self.action_manager.do_action(action);
725 } else if (openerp._modules_loaded) { // TODO: find better option than this
726 this.load_url_state()
728 this.session.on_modules_loaded.add({
729 callback: $.proxy(this, 'load_url_state'),
736 * Loads state from URL if any, or checks if there is a home action and
737 * loads that, assuming we're at the index
739 load_url_state: function () {
741 // TODO: add actual loading if there is url state to unpack, test on window.location.hash
744 if (!this.session.uid) { return; }
745 var ds = new openerp.base.DataSetSearch(this, 'res.users');
746 ds.read_ids([this.session.uid], ['action_id'], function (users) {
747 var home_action = users[0].action_id;
752 self.execute_home_action(home_action[0], ds);
755 default_home: function () {
758 * Bundles the execution of the home action
760 * @param {Number} action action id
761 * @param {openerp.base.DataSet} dataset action executor
763 execute_home_action: function (action, dataset) {
765 this.rpc('/base/action/load', {
767 context: dataset.get_context()
769 var action = meh.result;
770 action.context = _.extend(action.context || {}, {
773 active_model: dataset.model
775 self.action_manager.do_action(action);
778 on_menu_action: function(action) {
779 this.action_manager.do_action(action);
781 do_about: function() {
785 openerp.base.webclient = function(element_id) {
786 // TODO Helper to start webclient rename it openerp.base.webclient
787 var client = new openerp.base.WebClient(element_id);
794 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: