-
Notifications
You must be signed in to change notification settings - Fork 20
/
promos.js
302 lines (276 loc) · 8.93 KB
/
promos.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*global window, document, $, Ajax, setLocation */
var AFFIRM_AFFIRM = AFFIRM_AFFIRM || {};
(function (AFFIRM_AFFIRM) {
'use strict';
AFFIRM_AFFIRM.promos = AFFIRM_AFFIRM.promos || {};
AFFIRM_AFFIRM.promos = {
/**
* Is initialized
*
* {Boolean}
*/
isInitialized: false,
/**
* Is loaded
*
* {Boolean}
*/
isLoaded: false,
/**
* Is As Low As initialized
*
* {bool}
*/
isALAInitialized: false,
/**
* Options for configurable product
*
* {Object}
*/
configurableOptions: {},
/**
* Is conf options initialized
*
* {bool}
*/
isConfigurableOptionsInitialized: false,
/**
* isDBPromoConfigurableInitialized
*
* {bool}
*/
isDBPromoConfigurableInitialized: false,
/**
* deBounceUpdatePromoConfigurable
*
* {Object}
*/
deBounceUpdatePromoConfigurable: {},
/**
* Config
*
* {Object}
*/
config: {},
/**
* Delay
*
* @type {Number}
*/
REQUEST_CONFIGURABLE_DELAY: 1200,
/**
* Initialize
*
* @param {String} apiKey
* @param {String} affirmJsUrl
* @returns {AFFIRM_AFFIRM.promos}
*/
initialize: function(apiKey, affirmJsUrl, sessionId) {
sessionId = (typeof sessionId !== 'undefined') ? sessionId : 'null';
this.config = {
public_api_key: apiKey,
script: affirmJsUrl,
session_id: sessionId
};
this.setIsInitialized(true);
return this;
},
/**
* Load script
* @returns {AFFIRM_AFFIRM.promos}
*/
loadScript: function() {
(function(l,g,m,e,a,f,b){var d,c=l[m]||{},
h=document.createElement(f),
n=document.getElementsByTagName(f)[0],
k=function(a,b,c){
return function(){a[b]._.push([c,arguments])}
};
c[e]=k(c,e,"set");
d=c[e];c[a]={};c[a]._=[];d._=[];c[a][b]=k(c,a,b);a=0;
for(b="set add save post open empty reset on off trigger ready setProduct".split(" ");
a<b.length;a++)d[b[a]]=k(c,e,b[a]);a=0;for(b=["get","token","url","items"];
a<b.length;a++)d[b[a]]=function(){};h.async=!0;
h.src=g[f];n.parentNode.insertBefore(h,n);
delete g[f];d(g);l[m]=c})(window,this.config,
"affirm","checkout","ui","script","ready");
this.setIsLoaded(true);
return this;
},
/**
* Set loaded flag
*
* @param {Boolean} flag
* @returns {AFFIRM_AFFIRM.promos}
*/
setIsLoaded: function(flag) {
this.isLoaded = flag;
return this;
},
/**
* Get is script loaded
*
* @returns {Boolean}
*/
getIsScriptLoaded: function() {
return this.isLoaded;
},
/**
* Set initialized flag
*
* @param {Boolean} flag
* @returns {AFFIRM_AFFIRM.promos}
*/
setIsInitialized: function(flag) {
this.isInitialized = flag;
return this;
},
/**
* Get initialized flag
*
* @returns {Boolean}
*/
getIsInitialized: function() {
return this.isInitialized;
},
/**
* Set ALA initialized flag
*
* @returns {Boolean}
*/
getIsAsLowAsInitialized: function() {
return this.isALAInitialized;
},
/**
* Update promo blocks visibility
*/
updatePromoBlocksVisibility: function(visibility) {
var asLowAs = document.getElementById('learn-more');
if (asLowAs) {
asLowAs.style.visibility = visibility;
}
var promoBanners = document.getElementsByClassName('affirm-promo');
if (promoBanners[0]){
promoBanners[0].style.visibility = visibility;
}
return true;
},
/**
* Init configurable options
*
* @returns {AFFIRM_AFFIRM.promos}
*/
initConfigurableOptions: function(configurableInfo) {
this.configurableOptions = configurableInfo;
this.isConfigurableOptionsInitialized = true;
return this;
},
/**
* Get isConfigurableOptionsInitialized flag
*
* @returns {Boolean}
*/
getIsConfigurableOptionsInitialized: function() {
return this.isConfigurableOptionsInitialized;
},
/**
* Update for configurable
*
* @returns {boolean}
*/
hidePromoForConfigurableBackOrdered: function() {
var selectedSimpleOptions = {}, productId, key, productProperties, productSelected;
$$('select.super-attribute-select').each(function(item, index){
selectedSimpleOptions[item.attributeId] = item.value;
});
for (productId in this.configurableOptions) {
productSelected = true;
productProperties = this.configurableOptions[productId];
for (key in selectedSimpleOptions) {
if (productProperties[key] != selectedSimpleOptions[key]) {
productSelected = false;
}
}
if (productSelected && productProperties.backorders) {
this.updatePromoBlocksVisibility('hidden');
return true;
}
}
return false;
},
/** Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing.
*
* @param {Function} func
* @param {Number} wait
* @param {Boolean} immediate
*/
deBounce: function (func, wait, immediate) {
var timeout;
return function () {
var context = this,
args = arguments,
callNow,
later;
later = function () {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
},
/**
* Get de bounce singleton object
*
* @returns {Object}
*/
getDeBouncedPromoUpdateConfigurable: function () {
if (!this.isDBPromoConfigurableInitialized) {
this.deBounceUpdatePromoConfigurable = this.deBounce(
this.hidePromoForConfigurableBackOrdered, this.REQUEST_CONFIGURABLE_DELAY, false
).bind(this);
this.isDBPromoConfigurableInitialized = true;
}
return this.deBounceUpdatePromoConfigurable;
}
};
})(AFFIRM_AFFIRM);
/**
* Product options wrapper
*
* Override to update as low as on PDP.
*
* @returns {string}
*/
if ((typeof Product != 'undefined') && (typeof Product.OptionsPrice != 'undefined') &&
(typeof Product.OptionsPrice.prototype.reload != 'undefined')
) {
Product.OptionsPrice.prototype.reload = Product.OptionsPrice.prototype.reload.wrap(
function(reload) {
reload();
// Affirm changes region start
if (AFFIRM_AFFIRM.promos.getIsAsLowAsInitialized()) {
var deBounceUpdateAsLowAs, container = $(this.containers[0]);
deBounceUpdateAsLowAs = AFFIRM_AFFIRM.promos.getDeBouncedAsLowAs();
deBounceUpdateAsLowAs(0, true, container, this.priceFormat);
}
if (AFFIRM_AFFIRM.promos.getIsConfigurableOptionsInitialized()) {
var deBounceUpdatePromoConfigurable;
deBounceUpdatePromoConfigurable = AFFIRM_AFFIRM.promos.getDeBouncedPromoUpdateConfigurable();
if (!deBounceUpdatePromoConfigurable()) {
AFFIRM_AFFIRM.promos.updatePromoBlocksVisibility('visible');
}
}
//endregion
}
);
}