diff options
| author | Xavier Morel <xmo@openerp.com> | 2011-08-16 16:25:15 +0200 |
|---|---|---|
| committer | Xavier Morel <xmo@openerp.com> | 2011-08-16 16:25:15 +0200 |
| commit | ffae7cb4b3e7350d19b273feb100ba73cd022214 (patch) | |
| tree | 15a06c6746e4d6f9d7042a269362ed72728eeceb /addons/base/static/src/js/chrome.js | |
| parent | d33c95fc4568516c22e12fdcce901d8250c9d218 (diff) | |
| parent | 36a1a0f1c3f6fe471453d52955f97110220599a3 (diff) | |
[MERGE] from trunk
bzr revid: xmo@openerp.com-20110816142515-ohq33zvua5uq8cd3
Diffstat (limited to 'addons/base/static/src/js/chrome.js')
| -rw-r--r-- | addons/base/static/src/js/chrome.js | 343 |
1 files changed, 30 insertions, 313 deletions
diff --git a/addons/base/static/src/js/chrome.js b/addons/base/static/src/js/chrome.js index ecce83342a2..927a000f002 100644 --- a/addons/base/static/src/js/chrome.js +++ b/addons/base/static/src/js/chrome.js @@ -1,287 +1,9 @@ /*--------------------------------------------------------- - * OpenERP base library + * OpenERP Web chrome *---------------------------------------------------------*/ openerp.base.chrome = function(openerp) { -openerp.base.Session = openerp.base.Widget.extend( /** @lends openerp.base.Session# */{ - /** - * @constructs - * @param element_id to use for exception reporting - * @param server - * @param port - */ - init: function(parent, element_id, server, port) { - this._super(parent, element_id); - this.server = (server == undefined) ? location.hostname : server; - this.port = (port == undefined) ? location.port : port; - this.rpc_mode = (server == location.hostname) ? "ajax" : "jsonp"; - this.debug = true; - this.db = ""; - this.login = ""; - this.password = ""; - this.uid = false; - this.session_id = false; - this.module_list = []; - this.module_loaded = {"base": true}; - this.context = {}; - this.shortcuts = []; - this.active_id = null; - }, - start: function() { - this.session_restore(); - }, - /** - * Executes an RPC call, registering the provided callbacks. - * - * Registers a default error callback if none is provided, and handles - * setting the correct session id and session context in the parameter - * objects - * - * @param {String} url RPC endpoint - * @param {Object} params call parameters - * @param {Function} success_callback function to execute on RPC call success - * @param {Function} error_callback function to execute on RPC call failure - * @returns {jQuery.Deferred} jquery-provided ajax deferred - */ - rpc: function(url, params, success_callback, error_callback) { - var self = this; - // Construct a JSON-RPC2 request, method is currently unused - params.session_id = this.session_id; - - // Call using the rpc_mode - var deferred = $.Deferred(); - this.rpc_ajax(url, { - jsonrpc: "2.0", - method: "call", - params: params, - id:null - }).then(function () {deferred.resolve.apply(deferred, arguments);}, - function(error) {deferred.reject(error, $.Event());}); - return deferred.fail(function() { - deferred.fail(function(error, event) { - if (!event.isDefaultPrevented()) { - self.on_rpc_error(error, event); - } - }); - }).then(success_callback, error_callback).promise(); - }, - /** - * Raw JSON-RPC call - * - * @returns {jQuery.Deferred} ajax-based deferred object - */ - rpc_ajax: function(url, payload) { - var self = this; - this.on_rpc_request(); - // url can be an $.ajax option object - if (_.isString(url)) { - url = { - url: url - } - } - var ajax = _.extend({ - type: "POST", - url: url, - dataType: 'json', - contentType: 'application/json', - data: JSON.stringify(payload), - processData: false - }, url); - var deferred = $.Deferred(); - $.ajax(ajax).done(function(response, textStatus, jqXHR) { - self.on_rpc_response(); - if (!response.error) { - deferred.resolve(response["result"], textStatus, jqXHR); - return; - } - if (response.error.data.type !== "session_invalid") { - deferred.reject(response.error); - return; - } - self.uid = false; - self.on_session_invalid(function() { - self.rpc(url, payload.params, - function() { - deferred.resolve.apply(deferred, arguments); - }, - function(error, event) { - event.preventDefault(); - deferred.reject.apply(deferred, arguments); - }); - }); - }).fail(function(jqXHR, textStatus, errorThrown) { - self.on_rpc_response(); - var error = { - code: -32098, - message: "XmlHttpRequestError " + errorThrown, - data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] } - }; - deferred.reject(error); - }); - return deferred.promise(); - }, - on_rpc_request: function() { - }, - on_rpc_response: function() { - }, - on_rpc_error: function(error) { - }, - /** - * The session is validated either by login or by restoration of a previous session - */ - on_session_valid: function() { - if(!openerp._modules_loaded) - this.load_modules(); - }, - on_session_invalid: function(contination) { - }, - session_is_valid: function() { - return this.uid; - }, - session_login: function(db, login, password, success_callback) { - var self = this; - this.db = db; - this.login = login; - this.password = password; - var params = { db: this.db, login: this.login, password: this.password }; - this.rpc("/base/session/login", params, function(result) { - self.session_id = result.session_id; - self.uid = result.uid; - self.session_save(); - self.on_session_valid(); - if (success_callback) - success_callback(); - }); - }, - session_logout: function() { - this.uid = false; - }, - /** - * Reloads uid and session_id from local storage, if they exist - */ - session_restore: function () { - this.uid = this.get_cookie('uid'); - this.session_id = this.get_cookie('session_id'); - this.db = this.get_cookie('db'); - this.login = this.get_cookie('login'); - // we should do an rpc to confirm that this session_id is valid and if it is retrieve the information about db and login - // then call on_session_valid - this.on_session_valid(); - }, - /** - * Saves the session id and uid locally - */ - session_save: function () { - this.set_cookie('uid', this.uid); - this.set_cookie('session_id', this.session_id); - this.set_cookie('db', this.db); - this.set_cookie('login', this.login); - }, - logout: function() { - delete this.uid; - delete this.session_id; - delete this.db; - delete this.login; - this.set_cookie('uid', ''); - this.set_cookie('session_id', ''); - this.set_cookie('db', ''); - this.set_cookie('login', ''); - this.on_session_invalid(function() {}); - }, - /** - * Fetches a cookie stored by an openerp session - * - * @private - * @param name the cookie's name - */ - get_cookie: function (name) { - var nameEQ = this.element_id + '|' + name + '='; - var cookies = document.cookie.split(';'); - for(var i=0; i<cookies.length; ++i) { - var cookie = cookies[i].replace(/^\s*/, ''); - if(cookie.indexOf(nameEQ) === 0) { - return JSON.parse(decodeURIComponent(cookie.substring(nameEQ.length))); - } - } - return null; - }, - /** - * Create a new cookie with the provided name and value - * - * @private - * @param name the cookie's name - * @param value the cookie's value - * @param ttl the cookie's time to live, 1 year by default, set to -1 to delete - */ - set_cookie: function (name, value, ttl) { - ttl = ttl || 24*60*60*365; - document.cookie = [ - this.element_id + '|' + name + '=' + encodeURIComponent(JSON.stringify(value)), - 'max-age=' + ttl, - 'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString() - ].join(';'); - }, - /** - * Load additional web addons of that instance and init them - */ - load_modules: function() { - var self = this; - this.rpc('/base/session/modules', {}, function(result) { - self.module_list = result; - var modules = self.module_list.join(','); - if(self.debug || true) { - self.rpc('/base/webclient/csslist', {"mods": modules}, self.do_load_css); - self.rpc('/base/webclient/jslist', {"mods": modules}, self.do_load_js); - } else { - self.do_load_css(["/base/webclient/css?mods="+modules]); - self.do_load_js(["/base/webclient/js?mods="+modules]); - } - openerp._modules_loaded = true; - }); - }, - do_load_css: function (files) { - _.each(files, function (file) { - $('head').append($('<link>', { - 'href': file, - 'rel': 'stylesheet', - 'type': 'text/css' - })); - }); - }, - do_load_js: function(files) { - var self = this; - if(files.length != 0) { - var file = files.shift(); - var tag = document.createElement('script'); - tag.type = 'text/javascript'; - tag.src = file; - tag.onload = tag.onreadystatechange = function() { - if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done ) - return; - tag.onload_done = true; - self.do_load_js(files); - }; - document.head.appendChild(tag); - } else { - this.on_modules_loaded(); - } - }, - on_modules_loaded: function() { - for(var j=0; j<this.module_list.length; j++) { - var mod = this.module_list[j]; - if(this.module_loaded[mod]) - continue; - openerp[mod] = {}; - // init module mod - if(openerp._openerp[mod] != undefined) { - openerp._openerp[mod](openerp); - this.module_loaded[mod] = true; - } - } - } -}); - openerp.base.Notification = openerp.base.Widget.extend({ init: function(parent, element_id) { this._super(parent, element_id); @@ -307,10 +29,10 @@ openerp.base.Notification = openerp.base.Widget.extend({ openerp.base.Dialog = openerp.base.OldWidget.extend({ dialog_title: "", identifier_prefix: 'dialog', - init: function (parent, options) { + init: function (parent, dialog_options) { var self = this; this._super(parent); - this.options = { + this.dialog_options = { modal: true, width: 'auto', min_width: 0, @@ -320,27 +42,25 @@ openerp.base.Dialog = openerp.base.OldWidget.extend({ max_height: '100%', autoOpen: false, buttons: {}, - beforeClose: function () { - self.on_close(); - } + beforeClose: function () { self.on_close(); } }; for (var f in this) { if (f.substr(0, 10) == 'on_button_') { - this.options.buttons[f.substr(10)] = this[f]; + this.dialog_options.buttons[f.substr(10)] = this[f]; } } - if (options) { - this.set_options(options); + if (dialog_options) { + this.set_options(dialog_options); } }, set_options: function(options) { options = options || {}; - options.width = this.get_width(options.width || this.options.width); - options.min_width = this.get_width(options.min_width || this.options.min_width); - options.max_width = this.get_width(options.max_width || this.options.max_width); - options.height = this.get_height(options.height || this.options.height); - options.min_height = this.get_height(options.min_height || this.options.min_height); - options.max_height = this.get_height(options.max_height || this.options.max_width); + 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; @@ -353,7 +73,7 @@ openerp.base.Dialog = openerp.base.OldWidget.extend({ if (!options.title && this.dialog_title) { options.title = this.dialog_title; } - _.extend(this.options, options); + _.extend(this.dialog_options, options); }, get_width: function(val) { return this.get_size(val.toString(), $(window.top).width()); @@ -370,20 +90,18 @@ openerp.base.Dialog = openerp.base.OldWidget.extend({ return parseInt(val, 10); } }, - start: function (auto_open) { - this.$dialog = $('<div id="' + this.element_id + '"></div>').dialog(this.options); - if (auto_open !== false) { - this.open(); - } + start: function () { + this.$dialog = $('<div id="' + this.element_id + '"></div>').dialog(this.dialog_options); this._super(); + return this; }, - open: function(options) { + open: function(dialog_options) { // TODO fme: bind window on resize if (this.template) { this.$element.html(this.render()); } - this.set_options(options); - this.$dialog.dialog(this.options).dialog('open'); + 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. @@ -913,7 +631,7 @@ openerp.base.Header = openerp.base.Widget.extend({ }, on_logout: function() { - this.$element.find('#shortcuts ul li').remove(); + this.$element.find('.oe-shortcuts ul').empty(); } }); @@ -1005,12 +723,10 @@ openerp.base.Homepage = openerp.base.Widget.extend({ openerp.base.Preferences = openerp.base.Widget.extend({ }); -openerp.base.ImportExport = openerp.base.Widget.extend({ -}); - openerp.base.WebClient = openerp.base.Widget.extend({ init: function(element_id) { this._super(null, element_id); + openerp.webclient = this; QWeb.add_template("/base/static/src/xml/base.xml"); var params = {}; @@ -1019,10 +735,10 @@ openerp.base.WebClient = openerp.base.Widget.extend({ } this.$element.html(QWeb.render("Interface", params)); - this.session = new openerp.base.Session(this,"oe_errors"); + this.session = new openerp.base.Session(); this.loading = new openerp.base.Loading(this,"oe_loading"); this.crashmanager = new openerp.base.CrashManager(this); - this.crashmanager.start(false); + this.crashmanager.start(); // Do you autorize this ? will be replaced by notify() in controller openerp.base.Widget.prototype.notification = new openerp.base.Notification(this, "oe_notification"); @@ -1038,8 +754,6 @@ openerp.base.WebClient = openerp.base.Widget.extend({ this.menu = new openerp.base.Menu(this, "oe_menu", "oe_secondary_menu"); this.menu.on_action.add(this.on_menu_action); this.header.on_action.add(this.on_menu_action); - - }, start: function() { this.session.start(); @@ -1049,8 +763,10 @@ openerp.base.WebClient = openerp.base.Widget.extend({ this.notification.notify("OpenERP Client", "The openerp client has been initialized."); }, on_logged: function() { - this.action_manager = new openerp.base.ActionManager(this, "oe_app"); - this.action_manager.start(); + if(this.action_manager) + this.action_manager.stop(); + this.action_manager = new openerp.base.ActionManager(this); + this.action_manager.appendTo($("#oe_app")); // if using saved actions, load the action and give it to action manager var parameters = jQuery.deparam(jQuery.param.querystring()); @@ -1090,7 +806,8 @@ openerp.base.WebClient = openerp.base.Widget.extend({ self.execute_home_action(home_action[0], ds); }) }, - default_home: function () { }, + default_home: function () { + }, /** * Bundles the execution of the home action * |
