PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.1.6
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.1.6
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / assets / js / hurrytimer.js

hurrytimer.js in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.1.6, at assets/js/hurrytimer.js

407 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 'use strict';
2
3 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5 function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
6
7 function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
8
9 var HurrytimerAction =
10 /*#__PURE__*/
11 function () {
12 function HurrytimerAction(elementRef) {
13 _classCallCheck(this, HurrytimerAction);
14
15 this.elementRef = elementRef;
16 }
17 /**
18 * Hide campaign.
19 */
20
21
22 _createClass(HurrytimerAction, [{
23 key: "hide",
24 value: function hide() {
25 // We don't hide campaign if there is a message to display.
26 if (this.elementRef.find('.hurrytimer-campaign-message').length) {
27 return;
28 }
29
30 var stickyBar = this.elementRef.closest('.hurrytimer-sticky');
31
32 if (stickyBar.length) {
33 stickyBar.remove();
34 } else {
35 this.elementRef.remove();
36 }
37 }
38 /**
39 * Redirect to the given url.
40 * @param url
41 */
42
43 }, {
44 key: "displayMessage",
45
46 /**
47 * Display message by replacing campaign content with the given message.
48 * @param message
49 */
50 value: function displayMessage(message) {
51 var html = "<div class=\"hurrytimer-campaign-message\">".concat(message, "</div>");
52 this.elementRef.html(html);
53 }
54 }], [{
55 key: "redirect",
56 value: function redirect(url) {
57 window.location.href = url;
58 }
59 /**
60 * Hide "Add to cart" button.
61 * @return void
62 */
63
64 }, {
65 key: "hideAddToCartButton",
66 value: function hideAddToCartButton() {
67 var button = document.querySelector('.single_add_to_cart_button');
68
69 if (button) {
70 button.remove();
71 }
72 }
73 }]);
74
75 return HurrytimerAction;
76 }();
77 "use strict";
78
79 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
80
81 function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
82
83 function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
84
85 var HurrytimerCampaign =
86 /*#__PURE__*/
87 function () {
88 function HurrytimerCampaign(elementRef, config) {
89 _classCallCheck(this, HurrytimerCampaign);
90
91 this.config = config;
92 this.elementRef = elementRef;
93 this.actionsOptions = hurrytimer_ajax_object.actionsOptions;
94 this.restartOptions = hurrytimer_ajax_object.restartOptions;
95 }
96 /**
97 * @param endDateInMS
98 * @return void
99 */
100
101
102 _createClass(HurrytimerCampaign, [{
103 key: "setCookie",
104 value: function setCookie(endDateInMS) {
105 Cookies.set(this.config.cookieName, endDateInMS, {
106 expires: 365
107 });
108 }
109 /**
110 * Returns end date for the given duration.
111 * @return {Date}
112 */
113
114 }, {
115 key: "getEndDate",
116 value: function getEndDate() {
117 if (this.config.isRegular) {
118 return new Date(this.config.endDate);
119 }
120
121 var date = new Date(this.config.endDate);
122
123 if (!this.config.endDate || this.allowReset() || this.allowRestart()) {
124 date = this.calculateDate();
125 } // Set or refresh cookie expiry date.
126
127
128 this.setCookie(date.getTime());
129 return date;
130 }
131 /**
132 * Returns true if the campaign should reset.
133 *
134 * @return {number}
135 */
136
137 }, {
138 key: "allowReset",
139 value: function allowReset() {
140 return this.config.reset;
141 }
142 /**
143 * Returns true if the campaign will restart.
144 * @return {boolean}
145 */
146
147 }, {
148 key: "allowRestart",
149 value: function allowRestart() {
150 if (this.config.isRegular) return false;
151 return this.isExpired() && (this.allowRestartImmediately() || this.allowRestartAfterReload());
152 }
153 /**
154 * Campaign expired.
155 */
156
157 }, {
158 key: "isExpired",
159 value: function isExpired() {
160 var today = new Date();
161 return this.config.endDate < today;
162 }
163 }, {
164 key: "allowRestartAfterReload",
165 value: function allowRestartAfterReload() {
166 return this.config.restart == this.restartOptions.afterReload;
167 }
168 }, {
169 key: "allowRestartImmediately",
170 value: function allowRestartImmediately() {
171 return this.config.restart == this.restartOptions.immediately;
172 }
173 /**
174 * Returns true if the campaign has an action.
175 */
176
177 }, {
178 key: "hasAction",
179 value: function hasAction() {
180 return this.config.actions.length;
181 }
182 /**
183 * Calculate date based on the given duration.
184 * @return {Date}
185 */
186
187 }, {
188 key: "calculateDate",
189 value: function calculateDate() {
190 var date = new Date();
191 date.setSeconds(date.getSeconds() + this.config.duration);
192 return date;
193 }
194 /**
195 * Run registered actions.
196 */
197
198 }, {
199 key: "executeActions",
200 value: function executeActions() {
201 // No action, abort.
202 if (this.hasAction()) {
203 var _iteratorNormalCompletion = true;
204 var _didIteratorError = false;
205 var _iteratorError = undefined;
206
207 try {
208 for (var _iterator = this.config.actions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
209 var action = _step.value;
210 var actionManager = new HurrytimerAction(this.elementRef);
211
212 switch (action['id']) {
213 case this.actionsOptions.redirect:
214 HurrytimerAction.redirect(action['redirectUrl']);
215 break;
216
217 case this.actionsOptions.hideAddToCartButton:
218 HurrytimerAction.hideAddToCartButton();
219 break;
220
221 case this.actionsOptions.displayMessage:
222 actionManager.displayMessage(action['message']);
223 break;
224
225 case this.actionsOptions.hide:
226 actionManager.hide();
227 break;
228 }
229 }
230 } catch (err) {
231 _didIteratorError = true;
232 _iteratorError = err;
233 } finally {
234 try {
235 if (!_iteratorNormalCompletion && _iterator.return != null) {
236 _iterator.return();
237 }
238 } finally {
239 if (_didIteratorError) {
240 throw _iteratorError;
241 }
242 }
243 }
244 }
245 }
246 }, {
247 key: "maybeShowCampaign",
248 value: function maybeShowCampaign() {
249 if (this.elementRef.length) {
250 this.elementRef.removeClass('hurryt-loading');
251 }
252
253 var stickyBar = this.elementRef.closest('.hurrytimer-sticky');
254
255 if (stickyBar.length) {
256 stickyBar.removeClass('hurryt-loading');
257 }
258 }
259 /**
260 * Maybe run countdown timer.
261 */
262
263 }, {
264 key: "run",
265 value: function run() {
266 var _this = this;
267
268 this.elementRef.countdown(this.getEndDate(), function (e) {
269 return _this.onCountdownRun(e);
270 }).on('finish.countdown', function (_) {
271 return _this.onCountdownFinish();
272 });
273 var stickyBar = this.elementRef.closest('.hurrytimer-sticky');
274 this.handleStickyBar(stickyBar);
275 }
276 /**
277 * Handle sticky bar visibility.
278 * @param {*} stickyBar
279 */
280
281 }, {
282 key: "handleStickyBar",
283 value: function handleStickyBar(stickyBar) {
284 var _this2 = this;
285
286 if (stickyBar.length === 0) return;
287 var dismissCookie = Cookies.get("_ht_CDT-".concat(this.config.id, "_dismissed")); // Stick bar hasn't been dismissed.
288
289 if (dismissCookie == undefined) {
290 stickyBar.on('click', '.hurrytimer-sticky-close', function () {
291 return _this2.onStickyBarDismiss(stickyBar);
292 });
293 } else {
294 this.hideStickyBar(stickyBar);
295 }
296 }
297 /**
298 * Hide Sticky Bar
299 * @param {*} stickyBar
300 */
301
302 }, {
303 key: "hideStickyBar",
304 value: function hideStickyBar(stickyBar) {
305 if (stickyBar.length === 0) return;
306 var isTopPinned = stickyBar.css('top') === '0px';
307 stickyBar.remove();
308
309 if (isTopPinned) {
310 jQuery('body').css('margin-top', 0);
311 } else {
312 jQuery('body').css('margin-bottom', 0);
313 }
314 }
315 /**
316 * Handle sticky bar dismiss.
317 */
318
319 }, {
320 key: "onStickyBarDismiss",
321 value: function onStickyBarDismiss(stickyBar) {
322 this.hideStickyBar(stickyBar);
323 Cookies.set("_ht_CDT-".concat(this.config.id, "_dismissed"), '1', {
324 expires: +hurrytimer_ajax_object.sticky_bar_hide_timeout
325 });
326 }
327 /**
328 * Countdown timer start callback.
329 * @param event
330 */
331
332 }, {
333 key: "onCountdownRun",
334 value: function onCountdownRun(event) {
335 this.render(event);
336
337 if (event.elapsed) {
338 this.executeActions();
339 }
340
341 this.maybeShowCampaign();
342 }
343 /**
344 * Render countdown timer.
345 * @param event
346 */
347
348 }, {
349 key: "render",
350 value: function render(event) {
351 this.elementRef.find('.hurrytimer-timer').html(event.strftime(this.config.template));
352 }
353 /**
354 * Countdown timer finish callback.
355 */
356
357 }, {
358 key: "onCountdownFinish",
359 value: function onCountdownFinish() {
360 this.executeActions();
361 this.maybeShowCampaign();
362
363 if (this.allowRestartImmediately()) {
364 this.run();
365 }
366 }
367 }]);
368
369 return HurrytimerCampaign;
370 }();
371 'use strict';
372
373 (function (jQuery) {
374 // Body element.
375 var bodyElementRef = jQuery('body');
376 jQuery('.hurrytimer-campaign').each(function () {
377 // Campaign element.
378 var campaignElementRef = jQuery(this); // Handle sticky bar if present.
379
380 var stickyBarElementRef = campaignElementRef.closest('.hurrytimer-sticky'); // Display sticky bar if present.
381
382 if (stickyBarElementRef.length) {
383 bodyElementRef.append(stickyBarElementRef);
384 jQuery(window).resize(function () {
385 if (stickyBarElementRef.css('top') === '0px') {
386 // Pin at the top.
387 bodyElementRef.css('margin-top', stickyBarElementRef.outerHeight());
388 } else {
389 // Pin at the bottom.
390 bodyElementRef.css('margin-bottom', stickyBarElementRef.outerHeight());
391 }
392 });
393 setTimeout(function () {
394 jQuery(window).trigger('resize');
395 });
396 } // Campaign config.
397
398
399 var config = campaignElementRef.data('config');
400 if (config === undefined) return; // Clean up DOM.
401
402 campaignElementRef.removeAttr('data-config');
403 var campaign = new HurrytimerCampaign(campaignElementRef, config); // Start campaign.
404
405 campaign.run();
406 });
407 })(jQuery);