/*--------------------------------------------------------- * OpenERP Web chrome *---------------------------------------------------------*/ openerp.base.chrome = function(openerp) { openerp.base.Notification = openerp.base.Widget.extend({ init: function(parent, element_id) { this._super(parent, element_id); this.$element.notify({ speed: 500, expires: 1500 }); }, notify: function(title, text) { this.$element.notify('create', { title: title, text: text }); }, warn: function(title, text) { this.$element.notify('create', 'oe_notification_alert', { title: title, text: text }); } }); openerp.base.Dialog = openerp.base.OldWidget.extend({ dialog_title: "", identifier_prefix: 'dialog', init: function (parent, dialog_options) { var self = this; this._super(parent); this.dialog_options = { modal: true, width: 'auto', min_width: 0, max_width: '100%', height: 'auto', min_height: 0, max_height: '100%', autoOpen: false, buttons: {}, beforeClose: function () { self.on_close(); } }; for (var f in this) { if (f.substr(0, 10) == 'on_button_') { this.dialog_options.buttons[f.substr(10)] = this[f]; } } if (dialog_options) { this.set_options(dialog_options); } }, set_options: function(options) { options = options || {}; options.width = this.get_width(options.width || this.dialog_options.width); options.min_width = this.get_width(options.min_width || this.dialog_options.min_width); options.max_width = this.get_width(options.max_width || this.dialog_options.max_width); options.height = this.get_height(options.height || this.dialog_options.height); options.min_height = this.get_height(options.min_height || this.dialog_options.min_height); options.max_height = this.get_height(options.max_height || this.dialog_options.max_width); if (options.width !== 'auto') { if (options.width > options.max_width) options.width = options.max_width; if (options.width < options.min_width) options.width = options.min_width; } if (options.height !== 'auto') { if (options.height > options.max_height) options.height = options.max_height; if (options.height < options.min_height) options.height = options.min_height; } if (!options.title && this.dialog_title) { options.title = this.dialog_title; } _.extend(this.dialog_options, options); }, get_width: function(val) { return this.get_size(val.toString(), $(window.top).width()); }, get_height: function(val) { return this.get_size(val.toString(), $(window.top).height()); }, get_size: function(val, available_size) { if (val === 'auto') { return val; } else if (val.slice(-1) == "%") { return Math.round(available_size / 100 * parseInt(val.slice(0, -1), 10)); } else { return parseInt(val, 10); } }, start: function () { this.$dialog = $('
').dialog(this.dialog_options); this._super(); return this; }, open: function(dialog_options) { // TODO fme: bind window on resize if (this.template) { this.$element.html(this.render()); } this.set_options(dialog_options); this.$dialog.dialog(this.dialog_options).dialog('open'); }, close: function() { // Closes the dialog but leave it in a state where it could be opened again. this.$dialog.dialog('close'); }, on_close: function() { }, stop: function () { // Destroy widget this.close(); this.$dialog.dialog('destroy'); } }); openerp.base.CrashManager = openerp.base.Dialog.extend({ identifier_prefix: 'dialog_crash', init: function(parent) { this._super(parent); this.session.on_rpc_error.add(this.on_rpc_error); }, on_button_Ok: function() { this.close(); }, on_rpc_error: function(error) { this.error = error; if (error.data.fault_code) { var split = error.data.fault_code.split('\n')[0].split(' -- '); if (split.length > 1) { error.type = split.shift(); error.data.fault_code = error.data.fault_code.substr(error.type.length + 4); } } if (error.code === 200 && error.type) { this.dialog_title = "OpenERP " + _.capitalize(error.type); this.template = 'DialogWarning'; this.open({ width: 'auto', height: 'auto' }); } else { this.dialog_title = "OpenERP Error"; this.template = 'DialogTraceback'; this.open({ width: 'auto', height: 'auto' }); } } }); openerp.base.Loading = openerp.base.Widget.extend({ init: function(parent, element_id) { this._super(parent, element_id); this.count = 0; this.session.on_rpc_request.add_first(this.on_rpc_event, 1); this.session.on_rpc_response.add_last(this.on_rpc_event, -1); }, on_rpc_event : function(increment) { this.count += increment; if (this.count) { //this.$element.html(QWeb.render("Loading", {})); this.$element.html("Loading ("+this.count+")"); this.$element.show(); } else { this.$element.fadeOut(); } } }); openerp.base.Database = openerp.base.Widget.extend({ init: function(parent, element_id, option_id) { this._super(parent, element_id); this.$option_id = $('#' + option_id); }, start: function() { this.$element.html(QWeb.render("Database", this)); this.$element.closest(".openerp") .removeClass("login-mode") .addClass("database_block"); var self = this; var fetch_db = this.rpc("/base/database/get_list", {}, function(result) { self.db_list = result.db_list; }); var fetch_langs = this.rpc("/base/session/get_lang_list", {}, function(result) { if (result.error) { self.display_error(result); return; } self.lang_list = result.lang_list; }); $.when(fetch_db, fetch_langs).then(function () {self.do_create();}); this.$element.find('#db-create').click(this.do_create); this.$element.find('#db-drop').click(this.do_drop); this.$element.find('#db-backup').click(this.do_backup); this.$element.find('#db-restore').click(this.do_restore); this.$element.find('#db-change-password').click(this.do_change_password); this.$element.find('#back-to-login').click(function() { self.stop(); }); }, stop: function () { this.$option_id.empty(); this.$element .find('#db-create, #db-drop, #db-backup, #db-restore, #db-change-password, #back-to-login') .unbind('click') .end() .closest(".openerp") .addClass("login-mode") .removeClass("database_block") .end() .empty(); }, /** * Converts a .serializeArray() result into a dict. Does not bother folding * multiple identical keys into an array, last key wins. * * @param {Array} array */ to_object: function (array) { var result = {}; _(array).each(function (record) { result[record.name] = record.value; }); return result; }, /** * Waits until the new database is done creating, then unblocks the UI and * logs the user in as admin * * @param {Number} db_creation_id identifier for the db-creation operation, used to fetch the current installation progress * @param {Object} info info fields for this database creation * @param {String} info.db name of the database being created * @param {String} info.password super-admin password for the database */ wait_for_newdb: function (db_creation_id, info) { var self = this; self.rpc('/base/database/progress', { id: db_creation_id, password: info.password }, function (result) { var progress = result[0]; // I'd display a progress bar, but turns out the progress status // the server report kind-of blows goats: it's at 0 for ~75% of // the installation, then jumps to 75%, then jumps down to either // 0 or ~40%, then back up to 75%, then terminates. Let's keep that // mess hidden behind a not-very-useful but not overly weird // message instead. if (progress < 1) { setTimeout(function () { self.wait_for_newdb(db_creation_id, info); }, 500); return; } var admin = result[1][0]; setTimeout(function () { self.stop(); self.widget_parent.do_login( info.db, admin.login, admin.password); $.unblockUI(); }); }); }, /** * Displays an error dialog resulting from the various RPC communications * failing over themselves * * @param {Object} error error description * @param {String} error.title title of the error dialog * @param {String} error.error message of the error dialog */ display_error: function (error) { return $('
').dialog({ modal: true, title: error.title, buttons: { Ok: function() { $(this).dialog("close"); } } }).html(error.error); }, do_create: function() { var self = this; self.$option_id.html(QWeb.render("Database.CreateDB", self)); self.$option_id.find("form[name=create_db_form]").validate({ submitHandler: function (form) { var fields = $(form).serializeArray(); $.blockUI(); self.rpc("/base/database/create", {'fields': fields}, function(result) { if (result.error) { $.unblockUI(); self.display_error(result); return; } self.db_list.push(self.to_object(fields)['db_name']); self.db_list.sort(); var form_obj = self.to_object(fields); self.wait_for_newdb(result, { password: form_obj['super_admin_pwd'], db: form_obj['db_name'] }); }); } }); }, do_drop: function() { var self = this; self.$option_id.html(QWeb.render("DropDB", self)); self.$option_id.find("form[name=drop_db_form]").validate({ submitHandler: function (form) { var $form = $(form), fields = $form.serializeArray(), $db_list = $form.find('select[name=drop_db]'), db = $db_list.val(); if (!confirm("Do you really want to delete the database: " + db + " ?")) { return; } self.rpc("/base/database/drop", {'fields': fields}, function(result) { if (result.error) { self.display_error(result); return; } $db_list.find(':selected').remove(); self.db_list.splice(_.indexOf(self.db_list, db, true), 1); self.notification.notify("Dropping database", "The database '" + db + "' has been dropped"); }); } }); }, wait_for_file: function (token, cleanup) { var self = this, cookie_name = 'fileToken', cookie_length = cookie_name.length; this.backup_timer = setInterval(function () { var cookies = document.cookie.split(';'); for(var i=0; i