From 80657b1c29ec9dea947426fccc1ffc7b20a6d97a Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Mon, 24 Oct 2011 11:07:27 +0200 Subject: [IMP] support for jsonp bzr revid: chs@openerp.com-20111024090727-f8w5wv08ugxnrpt8 --- addons/web/static/src/js/core.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 4ed33341ea6..1b6a6302f72 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -346,12 +346,12 @@ openerp.web.Session = openerp.web.CallbackEnabled.extend( /** @lends openerp.web * @param {String} [server] JSON-RPC endpoint hostname * @param {String} [port] JSON-RPC endpoint port */ - init: function(server, port) { + init: function(server) { this._super(); - this.server = (server == undefined) ? location.hostname : server; - this.port = (port == undefined) ? location.port : port; - this.rpc_mode = (server == location.hostname) ? "ajax" : "jsonp"; - this.debug = (window.location.search.indexOf('?debug') !== -1); + var hostname = _('%s//%s').sprintf(location.protocol, location.host); + this.server = (server == undefined) ? hostname : server; + this.rpc_mode = (this.server == hostname) ? "oe-json" : "oe-jsonp"; + this.debug = ($.deparam($.param.querystring()).debug != undefined); this.session_id = false; this.uid = false; this.user_context= {}; @@ -390,13 +390,9 @@ openerp.web.Session = openerp.web.CallbackEnabled.extend( /** @lends openerp.web // Call using the rpc_mode var deferred = $.Deferred(); - this.rpc_ajax(url, { - jsonrpc: "2.0", - method: "call", - params: params, - id: _.uniqueId('browser-client-') - }).then(function () {deferred.resolve.apply(deferred, arguments);}, - function(error) {deferred.reject(error, $.Event());}); + this.rpc_ajax(url, params) + .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()) { @@ -422,10 +418,11 @@ openerp.web.Session = openerp.web.CallbackEnabled.extend( /** @lends openerp.web var ajax = _.extend({ type: "POST", url: url, - dataType: 'json', + dataType: this.rpc_mode, contentType: 'application/json', - data: JSON.stringify(payload), - processData: false + data: payload, + processData: false, + openerp: _.extend({}, this), // need a plainObject }, url); var deferred = $.Deferred(); $.ajax(ajax).done(function(response, textStatus, jqXHR) { @@ -440,7 +437,7 @@ openerp.web.Session = openerp.web.CallbackEnabled.extend( /** @lends openerp.web } self.uid = false; self.on_session_invalid(function() { - self.rpc(url, payload.params, + self.rpc(url, payload, function() { deferred.resolve.apply(deferred, arguments); }, -- cgit v1.2.3 From bccc64ac7c2a6671a15f74d13465c27b8f13e9d8 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Wed, 9 Nov 2011 09:16:26 +0100 Subject: [FIX] use absolute url when getting css/js/qweb files + use a static proxy for getting qweb files bzr revid: chs@openerp.com-20111109081626-l868h7w8glinbxdl --- addons/web/static/src/js/core.js | 49 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 951e6e2b01f..fac18f18734 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -349,7 +349,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. init: function(server) { this._super(); var hostname = _('%s//%s').sprintf(location.protocol, location.host); - this.server = (server == undefined) ? hostname : server; + this.server = _.rtrim((server == undefined) ? hostname : server, '/'); this.rpc_mode = (this.server == hostname) ? "oe-json" : "oe-jsonp"; this.debug = ($.deparam($.param.querystring()).debug != undefined); this.session_id = false; @@ -370,6 +370,29 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. start: function() { this.session_restore(); }, + + get_absolute_url: function(path) { + var r_has_protocol = /^https?:\/\//, + r_absolute_internal = /^\/[^\/]/; // starts with / (but not //) + + + if (r_has_protocol.test(path)) { + if (!_(path).startsWith(this.server + '/')) { + $.error('can only contact openerp.server'); + } + absolute_url = path; + } else if (r_absolute_internal.test(path)) { + absolute_url = this.server + path; + } else { // relative url + // XXX is it correct to mix document.location and this.server ?? + var parts = document.location.pathname.split('/'); + parts.pop(); + parts.push(path); + absolute_url = this.server + parts.join('/'); + } + return absolute_url + }, + /** * Executes an RPC call, registering the provided callbacks. * @@ -574,11 +597,11 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. self.user_context.lang.replace("_", "-") + ".js" ]; + self.rpc('/web/webclient/qweblist', {"mods": modules}, self.do_load_qweb); self.rpc('/web/webclient/csslist', {"mods": modules}, self.do_load_css); self.rpc('/web/webclient/jslist', {"mods": modules}, function(files) { self.do_load_js(file_list.concat(files)); }); - self.rpc('/web/webclient/qweblist', {"mods": modules}, self.do_load_qweb); openerp._modules_loaded = true; }); }); @@ -587,7 +610,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. var self = this; _.each(files, function (file) { $('head').append($('', { - 'href': file, + 'href': self.get_absolute_url(file), 'rel': 'stylesheet', 'type': 'text/css' })); @@ -599,7 +622,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. var file = files.shift(); var tag = document.createElement('script'); tag.type = 'text/javascript'; - tag.src = file; + tag.src = self.get_absolute_url(file); tag.onload = tag.onreadystatechange = function() { if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done ) return; @@ -615,7 +638,23 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. do_load_qweb: function(files) { var self = this; _.each(files, function(file) { - openerp.web.qweb.add_template(file); + self.rpc({ + //url: self.get_absolute_url(file), + url: '/web/jsonp/static_proxy', + async: false, + //dataType: 'text', + + }, + { + path: file, + }, + function(xml) { + //function(result) { + // console.log('do_load_qweb', result); + // var xml = result.result; + //}).then(function(xml) { + openerp.web.qweb.add_template(_(xml).trim()); + }); }); }, on_modules_loaded: function() { -- cgit v1.2.3 From 7e9d606238ed1fa00d2bf8b852987889566c49e0 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Tue, 15 Nov 2011 18:04:08 +0100 Subject: [IMP] Connection: new method to bind to a server. Must be called before login [IMP] loading of qweb/css/js files are now sequencial bzr revid: chs@openerp.com-20111115170408-u1i805s1854igyja --- addons/web/static/src/js/core.js | 134 +++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 49 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 7cd81d2a7c1..590438c09dc 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -346,29 +346,43 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. * @param {String} [server] JSON-RPC endpoint hostname * @param {String} [port] JSON-RPC endpoint port */ - init: function(server) { + init: function() { + + // FIXME need to be a real singleton. + // create more than one Connection on the same instance will not works + this._super(); - var hostname = _('%s//%s').sprintf(location.protocol, location.host); - this.server = _.rtrim((server == undefined) ? hostname : server, '/'); - this.rpc_mode = (this.server == hostname) ? "oe-json" : "oe-jsonp"; + this.server = null; this.debug = ($.deparam($.param.querystring()).debug != undefined); + // TODO: session should have an optional name indicating that they'll + // be saved to (and revived from) cookies + this.name = 'session'; + }, + + bind: function(server, continuation) { + + if(_.isFunction(server)) { + continuation = server; + server = null; + } + + if (this.server) { + throw new Error("Connection already bind to " + this.server); + } + var hostname = _.str.sprintf('%s//%s', location.protocol, location.host); + this.server = _.str.rtrim((!server) ? hostname : server, '/'); + this.rpc_mode = (this.server == hostname) ? "oe-json" : "oe-jsonp"; this.session_id = false; this.uid = false; this.user_context= {}; this.db = false; + this.module_loading = $.Deferred(); this.module_list = []; this.module_loaded = {"web": true}; this.context = {}; this.shortcuts = []; this.active_id = null; - // TODO: session should have an optional name indicating that they'll - // be saved to (and revived from) cookies - this.name = 'session'; - this.do_load_qweb(['/web/webclient/qweb']); - }, - - start: function() { - this.session_restore(); + this.do_load_qweb(['/web/webclient/qweb'], continuation); }, get_absolute_url: function(path) { @@ -377,7 +391,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. if (r_has_protocol.test(path)) { - if (!_(path).startsWith(this.server + '/')) { + if (!_.str.startsWith(path, this.server + '/')) { $.error('can only contact openerp.server'); } absolute_url = path; @@ -426,6 +440,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }); }).then(success_callback, error_callback).promise(); }, + /** * Raw JSON-RPC call * @@ -491,9 +506,8 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. /** * 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_valid: function(continuation) { + this.load_modules(continuation); }, on_session_invalid: function(contination) { }, @@ -509,15 +523,17 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. self.user_context = result.context; self.db = result.db; self.session_save(); - self.on_session_valid(); + + self.on_session_valid(success_callback); return true; - }).then(success_callback); + //}).done(success_callback); + }); }, login: function() { this.session_login.apply(this, arguments); }, /** * Reloads uid and session_id from local storage, if they exist */ - session_restore: function () { + session_restore: function (continuation) { var self = this; this.session_id = this.get_cookie('session_id'); return this.rpc("/web/session/get_session_info", {}).then(function(result) { @@ -525,11 +541,11 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. self.user_context = result.context; self.db = result.db; if (self.uid) - self.on_session_valid(); + self.on_session_valid(continuation); else - self.on_session_invalid(); + self.on_session_invalid(continuation); }, function() { - self.on_session_invalid(); + self.on_session_invalid(continuation); }); }, /** @@ -583,9 +599,17 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }, /** * Load additional web addons of that instance and init them + * FIXME do all actions in sync... */ - load_modules: function() { + load_modules: function(continuation) { var self = this; + + this.module_loading.done(continuation); + //if (this.module_loading.state() !== "pending") { + if (this.module_loading.isResolved() || this.module_loading.isRejected()) { + return; + } + this.rpc('/web/session/modules', {}, function(result) { self.module_list = result; var lang = self.user_context.lang; @@ -597,16 +621,36 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. self.user_context.lang.replace("_", "-") + ".js" ]; - self.rpc('/web/webclient/qweblist', {"mods": modules}, self.do_load_qweb); - self.rpc('/web/webclient/csslist', {"mods": modules}, self.do_load_css); - self.rpc('/web/webclient/jslist', {"mods": modules}, function(files) { + /* + self.rpc('/web/webclient/qweblist', {mods: modules}, self.do_load_qweb); + self.rpc('/web/webclient/csslist', {mods: modules}, self.do_load_css); + self.rpc('/web/webclient/jslist', {mods: modules}, function(files) { self.do_load_js(file_list.concat(files)); }); openerp._modules_loaded = true; + // */ + + self.rpc('/web/webclient/qweblist', {mods: modules}, function(files) { + self.do_load_qweb(files, function() { + // once qweb files are loaded... + self.rpc('/web/webclient/csslist', {mods: modules}, function(files) { + self.do_load_css(files, function() { + // once css files are loaded + self.rpc('/web/webclient/jslist', {mods: modules}, function(files) { + self.do_load_js(file_list.concat(files), function() { + self.on_modules_loaded(); + self.module_loading.resolve(); + }); + }); + }); + }); + }); + }); + }); }); }, - do_load_css: function (files) { + do_load_css: function (files, callback) { var self = this; _.each(files, function (file) { $('head').append($('', { @@ -615,8 +659,9 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. 'type': 'text/css' })); }); + if (callback) { callback(); } }, - do_load_js: function(files) { + do_load_js: function(files, callback) { var self = this; if(files.length != 0) { var file = files.shift(); @@ -627,35 +672,26 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. if ( (tag.readyState && tag.readyState != "loaded" && tag.readyState != "complete") || tag.onload_done ) return; tag.onload_done = true; - self.do_load_js(files); + self.do_load_js(files, callback); }; var head = document.head || document.getElementsByTagName('head')[0]; head.appendChild(tag); } else { - this.on_modules_loaded(); + if (callback) { callback(); } } }, - do_load_qweb: function(files) { + do_load_qweb: function(files, callback) { var self = this; - _.each(files, function(file) { - self.rpc({ - //url: self.get_absolute_url(file), - url: '/web/jsonp/static_proxy', - async: false, - //dataType: 'text', - - }, - { - path: file, - }, - function(xml) { - //function(result) { - // console.log('do_load_qweb', result); - // var xml = result.result; - //}).then(function(xml) { - openerp.web.qweb.add_template(_(xml).trim()); + + if (files.length != 0) { + var file = files.shift(); + self.rpc('/web/jsonp/static_proxy', {path: file}, function(xml) { + openerp.web.qweb.add_template(_.str.trim(xml)); + self.do_load_qweb(files, callback); }); - }); + } else { + if (callback) { callback(); } + } }, on_modules_loaded: function() { for(var j=0; j Date: Thu, 17 Nov 2011 13:48:19 +0100 Subject: [FIX] typo bzr revid: chs@openerp.com-20111117124819-qmxoit9i2ryo8szo --- addons/web/static/src/js/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 590438c09dc..8d7bed56b5b 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -367,7 +367,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. } if (this.server) { - throw new Error("Connection already bind to " + this.server); + throw new Error("Connection already bound to " + this.server); } var hostname = _.str.sprintf('%s//%s', location.protocol, location.host); this.server = _.str.rtrim((!server) ? hostname : server, '/'); -- cgit v1.2.3 From 4e85f0762cc69af8efdb88ec857329ac955afe0b Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Thu, 17 Nov 2011 13:49:26 +0100 Subject: [IMP] Connection: add utility method that bind() then login() bzr revid: chs@openerp.com-20111117124926-y3z3osoq76va0kjy --- addons/web/static/src/js/core.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 8d7bed56b5b..113b83f57dc 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -385,6 +385,13 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. this.do_load_qweb(['/web/webclient/qweb'], continuation); }, + connect: function(server, db, login, password, continuation) { + var self = this; + this.bind(server, function() { + self.login(db, login, password, continuation); + }); + }, + get_absolute_url: function(path) { var r_has_protocol = /^https?:\/\//, r_absolute_internal = /^\/[^\/]/; // starts with / (but not //) -- cgit v1.2.3 From c5bcea713d360a71ca1d94d385d6e5110d1ff142 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Tue, 22 Nov 2011 13:20:16 +0100 Subject: [FIX] set correct session name bzr revid: chs@openerp.com-20111122122016-ykckmih2q7dfb4yb --- addons/web/static/src/js/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index e3f85a92503..0c3eaff56ed 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -360,7 +360,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. this.debug = ($.deparam($.param.querystring()).debug != undefined); // TODO: session should have an optional name indicating that they'll // be saved to (and revived from) cookies - this.name = 'session'; + this.name = openerp._session_id; }, bind: function(server, continuation) { -- cgit v1.2.3 From f7893968428df6cb518db4615e96b1d0e627d294 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Tue, 22 Nov 2011 13:20:46 +0100 Subject: [IMP] correct var name bzr revid: chs@openerp.com-20111122122046-kl6f4o4ej1mdcd1o --- addons/web/static/src/js/core.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 0c3eaff56ed..19607bc11a2 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -398,7 +398,8 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }, get_absolute_url: function(path) { - var r_has_protocol = /^https?:\/\//, + var absolute_url, + r_has_protocol = /^https?:\/\//, r_absolute_internal = /^\/[^\/]/; // starts with / (but not //) @@ -416,7 +417,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. parts.push(path); absolute_url = this.server + parts.join('/'); } - return absolute_url + return absolute_url; }, /** @@ -488,7 +489,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. return; } self.uid = false; - self.on_session_invalid(function() { + self.on_session_invalid(function() { // retry self.rpc(url, payload, function() { deferred.resolve.apply(deferred, arguments); @@ -521,7 +522,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. on_session_valid: function(continuation) { this.load_modules(continuation); }, - on_session_invalid: function(contination) { + on_session_invalid: function(continuation) { }, session_is_valid: function() { return this.uid; -- cgit v1.2.3 From 0b314b259610290d3a2cc1fe9f8a1a24848c4139 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Wed, 14 Dec 2011 14:45:12 +0100 Subject: [IMP] add session.server as a global variable for qweb temlates bzr revid: chs@openerp.com-20111214134512-byuq129f02t3fk4s --- addons/web/static/src/js/core.js | 1 + 1 file changed, 1 insertion(+) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 7123fe2934b..e1591f46235 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -375,6 +375,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. } var hostname = _.str.sprintf('%s//%s', location.protocol, location.host); this.server = _.str.rtrim((!server) ? hostname : server, '/'); + openerp.web.qweb.default_dict['_s'] = this.server this.rpc_mode = (this.server == hostname) ? "oe-json" : "oe-jsonp"; this.session_id = false; this.uid = false; -- cgit v1.2.3 From 31e65bdddf56510db08116029113d5966cd6a51f Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Thu, 15 Dec 2011 13:07:32 +0100 Subject: partial rework to merge trunk bzr revid: al@openerp.com-20111215120732-is7pab5e5rsfqpj0 --- addons/web/static/src/js/core.js | 96 +++++++++------------------------------- 1 file changed, 22 insertions(+), 74 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index e1591f46235..1f5e29a311a 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -362,7 +362,6 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. // be saved to (and revived from) cookies this.name = openerp._session_id; }, - bind: function(server, continuation) { if(_.isFunction(server)) { @@ -390,37 +389,12 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. this.active_id = null; this.do_load_qweb(['/web/webclient/qweb'], continuation); }, - connect: function(server, db, login, password, continuation) { var self = this; this.bind(server, function() { self.login(db, login, password, continuation); }); }, - - get_absolute_url: function(path) { - var absolute_url, - r_has_protocol = /^https?:\/\//, - r_absolute_internal = /^\/[^\/]/; // starts with / (but not //) - - - if (r_has_protocol.test(path)) { - if (!_.str.startsWith(path, this.server + '/')) { - $.error('can only contact openerp.server'); - } - absolute_url = path; - } else if (r_absolute_internal.test(path)) { - absolute_url = this.server + path; - } else { // relative url - // XXX is it correct to mix document.location and this.server ?? - var parts = document.location.pathname.split('/'); - parts.pop(); - parts.push(path); - absolute_url = this.server + parts.join('/'); - } - return absolute_url; - }, - /** * Executes an RPC call, registering the provided callbacks. * @@ -454,7 +428,6 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }); }).then(success_callback, error_callback).promise(); }, - /** * Raw JSON-RPC call * @@ -521,7 +494,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. * The session is validated either by login or by restoration of a previous session */ on_session_valid: function(continuation) { - this.load_modules(continuation); + this.load_modules().then(function() { continuation() } ); }, on_session_invalid: function(continuation) { }, @@ -547,7 +520,9 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. //}).done(success_callback); }); }, - login: function() { this.session_authenticate.apply(this, arguments); }, + login: function() { + this.session_authenticate.apply(this, arguments); + }, /** * Reloads uid and session_id from local storage, if they exist */ @@ -628,17 +603,12 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }, /** * Load additional web addons of that instance and init them - * FIXME do all actions in sync... */ - load_modules: function(continuation) { + load_modules: function() { var self = this; - - this.module_loading.done(continuation); - //if (this.module_loading.state() !== "pending") { - if (this.module_loading.isResolved() || this.module_loading.isRejected()) { - return; + if(openerp._modules_loaded) { + return $.when(); } - this.rpc('/web/session/modules', {}, function(result) { self.module_list = result; var lang = self.user_context.lang; @@ -649,37 +619,17 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. var file_list = ["/web/static/lib/datejs/globalization/" + self.user_context.lang.replace("_", "-") + ".js" ]; - - /* - self.rpc('/web/webclient/qweblist', {mods: modules}, self.do_load_qweb); - self.rpc('/web/webclient/csslist', {mods: modules}, self.do_load_css); - self.rpc('/web/webclient/jslist', {mods: modules}, function(files) { - self.do_load_js(file_list.concat(files)); - }); - openerp._modules_loaded = true; - // */ - - self.rpc('/web/webclient/qweblist', {mods: modules}, function(files) { - self.do_load_qweb(files, function() { - // once qweb files are loaded... - self.rpc('/web/webclient/csslist', {mods: modules}, function(files) { - self.do_load_css(files, function() { - // once css files are loaded - self.rpc('/web/webclient/jslist', {mods: modules}, function(files) { - self.do_load_js(file_list.concat(files), function() { - self.on_modules_loaded(); - self.module_loading.resolve(); - }); - }); - }); - }); - }); - }); - + return $.when( + self.rpc('/web/webclient/qweblist', {mods: modules}, self.do_load_qweb), + self.rpc('/web/webclient/csslist', {mods: modules}, self.do_load_css), + self.rpc('/web/webclient/jslist', {mods: modules}, function(files) { + self.do_load_js(file_list.concat(files)); + }) + ); }); }); }, - do_load_css: function (files, callback) { + do_load_css: function (files) { var self = this; _.each(files, function (file) { $('head').append($('', { @@ -688,9 +638,8 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. 'type': 'text/css' })); }); - if (callback) { callback(); } }, - do_load_js: function(files, callback) { + do_load_js: function(files) { var self = this; if(files.length != 0) { var file = files.shift(); @@ -706,21 +655,18 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. var head = document.head || document.getElementsByTagName('head')[0]; head.appendChild(tag); } else { - if (callback) { callback(); } + self.on_modules_loaded(); } }, - do_load_qweb: function(files, callback) { + do_load_qweb: function(files) { var self = this; - if (files.length != 0) { var file = files.shift(); - self.rpc('/web/jsonp/static_proxy', {path: file}, function(xml) { + self.rpc('/web/proxy/load', {path: file}, function(xml) { openerp.web.qweb.add_template(_.str.trim(xml)); self.do_load_qweb(files, callback); }); - } else { - if (callback) { callback(); } - } + } }, on_modules_loaded: function() { for(var j=0; j Date: Thu, 15 Dec 2011 14:33:05 +0100 Subject: move jq_ajax.js bzr revid: al@openerp.com-20111215133305-c7jgyqdes33drllk --- addons/web/static/src/js/core.js | 181 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 1f5e29a311a..96e38b4e4d0 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -342,6 +342,183 @@ openerp.web.CallbackEnabled = openerp.web.Class.extend(/** @lends openerp.web.Ca } }); +$.ajaxSetup({ + converters: { + "json oe-jsonp": true, + "json oe-json": true, + } +}); + +// common preconditions checks +$.ajaxPrefilter("oe-json oe-jsonp", function(options, originalOptions, jqXHR) { + if (!$.isPlainObject(options.openerp)) { + console.error(options.openerp); + $.error('"openerp" option is required.'); + } + + if (!$.isPlainObject(options.data)) { + $.error('data must not be serialized'); + } + options.processData = false; +}); + +$.ajaxPrefilter("oe-json", function(options, originalOptions, jqXHR) { + options.data = JSON.stringify({ + jsonrpc: '2.0', + method: 'call', + params: options.data, + id: _.uniqueId('browser-client-') + }); + + return 'json'; +}); + +$.ajaxPrefilter("oe-jsonp", function(options, originalOptions, jqXHR) { + + options.crossDomain = true; + + var data = null; + if (options.data) { + data = $.param({r:JSON.stringify(options.data)}); + } + + + + var max_url_length = options.max_url_length || 1000, + absolute_url, full_url; + /* + var r_has_protocol = /^https?:\/\//, + r_absolute_internal = /^\/[^\/]/; // starts with / (but not //) + + + if (r_has_protocol.test(options.url)) { + if (!_(options.url).startsWith(options.openerp.server + '/')) { + $.error('can only contact openerp.server'); + } + absolute_url = options.url; + } else if (r_absolute_internal.test(options.url)) { + absolute_url = options.openerp.server + options.url; + } else { // relative url + var parts = document.location.pathname.split('/'); + parts.pop(); + parts.push(options.url); + absolute_url = options.openerp.server + parts.join('/'); + } + // */ + + var absolute_url = options.openerp.get_absolute_url(options.url); + + /// now, made the same url changes that jQuery will do... + var rquery = /\?/, + rts = /([?&])_=[^&]*/; + + full_url = absolute_url; + if (data) { + full_url += (rquery.test(full_url) ? "&" : "?") + data; + } + + // Add anti-cache in url if needed + if (!options.cache) { + var ts = $.now(), + // try replacing _= if it is there + ret = full_url.replace(rts, "$1_=" + ts); + + // if nothing was replaced, add timestamp to the end + full_url = ret + ((ret === full_url) ? (rquery.test(full_url) ? "&" : "?") + "_=" + ts : ""); + } + + options.url = absolute_url; + + if (full_url.length < max_url_length) { + options.type = "GET"; + options.data = data; + return "jsonp"; // classic jsonp query... + } +}); + +$.ajaxTransport("oe-jsonp", function(options, originalOptions, jqXHR) { + + var $iframe = null; + var $form = $('
') + .attr('method', 'POST') + .attr('enctype', "multipart/form-data") + .attr('action', options.openerp.server + "/web/jsonp/post") + .hide() + .appendTo($('body')) + ; + + function cleanUp() { + if ($iframe) { + $iframe.unbind("load").attr("src", "javascript:false;").remove(); + } + $form.remove(); + } + + return { + + send: function(headers, completeCallback) { + + var ifid = _.uniqueId('oe_jsonp_iframe_'); + var request_id = _.uniqueId('browser-client-'); + var oe_callback = _.uniqueId('oe_callback_'); + + window[oe_callback] = function(result) { + completeCallback(200, 'success', {json: result}); + }; + + + var display = options.openerp.debug ? 'block' : 'none'; + $iframe = $(_.str.sprintf("", ifid, ifid, display)); + + + // the first bind is fired up when the iframe is added to the DOM + $iframe.bind('load', function() { + // the second bind is fired up when the result of the form submission is received + $iframe.unbind('load').bind('load', function() { + // we cannot access the content of remote iframe. + // but we don't care, we try to get the result in any cases + + $.ajax({ + type: "GET", + url: options.url, + dataType: 'jsonp', + jsonp: false, // do not append callback=? argument on query string + jsonpCallback: oe_callback, + data: { + sid: options.openerp.session_id, + rid: request_id, + }, + }).always(function() { + cleanUp(); + }); + + }); + + + // now that the iframe can receive data, we fill and submit the form + var params = JSON.stringify(options.data); + + $form + .append($('').attr('value', options.openerp.session_id)) + .append($('').attr('value', request_id)) + .append($('').attr('value', params)) + .append($('').attr('value', oe_callback)) + .submit() + ; + + }); + + $form.attr('target', ifid) // set the iframe as target of the form + .after($iframe); // append the iframe to the DOM (will trigger the first load) + + }, + abort: function() { + cleanUp(); + }, + }; + +}); + openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.web.Connection# */{ /** * @constructs openerp.web.Connection @@ -351,10 +528,6 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. * @param {String} [port] JSON-RPC endpoint port */ init: function() { - - // FIXME need to be a real singleton. - // create more than one Connection on the same instance will not works - this._super(); this.server = null; this.debug = ($.deparam($.param.querystring()).debug != undefined); -- cgit v1.2.3 From 4206e4d2627be8ca252fdfce2f90654ff9db0055 Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Thu, 15 Dec 2011 19:32:59 +0100 Subject: connection jsonp wip bzr revid: al@openerp.com-20111215183259-vam7tsjlkqhjam69 --- addons/web/static/src/js/core.js | 356 ++++++++++++--------------------------- 1 file changed, 105 insertions(+), 251 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 96e38b4e4d0..55bce373330 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -342,183 +342,6 @@ openerp.web.CallbackEnabled = openerp.web.Class.extend(/** @lends openerp.web.Ca } }); -$.ajaxSetup({ - converters: { - "json oe-jsonp": true, - "json oe-json": true, - } -}); - -// common preconditions checks -$.ajaxPrefilter("oe-json oe-jsonp", function(options, originalOptions, jqXHR) { - if (!$.isPlainObject(options.openerp)) { - console.error(options.openerp); - $.error('"openerp" option is required.'); - } - - if (!$.isPlainObject(options.data)) { - $.error('data must not be serialized'); - } - options.processData = false; -}); - -$.ajaxPrefilter("oe-json", function(options, originalOptions, jqXHR) { - options.data = JSON.stringify({ - jsonrpc: '2.0', - method: 'call', - params: options.data, - id: _.uniqueId('browser-client-') - }); - - return 'json'; -}); - -$.ajaxPrefilter("oe-jsonp", function(options, originalOptions, jqXHR) { - - options.crossDomain = true; - - var data = null; - if (options.data) { - data = $.param({r:JSON.stringify(options.data)}); - } - - - - var max_url_length = options.max_url_length || 1000, - absolute_url, full_url; - /* - var r_has_protocol = /^https?:\/\//, - r_absolute_internal = /^\/[^\/]/; // starts with / (but not //) - - - if (r_has_protocol.test(options.url)) { - if (!_(options.url).startsWith(options.openerp.server + '/')) { - $.error('can only contact openerp.server'); - } - absolute_url = options.url; - } else if (r_absolute_internal.test(options.url)) { - absolute_url = options.openerp.server + options.url; - } else { // relative url - var parts = document.location.pathname.split('/'); - parts.pop(); - parts.push(options.url); - absolute_url = options.openerp.server + parts.join('/'); - } - // */ - - var absolute_url = options.openerp.get_absolute_url(options.url); - - /// now, made the same url changes that jQuery will do... - var rquery = /\?/, - rts = /([?&])_=[^&]*/; - - full_url = absolute_url; - if (data) { - full_url += (rquery.test(full_url) ? "&" : "?") + data; - } - - // Add anti-cache in url if needed - if (!options.cache) { - var ts = $.now(), - // try replacing _= if it is there - ret = full_url.replace(rts, "$1_=" + ts); - - // if nothing was replaced, add timestamp to the end - full_url = ret + ((ret === full_url) ? (rquery.test(full_url) ? "&" : "?") + "_=" + ts : ""); - } - - options.url = absolute_url; - - if (full_url.length < max_url_length) { - options.type = "GET"; - options.data = data; - return "jsonp"; // classic jsonp query... - } -}); - -$.ajaxTransport("oe-jsonp", function(options, originalOptions, jqXHR) { - - var $iframe = null; - var $form = $('') - .attr('method', 'POST') - .attr('enctype', "multipart/form-data") - .attr('action', options.openerp.server + "/web/jsonp/post") - .hide() - .appendTo($('body')) - ; - - function cleanUp() { - if ($iframe) { - $iframe.unbind("load").attr("src", "javascript:false;").remove(); - } - $form.remove(); - } - - return { - - send: function(headers, completeCallback) { - - var ifid = _.uniqueId('oe_jsonp_iframe_'); - var request_id = _.uniqueId('browser-client-'); - var oe_callback = _.uniqueId('oe_callback_'); - - window[oe_callback] = function(result) { - completeCallback(200, 'success', {json: result}); - }; - - - var display = options.openerp.debug ? 'block' : 'none'; - $iframe = $(_.str.sprintf("", ifid, ifid, display)); - - - // the first bind is fired up when the iframe is added to the DOM - $iframe.bind('load', function() { - // the second bind is fired up when the result of the form submission is received - $iframe.unbind('load').bind('load', function() { - // we cannot access the content of remote iframe. - // but we don't care, we try to get the result in any cases - - $.ajax({ - type: "GET", - url: options.url, - dataType: 'jsonp', - jsonp: false, // do not append callback=? argument on query string - jsonpCallback: oe_callback, - data: { - sid: options.openerp.session_id, - rid: request_id, - }, - }).always(function() { - cleanUp(); - }); - - }); - - - // now that the iframe can receive data, we fill and submit the form - var params = JSON.stringify(options.data); - - $form - .append($('').attr('value', options.openerp.session_id)) - .append($('').attr('value', request_id)) - .append($('').attr('value', params)) - .append($('').attr('value', oe_callback)) - .submit() - ; - - }); - - $form.attr('target', ifid) // set the iframe as target of the form - .after($iframe); // append the iframe to the DOM (will trigger the first load) - - }, - abort: function() { - cleanUp(); - }, - }; - -}); - openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.web.Connection# */{ /** * @constructs openerp.web.Connection @@ -529,26 +352,17 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. */ init: function() { this._super(); - this.server = null; - this.debug = ($.deparam($.param.querystring()).debug != undefined); // TODO: session should have an optional name indicating that they'll // be saved to (and revived from) cookies this.name = openerp._session_id; }, - bind: function(server, continuation) { - - if(_.isFunction(server)) { - continuation = server; - server = null; - } - - if (this.server) { - throw new Error("Connection already bound to " + this.server); - } - var hostname = _.str.sprintf('%s//%s', location.protocol, location.host); - this.server = _.str.rtrim((!server) ? hostname : server, '/'); - openerp.web.qweb.default_dict['_s'] = this.server - this.rpc_mode = (this.server == hostname) ? "oe-json" : "oe-jsonp"; + bind: function(host, protocol) { + this.host = (host == undefined) ? location.host : host; + this.protocol = (protocol == undefined) ? location.protocol : protocol; + this.prefix = this.protocol + '//' + this.host; + openerp.web.qweb.default_dict['_s'] = this.prefix + this.rpc_mode = (host == location.host) ? "json" : "jsonp"; + this.debug = (window.location.search.indexOf('?debug') !== -1); this.session_id = false; this.uid = false; this.username = false; @@ -562,12 +376,6 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. this.active_id = null; this.do_load_qweb(['/web/webclient/qweb'], continuation); }, - connect: function(server, db, login, password, continuation) { - var self = this; - this.bind(server, function() { - self.login(db, login, password, continuation); - }); - }, /** * Executes an RPC call, registering the provided callbacks. * @@ -583,79 +391,130 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. */ rpc: function(url, params, success_callback, error_callback) { var self = this; + // url can be an $.ajax option object + if (_.isString(url)) { + url = { url: url }; + } // Construct a JSON-RPC2 request, method is currently unused params.session_id = this.session_id; if (this.debug) params.debug = 1; - + var payload = { + jsonrpc: '2.0', + method: 'call', + params: params, + id: _.uniqueId('oe-') + }; // Call using the rpc_mode var deferred = $.Deferred(); - this.rpc_ajax(url, params) - .then(function () {deferred.resolve.apply(deferred, arguments);}, - function(error) {deferred.reject(error, $.Event());}); - return deferred.fail(function() { + this.on_rpc_request(); + this.rpc_json(url, payload).then( + function (response, textStatus, jqXHR) { + self.on_rpc_response(); + if (!response.error) { + deferred.resolve(response["result"], textStatus, jqXHR); + } else if (response.error.data.type === "session_invalid") { + self.uid = false; + self.on_session_invalid(function() { + self.rpc(url, payload.params, + function() { deferred.resolve.apply(deferred, arguments); }, + function() { deferred.reject.apply(deferred, arguments); }); + }); + } else { + deferred.reject(response.error, $.Event()); + } + }, + 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, $.Event()); + }); + // Allow deferred user to disable on_rpc_error in fail + deferred.fail(function() { deferred.fail(function(error, event) { if (!event.isDefaultPrevented()) { self.on_rpc_error(error, event); } }); }).then(success_callback, error_callback).promise(); + return deferred; }, /** * Raw JSON-RPC call * * @returns {jQuery.Deferred} ajax-webd deferred object */ - rpc_ajax: function(url, payload) { + rpc_json: 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: this.rpc_mode, + dataType: 'json', contentType: 'application/json', - data: payload, + data: JSON.stringify(payload), processData: false, - openerp: _.extend({}, this), // need a plainObject }, 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; + return $.ajax(ajax); + }, + rpc_jsonp: function(url, payload) { + var self = this; + var ajax = _.extend({ + type: "GET", + dataType: 'jsonp', + jsonp: 'jsonp', + cache: false, + data: { + session_id: this.session_id, + id: payload.id, } - self.uid = false; - self.on_session_invalid(function() { // retry - self.rpc(url, payload, - 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] } + }, url); + var payload_str = JSON.stringify(payload); + var payload_url = $.param({r:payload_str}); + if(playload_url.length < 2000) { + // Direct json request + ajax.data.r = payload_str; + return $.ajax(ajax); + } else { + // Indirect json request + var ifid = _.uniqueId('oe_rpc_iframe_'); + var display = options.openerp.debug ? 'block' : 'none'; + var $iframe = $(_.str.sprintf("", ifid, ifid, display)); + var $form = $('') + .attr('method', 'POST') + .attr('target', ifid) + .attr('enctype', "multipart/form-data") + .attr('action', ajax.url) + .append($('').attr('value', payload_str)) + .hide() + .appendTo($('body')); + var cleanUp = function() { + if ($iframe) { + $iframe.unbind("load").attr("src", "javascript:false;").remove(); + } + $form.remove(); }; - deferred.reject(error); - }); - return deferred.promise(); + var deferred = $.Deferred(); + // the first bind is fired up when the iframe is added to the DOM + $iframe.bind('load', function() { + // the second bind is fired up when the result of the form submission is received + $iframe.unbind('load').bind('load', function() { + $.ajax(ajax).always(function() { + cleanUp(); + }).then( + function() { deferred.resolve.apply(deferred, arguments); }, + function() { deferred.reject.apply(deferred, arguments); } + ); + }); + // now that the iframe can receive data, we fill and submit the form + $form.submit(); + }); + // append the iframe to the DOM (will trigger the first load) + $form.after($iframe); + return deffered; + } }, on_rpc_request: function() { }, @@ -687,15 +546,10 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. username: result.login }); self.session_save(); - self.on_session_valid(success_callback); return true; - //}).done(success_callback); }); }, - login: function() { - this.session_authenticate.apply(this, arguments); - }, /** * Reloads uid and session_id from local storage, if they exist */ -- cgit v1.2.3 From 8721df9d549e72b7154ae34dff53aa4b82718b85 Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Fri, 16 Dec 2011 01:44:02 +0100 Subject: [IMP] massive connection deferrization, json works again bzr revid: al@openerp.com-20111216004402-q6v26822swarra7r --- addons/web/static/src/js/core.js | 120 ++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 65 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 55bce373330..f28d0adfc03 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -352,11 +352,11 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. */ init: function() { this._super(); - // TODO: session should have an optional name indicating that they'll - // be saved to (and revived from) cookies + // TODO: session store in cookie should be optional this.name = openerp._session_id; }, bind: function(host, protocol) { + var self = this; this.host = (host == undefined) ? location.host : host; this.protocol = (protocol == undefined) ? location.protocol : protocol; this.prefix = this.protocol + '//' + this.host; @@ -368,13 +368,13 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. this.username = false; this.user_context= {}; this.db = false; - this.module_loading = $.Deferred(); this.module_list = []; this.module_loaded = {"web": true}; this.context = {}; this.shortcuts = []; this.active_id = null; - this.do_load_qweb(['/web/webclient/qweb'], continuation); + this.ready = $.Deferred(); + return this.session_restore(); }, /** * Executes an RPC call, registering the provided callbacks. @@ -523,73 +523,51 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. on_rpc_error: function(error) { }, /** - * The session is validated either by login or by restoration of a previous session + * Init a session, reloads from cookie, if it exists */ - on_session_valid: function(continuation) { - this.load_modules().then(function() { continuation() } ); - }, - on_session_invalid: function(continuation) { - }, - session_is_valid: function() { - return this.uid; - }, - session_authenticate: function(db, login, password, success_callback) { + session_restore: function () { 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, function(result) { + // TODO: session store in cookie should be optional + this.session_id = this.get_cookie('session_id'); + return this.rpc("/web/session/get_session_info", {}).pipe(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, - uid: result.uid, - user_context: result.context, db: result.db, - username: result.login + username: result.login, + uid: result.uid, + user_context: result.context }); - self.session_save(); - self.on_session_valid(success_callback); - return true; + var deferred = self.do_load_qweb(['/web/webclient/qweb']); + if(self.uid) { + return deferred.then(self.load_modules()); + } + return deferred; }); }, /** - * Reloads uid and session_id from local storage, if they exist + * The session is validated either by login or by restoration of a previous session */ - session_restore: function (continuation) { + session_authenticate: function(db, login, password) { var self = this; - this.session_id = this.get_cookie('session_id'); - 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...) but should not call - // on_session_valid again as it triggers reloading the menu - var already_logged = self.uid; + 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) { _.extend(self, { - uid: result.uid, - user_context: result.context, + session_id: result.session_id, db: result.db, - username: result.login + username: result.login, + uid: result.uid, + user_context: result.context }); - if (!already_logged) { - if (self.uid) { - self.on_session_valid(continuation); - } else { - self.on_session_invalid(continuation); - } - } - }, function() { - self.on_session_invalid(continuation); + // TODO: session store in cookie should be optional + self.set_cookie('session_id', this.session_id); + return self.load_modules(); }); }, - /** - * Saves the session id and uid locally - */ - session_save: function () { - this.set_cookie('session_id', this.session_id); - }, - logout: function() { + session_logout: function() { this.set_cookie('session_id', ''); - this.reload_client(); - }, - reload_client: function() { window.location.reload(); }, /** @@ -640,19 +618,21 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. self.module_list = result; var lang = self.user_context.lang; var params = { mods: ["web"].concat(result), lang: lang}; - self.rpc('/web/webclient/translations',params).then(function(transs) { + self.rpc('/web/webclient/translations',params).pipe(function(transs) { openerp.web._t.database.set_bundle(transs); var modules = self.module_list.join(','); var file_list = ["/web/static/lib/datejs/globalization/" + self.user_context.lang.replace("_", "-") + ".js" ]; return $.when( - self.rpc('/web/webclient/qweblist', {mods: modules}, self.do_load_qweb), self.rpc('/web/webclient/csslist', {mods: modules}, self.do_load_css), - self.rpc('/web/webclient/jslist', {mods: modules}, function(files) { + self.rpc('/web/webclient/qweblist', {mods: modules}).pipe(self.do_load_qweb), + self.rpc('/web/webclient/jslist', {mods: modules}).pipe(function(files) { self.do_load_js(file_list.concat(files)); }) - ); + ).then(function() { + self.ready.resolve(); + }); }); }); }, @@ -660,7 +640,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. var self = this; _.each(files, function (file) { $('head').append($('', { - 'href': self.get_absolute_url(file), + 'href': self.get_url(file), 'rel': 'stylesheet', 'type': 'text/css' })); @@ -668,32 +648,39 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }, 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_absolute_url(file); + 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, callback); + self.do_load_js(files).then(function () { + d.resolve(); + }); }; var head = document.head || document.getElementsByTagName('head')[0]; head.appendChild(tag); } else { self.on_modules_loaded(); + d.resolve(); } + return d; }, do_load_qweb: function(files) { var self = this; if (files.length != 0) { var file = files.shift(); - self.rpc('/web/proxy/load', {path: file}, function(xml) { + return self.rpc('/web/proxy/load', {path: file}).pipe(function(xml) { openerp.web.qweb.add_template(_.str.trim(xml)); - self.do_load_qweb(files, callback); + return self.do_load_qweb(files); }); - } + } else { + return $.when(); + } }, on_modules_loaded: function() { for(var j=0; j Date: Fri, 16 Dec 2011 01:48:23 +0100 Subject: [IMP] minor changes bzr revid: al@openerp.com-20111216004823-s3lh80nymv2xu6fs --- addons/web/static/src/js/core.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index f28d0adfc03..695a833da81 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -374,7 +374,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. this.shortcuts = []; this.active_id = null; this.ready = $.Deferred(); - return this.session_restore(); + return this.session_init(); }, /** * Executes an RPC call, registering the provided callbacks. @@ -415,6 +415,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. deferred.resolve(response["result"], textStatus, jqXHR); } else if (response.error.data.type === "session_invalid") { self.uid = false; + // TODO deprecate or use a deferred on login.do_ask_login() self.on_session_invalid(function() { self.rpc(url, payload.params, function() { deferred.resolve.apply(deferred, arguments); }, @@ -525,7 +526,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. /** * Init a session, reloads from cookie, if it exists */ - session_restore: function () { + session_init: function () { var self = this; // TODO: session store in cookie should be optional this.session_id = this.get_cookie('session_id'); -- cgit v1.2.3 From d315489256557498de4e3c641a7cc28e6550d575 Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Fri, 16 Dec 2011 02:25:42 +0100 Subject: [FIX] session cookie and logout bzr revid: al@openerp.com-20111216012542-svbiuf64x017wll5 --- addons/web/static/src/js/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 695a833da81..a46f7d5cac8 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -563,7 +563,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. user_context: result.context }); // TODO: session store in cookie should be optional - self.set_cookie('session_id', this.session_id); + self.set_cookie('session_id', self.session_id); return self.load_modules(); }); }, -- cgit v1.2.3 From 770a738b9b0e773506c38af0a14e91476ae14e1b Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Fri, 16 Dec 2011 02:30:51 +0100 Subject: [FIX] more deferred bzr revid: al@openerp.com-20111216013051-x45j314u7d4zvfoq --- addons/web/static/src/js/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index a46f7d5cac8..7efd673f124 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -629,7 +629,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. self.rpc('/web/webclient/csslist', {mods: modules}, self.do_load_css), self.rpc('/web/webclient/qweblist', {mods: modules}).pipe(self.do_load_qweb), self.rpc('/web/webclient/jslist', {mods: modules}).pipe(function(files) { - self.do_load_js(file_list.concat(files)); + return self.do_load_js(file_list.concat(files)); }) ).then(function() { self.ready.resolve(); -- cgit v1.2.3 From 28715cfaab85970c3bbabf49bc12a5dfa2efc200 Mon Sep 17 00:00:00 2001 From: Antony Lesuisse Date: Fri, 16 Dec 2011 02:49:45 +0100 Subject: [FIX] jsonp fixes currently untested bzr revid: al@openerp.com-20111216014945-mhq4eee2hy9mip27 --- addons/web/static/src/js/core.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'addons/web/static/src/js/core.js') diff --git a/addons/web/static/src/js/core.js b/addons/web/static/src/js/core.js index 7efd673f124..3d66c4f4ccf 100644 --- a/addons/web/static/src/js/core.js +++ b/addons/web/static/src/js/core.js @@ -361,7 +361,8 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. this.protocol = (protocol == undefined) ? location.protocol : protocol; this.prefix = this.protocol + '//' + this.host; openerp.web.qweb.default_dict['_s'] = this.prefix - this.rpc_mode = (host == location.host) ? "json" : "jsonp"; + this.rpc_mode = (this.host == location.host) ? "json" : "jsonp"; + this.rpc_function = (this.host == location.host) ? this.rpc_json : this.rpc_jsonp; this.debug = (window.location.search.indexOf('?debug') !== -1); this.session_id = false; this.uid = false; @@ -403,12 +404,11 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. jsonrpc: '2.0', method: 'call', params: params, - id: _.uniqueId('oe-') + id: _.uniqueId('r') }; - // Call using the rpc_mode var deferred = $.Deferred(); this.on_rpc_request(); - this.rpc_json(url, payload).then( + this.rpc_function(url, payload).then( function (response, textStatus, jqXHR) { self.on_rpc_response(); if (!response.error) { @@ -462,32 +462,35 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }, rpc_jsonp: function(url, payload) { var self = this; + // extracted from payload to set on the url + var data = { + session_id: this.session_id, + id: payload.id, + }; + url.url = this.get_url(url.url); var ajax = _.extend({ type: "GET", dataType: 'jsonp', jsonp: 'jsonp', cache: false, - data: { - session_id: this.session_id, - id: payload.id, - } + data: data }, url); var payload_str = JSON.stringify(payload); var payload_url = $.param({r:payload_str}); if(playload_url.length < 2000) { - // Direct json request + // Direct jsonp request ajax.data.r = payload_str; return $.ajax(ajax); } else { - // Indirect json request - var ifid = _.uniqueId('oe_rpc_iframe_'); + // Indirect jsonp request + var ifid = _.uniqueId('oe_rpc_iframe'); var display = options.openerp.debug ? 'block' : 'none'; var $iframe = $(_.str.sprintf("", ifid, ifid, display)); var $form = $('') .attr('method', 'POST') .attr('target', ifid) .attr('enctype', "multipart/form-data") - .attr('action', ajax.url) + .attr('action', ajax.url + '?' + $.param(data)) .append($('').attr('value', payload_str)) .hide() .appendTo($('body')); @@ -514,7 +517,7 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. }); // append the iframe to the DOM (will trigger the first load) $form.after($iframe); - return deffered; + return deferred; } }, on_rpc_request: function() { @@ -696,6 +699,9 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. } } }, + get_url: function (file) { + return this.prefix + file; + }, /** * Cooperative file download implementation, for ajaxy APIs. * @@ -716,9 +722,6 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp. * @param {Function} [options.error] callback in case of request error, provided with the error body * @param {Function} [options.complete] called after both ``success`` and ``error` callbacks have executed */ - get_url: function (file) { - return this.prefix + file; - }, get_file: function (options) { // need to detect when the file is done downloading (not used // yet, but we'll need it to fix the UI e.g. with a throbber -- cgit v1.2.3