/*--------------------------------------------------------- * OpenERP Web core *--------------------------------------------------------*/ var console; if (!console) { // Even IE9 only exposes console object if debug window opened console = {}; ('log error debug info warn assert clear dir dirxml trace group' + ' groupCollapsed groupEnd time timeEnd profile profileEnd count' + ' exception').split(/\s+/).forEach(function(property) { console[property] = _.identity; }); } openerp.web.coresetup = function(instance) { /** Session openerp specific RPC class */ instance.web.Session = instance.web.JsonRPC.extend( /** @lends instance.web.Session# */{ init: function() { this._super.apply(this, arguments); // TODO: session store in cookie should be optional this.name = instance._session_id; this.qweb_mutex = new $.Mutex(); }, rpc: function(url, params, options) { return this._super(url, params, options); }, /** * Setup a sessionm */ session_bind: function(origin) { if (!_.isUndefined(this.origin)) { if (this.origin === origin) { return $.when(); } throw new Error('Session already bound to ' + this.origin); } var self = this; this.setup(origin); instance.web.qweb.default_dict['_s'] = this.origin; this.uid = null; this.username = null; this.user_context= {}; this.db = null; this.module_list = instance._modules.slice(); this.module_loaded = {}; _(this.module_list).each(function (mod) { self.module_loaded[mod] = true; }); this.active_id = null; return this.session_init(); }, /** * Init a session, reloads from cookie, if it exists */ session_init: function () { var self = this; return this.session_reload().then(function(result) { var modules = instance._modules.join(','); var deferred = self.rpc('/web/webclient/qweblist', {mods: modules}).then(self.load_qweb.bind(self)); if(self.session_is_valid()) { return deferred.then(function() { return self.load_modules(); }); } return $.when( deferred, self.rpc('/web/webclient/bootstrap_translations', {mods: instance._modules}).then(function(trans) { instance.web._t.database.set_bundle(trans); }) ); }); }, /** * (re)loads the content of a session: db name, username, user id, session * context and status of the support contract * * @returns {$.Deferred} deferred indicating the session is done reloading */ session_reload: function () { var self = this; var def = $.when(); if (this.override_session) { if (! this.session_id) { def = this.rpc("/gen_session_id", {}).then(function(result) { self.session_id = result; }); } } else { this.session_id = this.get_cookie('session_id'); } return def.then(function() { return self.rpc("/web/session/get_session_info", {}) }).then(function(result) { // If immediately follows a login (triggered by trying to restore // an invalid session or no session at all), refresh session data // (should not change, but just in case...) _.extend(self, result); }); }, session_is_valid: function() { var db = $.deparam.querystring().db; if (db && this.db !== db) { return false; } return !!this.uid; }, /** * The session is validated either by login or by restoration of a previous session */ session_authenticate: function(db, login, password, _volatile) { var self = this; var base_location = document.location.protocol + '//' + document.location.host; var params = { db: db, login: login, password: password, base_location: base_location }; return this.rpc("/web/session/authenticate", params).then(function(result) { if (!result.uid) { return $.Deferred().reject(); } _.extend(self, result); return self.load_modules(); }); }, session_logout: function() { $.bbq.removeState(); return this.rpc("/web/session/destroy", {}); }, get_cookie: function (name) { if (!this.name) { return null; } var nameEQ = this.name + '|' + name + '='; var cookies = document.cookie.split(';'); for(var i=0; i', { 'href': self.url(file, null), 'rel': 'stylesheet', 'type': 'text/css' })); }); }, load_js: function(files) { var self = this; var d = $.Deferred(); if(files.length !== 0) { var file = files.shift(); var tag = document.createElement('script'); tag.type = 'text/javascript'; tag.src = self.url(file, null); tag.onload = tag.onreadystatechange = function() { if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done ) return; tag.onload_done = true; self.load_js(files).done(function () { d.resolve(); }); }; var head = document.head || document.getElementsByTagName('head')[0]; head.appendChild(tag); } else { d.resolve(); } return d; }, load_qweb: function(files) { var self = this; _.each(files, function(file) { self.qweb_mutex.exec(function() { return self.rpc('/web/proxy/load', {path: file}).then(function(xml) { if (!xml) { return; } instance.web.qweb.add_template(_.str.trim(xml)); }); }); }); return self.qweb_mutex.def; }, on_modules_loaded: function() { for(var j=0; j'); var complete = function () { if (options.complete) { options.complete(); } clearTimeout(timer); $form_data.remove(); $target.remove(); if (remove_form && $form) { $form.remove(); } }; var $target = $('