From f14bbf1bdc70873ddd8915516c5c3ab6ae72a796 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Tue, 20 Jul 2021 13:18:51 +0000 Subject: [PATCH] [REF] web, *: move cookie utils out of legacy core utils *: im_livechat Cookie utils will be required on the frontend side but before the lazy loading. This commit splits them out of default utils to achieve that. Note: kept as "legacy" utils anyway, we'll handle that when the time is right... Part of https://github.com/odoo/odoo/pull/67140 task-2381049 --- addons/im_livechat/__manifest__.py | 1 + addons/web/__manifest__.py | 2 + .../static/src/legacy/js/core/cookie_utils.js | 41 +++++++++++++++++++ addons/web/static/src/legacy/js/core/utils.js | 39 ++---------------- 4 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 addons/web/static/src/legacy/js/core/cookie_utils.js diff --git a/addons/im_livechat/__manifest__.py b/addons/im_livechat/__manifest__.py index 764b05db1f3..8e1e40cc515 100644 --- a/addons/im_livechat/__manifest__.py +++ b/addons/im_livechat/__manifest__.py @@ -143,6 +143,7 @@ Help your customers with this chat, and analyse their feedback. 'web/static/src/session.js', 'web/static/src/legacy/js/core/session.js', 'web/static/src/legacy/js/core/concurrency.js', + 'web/static/src/legacy/js/core/cookie_utils.js', 'web/static/src/legacy/js/core/utils.js', 'web/static/src/legacy/js/core/dom.js', 'web/static/src/legacy/js/core/qweb.js', diff --git a/addons/web/__manifest__.py b/addons/web/__manifest__.py index 2ea2ea74fbe..c23f3ba1947 100644 --- a/addons/web/__manifest__.py +++ b/addons/web/__manifest__.py @@ -58,6 +58,7 @@ This module provides the core of the Odoo Web Client. 'web/static/src/legacy/js/promise_extension.js', 'web/static/src/boot.js', 'web/static/src/session.js', + 'web/static/src/legacy/js/core/cookie_utils.js', ], 'web.assets_common': [ ('include', 'web._assets_helpers'), @@ -75,6 +76,7 @@ This module provides the core of the Odoo Web Client. ('remove', 'web/static/src/legacy/js/promise_extension.js'), ('remove', 'web/static/src/boot.js'), ('remove', 'web/static/src/session.js'), + ('remove', 'web/static/src/legacy/js/core/cookie_utils.js'), ], 'web.assets_backend': [ ('include', 'web._assets_helpers'), diff --git a/addons/web/static/src/legacy/js/core/cookie_utils.js b/addons/web/static/src/legacy/js/core/cookie_utils.js new file mode 100644 index 00000000000..2aeca9bd370 --- /dev/null +++ b/addons/web/static/src/legacy/js/core/cookie_utils.js @@ -0,0 +1,41 @@ +odoo.define('web.utils.cookies', function (require) { +"use strict"; + +return { + /** + * Reads the cookie described by the given name. + * + * @param {string} cookieName + * @returns {string} + */ + get_cookie(cookieName) { + var cookies = document.cookie ? document.cookie.split('; ') : []; + for (var i = 0, l = cookies.length; i < l; i++) { + var parts = cookies[i].split('='); + var name = parts.shift(); + var cookie = parts.join('='); + + if (cookieName && cookieName === name) { + return cookie; + } + } + return ""; + }, + /** + * Creates a cookie. + * + * @param {string} name the name of the cookie + * @param {string} value the value stored in the cookie + * @param {integer} ttl time to live of the cookie in millis. -1 to erase the cookie. + */ + set_cookie(name, value, ttl) { + ttl = ttl || 24 * 60 * 60 * 365; + document.cookie = [ + `${name}=${value}`, + 'path=/', + `max-age=${ttl}`, + `expires=${new Date(new Date().getTime() + ttl * 1000).toGMTString()}` + ].join(';'); + }, +}; +}); diff --git a/addons/web/static/src/legacy/js/core/utils.js b/addons/web/static/src/legacy/js/core/utils.js index 95ad897c9a1..ab2806b30b4 100644 --- a/addons/web/static/src/legacy/js/core/utils.js +++ b/addons/web/static/src/legacy/js/core/utils.js @@ -8,6 +8,7 @@ odoo.define('web.utils', function (require) { */ var translation = require('web.translation'); +var cookieUtils = require('web.utils.cookies'); var _t = translation._t; var id = -1; @@ -311,7 +312,7 @@ function Markup(v, ...exprs) { return new _Markup(s); } -var utils = { +var utils = Object.assign({ AlreadyDefinedPatchError, UnknownPatchError, Markup, @@ -390,25 +391,6 @@ var utils = { generateID: function () { return ++id; }, - /** - * Read the cookie described by c_name - * - * @param {string} c_name - * @returns {string} - */ - get_cookie: function (c_name) { - var cookies = document.cookie ? document.cookie.split('; ') : []; - for (var i = 0, l = cookies.length; i < l; i++) { - var parts = cookies[i].split('='); - var name = parts.shift(); - var cookie = parts.join('='); - - if (c_name && c_name === name) { - return cookie; - } - } - return ""; - }, /** * Gets dataURL (base64 data) from the given file or blob. * Technically wraps FileReader.readAsDataURL in Promise. @@ -849,21 +831,6 @@ var utils = { str = "" + str; return str + new Array(size - str.length + 1).join('0'); }, - /** - * Create a cookie - * @param {String} name the name of the cookie - * @param {String} value the value stored in the cookie - * @param {Integer} ttl time to live of the cookie in millis. -1 to erase the cookie. - */ - set_cookie: function (name, value, ttl) { - ttl = ttl || 24*60*60*365; - document.cookie = [ - name + '=' + value, - 'path=/', - 'max-age=' + ttl, - 'expires=' + new Date(new Date().getTime() + ttl*1000).toGMTString() - ].join(';'); - }, /** * Return a shallow copy of a given array sorted by a given criterion or a default one. * The given criterion can either be: @@ -1174,7 +1141,7 @@ var utils = { ['name', '=like', '%.report_assets\_%.css'], ]; }, -}; +}, cookieUtils); return utils;