| 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', function (e) { |
| 247 |
return _this.onCountdownFinish(e); |
| 248 |
}); |
| 249 |
} |
| 250 |
/** |
| 251 |
* Countdown timer start callback. |
| 252 |
* @param event |
| 253 |
*/ |
| 254 |
|
| 255 |
}, { |
| 256 |
key: "onCountdownRun", |
| 257 |
value: function onCountdownRun(event) { |
| 258 |
this.render(event); |
| 259 |
|
| 260 |
if (event.elapsed) { |
| 261 |
this.executeActions(); |
| 262 |
} |
| 263 |
} |
| 264 |
/** |
| 265 |
* Render countdown timer. |
| 266 |
* @param event |
| 267 |
*/ |
| 268 |
|
| 269 |
}, { |
| 270 |
key: "render", |
| 271 |
value: function render(event) { |
| 272 |
this.elementRef.find('.hurrytimer-timer').html(event.strftime(this.config.template)); |
| 273 |
} |
| 274 |
/** |
| 275 |
* Countdown timer finish callback. |
| 276 |
*/ |
| 277 |
|
| 278 |
}, { |
| 279 |
key: "onCountdownFinish", |
| 280 |
value: function onCountdownFinish() { |
| 281 |
this.executeActions(); |
| 282 |
|
| 283 |
if (this.allowRestartImmediately()) { |
| 284 |
this.runCountdown(); |
| 285 |
} |
| 286 |
} |
| 287 |
}]); |
| 288 |
|
| 289 |
return HurrytimerCampaign; |
| 290 |
}(); |
| 291 |
"use strict"; |
| 292 |
|
| 293 |
(function (jQuery) { |
| 294 |
jQuery('.hurrytimer-campaign').each(function () { |
| 295 |
var campaignElementRef = jQuery(this); |
| 296 |
var config = campaignElementRef.data('config'); |
| 297 |
if (config === undefined) return; |
| 298 |
console.log({ |
| 299 |
config: config |
| 300 |
}); |
| 301 |
campaignElementRef.removeAttr('data-config'); |
| 302 |
var campaign = new HurrytimerCampaign(campaignElementRef, config); |
| 303 |
campaign.run(); |
| 304 |
}); |
| 305 |
})(jQuery); |