From b115e66c1751e88dd3d4e703361f60b922546534 Mon Sep 17 00:00:00 2001 From: Alexey Aleev Date: Mon, 24 Jul 2023 13:17:34 +0200 Subject: [PATCH] Use the new version of libraries: once and cookie --- .../js/hb-plugins/hb-location-finder.js | 11 ++++++++--- openy_home_branch/js/hb_cookies_storage.js | 12 ++++++------ openy_home_branch/openy_home_branch.libraries.yml | 6 +++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/openy_home_branch/js/hb-plugins/hb-location-finder.js b/openy_home_branch/js/hb-plugins/hb-location-finder.js index c5469e69..6f886f5a 100644 --- a/openy_home_branch/js/hb-plugins/hb-location-finder.js +++ b/openy_home_branch/js/hb-plugins/hb-location-finder.js @@ -3,7 +3,7 @@ * Location finder extension with Home Branch logic. */ -(function ($, Drupal, drupalSettings) { +(function ($, Drupal, drupalSettings, once) { "use strict"; @@ -14,7 +14,12 @@ */ Drupal.behaviors.openyHomeBranchLocationFinderSort = { attach: function (context, settings) { - $(document).once().on('hb-location-finder-sort', function (event, el) { + if (!once('openyHomeBranchLocationFinderSort', 'html').length) { + // Early return avoid changing the indentation + // for the rest of the code. + return; + } + $(document).on('hb-location-finder-sort', function (event, el) { var items = $(el).find('.views-row'); items.each(function (index) { var weight = $(this).attr('data-hb-sort'); @@ -95,4 +100,4 @@ } }); -})(jQuery, Drupal, drupalSettings); +})(jQuery, Drupal, drupalSettings, once); diff --git a/openy_home_branch/js/hb_cookies_storage.js b/openy_home_branch/js/hb_cookies_storage.js index c8faf9e9..4443f7d9 100644 --- a/openy_home_branch/js/hb_cookies_storage.js +++ b/openy_home_branch/js/hb_cookies_storage.js @@ -3,7 +3,7 @@ * Cookies storage JavaScript for the Open Y Home Branch module. */ -(function ($, Drupal, drupalSettings) { +(function(Drupal, $, drupalSettings, once, cookies) { "use strict"; @@ -80,15 +80,15 @@ // Move current data to Cookies storage. updateStorage: function () { $(document).trigger('hb-before-storage-update', this.data); - $.cookie('home_branch', JSON.stringify(this.data), { expires: 360, path: drupalSettings.path.baseUrl }); + cookies.set('home_branch', JSON.stringify(this.data), { expires: 360, path: drupalSettings.path.baseUrl }); $(document).trigger('hb-after-storage-update', this.data); }, // Load data from Cookies storage. loadFromStorage: function () { - var data = $.cookie('home_branch'); + var data = cookies.get('home_branch'); if (data !== undefined) { - return JSON.parse($.cookie('home_branch')); + return JSON.parse(cookies.get('home_branch')); } // Return default values if storage not defined. return this.data; @@ -111,11 +111,11 @@ return; } - $(context).find('body').once('home-branch-cookies-storage').each(function () { + $(once('home-branch-cookies-storage', 'body', context)).each(function () { // Init Home Branch Cookies storage on page load. Drupal.homeBranch.init(); }); } }; -})(jQuery, Drupal, drupalSettings); +})(Drupal, jQuery, drupalSettings, once, window.Cookies); diff --git a/openy_home_branch/openy_home_branch.libraries.yml b/openy_home_branch/openy_home_branch.libraries.yml index 9e3519bc..2888f3d4 100644 --- a/openy_home_branch/openy_home_branch.libraries.yml +++ b/openy_home_branch/openy_home_branch.libraries.yml @@ -1,13 +1,13 @@ cookies_storage: - version: 0.1 + version: 0.2 js: js/hb_cookies_storage.js: {} dependencies: - core/drupal - core/drupalSettings - core/jquery - - core/jquery.once - - core/jquery.cookie + - core/once + - core/js-cookie hb_plugin_base: version: 0.1