1
1
var LOCAL_STORAGE_WISHLIST_KEY = 'shopify-wishlist' ;
2
2
var LOCAL_STORAGE_DELIMITER = ',' ;
3
3
var BUTTON_ACTIVE_CLASS = 'active' ;
4
+ var GRID_LOADED_CLASS = 'loaded' ;
4
5
5
6
var selectors = {
6
7
button : '[button-wishlist]' ,
7
8
grid : '[grid-wishlist]' ,
8
9
} ;
9
10
10
11
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
11
- var buttons = document . querySelectorAll ( selectors . button ) || [ ] ;
12
- if ( buttons . length ) setupButtons ( buttons ) ;
12
+ initButtons ( ) ;
13
+ initGrid ( ) ;
14
+ } ) ;
13
15
14
- var grid = document . querySelector ( selectors . grid ) || false ;
15
- if ( grid ) setupGrid ( grid ) ;
16
+ document . addEventListener ( 'shopify-wishlist:updated' , function ( event ) {
17
+ console . log ( '[Shopify Wishlist] Wishlist Updated ✅' , event . detail . wishlist ) ;
18
+ initGrid ( ) ;
19
+ } ) ;
20
+
21
+ document . addEventListener ( 'shopify-wishlist:init-product-grid' , function ( event ) {
22
+ console . log ( '[Shopify Wishlist] Wishlist Product List Loaded ✅' , event . detail . wishlist ) ;
23
+ } ) ;
24
+
25
+ document . addEventListener ( 'shopify-wishlist:init-buttons' , function ( event ) {
26
+ console . log ( '[Shopify Wishlist] Wishlist Buttons Loaded ✅' , event . detail . wishlist ) ;
16
27
} ) ;
17
28
18
29
var setupGrid = function ( grid ) {
@@ -26,15 +37,20 @@ var setupGrid = function (grid) {
26
37
Promise . all ( requests ) . then ( function ( responses ) {
27
38
var wishlistProductCards = responses . join ( '' ) ;
28
39
grid . innerHTML = wishlistProductCards ;
29
- var buttons = grid . querySelectorAll ( selectors . button ) || [ ] ;
30
- if ( buttons . length ) setupButtons ( buttons ) ;
40
+ grid . classList . add ( GRID_LOADED_CLASS ) ;
41
+ initButtons ( ) ;
42
+
43
+ var event = new CustomEvent ( 'shopify-wishlist:init-product-grid' , {
44
+ detail : { wishlist : wishlist }
45
+ } ) ;
46
+ document . dispatchEvent ( event ) ;
31
47
} ) ;
32
48
} ;
33
49
34
50
var setupButtons = function ( buttons ) {
35
51
buttons . forEach ( function ( button ) {
36
52
var productHandle = button . dataset . productHandle || false ;
37
- if ( ! productHandle ) return console . error ( '[Wishlist] Missing `data-product-handle` attribute. Failed to update the wishlist.' ) ;
53
+ if ( ! productHandle ) return console . error ( '[Shopify Wishlist] Missing `data-product-handle` attribute. Failed to update the wishlist.' ) ;
38
54
if ( wishlistContains ( productHandle ) ) button . classList . add ( BUTTON_ACTIVE_CLASS ) ;
39
55
button . addEventListener ( 'click' , function ( ) {
40
56
updateWishlist ( productHandle ) ;
@@ -43,6 +59,21 @@ var setupButtons = function (buttons) {
43
59
} ) ;
44
60
} ;
45
61
62
+ var initGrid = function ( ) {
63
+ var grid = document . querySelector ( selectors . grid ) || false ;
64
+ if ( grid ) setupGrid ( grid ) ;
65
+ } ;
66
+
67
+ var initButtons = function ( ) {
68
+ var buttons = document . querySelectorAll ( selectors . button ) || [ ] ;
69
+ if ( buttons . length ) setupButtons ( buttons ) ;
70
+ else return ;
71
+ var event = new CustomEvent ( 'shopify-wishlist:init-buttons' , {
72
+ detail : { wishlist : getWishlist ( ) }
73
+ } ) ;
74
+ document . dispatchEvent ( event ) ;
75
+ } ;
76
+
46
77
var getWishlist = function ( ) {
47
78
var wishlist = localStorage . getItem ( LOCAL_STORAGE_WISHLIST_KEY ) || false ;
48
79
if ( wishlist ) return wishlist . split ( LOCAL_STORAGE_DELIMITER ) ;
@@ -53,6 +84,12 @@ var setWishlist = function (array) {
53
84
var wishlist = array . join ( LOCAL_STORAGE_DELIMITER ) ;
54
85
if ( array . length ) localStorage . setItem ( LOCAL_STORAGE_WISHLIST_KEY , wishlist ) ;
55
86
else localStorage . removeItem ( LOCAL_STORAGE_WISHLIST_KEY ) ;
87
+
88
+ var event = new CustomEvent ( 'shopify-wishlist:updated' , {
89
+ detail : { wishlist : array }
90
+ } ) ;
91
+ document . dispatchEvent ( event ) ;
92
+
56
93
return wishlist ;
57
94
} ;
58
95
0 commit comments