forked from carolineschnapp/recently-viewed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.products.js
189 lines (155 loc) · 5.58 KB
/
jquery.products.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*global Shopify */
/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};
/**
* Module to show Recently Viewed Products
*
* Copyright (c) 2014 Caroline Schnapp (11heavens.com)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
Shopify.Products = (function() {
var config = {
howManyToShow: 3,
howManyToStoreInMemory: 10,
wrapperId: 'recently-viewed-products',
templateId: 'recently-viewed-product-template',
onComplete: null
};
var productHandleQueue = [];
var wrapper = null;
var template = null;
var shown = 0;
var cookie = {
configuration: {
expires: 90,
path: '/',
domain: window.location.hostname
},
name: 'shopify_recently_viewed',
write: function(recentlyViewed) {
jQuery.cookie(this.name, recentlyViewed.join(' '), this.configuration);
},
read: function() {
var recentlyViewed = [];
var cookieValue = jQuery.cookie(this.name);
if (cookieValue !== null) {
recentlyViewed = cookieValue.split(' ');
}
return recentlyViewed;
},
destroy: function() {
jQuery.cookie(this.name, null, this.configuration);
},
remove: function(productHandle) {
var recentlyViewed = this.read();
var position = jQuery.inArray(productHandle, recentlyViewed);
if (position !== -1) {
recentlyViewed.splice(position, 1);
this.write(recentlyViewed);
}
}
};
var finalize = function() {
wrapper.show();
// If we have a callback.
if (config.onComplete) {
try { config.onComplete() } catch (error) { }
}
};
var moveAlong = function() {
if (productHandleQueue.length && shown < config.howManyToShow) {
jQuery.ajax({
dataType: 'json',
url: '/products/' + productHandleQueue[0] + '.js',
cache: false,
success: function(product) {
template.tmpl(product).appendTo(wrapper);
productHandleQueue.shift();
shown++;
moveAlong();
},
error: function() {
cookie.remove(productHandleQueue[0]);
productHandleQueue.shift();
moveAlong();
}
});
}
else {
finalize();
}
};
return {
showRecentlyViewed: function(params) {
params = params || {};
// Update defaults.
jQuery.extend(config, params);
// Read cookie.
productHandleQueue = cookie.read();
// Remove current page from list
var phq = productHandleQueue;
var productHandle = this.getProductHandle();
if (phq.indexOf(productHandle) > -1) {
phq.splice(phq.indexOf(productHandle), 1);
}
// Template and element where to insert.
template = jQuery('#' + config.templateId);
wrapper = jQuery('#' + config.wrapperId);
// How many products to show.
config.howManyToShow = Math.min(phq.length, config.howManyToShow);
// If we have any to show.
if (config.howManyToShow && template.length && wrapper.length) {
// Getting each product with an Ajax call and rendering it on the page.
moveAlong();
}
},
getProductHandle: function() {
return window.location.pathname.match(/\/products\/([a-z0-9\-]+)/)[1];
},
getConfig: function() {
return config;
},
clearList: function() {
cookie.destroy();
},
recordRecentlyViewed: function(params) {
params = params || {};
// Update defaults.
jQuery.extend(config, params);
// Read cookie.
var recentlyViewed = cookie.read();
// If we are on a product page.
if (window.location.pathname.indexOf('/products/') !== -1) {
// What is the product handle on this page.
var productHandle = this.getProductHandle();
// In what position is that product in memory.
var position = jQuery.inArray(productHandle, recentlyViewed);
// If not in memory.
if (position === -1) {
// Add product at the start of the list.
recentlyViewed.unshift(productHandle);
// Only keep what we need.
recentlyViewed = recentlyViewed.splice(0, config.howManyToStoreInMemory);
}
else {
// Remove the product and place it at start of list.
recentlyViewed.splice(position, 1);
recentlyViewed.unshift(productHandle);
}
// Update cookie.
cookie.write(recentlyViewed);
}
}
};
})();