Skip to content

Commit

Permalink
Merge pull request #31 from aleevas/fixJShomeBranch
Browse files Browse the repository at this point in the history
feat(upgrade): Use the new version of libraries: once and cookie
  • Loading branch information
podarok authored Jul 24, 2023
2 parents ac66c3f + b115e66 commit ccb71af
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
11 changes: 8 additions & 3 deletions openy_home_branch/js/hb-plugins/hb-location-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Location finder extension with Home Branch logic.
*/

(function ($, Drupal, drupalSettings) {
(function ($, Drupal, drupalSettings, once) {

"use strict";

Expand All @@ -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');
Expand Down Expand Up @@ -95,4 +100,4 @@
}
});

})(jQuery, Drupal, drupalSettings);
})(jQuery, Drupal, drupalSettings, once);
12 changes: 6 additions & 6 deletions openy_home_branch/js/hb_cookies_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Cookies storage JavaScript for the Open Y Home Branch module.
*/

(function ($, Drupal, drupalSettings) {
(function(Drupal, $, drupalSettings, once, cookies) {

"use strict";

Expand Down Expand Up @@ -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;
Expand All @@ -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);
6 changes: 3 additions & 3 deletions openy_home_branch/openy_home_branch.libraries.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ccb71af

Please sign in to comment.