From 800223ae7c5891bf700d45eb6d5f541093dc273c Mon Sep 17 00:00:00 2001 From: "Pulinckx Pierre (PIPU)" Date: Tue, 18 Apr 2023 10:49:17 +0000 Subject: [PATCH] [REF] *: Replace underscore _.each() by native JS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced _.each() functions (average 235 occurences) Description of the refactoring this PR addresses: Current behavior before PR: There are underscore.js function enumerated above used in odoo. Desired behavior after PR is merged: These functions has been replaced by native javascript prototypes/methods/functions. TaskId : 3246238 closes odoo/odoo#118565 Signed-off-by: Georis François (fge) --- addons/mail/static/tests/mail_utils_tests.js | 9 +- .../static/src/js/mass_mailing_html_field.js | 2 +- addons/point_of_sale/static/src/js/models.js | 4 +- addons/portal/static/src/js/portal_chatter.js | 2 +- .../portal/static/src/js/portal_composer.js | 2 +- addons/portal/static/src/js/portal_sidebar.js | 4 +- .../static/src/js/portal_chatter.js | 14 +- .../static/src/js/purchase_portal_sidebar.js | 100 ++++++++------- .../sale/static/src/js/sale_portal_sidebar.js | 4 +- addons/sale/static/src/js/variant_mixin.js | 15 ++- .../src/js/product_configurator_modal.js | 4 +- .../static/src/js/sale_product_field.js | 2 +- .../frontend/keyboard_navigation_mixin.js | 8 +- addons/web/static/src/legacy/js/core/ajax.js | 4 +- addons/web/static/src/legacy/js/core/class.js | 4 +- .../web/static/src/legacy/js/core/context.js | 4 +- .../web/static/src/legacy/js/core/domain.js | 4 +- addons/web/static/src/legacy/js/core/misc.js | 6 +- .../web/static/src/legacy/js/core/mixins.js | 24 ++-- .../web/static/src/legacy/js/core/py_utils.js | 2 +- .../static/src/legacy/js/core/translation.js | 4 +- addons/web/static/src/legacy/js/core/utils.js | 6 +- .../src/legacy/js/fields/relational_fields.js | 6 +- .../src/legacy/js/public/public_root.js | 4 +- .../src/legacy/js/public/public_widget.js | 2 +- .../web/static/src/legacy/js/services/core.js | 4 +- .../src/legacy/js/views/basic/basic_model.js | 120 +++++++++--------- .../views/standalone_field_manager_mixin.js | 4 +- .../src/legacy/js/widgets/colorpicker.js | 4 +- .../src/legacy/js/widgets/domain_selector.js | 22 ++-- .../static/src/legacy/legacy_load_views.js | 6 +- .../src/views/basic_relational_model.js | 4 +- .../web/static/tests/helpers/mock_server.js | 4 +- .../tests/legacy/helpers/mock_server.js | 52 ++++---- .../tests/legacy/helpers/test_utils_mock.js | 14 +- addons/web_editor/static/src/js/common/ace.js | 82 ++++++------ .../static/src/js/editor/snippets.editor.js | 4 +- .../static/src/js/editor/snippets.options.js | 18 +-- .../web_editor/static/src/js/wysiwyg/fonts.js | 2 +- .../src/js/wysiwyg/widgets/color_palette.js | 8 +- addons/web_editor/static/tests/test_utils.js | 2 +- .../src/components/translator/translator.js | 8 +- .../static/src/js/backend/dashboard.js | 4 +- .../src/js/content/snippets.animation.js | 26 ++-- .../static/src/js/editor/snippets.options.js | 4 +- .../src/snippets/s_facebook_page/options.js | 4 +- .../src/snippets/s_image_gallery/options.js | 12 +- .../src/snippets/s_product_catalog/options.js | 4 +- .../src/snippets/s_table_of_content/000.js | 2 +- .../snippets/s_table_of_content/options.js | 2 +- .../static/src/snippets/s_website_form/000.js | 2 +- .../src/snippets/s_website_form/options.js | 8 +- .../website_event_track_proposal_form_tags.js | 2 +- .../static/src/js/website_forum.js | 14 +- .../static/src/js/website_links.js | 2 +- .../static/src/snippets/s_donation/000.js | 2 +- .../static/src/snippets/s_donation/options.js | 6 +- .../static/src/js/website_sale.editor.js | 9 +- .../static/src/js/website_sale.js | 18 +-- .../static/src/js/website_sale_delivery.js | 2 +- .../static/src/js/website_sale_comparison.js | 4 +- addons/website_slides/static/src/js/slides.js | 2 +- .../static/src/js/slides_course_tag_add.js | 2 +- .../static/src/js/slides_upload.js | 4 +- .../src/js/website.twitter.animation.js | 8 +- 65 files changed, 389 insertions(+), 352 deletions(-) diff --git a/addons/mail/static/tests/mail_utils_tests.js b/addons/mail/static/tests/mail_utils_tests.js index 42e1437ee3a..059abd627ea 100644 --- a/addons/mail/static/tests/mail_utils_tests.js +++ b/addons/mail/static/tests/mail_utils_tests.js @@ -18,7 +18,7 @@ QUnit.test("add_link utility function", function (assert) { "https://tenor.com/view/chỗgiặt-dog-smile-gif-13860250": true, "http://www.boîtenoire.be": true, }; - _.each(testInputs, function (willLinkify, content) { + for (const [content, willLinkify] of Object.entries(testInputs)) { const output = utils.parseAndTransform(content, utils.addLink); if (willLinkify) { assert.strictEqual(output.indexOf(" { if ($layout.hasClass(themeParams.className)) { selectedTheme = themeParams; } diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index 4dadd4ae251..1bd84a9026d 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -1024,7 +1024,7 @@ export class PosGlobalState extends PosModel { return orm .call("pos.order", "create_from_ui", [orders, options.draft || false]) .then(function (server_ids) { - _.each(order_ids_to_sync, function (order_id) { + order_ids_to_sync.forEach((order_id) => { self.db.remove_order(order_id); }); self.failed = false; @@ -3160,7 +3160,7 @@ export class Order extends PosModel { this.pricelist = pricelist; var lines_to_recompute = this.get_orderlines().filter((line) => !line.price_manually_set); - _.each(lines_to_recompute, function (line) { + lines_to_recompute.forEach((line) => { line.set_unit_price( line.product.get_price(self.pricelist, line.get_quantity(), line.get_price_extra()) ); diff --git a/addons/portal/static/src/js/portal_chatter.js b/addons/portal/static/src/js/portal_chatter.js index 2d20150a626..8c92bf8830b 100644 --- a/addons/portal/static/src/js/portal_chatter.js +++ b/addons/portal/static/src/js/portal_chatter.js @@ -102,7 +102,7 @@ var PortalChatter = publicWidget.Widget.extend({ * @returns {Array} */ preprocessMessages(messages) { - _.each(messages, function (m) { + messages.forEach((m) => { m['author_avatar_url'] = sprintf('/web/image/%s/%s/author_avatar/50x50', 'mail.message', m.id); m['published_date_str'] = sprintf(_t('Published on %s'), moment(time.str_to_datetime(m.date)).format('MMMM Do YYYY, h:mm:ss a')); m['body'] = Markup(m.body); diff --git a/addons/portal/static/src/js/portal_composer.js b/addons/portal/static/src/js/portal_composer.js index 6e91d5e2084..70276392bb5 100644 --- a/addons/portal/static/src/js/portal_composer.js +++ b/addons/portal/static/src/js/portal_composer.js @@ -52,7 +52,7 @@ var PortalComposer = publicWidget.Widget.extend({ return this._super.apply(this, arguments).then(function () { if (self.options.default_attachment_ids) { self.attachments = self.options.default_attachment_ids || []; - _.each(self.attachments, function(attachment) { + self.attachments.forEach((attachment) => { attachment.state = 'done'; }); self._updateAttachments(); diff --git a/addons/portal/static/src/js/portal_sidebar.js b/addons/portal/static/src/js/portal_sidebar.js index f9f90ad7669..72f0d0fad4e 100644 --- a/addons/portal/static/src/js/portal_sidebar.js +++ b/addons/portal/static/src/js/portal_sidebar.js @@ -28,8 +28,8 @@ var PortalSidebar = publicWidget.Widget.extend({ * @private */ _setDelayLabel: function () { - var $sidebarTimeago = this.$el.find('.o_portal_sidebar_timeago'); - _.each($sidebarTimeago, function (el) { + var $sidebarTimeago = this.$el.find('.o_portal_sidebar_timeago').toArray(); + $sidebarTimeago.forEach((el) => { var dateTime = moment(time.auto_str_to_date($(el).attr('datetime'))), today = moment().startOf('day'), diff = dateTime.diff(today, 'days', true), diff --git a/addons/portal_rating/static/src/js/portal_chatter.js b/addons/portal_rating/static/src/js/portal_chatter.js index e2fb17b535c..801d0b0b1fa 100644 --- a/addons/portal_rating/static/src/js/portal_chatter.js +++ b/addons/portal_rating/static/src/js/portal_chatter.js @@ -65,7 +65,7 @@ PortalChatter.include({ var self = this; messages = this._super.apply(this, arguments); if (this.options['display_rating']) { - _.each(messages, function (m, i) { + messages.forEach((m, i) => { m.rating_value = self.roundToHalf(m['rating_value']); m.rating = self._preprocessCommentData(m.rating, i); }); @@ -136,12 +136,14 @@ PortalChatter.include({ 'avg': Math.round(result['rating_stats']['avg'] * 100) / 100, 'percent': [], }; - _.each(_.sortBy(Object.keys(result['rating_stats']['percent'])).reverse(), function (rating) { - ratingData['percent'].push({ - 'num': self.roundToHalf(rating), - 'percent': utils.round_precision(result['rating_stats']['percent'][rating], 0.01), + _.sortBy(Object.keys(result["rating_stats"]["percent"])) + .reverse() + .forEach((rating) => { + ratingData["percent"].push({ + num: self.roundToHalf(rating), + percent: utils.round_precision(result["rating_stats"]["percent"][rating], 0.01), + }); }); - }); this.set('rating_card_values', ratingData); }, /** diff --git a/addons/purchase/static/src/js/purchase_portal_sidebar.js b/addons/purchase/static/src/js/purchase_portal_sidebar.js index e924b5679f6..03f15ab5eeb 100644 --- a/addons/purchase/static/src/js/purchase_portal_sidebar.js +++ b/addons/purchase/static/src/js/purchase_portal_sidebar.js @@ -59,35 +59,19 @@ publicWidget.registry.PurchasePortalSidebar = PortalSidebar.extend({ "id", "" ); - _.each(this.spyWatched.find("#quote_content h2, #quote_content h3"), function (el) { - var id, text; - switch (el.tagName.toLowerCase()) { - case "h2": - id = self._setElementId("quote_header_", el); - text = self._extractText($(el)); - if (!text) { - break; - } - lastLI = $("