From aa5c781d90b9f650da11f1362914fa687371efc3 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Wed, 31 Jul 2013 16:48:05 +0200 Subject: Removed dependency to underscore.string in openerpframework.js bzr revid: nicolas.vanhoren@openerp.com-20130731144805-9hbxxfbrweb0clhx --- addons/web/static/src/js/openerpframework.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 374493b1f7e..e3e972c684e 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -622,8 +622,7 @@ openerp.web.Widget = openerp.web.Class.extend(openerp.web.PropertiesMixin, { renderElement: function() { var $el; if (this.template) { - $el = $(_.str.trim(openerp.web.qweb.render( - this.template, {widget: this}))); + $el = $(openerp.web.qweb.render(this.template, {widget: this}).trim()); } else { $el = this._make_descriptive(); } -- cgit v1.2.3 From d7a9a596a7759475b840efd2ed884a5506aa83a8 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Wed, 31 Jul 2013 16:56:23 +0200 Subject: Put JSONRpc class in openerpframework bzr revid: nicolas.vanhoren@openerp.com-20130731145623-lxac6w12ocoh2e8e --- addons/web/static/src/js/corelib.js | 200 --------------------------- addons/web/static/src/js/openerpframework.js | 199 ++++++++++++++++++++++++++ 2 files changed, 199 insertions(+), 200 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/corelib.js b/addons/web/static/src/js/corelib.js index c419685c222..e33b4d73044 100644 --- a/addons/web/static/src/js/corelib.js +++ b/addons/web/static/src/js/corelib.js @@ -199,206 +199,6 @@ instance.web.Registry = instance.web.Class.extend({ } }); -instance.web.JsonRPC = instance.web.Class.extend(instance.web.PropertiesMixin, { - triggers: { - 'request': 'Request sent', - 'response': 'Response received', - 'response_failed': 'HTTP Error response or timeout received', - 'error': 'The received response is an JSON-RPC error', - }, - /** - * @constructs instance.web.JsonRPC - * - * @param {String} [server] JSON-RPC endpoint hostname - * @param {String} [port] JSON-RPC endpoint port - */ - init: function() { - instance.web.PropertiesMixin.init.call(this); - this.server = null; - this.debug = ($.deparam($.param.querystring()).debug !== undefined); - this.override_session = false; - this.session_id = undefined; - }, - setup: function(origin) { - var window_origin = location.protocol+"//"+location.host, self=this; - this.origin = origin ? _.str.rtrim(origin,'/') : window_origin; - this.prefix = this.origin; - this.server = this.origin; // keep chs happy - this.rpc_function = (this.origin == window_origin) ? this.rpc_json : this.rpc_jsonp; - }, - /** - * 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 {Object} options additional options for rpc call - * @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, options) { - var self = this; - options = options || {}; - // url can be an $.ajax option object - if (_.isString(url)) { - url = { url: url }; - } - _.defaults(params, { - context: this.user_context || {} - }); - // Construct a JSON-RPC2 request, method is currently unused - var payload = { - jsonrpc: '2.0', - method: 'call', - params: params, - id: _.uniqueId('r') - }; - var deferred = $.Deferred(); - if (! options.shadow) - this.trigger('request', url, payload); - - if (options.timeout) - url.timeout = options.timeout; - - this.rpc_function(url, payload).then( - function (response, textStatus, jqXHR) { - if (! options.shadow) - self.trigger('response', response); - if (!response.error) { - deferred.resolve(response["result"], textStatus, jqXHR); - } else if (response.error.code === 100) { - self.uid = false; - } else { - deferred.reject(response.error, $.Event()); - } - }, - function(jqXHR, textStatus, errorThrown) { - if (! options.shadow) - self.trigger('response_failed', jqXHR); - 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 rpc_error call in fail - deferred.fail(function() { - deferred.fail(function(error, event) { - if (!event.isDefaultPrevented()) { - self.trigger('error', error, event); - } - }); - }); - return deferred; - }, - /** - * Raw JSON-RPC call - * - * @returns {jQuery.Deferred} ajax-webd deferred object - */ - rpc_json: function(url, payload) { - var self = this; - var ajax = _.extend({ - type: "POST", - dataType: 'json', - contentType: 'application/json', - data: JSON.stringify(payload), - processData: false, - headers: { - "X-Openerp-Session-Id": this.override_session ? this.session_id : undefined, - }, - }, url); - if (this.synch) - ajax.async = false; - return $.ajax(ajax); - }, - 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, - }; - - var set_sid = function (response, textStatus, jqXHR) { - // If response give us the http session id, we store it for next requests... - if (response.session_id) { - self.session_id = response.session_id; - } - }; - - url.url = this.url(url.url, null); - var ajax = _.extend({ - type: "GET", - dataType: 'jsonp', - jsonp: 'jsonp', - cache: false, - data: data - }, url); - if (this.synch) - ajax.async = false; - var payload_str = JSON.stringify(payload); - var payload_url = $.param({r:payload_str}); - if (payload_url.length < 2000) { - // Direct jsonp request - ajax.data.r = payload_str; - return $.ajax(ajax).done(set_sid); - } else { - // Indirect jsonp request - var ifid = _.uniqueId('oe_rpc_iframe'); - var display = self.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 + '?jsonp=1&' + $.param(data)) - .append($('').attr('value', payload_str)) - .hide() - .appendTo($('body')); - var cleanUp = function() { - if ($iframe) { - $iframe.unbind("load").remove(); - } - $form.remove(); - }; - 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(); - }).done(function() { - deferred.resolve.apply(deferred, arguments); - }).fail(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 deferred.done(set_sid); - } - }, - - url: function(path, params) { - params = _.extend(params || {}); - if (this.override_session) - params.session_id = this.session_id; - var qs = '?' + $.param(params); - var prefix = _.any(['http://', 'https://', '//'], _.bind(_.str.startsWith, null, path)) ? '' : this.prefix; - return prefix + path + qs; - }, -}); - instance.web.py_eval = function(expr, context) { return py.eval(expr, _.extend({}, context || {}, {"true": true, "false": false, "null": null})); }; diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index e3e972c684e..39c3165967d 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -768,6 +768,205 @@ openerp.web.qweb.default_dict = { 'JSON': JSON }; +openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { + triggers: { + 'request': 'Request sent', + 'response': 'Response received', + 'response_failed': 'HTTP Error response or timeout received', + 'error': 'The received response is an JSON-RPC error', + }, + /** + * @constructs openerp.web.JsonRPC + * + * @param {String} [server] JSON-RPC endpoint hostname + * @param {String} [port] JSON-RPC endpoint port + */ + init: function() { + openerp.web.PropertiesMixin.init.call(this); + this.server = null; + this.override_session = false; + this.session_id = undefined; + }, + setup: function(origin) { + var window_origin = location.protocol+"//"+location.host, self=this; + this.origin = origin ? _.str.rtrim(origin,'/') : window_origin; + this.prefix = this.origin; + this.server = this.origin; // keep chs happy + this.rpc_function = (this.origin == window_origin) ? this.rpc_json : this.rpc_jsonp; + }, + /** + * 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 {Object} options additional options for rpc call + * @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, options) { + var self = this; + options = options || {}; + // url can be an $.ajax option object + if (_.isString(url)) { + url = { url: url }; + } + _.defaults(params, { + context: this.user_context || {} + }); + // Construct a JSON-RPC2 request, method is currently unused + var payload = { + jsonrpc: '2.0', + method: 'call', + params: params, + id: _.uniqueId('r') + }; + var deferred = $.Deferred(); + if (! options.shadow) + this.trigger('request', url, payload); + + if (options.timeout) + url.timeout = options.timeout; + + this.rpc_function(url, payload).then( + function (response, textStatus, jqXHR) { + if (! options.shadow) + self.trigger('response', response); + if (!response.error) { + deferred.resolve(response["result"], textStatus, jqXHR); + } else if (response.error.code === 100) { + self.uid = false; + } else { + deferred.reject(response.error, $.Event()); + } + }, + function(jqXHR, textStatus, errorThrown) { + if (! options.shadow) + self.trigger('response_failed', jqXHR); + 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 rpc_error call in fail + deferred.fail(function() { + deferred.fail(function(error, event) { + if (!event.isDefaultPrevented()) { + self.trigger('error', error, event); + } + }); + }); + return deferred; + }, + /** + * Raw JSON-RPC call + * + * @returns {jQuery.Deferred} ajax-webd deferred object + */ + rpc_json: function(url, payload) { + var self = this; + var ajax = _.extend({ + type: "POST", + dataType: 'json', + contentType: 'application/json', + data: JSON.stringify(payload), + processData: false, + headers: { + "X-Openerp-Session-Id": this.override_session ? this.session_id : undefined, + }, + }, url); + if (this.synch) + ajax.async = false; + return $.ajax(ajax); + }, + 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, + }; + + var set_sid = function (response, textStatus, jqXHR) { + // If response give us the http session id, we store it for next requests... + if (response.session_id) { + self.session_id = response.session_id; + } + }; + + url.url = this.url(url.url, null); + var ajax = _.extend({ + type: "GET", + dataType: 'jsonp', + jsonp: 'jsonp', + cache: false, + data: data + }, url); + if (this.synch) + ajax.async = false; + var payload_str = JSON.stringify(payload); + var payload_url = $.param({r:payload_str}); + if (payload_url.length < 2000) { + // Direct jsonp request + ajax.data.r = payload_str; + return $.ajax(ajax).done(set_sid); + } else { + // Indirect jsonp request + var ifid = _.uniqueId('oe_rpc_iframe'); + var display = '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 + '?jsonp=1&' + $.param(data)) + .append($('').attr('value', payload_str)) + .hide() + .appendTo($('body')); + var cleanUp = function() { + if ($iframe) { + $iframe.unbind("load").remove(); + } + $form.remove(); + }; + 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(); + }).done(function() { + deferred.resolve.apply(deferred, arguments); + }).fail(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 deferred.done(set_sid); + } + }, + + url: function(path, params) { + params = _.extend(params || {}); + if (this.override_session) + params.session_id = this.session_id; + var qs = '?' + $.param(params); + var prefix = _.any(['http://', 'https://', '//'], _.bind(_.str.startsWith, null, path)) ? '' : this.prefix; + return prefix + path + qs; + }, +}); + openerp.declare = declare; return openerp; -- cgit v1.2.3 From 9fef7daead45f905b2cfffcb81e0afa3d6ee1de3 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Wed, 31 Jul 2013 17:17:20 +0200 Subject: Added niv's functions to make jsonrpc and removed some references to _.str bzr revid: nicolas.vanhoren@openerp.com-20130731151720-1iiohpkc1f6jqv3o --- addons/web/static/src/js/openerpframework.js | 56 ++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 39c3165967d..81295dc1718 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -768,6 +768,52 @@ openerp.web.qweb.default_dict = { 'JSON': JSON }; +var genericJsonRpc = function(fct_name, params, fct) { + var data = { + jsonrpc: "2.0", + method: fct_name, + params: params, + id: Math.floor(Math.random() * 1000 * 1000 * 1000), + }; + return fct(data).pipe(function(result) { + if (result.error !== undefined) { + console.error("Server application error", result.error); + return $.Deferred().reject("server", result.error); + } else { + return result.result; + } + }, function() { + console.error("JsonRPC communication error", _.toArray(arguments)); + var def = $.Deferred(); + return def.reject.apply(def, ["communication"].concat(_.toArray(arguments))); + }); +}; + +openerp.web.jsonRpc = function(url, fct_name, params, settings) { + return genericJsonRpc(fct_name, params, function(data) { + return $.ajax(url, _.extend({}, settings, { + url: url, + dataType: 'json', + type: 'POST', + data: JSON.stringify(data), + contentType: 'application/json', + })); + }); +}; + +openerp.web.jsonpRpc = function(url, fct_name, params, settings) { + return genericJsonRpc(fct_name, params, function(data) { + return $.ajax(url, _.extend({}, settings, { + url: url, + dataType: 'jsonp', + jsonp: 'jsonp', + type: 'GET', + cache: false, + data: {r: JSON.stringify(data)}, + })); + }); +}; + openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { triggers: { 'request': 'Request sent', @@ -788,8 +834,10 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { this.session_id = undefined; }, setup: function(origin) { - var window_origin = location.protocol+"//"+location.host, self=this; - this.origin = origin ? _.str.rtrim(origin,'/') : window_origin; + // must be able to customize server + var window_origin = location.protocol + "//" + location.host; + var self = this; + this.origin = origin ? origin.replace( /\/+$/, '') : window_origin; this.prefix = this.origin; this.server = this.origin; // keep chs happy this.rpc_function = (this.origin == window_origin) ? this.rpc_json : this.rpc_jsonp; @@ -962,7 +1010,9 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { if (this.override_session) params.session_id = this.session_id; var qs = '?' + $.param(params); - var prefix = _.any(['http://', 'https://', '//'], _.bind(_.str.startsWith, null, path)) ? '' : this.prefix; + var prefix = _.any(['http://', 'https://', '//'], function(el) { + return path.length >= el.length && path.slice(0, el.length) === el; + }) ? '' : this.prefix; return prefix + path + qs; }, }); -- cgit v1.2.3 From a51e0424004dcd3d06e34bd023cc08acca4218d8 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Wed, 31 Jul 2013 17:19:58 +0200 Subject: Removed some more references to _.str bzr revid: nicolas.vanhoren@openerp.com-20130731151958-bgc0w02et69mf1ok --- addons/web/static/src/js/openerpframework.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 81295dc1718..e862c90557b 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -968,7 +968,8 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { // Indirect jsonp request var ifid = _.uniqueId('oe_rpc_iframe'); var display = 'none'; - var $iframe = $(_.str.sprintf("", ifid, ifid, display)); + var html = ""; + var $iframe = $(html); var $form = $('') .attr('method', 'POST') .attr('target', ifid) -- cgit v1.2.3 From dae87b45352d18f91a8e27b0b868c4fd8ef3a1b0 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Wed, 31 Jul 2013 18:34:15 +0200 Subject: Try to reimplement jsonrpc with niv's methods, still a lot of tests fail bzr revid: nicolas.vanhoren@openerp.com-20130731163415-v6ec0nqsr0qj91a6 --- addons/web/static/src/js/openerpframework.js | 167 ++++++--------------------- addons/web/static/src/js/testing.js | 15 ++- 2 files changed, 43 insertions(+), 139 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index e862c90557b..e84ad5f9061 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -840,7 +840,7 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { this.origin = origin ? origin.replace( /\/+$/, '') : window_origin; this.prefix = this.origin; this.server = this.origin; // keep chs happy - this.rpc_function = (this.origin == window_origin) ? this.rpc_json : this.rpc_jsonp; + this.is_jsonp = this.origin !== window_origin; }, /** * Executes an RPC call, registering the provided callbacks. @@ -859,153 +859,54 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { rpc: function(url, params, options) { var self = this; options = options || {}; - // url can be an $.ajax option object - if (_.isString(url)) { - url = { url: url }; + //TODO: remove + var jqOptions = {}; + if (! _.isString(url)) { + _.extend(jqOptions, url); + url = url.url; } - _.defaults(params, { - context: this.user_context || {} - }); - // Construct a JSON-RPC2 request, method is currently unused - var payload = { - jsonrpc: '2.0', - method: 'call', - params: params, - id: _.uniqueId('r') - }; - var deferred = $.Deferred(); - if (! options.shadow) - this.trigger('request', url, payload); - + // TODO correct handling of timeouts if (options.timeout) - url.timeout = options.timeout; - - this.rpc_function(url, payload).then( - function (response, textStatus, jqXHR) { + jqOptions.timeout = options.timeout; + if (! options.shadow) + this.trigger('request'); + var p; + if (! this.is_jsonp) { + p = openerp.web.jsonRpc(url, "call", params, jqOptions); + } else { + p = openerp.web.jsonpRpc(url, "call", params, jqOptions); + } + p = p.then(function (result) { + if (! options.shadow) + self.trigger('response'); + return result; + }, function(type, error, textStatus, errorThrown) { + if (type === "server") { if (! options.shadow) - self.trigger('response', response); - if (!response.error) { - deferred.resolve(response["result"], textStatus, jqXHR); - } else if (response.error.code === 100) { + self.trigger('response'); + if (error.code === 100) { self.uid = false; - } else { - deferred.reject(response.error, $.Event()); } - }, - function(jqXHR, textStatus, errorThrown) { + return $.Deferred().reject(error, $.Event()); + } else { if (! options.shadow) - self.trigger('response_failed', jqXHR); - var error = { + self.trigger('response_failed'); + var nerror = { code: -32098, message: "XmlHttpRequestError " + errorThrown, - data: {type: "xhr"+textStatus, debug: jqXHR.responseText, objects: [jqXHR, errorThrown] } + data: {type: "xhr"+textStatus, debug: error.responseText, objects: [error, errorThrown] } }; - deferred.reject(error, $.Event()); - }); - // Allow deferred user to disable rpc_error call in fail - deferred.fail(function() { - deferred.fail(function(error, event) { + return $.Deferred().reject(nerror, $.Event()); + } + }); + return p.fail(function() { // Allow deferred user to disable rpc_error call in fail + p.fail(function(error, event) { if (!event.isDefaultPrevented()) { self.trigger('error', error, event); } }); }); - return deferred; - }, - /** - * Raw JSON-RPC call - * - * @returns {jQuery.Deferred} ajax-webd deferred object - */ - rpc_json: function(url, payload) { - var self = this; - var ajax = _.extend({ - type: "POST", - dataType: 'json', - contentType: 'application/json', - data: JSON.stringify(payload), - processData: false, - headers: { - "X-Openerp-Session-Id": this.override_session ? this.session_id : undefined, - }, - }, url); - if (this.synch) - ajax.async = false; - return $.ajax(ajax); }, - 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, - }; - - var set_sid = function (response, textStatus, jqXHR) { - // If response give us the http session id, we store it for next requests... - if (response.session_id) { - self.session_id = response.session_id; - } - }; - - url.url = this.url(url.url, null); - var ajax = _.extend({ - type: "GET", - dataType: 'jsonp', - jsonp: 'jsonp', - cache: false, - data: data - }, url); - if (this.synch) - ajax.async = false; - var payload_str = JSON.stringify(payload); - var payload_url = $.param({r:payload_str}); - if (payload_url.length < 2000) { - // Direct jsonp request - ajax.data.r = payload_str; - return $.ajax(ajax).done(set_sid); - } else { - // Indirect jsonp request - var ifid = _.uniqueId('oe_rpc_iframe'); - var display = 'none'; - var html = ""; - var $iframe = $(html); - var $form = $('') - .attr('method', 'POST') - .attr('target', ifid) - .attr('enctype', "multipart/form-data") - .attr('action', ajax.url + '?jsonp=1&' + $.param(data)) - .append($('').attr('value', payload_str)) - .hide() - .appendTo($('body')); - var cleanUp = function() { - if ($iframe) { - $iframe.unbind("load").remove(); - } - $form.remove(); - }; - 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(); - }).done(function() { - deferred.resolve.apply(deferred, arguments); - }).fail(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 deferred.done(set_sid); - } - }, - url: function(path, params) { params = _.extend(params || {}); if (this.override_session) diff --git a/addons/web/static/src/js/testing.js b/addons/web/static/src/js/testing.js index af434c4c0f4..54ffe8ce408 100644 --- a/addons/web/static/src/js/testing.js +++ b/addons/web/static/src/js/testing.js @@ -53,25 +53,28 @@ openerp.testing = {}; testing.mockifyRPC = function (instance, responses) { var session = instance.session; session.responses = responses || {}; - session.rpc_function = function (url, payload) { + session.rpc = function(url, rparams, options) { + if (_.isString(url)) { + url = {url: url}; + } var fn, params; - var needle = payload.params.model + ':' + payload.params.method; + var needle = rparams.model + ':' + rparams.method; if (url.url === '/web/dataset/call_kw' && needle in this.responses) { fn = this.responses[needle]; params = [ - payload.params.args || [], - payload.params.kwargs || {} + rparams.args || [], + rparams.kwargs || {} ]; } else { fn = this.responses[url.url]; - params = [payload]; + params = [{params: rparams}]; } if (!fn) { return $.Deferred().reject({}, 'failed', _.str.sprintf("Url %s not found in mock responses, with arguments %s", - url.url, JSON.stringify(payload.params)) + url.url, JSON.stringify(rparams)) ).promise(); } try { -- cgit v1.2.3 From f6318cc753b9a521b2245159cce5dfe3c041a75e Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 1 Aug 2013 18:08:47 +0200 Subject: Yiiiiha! This day was shitty but that damn tests finally work. bzr revid: nicolas.vanhoren@openerp.com-20130801160847-tcx6va9x1kp0q7eo --- addons/web/static/src/js/testing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/testing.js b/addons/web/static/src/js/testing.js index 4e310a9f4a9..052fe3312bb 100644 --- a/addons/web/static/src/js/testing.js +++ b/addons/web/static/src/js/testing.js @@ -80,7 +80,7 @@ openerp.testing = {}; try { return $.when(fn.apply(null, params)).then(function (result) { // Wrap for RPC layer unwrapper thingy - return {result: result}; + return result; }); } catch (e) { // not sure why this looks like that -- cgit v1.2.3 From cc04d9463e7a3e2a0412c86976a5c7592956e744 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 1 Aug 2013 18:42:07 +0200 Subject: Added more tests bzr revid: nicolas.vanhoren@openerp.com-20130801164207-wwv34s2fgtlxbzxz --- addons/web/static/src/js/openerpframework.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index e84ad5f9061..d03c88e151a 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -827,11 +827,12 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { * @param {String} [server] JSON-RPC endpoint hostname * @param {String} [port] JSON-RPC endpoint port */ - init: function() { - openerp.web.PropertiesMixin.init.call(this); + init: function(parent, origin) { + openerp.web.PropertiesMixin.init.call(this, parent); this.server = null; this.override_session = false; this.session_id = undefined; + this.setup(origin); }, setup: function(origin) { // must be able to customize server @@ -840,7 +841,7 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { this.origin = origin ? origin.replace( /\/+$/, '') : window_origin; this.prefix = this.origin; this.server = this.origin; // keep chs happy - this.is_jsonp = this.origin !== window_origin; + this.origin_server = this.origin === window_origin; }, /** * Executes an RPC call, registering the provided callbacks. @@ -859,8 +860,8 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { rpc: function(url, params, options) { var self = this; options = options || {}; - //TODO: remove var jqOptions = {}; + // TODO: remove if (! _.isString(url)) { _.extend(jqOptions, url); url = url.url; @@ -870,12 +871,17 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { jqOptions.timeout = options.timeout; if (! options.shadow) this.trigger('request'); - var p; - if (! this.is_jsonp) { - p = openerp.web.jsonRpc(url, "call", params, jqOptions); + var fct; + if (this.force_method) { + fct = this.force_method; } else { - p = openerp.web.jsonpRpc(url, "call", params, jqOptions); + if (this.origin_server) { + fct = openerp.web.jsonRpc; + } else { + fct = openerp.web.jsonpRpc; + } } + var p = fct(url, "call", params, jqOptions); p = p.then(function (result) { if (! options.shadow) self.trigger('response'); -- cgit v1.2.3 From adc722348c9349bfbab965c07b65a3c6be175561 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Fri, 2 Aug 2013 16:32:23 +0200 Subject: Made the 2-steps jsonp rpc work more or less bzr revid: nicolas.vanhoren@openerp.com-20130802143223-49mf5dayd8ape1a5 --- addons/web/static/src/js/openerpframework.js | 196 +++++++++++++++++++-------- 1 file changed, 140 insertions(+), 56 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index d03c88e151a..226480c96dd 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -802,15 +802,71 @@ openerp.web.jsonRpc = function(url, fct_name, params, settings) { }; openerp.web.jsonpRpc = function(url, fct_name, params, settings) { + settings = settings || {}; return genericJsonRpc(fct_name, params, function(data) { - return $.ajax(url, _.extend({}, settings, { - url: url, - dataType: 'jsonp', - jsonp: 'jsonp', - type: 'GET', - cache: false, - data: {r: JSON.stringify(data)}, - })); + var payload_str = JSON.stringify(data); + var payload_url = $.param({r:payload_str}); + var force2step = settings.force2step || false; + delete settings.force2step; + var session_id = settings.session_id || null; + delete settings.session_id; + if (payload_url.length < 2000 && ! force2step) { + return $.ajax(url, _.extend({}, settings, { + url: url, + dataType: 'jsonp', + jsonp: 'jsonp', + type: 'GET', + cache: false, + data: {r: payload_str, session_id: session_id}, + })); + } else { + var args = {session_id: session_id, id: data.id}; + var ifid = _.uniqueId('oe_rpc_iframe'); + var html = ""; + var $iframe = $(html); + var nurl = 'jsonp=1&' + $.param(args); + nurl = url.indexOf("?") !== -1 ? url + "&" + nurl : url + "?" + nurl; + var $form = $('') + .attr('method', 'POST') + .attr('target', ifid) + .attr('enctype', "multipart/form-data") + .attr('action', nurl) + .append($('').attr('value', payload_str)) + .hide() + .appendTo($('body')); + var cleanUp = function() { + if ($iframe) { + $iframe.unbind("load").remove(); + } + $form.remove(); + }; + 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({ + url: url, + dataType: 'jsonp', + jsonp: 'jsonp', + type: 'GET', + cache: false, + data: {session_id: session_id, id: data.id}, + }).always(function() { + cleanUp(); + }).done(function() { + deferred.resolve.apply(deferred, arguments); + }).fail(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 deferred; + } }); }; @@ -827,11 +883,13 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { * @param {String} [server] JSON-RPC endpoint hostname * @param {String} [port] JSON-RPC endpoint port */ - init: function(parent, origin) { + init: function(parent, origin, options) { openerp.web.PropertiesMixin.init.call(this, parent); + options = options || {}; this.server = null; - this.override_session = false; + this.override_session = options.override_session || false; this.session_id = undefined; + this.avoid_recursion = false; this.setup(origin); }, setup: function(origin) { @@ -842,6 +900,30 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { this.prefix = this.origin; this.server = this.origin; // keep chs happy this.origin_server = this.origin === window_origin; + this.session_id = null; + }, + check_session_id: function(force) { + var self = this; + if (this.avoid_recursion) + return $.when(); + if (this.session_id) + return $.when(); // we already have the session id + if (this.origin_server && ! this.override_session && ! force) + return $.when(); // no need for session_id if we are using the origin server + this.avoid_recursion = true; + function always() { + self.avoid_recursion = false; + } + if (this.override_server) { + return this.rpc("/gen_session_id", {}).then(function() { + debugger; + }).always(always); + } else { + this.avoid_recursion = true; + return this.rpc("/web/session/get_session_info", {}).then(function(res) { + self.session_id = res.session_id; + }).always(always); + } }, /** * Executes an RPC call, registering the provided callbacks. @@ -859,65 +941,67 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { */ rpc: function(url, params, options) { var self = this; - options = options || {}; - var jqOptions = {}; - // TODO: remove - if (! _.isString(url)) { - _.extend(jqOptions, url); - url = url.url; - } - // TODO correct handling of timeouts - if (options.timeout) - jqOptions.timeout = options.timeout; - if (! options.shadow) - this.trigger('request'); - var fct; - if (this.force_method) { - fct = this.force_method; - } else { - if (this.origin_server) { + options = _.clone(options || {}); + var shadow = options.shadow || false; + delete options.shadow; + + return self.check_session_id().then(function() { + // TODO: remove + if (! _.isString(url)) { + _.extend(options, url); + url = url.url; + } + // TODO correct handling of timeouts + if (! shadow) + self.trigger('request'); + var fct; + if (self.origin_server) { fct = openerp.web.jsonRpc; } else { fct = openerp.web.jsonpRpc; + url = self.url(url, null); + options.session_id = self.session_id; } - } - var p = fct(url, "call", params, jqOptions); - p = p.then(function (result) { - if (! options.shadow) - self.trigger('response'); - return result; - }, function(type, error, textStatus, errorThrown) { - if (type === "server") { - if (! options.shadow) + var p = fct(url, "call", params, options); + p = p.then(function (result) { + if (! shadow) self.trigger('response'); - if (error.code === 100) { - self.uid = false; - } - return $.Deferred().reject(error, $.Event()); - } else { - if (! options.shadow) - self.trigger('response_failed'); - var nerror = { - code: -32098, - message: "XmlHttpRequestError " + errorThrown, - data: {type: "xhr"+textStatus, debug: error.responseText, objects: [error, errorThrown] } - }; - return $.Deferred().reject(nerror, $.Event()); - } - }); - return p.fail(function() { // Allow deferred user to disable rpc_error call in fail - p.fail(function(error, event) { - if (!event.isDefaultPrevented()) { - self.trigger('error', error, event); + return result; + }, function(type, error, textStatus, errorThrown) { + if (type === "server") { + if (! shadow) + self.trigger('response'); + if (error.code === 100) { + self.uid = false; + } + return $.Deferred().reject(error, $.Event()); + } else { + if (! shadow) + self.trigger('response_failed'); + var nerror = { + code: -32098, + message: "XmlHttpRequestError " + errorThrown, + data: {type: "xhr"+textStatus, debug: error.responseText, objects: [error, errorThrown] } + }; + return $.Deferred().reject(nerror, $.Event()); } }); + return p.fail(function() { // Allow deferred user to disable rpc_error call in fail + p.fail(function(error, event) { + if (!event.isDefaultPrevented()) { + self.trigger('error', error, event); + } + }); + }); }); }, url: function(path, params) { params = _.extend(params || {}); if (this.override_session) params.session_id = this.session_id; - var qs = '?' + $.param(params); + var qs = $.param(params); + if (qs.length > 0) + qs = "?" + qs; var prefix = _.any(['http://', 'https://', '//'], function(el) { return path.length >= el.length && path.slice(0, el.length) === el; }) ? '' : this.prefix; -- cgit v1.2.3 From ab03d9c280dbc34946fc2231abc54ab041a56d29 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Fri, 2 Aug 2013 17:08:15 +0200 Subject: Make the three jsonrpc protocols work correctly with and without session overriding and added tests for all the cases. That's one small step for a man, a giant leap for all the poor programmers that would have to use that shit one day. bzr revid: nicolas.vanhoren@openerp.com-20130802150815-k2yuf0b3ku5fvx7a --- addons/web/static/src/js/openerpframework.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 226480c96dd..d8e946d0bed 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -914,9 +914,9 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { function always() { self.avoid_recursion = false; } - if (this.override_server) { - return this.rpc("/gen_session_id", {}).then(function() { - debugger; + if (this.override_session) { + return this.rpc("/gen_session_id", {}).then(function(result) { + self.session_id = result; }).always(always); } else { this.avoid_recursion = true; @@ -957,10 +957,15 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { var fct; if (self.origin_server) { fct = openerp.web.jsonRpc; + if (self.override_session) { + options.headers = _.extend({}, options.headers, { + "X-Openerp-Session-Id": self.override_session ? self.session_id || '' : '', + }); + } } else { fct = openerp.web.jsonpRpc; url = self.url(url, null); - options.session_id = self.session_id; + options.session_id = self.session_id || ''; } var p = fct(url, "call", params, options); p = p.then(function (result) { -- cgit v1.2.3 From bd8519abb50377131192ad5dae502747ad8e335c Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Fri, 2 Aug 2013 17:24:32 +0200 Subject: Make it a little bit cleaner to fetch the session_id bzr revid: nicolas.vanhoren@openerp.com-20130802152432-stvg9eed7f6juvq9 --- addons/web/static/src/js/openerpframework.js | 40 +++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index d8e946d0bed..56f78286909 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -902,27 +902,26 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { this.origin_server = this.origin === window_origin; this.session_id = null; }, - check_session_id: function(force) { + check_session_id: function() { var self = this; if (this.avoid_recursion) return $.when(); if (this.session_id) return $.when(); // we already have the session id - if (this.origin_server && ! this.override_session && ! force) - return $.when(); // no need for session_id if we are using the origin server - this.avoid_recursion = true; - function always() { - self.avoid_recursion = false; - } - if (this.override_session) { + if (this.override_session || ! this.origin_server) { + // If we don't use the origin server we consider we should always create a new session. + // Even if some browsers could support cookies when using jsonp that behavior is + // not consistent and the browser creators are tending to removing that feature. + this.avoid_recursion = true; return this.rpc("/gen_session_id", {}).then(function(result) { self.session_id = result; - }).always(always); + }).always(function() { + self.avoid_recursion = false; + }); } else { - this.avoid_recursion = true; - return this.rpc("/web/session/get_session_info", {}).then(function(res) { - self.session_id = res.session_id; - }).always(always); + // normal use case, just use the cookie + self.session_id = openerp.web.get_cookie("session_id"); + return $.when(); } }, /** @@ -1014,6 +1013,21 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { }, }); +openerp.web.get_cookie = function(c_name) { + if (document.cookie.length > 0) { + var c_start = document.cookie.indexOf(c_name + "="); + if (c_start != -1) { + c_start = c_start + c_name.length + 1; + var c_end = document.cookie.indexOf(";", c_start); + if (c_end == -1) { + c_end = document.cookie.length; + } + return unescape(document.cookie.substring(c_start, c_end)); + } + } + return ""; +}; + openerp.declare = declare; return openerp; -- cgit v1.2.3 From 85b223847239c2637faaeb1f70da723fc45abe7a Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 12:18:49 +0200 Subject: minor improvements bzr revid: nicolas.vanhoren@openerp.com-20130805101849-rqqx4jcmbymv5hfl --- addons/web/static/src/js/openerpframework.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 56f78286909..db03a286728 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -773,7 +773,7 @@ var genericJsonRpc = function(fct_name, params, fct) { jsonrpc: "2.0", method: fct_name, params: params, - id: Math.floor(Math.random() * 1000 * 1000 * 1000), + id: Math.floor(Math.random() * 1000 * 1000 * 1000) }; return fct(data).pipe(function(result) { if (result.error !== undefined) { @@ -796,7 +796,7 @@ openerp.web.jsonRpc = function(url, fct_name, params, settings) { dataType: 'json', type: 'POST', data: JSON.stringify(data), - contentType: 'application/json', + contentType: 'application/json' })); }); }; @@ -817,7 +817,7 @@ openerp.web.jsonpRpc = function(url, fct_name, params, settings) { jsonp: 'jsonp', type: 'GET', cache: false, - data: {r: payload_str, session_id: session_id}, + data: {r: payload_str, session_id: session_id} })); } else { var args = {session_id: session_id, id: data.id}; @@ -851,7 +851,7 @@ openerp.web.jsonpRpc = function(url, fct_name, params, settings) { jsonp: 'jsonp', type: 'GET', cache: false, - data: {session_id: session_id, id: data.id}, + data: {session_id: session_id, id: data.id} }).always(function() { cleanUp(); }).done(function() { @@ -875,7 +875,7 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { 'request': 'Request sent', 'response': 'Response received', 'response_failed': 'HTTP Error response or timeout received', - 'error': 'The received response is an JSON-RPC error', + 'error': 'The received response is an JSON-RPC error' }, /** * @constructs openerp.web.JsonRPC @@ -958,7 +958,7 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { fct = openerp.web.jsonRpc; if (self.override_session) { options.headers = _.extend({}, options.headers, { - "X-Openerp-Session-Id": self.override_session ? self.session_id || '' : '', + "X-Openerp-Session-Id": self.override_session ? self.session_id || '' : '' }); } } else { @@ -1001,7 +1001,7 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { }, url: function(path, params) { params = _.extend(params || {}); - if (this.override_session) + if (this.override_session || (! this.origin_server)) params.session_id = this.session_id; var qs = $.param(params); if (qs.length > 0) @@ -1010,7 +1010,7 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { return path.length >= el.length && path.slice(0, el.length) === el; }) ? '' : this.prefix; return prefix + path + qs; - }, + } }); openerp.web.get_cookie = function(c_name) { -- cgit v1.2.3 From 22c56b1dd85b5302c03886fe5f6096f817a981a5 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 13:12:50 +0200 Subject: Ported some authentication methods to the Session class bzr revid: nicolas.vanhoren@openerp.com-20130805111250-ws23n3b6os54qsp3 --- addons/web/static/src/js/coresetup.js | 54 +++++--------------------- addons/web/static/src/js/openerpframework.js | 57 +++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 55 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/coresetup.js b/addons/web/static/src/js/coresetup.js index c8deca1c9ac..3f4ea4a183b 100644 --- a/addons/web/static/src/js/coresetup.js +++ b/addons/web/static/src/js/coresetup.js @@ -14,27 +14,23 @@ if (!console) { openerp.web.coresetup = function(instance) { +/* + Some retro-compatibility. +*/ +instance.web.JsonRPC = instance.web.Session; + /** Session openerp specific RPC class */ -instance.web.Session = instance.web.JsonRPC.extend( /** @lends instance.web.Session# */{ +instance.web.Session.include( /** @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; @@ -69,33 +65,6 @@ instance.web.Session = instance.web.JsonRPC.extend( /** @lends instance.web.Sess ); }); }, - /** - * (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) { @@ -106,15 +75,9 @@ instance.web.Session = instance.web.JsonRPC.extend( /** @lends instance.web.Sess /** * The session is validated either by login or by restoration of a previous session */ - session_authenticate: function(db, login, password, _volatile) { + session_authenticate: 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).then(function(result) { - if (!result.uid) { - return $.Deferred().reject(); - } - _.extend(self, result); + return $.when(this._super.apply(this, arguments)).then(function() { return self.load_modules(); }); }, @@ -550,6 +513,7 @@ instance.session = new instance.web.Session(); /** Configure default qweb */ instance.web._t = new instance.web.TranslationDataBase().build_translation_function(); +instance.web._t = new instance.web.TranslationDataBase().build_translation_function(); /** * Lazy translation function, only performs the translation when actually * printed (e.g. inserted into a template) diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index db03a286728..c80ee9ffcdb 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -870,7 +870,7 @@ openerp.web.jsonpRpc = function(url, fct_name, params, settings) { }); }; -openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { +openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { triggers: { 'request': 'Request sent', 'response': 'Response received', @@ -878,29 +878,66 @@ openerp.web.JsonRPC = openerp.web.Class.extend(openerp.web.PropertiesMixin, { 'error': 'The received response is an JSON-RPC error' }, /** - * @constructs openerp.web.JsonRPC - * - * @param {String} [server] JSON-RPC endpoint hostname - * @param {String} [port] JSON-RPC endpoint port + @constructs openerp.web.Session + + @param parent The parent of the newly created object. + @param {String} origin Url of the OpenERP server to contact with this session object + or `null` if the server to contact is the origin server. + @param {Dict} options A dictionary that can contain the following options: + + * "override_session": Default to false. If true, the current session object will + not try to re-use a previously created session id stored in a cookie. + * "session_id": Default to null. If specified, the specified session_id will be used + by this session object. Specifying this option automatically implies that the option + "override_session" is set to true. */ init: function(parent, origin, options) { openerp.web.PropertiesMixin.init.call(this, parent); options = options || {}; this.server = null; - this.override_session = options.override_session || false; - this.session_id = undefined; + this.session_id = options.session_id || null; + this.override_session = options.override_session || !!options.session_id || false; this.avoid_recursion = false; this.setup(origin); }, setup: function(origin) { // must be able to customize server var window_origin = location.protocol + "//" + location.host; - var self = this; - this.origin = origin ? origin.replace( /\/+$/, '') : window_origin; + origin = origin ? origin.replace( /\/+$/, '') : window_origin; + if (!_.isUndefined(this.origin) && this.origin !== origin) + throw new Error('Session already bound to ' + this.origin); + else + this.origin = origin; this.prefix = this.origin; this.server = this.origin; // keep chs happy this.origin_server = this.origin === window_origin; - this.session_id = null; + }, + /** + * (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 self.rpc("/web/session/get_session_info", {}).then(function(result) { + delete result.session_id; + _.extend(self, result); + }); + }, + /** + * The session is validated either by login or by restoration of a previous session + */ + session_authenticate: function(db, login, password) { + var self = this; + var params = {db: db, login: login, password: password}; + return this.rpc("/web/session/authenticate", params).then(function(result) { + if (!result.uid) { + return $.Deferred().reject(); + } + delete result.session_id; + _.extend(self, result); + }); }, check_session_id: function() { var self = this; -- cgit v1.2.3 From affb4f0589ad7a3d57dcbf191b9c15debb47530a Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 14:39:16 +0200 Subject: Ported model to the framework bzr revid: nicolas.vanhoren@openerp.com-20130805123916-s62upxq3jwqjygt0 --- addons/web/static/src/js/data.js | 26 +++--------- addons/web/static/src/js/openerpframework.js | 63 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 20 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index b63d44a408d..f333f6a3e4b 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -253,19 +253,11 @@ instance.web.QueryGroup = instance.web.Class.extend({ } }); -instance.web.Model = instance.web.Class.extend({ - /** - * @constructs instance.web.Model - * @extends instance.web.Class - * - * @param {String} model_name name of the OpenERP model this object is bound to - * @param {Object} [context] - * @param {Array} [domain] - */ - init: function (model_name, context, domain) { - this.name = model_name; - this._context = context || {}; - this._domain = domain || []; +instance.web.Model.include({ + session: function() { + if (! this._session) + return instance.session; + return this._super(); }, /** * @deprecated does not allow to specify kwargs, directly use call() instead @@ -294,13 +286,7 @@ instance.web.Model = instance.web.Class.extend({ args = []; } instance.web.pyeval.ensure_evaluated(args, kwargs); - var debug = instance.session.debug ? '/'+this.name+':'+method : ''; - return instance.session.rpc('/web/dataset/call_kw' + debug, { - model: this.name, - method: method, - args: args, - kwargs: kwargs - }, options); + return this._super(method, args, kwargs, options); }, /** * Fetches a Query instance bound to this model, for searching diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index c80ee9ffcdb..d3dd5f786a0 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -1050,6 +1050,69 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { } }); +openerp.web.Model = openerp.web.Class.extend({ + /** + new openerp.web.Model([session,] model_name[, context[, domain]]) + + @constructs instance.web.Model + @extends instance.web.Class + + @param {openerp.web.Session} [session] The session object used to communicate with + the server. + @param {String} model_name name of the OpenERP model this object is bound to + @param {Object} [context] + @param {Array} [domain] + */ + init: function () { + var session, model_name, context, domain; + var args = _.toArray(arguments); + args.reverse(); + session = args.pop(); + if (! (session instanceof openerp.web.Session)) { + model_name = session; + session = null; + } else { + model_name = args.pop(); + } + context = args.length > 0 ? args.pop() : null; + domain = args.length > 0 ? args.pop() : null; + + this.name = model_name; + this._context = context || {}; + this._domain = domain || []; + this._session = session; + }, + session: function() { + if (! this._session) + throw new Error("Not implemented"); + return this._session; + }, + /** + * Call a method (over RPC) on the bound OpenERP model. + * + * @param {String} method name of the method to call + * @param {Array} [args] positional arguments + * @param {Object} [kwargs] keyword arguments + * @param {Object} [options] additional options for the rpc() method + * @returns {jQuery.Deferred<>} call result + */ + call: function (method, args, kwargs, options) { + args = args || []; + kwargs = kwargs || {}; + if (!_.isArray(args)) { + // call(method, kwargs) + kwargs = args; + args = []; + } + return this.session().rpc('/web/dataset/call_kw', { + model: this.name, + method: method, + args: args, + kwargs: kwargs + }, options); + }, +}); + openerp.web.get_cookie = function(c_name) { if (document.cookie.length > 0) { var c_start = document.cookie.indexOf(c_name + "="); -- cgit v1.2.3 From 226b142ac6d421f7dc5f1bab204f5cde24f1505a Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 15:04:58 +0200 Subject: Simplified a little the Query class by removing a reference to a controller and using call_kw instead bzr revid: nicolas.vanhoren@openerp.com-20130805130458-21w806v9wc456oq4 --- addons/web/static/src/js/data.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index f333f6a3e4b..986c58270ca 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -58,19 +58,18 @@ instance.web.Query = instance.web.Class.extend({ }, _execute: function () { var self = this; - return instance.session.rpc('/web/dataset/search_read', { - model: this._model.name, - fields: this._fields || false, + return this._model.call('search_read', { domain: instance.web.pyeval.eval('domains', [this._model.domain(this._filter)]), - context: instance.web.pyeval.eval('contexts', - [this._model.context(this._context)]), + fields: this._fields || false, offset: this._offset, limit: this._limit, - sort: instance.web.serialize_sort(this._order_by) + order: instance.web.serialize_sort(this._order_by), + context: instance.web.pyeval.eval('contexts', + [this._model.context(this._context)]), }).then(function (results) { self._count = results.length; - return results.records; + return results; }, null); }, /** -- cgit v1.2.3 From d256d16e3ae3181720ce9f01dd1c58e5c0a3d063 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 15:33:09 +0200 Subject: Fixed potential security issue + some details bzr revid: nicolas.vanhoren@openerp.com-20130805133309-jds80qa5j5lgwslq --- addons/web/static/src/js/openerpframework.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index d3dd5f786a0..43a45e2d1d2 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -1047,7 +1047,10 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { return path.length >= el.length && path.slice(0, el.length) === el; }) ? '' : this.prefix; return prefix + path + qs; - } + }, + model: function(model_name) { + return new openerp.web.Model(this, model_name); + }, }); openerp.web.Model = openerp.web.Class.extend({ @@ -1084,7 +1087,7 @@ openerp.web.Model = openerp.web.Class.extend({ }, session: function() { if (! this._session) - throw new Error("Not implemented"); + throw new Error("Not session specified"); return this._session; }, /** -- cgit v1.2.3 From 76ef945c96c8cb3bbd823965a00232b451ed8a1a Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 15:40:59 +0200 Subject: Corrected some stuff in openerp.web.Model bzr revid: nicolas.vanhoren@openerp.com-20130805134059-3ayw6b0tqqberkaw --- addons/web/static/src/js/data.js | 34 ++++++++++++++++++++++++++-- addons/web/static/src/js/openerpframework.js | 10 +++----- 2 files changed, 35 insertions(+), 9 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index 986c58270ca..d501b1263b1 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -253,6 +253,36 @@ instance.web.QueryGroup = instance.web.Class.extend({ }); instance.web.Model.include({ + /** + new openerp.web.Model([session,] model_name[, context[, domain]]) + + @constructs instance.web.Model + @extends instance.web.Class + + @param {openerp.web.Session} [session] The session object used to communicate with + the server. + @param {String} model_name name of the OpenERP model this object is bound to + @param {Object} [context] + @param {Array} [domain] + */ + init: function() { + var session, model_name, context, domain; + var args = _.toArray(arguments); + args.reverse(); + session = args.pop(); + if (session && ! (session instanceof openerp.web.Session)) { + model_name = session; + session = null; + } else { + model_name = args.pop(); + } + context = args.length > 0 ? args.pop() : null; + domain = args.length > 0 ? args.pop() : null; + + this._super(session, model_name, context, domain); + this._context = context || {}; + this._domain = domain || []; + }, session: function() { if (! this._session) return instance.session; @@ -303,7 +333,7 @@ instance.web.Model.include({ * @param {String} signal signal to trigger on the workflow */ exec_workflow: function (id, signal) { - return instance.session.rpc('/web/dataset/exec_workflow', { + return this.session().rpc('/web/dataset/exec_workflow', { model: this.name, id: id, signal: signal @@ -340,7 +370,7 @@ instance.web.Model.include({ */ call_button: function (method, args) { instance.web.pyeval.ensure_evaluated(args, {}); - return instance.session.rpc('/web/dataset/call_button', { + return this.session().rpc('/web/dataset/call_button', { model: this.name, method: method, // Should not be necessary anymore. Integrate remote in this? diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 43a45e2d1d2..94149f94dbd 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -1055,7 +1055,7 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { openerp.web.Model = openerp.web.Class.extend({ /** - new openerp.web.Model([session,] model_name[, context[, domain]]) + new openerp.web.Model([session,] model_name) @constructs instance.web.Model @extends instance.web.Class @@ -1067,22 +1067,18 @@ openerp.web.Model = openerp.web.Class.extend({ @param {Array} [domain] */ init: function () { - var session, model_name, context, domain; + var session, model_name; var args = _.toArray(arguments); args.reverse(); session = args.pop(); - if (! (session instanceof openerp.web.Session)) { + if (session && ! (session instanceof openerp.web.Session)) { model_name = session; session = null; } else { model_name = args.pop(); } - context = args.length > 0 ? args.pop() : null; - domain = args.length > 0 ? args.pop() : null; this.name = model_name; - this._context = context || {}; - this._domain = domain || []; this._session = session; }, session: function() { -- cgit v1.2.3 From b3c4d025908bc8923069154de4673fae283cd18e Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 16:44:31 +0200 Subject: Improved translation controller to be more flexible bzr revid: nicolas.vanhoren@openerp.com-20130805144431-ycv6gwv99c5bh1p1 --- addons/web/static/src/js/coresetup.js | 1 - 1 file changed, 1 deletion(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/coresetup.js b/addons/web/static/src/js/coresetup.js index 3f4ea4a183b..59f2c6a3a96 100644 --- a/addons/web/static/src/js/coresetup.js +++ b/addons/web/static/src/js/coresetup.js @@ -513,7 +513,6 @@ instance.session = new instance.web.Session(); /** Configure default qweb */ instance.web._t = new instance.web.TranslationDataBase().build_translation_function(); -instance.web._t = new instance.web.TranslationDataBase().build_translation_function(); /** * Lazy translation function, only performs the translation when actually * printed (e.g. inserted into a template) -- cgit v1.2.3 From 38699e04cf74399c4ee35881feddc170e4073c87 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 16:55:14 +0200 Subject: Copied a part of translation database to openerpframework.js bzr revid: nicolas.vanhoren@openerp.com-20130805145514-uuywlhleyfz2xoj4 --- addons/web/static/src/js/coresetup.js | 54 ++-------------------- addons/web/static/src/js/openerpframework.js | 67 +++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 58 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/coresetup.js b/addons/web/static/src/js/coresetup.js index 59f2c6a3a96..7f73b867a0e 100644 --- a/addons/web/static/src/js/coresetup.js +++ b/addons/web/static/src/js/coresetup.js @@ -375,58 +375,13 @@ instance.web.Bus = instance.web.Class.extend(instance.web.EventDispatcherMixin, }); instance.web.bus = new instance.web.Bus(); -/** OpenERP Translations */ -instance.web.TranslationDataBase = instance.web.Class.extend(/** @lends instance.web.TranslationDataBase# */{ - /** - * @constructs instance.web.TranslationDataBase - * @extends instance.web.Class - */ - init: function() { - this.db = {}; - this.parameters = {"direction": 'ltr', - "date_format": '%m/%d/%Y', - "time_format": '%H:%M:%S', - "grouping": [], - "decimal_point": ".", - "thousands_sep": ","}; - }, +instance.web.TranslationDataBase.include({ set_bundle: function(translation_bundle) { - var self = this; - this.db = {}; - var modules = _.keys(translation_bundle.modules); - modules.sort(); - if (_.include(modules, "web")) { - modules = ["web"].concat(_.without(modules, "web")); - } - _.each(modules, function(name) { - self.add_module_translation(translation_bundle.modules[name]); - }); + this._super(translation_bundle); if (translation_bundle.lang_parameters) { - this.parameters = translation_bundle.lang_parameters; - this.parameters.grouping = py.eval( - this.parameters.grouping); + this.parameters.grouping = py.eval(this.parameters.grouping); } }, - add_module_translation: function(mod) { - var self = this; - _.each(mod.messages, function(message) { - self.db[message.id] = message.string; - }); - }, - build_translation_function: function() { - var self = this; - var fcnt = function(str) { - var tmp = self.get(str); - return tmp === undefined ? str : tmp; - }; - fcnt.database = this; - return fcnt; - }, - get: function(key) { - if (this.db[key]) - return this.db[key]; - return undefined; - } }); /** Custom jQuery plugins */ @@ -511,8 +466,6 @@ $.async_when = function() { /** Setup default session */ instance.session = new instance.web.Session(); -/** Configure default qweb */ -instance.web._t = new instance.web.TranslationDataBase().build_translation_function(); /** * Lazy translation function, only performs the translation when actually * printed (e.g. inserted into a template) @@ -529,7 +482,6 @@ instance.web._lt = function (s) { }; instance.web.qweb.debug = instance.session.debug; _.extend(instance.web.qweb.default_dict, { - '_t' : instance.web._t, '__debug__': instance.session.debug, }); instance.web.qweb.preprocess_node = function() { diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 94149f94dbd..37995cbd65e 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -761,13 +761,6 @@ openerp.web.Widget = openerp.web.Class.extend(openerp.web.PropertiesMixin, { } }); -openerp.web.qweb = new QWeb2.Engine(); - -openerp.web.qweb.default_dict = { - '_' : _, - 'JSON': JSON -}; - var genericJsonRpc = function(fct_name, params, fct) { var data = { jsonrpc: "2.0", @@ -1112,6 +1105,58 @@ openerp.web.Model = openerp.web.Class.extend({ }, }); +/** OpenERP Translations */ +openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends instance.web.TranslationDataBase# */{ + /** + * @constructs instance.web.TranslationDataBase + * @extends instance.web.Class + */ + init: function() { + this.db = {}; + this.parameters = {"direction": 'ltr', + "date_format": '%m/%d/%Y', + "time_format": '%H:%M:%S', + "grouping": [], + "decimal_point": ".", + "thousands_sep": ","}; + }, + set_bundle: function(translation_bundle) { + var self = this; + this.db = {}; + var modules = _.keys(translation_bundle.modules); + modules.sort(); + if (_.include(modules, "web")) { + modules = ["web"].concat(_.without(modules, "web")); + } + _.each(modules, function(name) { + self.add_module_translation(translation_bundle.modules[name]); + }); + if (translation_bundle.lang_parameters) { + this.parameters = translation_bundle.lang_parameters; + } + }, + add_module_translation: function(mod) { + var self = this; + _.each(mod.messages, function(message) { + self.db[message.id] = message.string; + }); + }, + build_translation_function: function() { + var self = this; + var fcnt = function(str) { + var tmp = self.get(str); + return tmp === undefined ? str : tmp; + }; + fcnt.database = this; + return fcnt; + }, + get: function(key) { + return this.db[key]; + } +}); + +openerp.web._t = new openerp.web.TranslationDataBase().build_translation_function(); + openerp.web.get_cookie = function(c_name) { if (document.cookie.length > 0) { var c_start = document.cookie.indexOf(c_name + "="); @@ -1127,6 +1172,14 @@ openerp.web.get_cookie = function(c_name) { return ""; }; +openerp.web.qweb = new QWeb2.Engine(); + +openerp.web.qweb.default_dict = { + '_' : _, + 'JSON': JSON, + '_t' : openerp.web._t, +}; + openerp.declare = declare; return openerp; -- cgit v1.2.3 From 1bd4b621425c7a96a501e61a37256453c6311cff Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 17:10:12 +0200 Subject: Perfected translations integration in the framework bzr revid: nicolas.vanhoren@openerp.com-20130805151012-wnc7wkyiz0eay5oh --- addons/web/static/src/js/coresetup.js | 5 +---- addons/web/static/src/js/openerpframework.js | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/coresetup.js b/addons/web/static/src/js/coresetup.js index 7f73b867a0e..77af4808605 100644 --- a/addons/web/static/src/js/coresetup.js +++ b/addons/web/static/src/js/coresetup.js @@ -156,10 +156,7 @@ instance.web.Session.include( /** @lends instance.web.Session# */{ }); }, load_translations: function() { - var params = { mods: this.module_list, lang: this.user_context.lang }; - return this.rpc('/web/webclient/translations', params).done(function(trans) { - instance.web._t.database.set_bundle(trans); - }); + return instance.web._t.database.load_translations(this, this.module_list, this.user_context.lang); }, load_css: function (files) { var self = this; diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 37995cbd65e..a34180b87a8 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -1152,7 +1152,26 @@ openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends instance.w }, get: function(key) { return this.db[key]; - } + }, + /** + Loads the translations from an OpenERP server. + + @param {openerp.web.Session} session The session object to contact the server. + @param {Array} [modules] The list of modules to load the translation. If not specified, + it will default to all the modules installed in the current database. + @param {Object} [lang] lang The language. If not specified it will default to the language + of the current user. + @returns {jQuery.Deferred} + */ + load_translations: function(session, modules, lang) { + var self = this; + return session.rpc('/web/webclient/translations', { + "mods": modules || null, + "lang": lang || null, + }).done(function(trans) { + self.set_bundle(trans); + }); + }, }); openerp.web._t = new openerp.web.TranslationDataBase().build_translation_function(); -- cgit v1.2.3 From 034e4893d5cd8c3dbd41030db765153b784cc519 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 5 Aug 2013 17:15:44 +0200 Subject: Some commas bzr revid: nicolas.vanhoren@openerp.com-20130805151544-pdaebd2o8bh70z7z --- addons/web/static/src/js/openerpframework.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index a34180b87a8..86431f639ed 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -1043,7 +1043,7 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { }, model: function(model_name) { return new openerp.web.Model(this, model_name); - }, + } }); openerp.web.Model = openerp.web.Class.extend({ @@ -1102,7 +1102,7 @@ openerp.web.Model = openerp.web.Class.extend({ args: args, kwargs: kwargs }, options); - }, + } }); /** OpenERP Translations */ @@ -1167,11 +1167,11 @@ openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends instance.w var self = this; return session.rpc('/web/webclient/translations', { "mods": modules || null, - "lang": lang || null, + "lang": lang || null }).done(function(trans) { self.set_bundle(trans); }); - }, + } }); openerp.web._t = new openerp.web.TranslationDataBase().build_translation_function(); @@ -1196,7 +1196,7 @@ openerp.web.qweb = new QWeb2.Engine(); openerp.web.qweb.default_dict = { '_' : _, 'JSON': JSON, - '_t' : openerp.web._t, + '_t' : openerp.web._t }; openerp.declare = declare; -- cgit v1.2.3 From cb55e11c700efa298a74da781a108185682325da Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Tue, 6 Aug 2013 10:58:12 +0200 Subject: Put everything in the framework directly in the openerp namespace instead of the openerp.web namespace. bzr revid: nicolas.vanhoren@openerp.com-20130806085812-ek8v8k0x4m7ocu4f --- addons/web/static/src/js/boot.js | 3 + addons/web/static/src/js/openerpframework.js | 83 ++++++++++++++-------------- 2 files changed, 44 insertions(+), 42 deletions(-) (limited to 'addons/web/static/src') diff --git a/addons/web/static/src/js/boot.js b/addons/web/static/src/js/boot.js index 87115fd30ea..f99d2025594 100644 --- a/addons/web/static/src/js/boot.js +++ b/addons/web/static/src/js/boot.js @@ -7,6 +7,9 @@ * @namespace openerp */ (function() { + // copy everything in the openerp namespace to openerp.web + openerp.web = _.clone(openerp); + var inited = false; _.extend(openerp, { diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 86431f639ed..13fb1845536 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -5,7 +5,6 @@ function declare($, _, QWeb2) { var openerp = {}; -openerp.web = {}; /** * Improved John Resig's inheritance, based on: @@ -21,7 +20,7 @@ openerp.web = {}; * * Example: * - * var Person = openerp.web.Class.extend({ + * var Person = openerp.Class.extend({ * init: function(isDancing){ * this.dancing = isDancing; * }, @@ -57,14 +56,14 @@ openerp.web = {}; var initializing = false, fnTest = /xyz/.test(function(){xyz();}) ? /\b_super\b/ : /.*/; // The web Class implementation (does nothing) - openerp.web.Class = function(){}; + openerp.Class = function(){}; /** * Subclass an existing class * * @param {Object} prop class-level properties (class attributes and instance methods) to set on the new class */ - openerp.web.Class.extend = function() { + openerp.Class.extend = function() { var _super = this.prototype; // Support mixins arguments var args = _.toArray(arguments); @@ -104,7 +103,7 @@ openerp.web = {}; // The dummy class constructor function Class() { - if(this.constructor !== openerp.web.Class){ + if(this.constructor !== openerp.Class){ throw new Error("You can only instanciate objects with the 'new' operator"); } // All construction is actually done in the init method @@ -165,7 +164,7 @@ openerp.web = {}; * When an object is destroyed, all its children are destroyed too releasing * any resource they could have reserved before. */ -openerp.web.ParentedMixin = { +openerp.ParentedMixin = { __parentedMixin : true, init: function() { this.__parentedDestroyed = false; @@ -274,7 +273,7 @@ openerp.web.ParentedMixin = { * http://backbonejs.org * */ -var Events = openerp.web.Class.extend({ +var Events = openerp.Class.extend({ on : function(events, callback, context) { var ev; events = events.split(/\s+/); @@ -355,10 +354,10 @@ var Events = openerp.web.Class.extend({ } }); -openerp.web.EventDispatcherMixin = _.extend({}, openerp.web.ParentedMixin, { +openerp.EventDispatcherMixin = _.extend({}, openerp.ParentedMixin, { __eventDispatcherMixin: true, init: function() { - openerp.web.ParentedMixin.init.call(this); + openerp.ParentedMixin.init.call(this); this.__edispatcherEvents = new Events(); this.__edispatcherRegisteredEvents = []; }, @@ -403,13 +402,13 @@ openerp.web.EventDispatcherMixin = _.extend({}, openerp.web.ParentedMixin, { this.off(cal[0], cal[2], cal[1]); }, this); this.__edispatcherEvents.off(); - openerp.web.ParentedMixin.destroy.call(this); + openerp.ParentedMixin.destroy.call(this); } }); -openerp.web.PropertiesMixin = _.extend({}, openerp.web.EventDispatcherMixin, { +openerp.PropertiesMixin = _.extend({}, openerp.EventDispatcherMixin, { init: function() { - openerp.web.EventDispatcherMixin.init.call(this); + openerp.EventDispatcherMixin.init.call(this); this.__getterSetterInternalMap = {}; }, set: function(arg1, arg2, arg3) { @@ -493,7 +492,7 @@ openerp.web.PropertiesMixin = _.extend({}, openerp.web.EventDispatcherMixin, { * * That will kill the widget in a clean way and erase its content from the dom. */ -openerp.web.Widget = openerp.web.Class.extend(openerp.web.PropertiesMixin, { +openerp.Widget = openerp.Class.extend(openerp.PropertiesMixin, { // Backbone-ish API tagName: 'div', id: null, @@ -510,14 +509,14 @@ openerp.web.Widget = openerp.web.Class.extend(openerp.web.PropertiesMixin, { /** * Constructs the widget and sets its parent if a parent is given. * - * @constructs openerp.web.Widget + * @constructs openerp.Widget * - * @param {openerp.web.Widget} parent Binds the current instance to the given Widget instance. + * @param {openerp.Widget} parent Binds the current instance to the given Widget instance. * When that widget is destroyed by calling destroy(), the current instance will be * destroyed too. Can be null. */ init: function(parent) { - openerp.web.PropertiesMixin.init.call(this); + openerp.PropertiesMixin.init.call(this); this.setParent(parent); // Bind on_/do_* methods to this // We might remove this automatic binding in the future @@ -541,7 +540,7 @@ openerp.web.Widget = openerp.web.Class.extend(openerp.web.PropertiesMixin, { if(this.$el) { this.$el.remove(); } - openerp.web.PropertiesMixin.destroy.call(this); + openerp.PropertiesMixin.destroy.call(this); }, /** * Renders the current widget and appends it to the given jQuery object or Widget. @@ -622,7 +621,7 @@ openerp.web.Widget = openerp.web.Class.extend(openerp.web.PropertiesMixin, { renderElement: function() { var $el; if (this.template) { - $el = $(openerp.web.qweb.render(this.template, {widget: this}).trim()); + $el = $(openerp.qweb.render(this.template, {widget: this}).trim()); } else { $el = this._make_descriptive(); } @@ -782,7 +781,7 @@ var genericJsonRpc = function(fct_name, params, fct) { }); }; -openerp.web.jsonRpc = function(url, fct_name, params, settings) { +openerp.jsonRpc = function(url, fct_name, params, settings) { return genericJsonRpc(fct_name, params, function(data) { return $.ajax(url, _.extend({}, settings, { url: url, @@ -794,7 +793,7 @@ openerp.web.jsonRpc = function(url, fct_name, params, settings) { }); }; -openerp.web.jsonpRpc = function(url, fct_name, params, settings) { +openerp.jsonpRpc = function(url, fct_name, params, settings) { settings = settings || {}; return genericJsonRpc(fct_name, params, function(data) { var payload_str = JSON.stringify(data); @@ -863,7 +862,7 @@ openerp.web.jsonpRpc = function(url, fct_name, params, settings) { }); }; -openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { +openerp.Session = openerp.Class.extend(openerp.PropertiesMixin, { triggers: { 'request': 'Request sent', 'response': 'Response received', @@ -871,7 +870,7 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { 'error': 'The received response is an JSON-RPC error' }, /** - @constructs openerp.web.Session + @constructs openerp.Session @param parent The parent of the newly created object. @param {String} origin Url of the OpenERP server to contact with this session object @@ -885,7 +884,7 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { "override_session" is set to true. */ init: function(parent, origin, options) { - openerp.web.PropertiesMixin.init.call(this, parent); + openerp.PropertiesMixin.init.call(this, parent); options = options || {}; this.server = null; this.session_id = options.session_id || null; @@ -950,7 +949,7 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { }); } else { // normal use case, just use the cookie - self.session_id = openerp.web.get_cookie("session_id"); + self.session_id = openerp.get_cookie("session_id"); return $.when(); } }, @@ -985,14 +984,14 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { self.trigger('request'); var fct; if (self.origin_server) { - fct = openerp.web.jsonRpc; + fct = openerp.jsonRpc; if (self.override_session) { options.headers = _.extend({}, options.headers, { "X-Openerp-Session-Id": self.override_session ? self.session_id || '' : '' }); } } else { - fct = openerp.web.jsonpRpc; + fct = openerp.jsonpRpc; url = self.url(url, null); options.session_id = self.session_id || ''; } @@ -1042,18 +1041,18 @@ openerp.web.Session = openerp.web.Class.extend(openerp.web.PropertiesMixin, { return prefix + path + qs; }, model: function(model_name) { - return new openerp.web.Model(this, model_name); + return new openerp.Model(this, model_name); } }); -openerp.web.Model = openerp.web.Class.extend({ +openerp.Model = openerp.Class.extend({ /** - new openerp.web.Model([session,] model_name) + new openerp.Model([session,] model_name) - @constructs instance.web.Model - @extends instance.web.Class + @constructs instance.Model + @extends instance.Class - @param {openerp.web.Session} [session] The session object used to communicate with + @param {openerp.Session} [session] The session object used to communicate with the server. @param {String} model_name name of the OpenERP model this object is bound to @param {Object} [context] @@ -1064,7 +1063,7 @@ openerp.web.Model = openerp.web.Class.extend({ var args = _.toArray(arguments); args.reverse(); session = args.pop(); - if (session && ! (session instanceof openerp.web.Session)) { + if (session && ! (session instanceof openerp.Session)) { model_name = session; session = null; } else { @@ -1106,10 +1105,10 @@ openerp.web.Model = openerp.web.Class.extend({ }); /** OpenERP Translations */ -openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends instance.web.TranslationDataBase# */{ +openerp.TranslationDataBase = openerp.Class.extend(/** @lends instance.TranslationDataBase# */{ /** - * @constructs instance.web.TranslationDataBase - * @extends instance.web.Class + * @constructs instance.TranslationDataBase + * @extends instance.Class */ init: function() { this.db = {}; @@ -1156,7 +1155,7 @@ openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends instance.w /** Loads the translations from an OpenERP server. - @param {openerp.web.Session} session The session object to contact the server. + @param {openerp.Session} session The session object to contact the server. @param {Array} [modules] The list of modules to load the translation. If not specified, it will default to all the modules installed in the current database. @param {Object} [lang] lang The language. If not specified it will default to the language @@ -1174,9 +1173,9 @@ openerp.web.TranslationDataBase = openerp.web.Class.extend(/** @lends instance.w } }); -openerp.web._t = new openerp.web.TranslationDataBase().build_translation_function(); +openerp._t = new openerp.TranslationDataBase().build_translation_function(); -openerp.web.get_cookie = function(c_name) { +openerp.get_cookie = function(c_name) { if (document.cookie.length > 0) { var c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1) { @@ -1191,12 +1190,12 @@ openerp.web.get_cookie = function(c_name) { return ""; }; -openerp.web.qweb = new QWeb2.Engine(); +openerp.qweb = new QWeb2.Engine(); -openerp.web.qweb.default_dict = { +openerp.qweb.default_dict = { '_' : _, 'JSON': JSON, - '_t' : openerp.web._t + '_t' : openerp._t }; openerp.declare = declare; -- cgit v1.2.3