/*--------------------------------------------------------- * OpenERP Web core *--------------------------------------------------------*/ var console; if (!console) { console = {log: function () {}}; } if (!console.debug) { console.debug = console.log; } 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, success_callback, error_callback) { params.session_id = this.session_id; return this._super(url, params, success_callback, error_callback); }, /** * Setup a sessionm */ session_bind: function(origin) { var self = this; this.setup(origin); instance.web.qweb.default_dict['_s'] = this.origin; this.session_id = false; this.uid = false; this.username = false; this.user_context= {}; this.db = false; this.openerp_entreprise = false; this.module_list = instance._modules.slice(); this.module_loaded = {}; _(this.module_list).each(function (mod) { self.module_loaded[mod] = true; }); this.context = {}; this.active_id = null; return this.session_init(); }, /** * Init a session, reloads from cookie, if it exists */ session_init: function () { var self = this; // TODO: session store in cookie should be optional this.session_id = this.get_cookie('session_id'); return this.session_reload().pipe(function(result) { var modules = instance._modules.join(','); var deferred = self.rpc('/web/webclient/qweblist', {mods: modules}).pipe(self.do_load_qweb); if(self.session_is_valid()) { return deferred.pipe(function() { return self.load_modules(); }); } return deferred; }); }, /** * (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; return this.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, { session_id: result.session_id, db: result.db, username: result.login, uid: result.uid, user_context: result.context, openerp_entreprise: result.openerp_entreprise }); }); }, session_is_valid: function() { 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).pipe(function(result) { if (!result.uid) { return $.Deferred().reject(); } _.extend(self, { session_id: result.session_id, db: result.db, username: result.login, uid: result.uid, user_context: result.context, openerp_entreprise: result.openerp_entreprise }); if (!_volatile) { self.set_cookie('session_id', self.session_id); } return self.load_modules(); }); }, session_logout: function() { this.set_cookie('session_id', ''); return this.rpc("/web/session/destroy", {}); }, on_session_valid: function() { }, /** * Called when a rpc call fail due to an invalid session. * By default, it's a noop */ on_session_invalid: function(retry_callback) { }, /** * Fetches a cookie stored by an openerp session * * @private * @param name the cookie's name */ 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.get_url(file), 'rel': 'stylesheet', 'type': 'text/css' })); }); }, do_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.get_url(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).then(function () { d.resolve(); }); }; var head = document.head || document.getElementsByTagName('head')[0]; head.appendChild(tag); } else { d.resolve(); } return d; }, do_load_qweb: function(files) { var self = this; _.each(files, function(file) { self.qweb_mutex.exec(function() { return self.rpc('/web/proxy/load', {path: file}).pipe(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 = $('