PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.0.0
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.0.0
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.0.0, at assets/js/hurrytimer.js

303 lines 8.2 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 // Priority to message action.
26 if (this.elementRef.find('.hurrytimer-campaign-message').length === 0) {
27 this.elementRef.html('');
28 }
29 }
30 /**
31 * Redirect to the given url.
32 * @param url
33 */
34
35 }, {
36 key: "displayMessage",
37
38 /**
39 * Display message by replacing campaign content with the given message.
40 * @param message
41 */
42 value: function displayMessage(message) {
43 var html = "<div class=\"hurrytimer-campaign-message\">".concat(message, "</div>");
44 this.elementRef.html(html);
45 }
46 }], [{
47 key: "redirect",
48 value: function redirect(url) {
49 window.location.href = url;
50 }
51 /**
52 * Hide "Add to cart" button.
53 * @return void
54 */
55
56 }, {
57 key: "hideAddToCartButton",
58 value: function hideAddToCartButton() {
59 var button = document.querySelector('.single_add_to_cart_button');
60
61 if (button.length) {
62 button.remove();
63 }
64 }
65 }]);
66
67 return HurrytimerAction;
68 }();
69 "use strict";
70
71 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
72
73 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); } }
74
75 function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
76
77 var HurrytimerCampaign =
78 /*#__PURE__*/
79 function () {
80 function HurrytimerCampaign(elementRef, config) {
81 _classCallCheck(this, HurrytimerCampaign);
82
83 this.config = config;
84 this.elementRef = elementRef;
85 this.actionsOptions = hurrytimer_ajax_object.actionsOptions;
86 this.restartOptions = hurrytimer_ajax_object.restartOptions;
87 }
88 /**
89 * @param endDateInMS
90 * @return void
91 */
92
93
94 _createClass(HurrytimerCampaign, [{
95 key: "setCookie",
96 value: function setCookie(endDateInMS) {
97 var expiryDate = new Date();
98 expiryDate.setMonth(expiryDate.getMonth() + 12);
99 document.cookie = "".concat(this.config.cookieName, "=").concat(endDateInMS, ";expires=").concat(expiryDate.toUTCString(), ";path=/");
100 }
101 /**
102 * Returns end date for the given duration.
103 * @return {Date}
104 */
105
106 }, {
107 key: "getEndDate",
108 value: function getEndDate() {
109 if (this.config.isRegular) {
110 return new Date(this.config.endDate);
111 }
112
113 if (!this.config.endDate || this.allowReset() || this.allowRestart()) {
114 var date = this.calculateDate();
115 this.setCookie(date.getTime());
116 return date;
117 }
118
119 return new Date(+this.config.endDate);
120 }
121 /**
122 * Returns true if the campaign should reset.
123 *
124 * @return {number}
125 */
126
127 }, {
128 key: "allowReset",
129 value: function allowReset() {
130 return this.config.reset;
131 }
132 /**
133 * Returns true if the campaign will restart.
134 * @return {boolean}
135 */
136
137 }, {
138 key: "allowRestart",
139 value: function allowRestart() {
140 if (this.config.isRegular) return false;
141 return this.isExpired() && (this.allowRestartImmediately() || this.allowRestartAfterReload());
142 }
143 /**
144 * Campaign expired.
145 */
146
147 }, {
148 key: "isExpired",
149 value: function isExpired() {
150 var today = new Date();
151 return this.config.endDate < today;
152 }
153 }, {
154 key: "allowRestartAfterReload",
155 value: function allowRestartAfterReload() {
156 return +this.config.restart === this.restartOptions.afterReload;
157 }
158 }, {
159 key: "allowRestartImmediately",
160 value: function allowRestartImmediately() {
161 return +this.config.restart === this.restartOptions.immediately;
162 }
163 /**
164 * Returns true if the campaign has an action.
165 */
166
167 }, {
168 key: "hasAction",
169 value: function hasAction() {
170 return this.config.actions.length;
171 }
172 /**
173 * Calculate date based on the given duration.
174 * @return {Date}
175 */
176
177 }, {
178 key: "calculateDate",
179 value: function calculateDate() {
180 var date = new Date();
181 date.setSeconds(date.getSeconds() + this.config.duration);
182 return date;
183 }
184 /**
185 * Run registered actions.
186 */
187
188 }, {
189 key: "executeActions",
190 value: function executeActions() {
191 // No action, abort.
192 if (!this.hasAction()) return;
193 var _iteratorNormalCompletion = true;
194 var _didIteratorError = false;
195 var _iteratorError = undefined;
196
197 try {
198 for (var _iterator = this.config.actions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
199 var action = _step.value;
200 var actionRunner = new HurrytimerAction(this.elementRef);
201
202 switch (action['id']) {
203 case this.actionsOptions.redirect:
204 HurrytimerAction.redirect(action['redirectUrl']);
205 break;
206
207 case this.actionsOptions.hideAddToCartButton:
208 HurrytimerAction.hideAddToCartButton();
209 break;
210
211 case this.actionsOptions.displayMessage:
212 actionRunner.displayMessage(action['message']);
213 break;
214
215 case this.actionsOptions.hide:
216 actionRunner.hide();
217 break;
218 }
219 }
220 } catch (err) {
221 _didIteratorError = true;
222 _iteratorError = err;
223 } finally {
224 try {
225 if (!_iteratorNormalCompletion && _iterator.return != null) {
226 _iterator.return();
227 }
228 } finally {
229 if (_didIteratorError) {
230 throw _iteratorError;
231 }
232 }
233 }
234 }
235 /**
236 * Maybe run countdown timer.
237 */
238
239 }, {
240 key: "run",
241 value: function run() {
242 var _this = this;
243
244 this.elementRef.countdown(this.getEndDate(), function (e) {
245 return _this.onCountdownRun(e);
246 }).on('finish.countdown', this.onCountdownFinish);
247 }
248 /**
249 * Countdown timer start callback.
250 * @param event
251 */
252
253 }, {
254 key: "onCountdownRun",
255 value: function onCountdownRun(event) {
256 this.render(event);
257
258 if (event.elapsed) {
259 this.executeActions();
260 }
261 }
262 /**
263 * Render countdown timer.
264 * @param event
265 */
266
267 }, {
268 key: "render",
269 value: function render(event) {
270 this.elementRef.find('.hurrytimer-timer').html(event.strftime(this.config.template));
271 }
272 /**
273 * Countdown timer finish callback.
274 */
275
276 }, {
277 key: "onCountdownFinish",
278 value: function onCountdownFinish() {
279 this.executeActions();
280
281 if (this.allowRestartImmediately()) {
282 this.runCountdown();
283 }
284 }
285 }]);
286
287 return HurrytimerCampaign;
288 }();
289 "use strict";
290
291 (function (jQuery) {
292 jQuery('.hurrytimer-campaign').each(function () {
293 var campaignElementRef = jQuery(this);
294 var config = campaignElementRef.data('config');
295 if (config === undefined) return;
296 console.log({
297 config: config
298 });
299 campaignElementRef.removeAttr('data-config');
300 var campaign = new HurrytimerCampaign(campaignElementRef, config);
301 campaign.run();
302 });
303 })(jQuery);