Advertisement
Paymh

Untitled

Oct 30th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.68 KB | None | 0 0
  1. (function($){
  2. $(function($){
  3. var $claim_treat_container = $('.claim-treat-container'),
  4. PRELOADS = {},
  5. FINGERPRINT = false,
  6. CONFIG = TWE.get('trick_or_treat_config', {}),
  7. CHANCE_OPTIONS = TWE.get('trick_or_treat_chances', {}),
  8. $trick_audio;
  9.  
  10. // Setup individual chances
  11. if (!$.isEmptyObject(CHANCE_OPTIONS)) {
  12. // We have chances on the page, so start preloading audio file, the tricks file and a fingerprint now to save time when we need it.
  13. chances_initialization();
  14. }
  15.  
  16. // Setup claim form page
  17. if (1 === $claim_treat_container.length) {
  18. claim_treat_initialization();
  19. }
  20.  
  21. function preload_script(id, url, check, inTWE, maxAttempts, checkDelay)
  22. {
  23. var attempts,
  24. loading = PRELOADS.hasOwnProperty(id); // Are we already loading it?
  25.  
  26. // Defaults
  27. if (!(maxAttempts > 0)) {
  28. maxAttempts = 30;
  29. }
  30. if (!(checkDelay > 0)) {
  31. checkDelay = 100;
  32. }
  33.  
  34. if (!loading) {
  35. PRELOADS[id] = $.Deferred();
  36. attempts = 0;
  37. $.ajax({
  38. url: url,
  39. dataType: 'script',
  40. cache:true
  41. }).done(check_loaded).fail(PRELOADS[id].reject);
  42. }
  43.  
  44. function check_loaded()
  45. {
  46. var loaded, result;
  47. if (true === inTWE) {
  48. result = TWE.get(check, false);
  49. loaded = (false !== result);
  50. } else {
  51. loaded = (check in window);
  52. result = (loaded) ? window[check] : false;
  53. }
  54.  
  55. if (loaded) {
  56. PRELOADS[id].resolve(result);
  57. } else if (++attempts > maxAttempts) {
  58. PRELOADS[id].reject(result);
  59. } else {
  60. window.setTimeout(check_loaded, checkDelay);
  61. }
  62. }
  63.  
  64. return PRELOADS[id];
  65. }
  66.  
  67. function get_fingerprint()
  68. {
  69. return preload_script('FINGERPRINT_LIB', TWE.get('themeurl', '/wp-content/themes/theweekendedition-v2') + '/js/lib/fingerprint2.min.js', 'Fingerprint2');
  70. }
  71.  
  72. function get_tricks()
  73. {
  74. return preload_script('TRICK', TWE.get('themeurl', '/wp-content/themes/theweekendedition-v2') + '/trick-or-treat/trick.min.js', 'get_trick', true);
  75. }
  76.  
  77. function get_trick_defs()
  78. {
  79. return preload_script('TRICK_DEFS', (TWE.get('is_mobile') ? TWE.get('mobilethemeurl', '/wp-content/themes/twemobile-v2timber') : TWE.get('themeurl', '/wp-content/themes/theweekendedition-v2')) + '/trick-or-treat/tricks.min.js', 'TRICKS', true);
  80. }
  81.  
  82. function get_chance_options(id)
  83. {
  84. return $.extend({}, {
  85. init_chance: halloween_chance_init,
  86. init_claim: $.noop,
  87. name: 'Prize',
  88. copy: 'You\'ve won a prize, thanks to The Weekend Edition and our valued partners'
  89. }, (id > 0 && CHANCE_OPTIONS.hasOwnProperty(id)) ? CHANCE_OPTIONS[id] : {});
  90. }
  91.  
  92. //call the function on line85 to load in the halloween class images
  93. function halloween_chance_init($chance)
  94. {
  95. var classes = ['halloween'];
  96. $chance.addClass(classes[0]);
  97. }
  98.  
  99. //call the function on line85 to load in the paniyiri class images
  100. function paniyiri_chance_init($chance)
  101. {
  102. var classes = ['baklava', 'gyro', 'olives'];
  103. $chance.addClass(classes[1]);
  104. }
  105.  
  106. function replace_variables(content, variables)
  107. {
  108. var result = content;
  109. $.each(variables, function(name, data) {
  110. result = result.replace(new RegExp('%' + name + '%', 'g'), data);
  111. });
  112.  
  113. return result;
  114. }
  115.  
  116. function chances_initialization()
  117. {
  118. if (1 == CONFIG.trick_sound) {
  119. $trick_audio = $('<audio></audio>').attr({
  120. 'src' : TWE.get('themeurl', '/wp-content/themes/theweekendedition-v2') + '/trick-or-treat/trick-sound.mp3',
  121. 'preload' : 'auto'
  122. }).appendTo('body');
  123. }
  124. get_tricks();
  125. get_trick_defs();
  126. get_fingerprint();
  127. $.each(CHANCE_OPTIONS, chance_request)
  128. }
  129.  
  130. function chance_request(id, data)
  131. {
  132. if (data.hasOwnProperty('nonce')) {
  133. $.getJSON(TWE.get('ajaxurl','/wp-admin/admin-ajax.php'), {action: 'get_chance', id: id, get_nonce: data.nonce}, function(data, status, xhr) {
  134. var $content = $(TWE.get('is_mobile', false) ? '.article-content' : '.content-area'),
  135. $paragraphs, $chance;
  136. if (!data.error && data.html && ($content.length === 1)) {
  137. $paragraphs = $content.find('p');
  138. $chance = $(data.html).insertAfter(1);
  139. chance_initialization($chance);
  140. }
  141. });
  142. }
  143. }
  144.  
  145. function claim_treat_initialization()
  146. {
  147. var query = TWE.getQuery(),
  148. showForm = false,
  149. $title = $(TWE.get('is_mobile') ? 'article.article-content > h1.article-title' : '.mainarea > h1.article-header'),
  150. options,
  151. $message;
  152.  
  153. if (query.claim_id > 0 && query.claim_nonce && query.fingerprint) {
  154. options = get_chance_options(query.claim_id);
  155. if (options.hasOwnProperty('id')) {
  156. showForm = true;
  157. }
  158. }
  159.  
  160. if (showForm && options.img) {
  161. /*$('<img/>').attr({
  162. 'src': options.img,
  163. 'alt': options.name
  164. }).insertBefore($title).wrap('<div class="trick-or-treat-prize-img"></div>');*/
  165. }
  166.  
  167. $message = $('<div class="claim-treat-message"></div>').html(showForm ? options.copy : 'But don\'t settle, keep searching.')
  168.  
  169. if (showForm) {
  170. $title.html(options.name);
  171. track('claim_treat_init', query.claim_id);
  172. } else {
  173. $title.html('Sorry, you need to find a prize first in order to claim it!');
  174. $message.addClass('claim-treat-invalid');
  175. track('claim_treat_init_bad');
  176. }
  177.  
  178. $message.insertBefore($claim_treat_container);
  179.  
  180. if (showForm && options && $.isFunction(options.init_claim)) {
  181. options.init_claim($claim_treat_container, $message);
  182. }
  183.  
  184. $claim_treat_container.toggle(showForm);
  185. }
  186.  
  187. function chance_initialization($chance)
  188. {
  189. var id = $chance.data('chance-id'),
  190. request_nonce = $chance.data('request-nonce'),
  191. options;
  192.  
  193. if (id && request_nonce) {
  194. track('init_chance', id);
  195. options = get_chance_options(id);
  196. if ($.isFunction(options.init_chance)) {
  197. // If there is extra initialization defined for this particular chance, do it now.
  198. options.init_chance($chance);
  199. }
  200. $chance.one('click', function(e){
  201. get_fingerprint().always(function(){
  202. if ($.isFunction(Fingerprint2)) {
  203. new Fingerprint2().get(function(fingerprint){
  204. $chance.data('fingerprint',fingerprint);
  205. trick_or_treat_request($chance, id, request_nonce, fingerprint);
  206. });
  207. } else {
  208. trick_or_treat_request($chance, id, request_nonce, false);
  209. }
  210. });
  211. e.preventDefault();
  212. }).addClass('ready');
  213. } else {
  214. // If for some reason a chance has ended up in the frontend without the required data, get rid of it.
  215. $chance.remove();
  216. track('broken_chance');
  217. }
  218. }
  219.  
  220. function trick_or_treat_request($chance, id, request_nonce, fingerprint)
  221. {
  222. //console.log("trick or treat");<
  223. if (false === fingerprint) {
  224. track('failed_fingerprint', id);
  225. $chance.on('click', show_trick_notification).addClass('trick').click();
  226. } else {
  227. track('request', id);
  228. $chance.addClass('loading-response');
  229. $.getJSON(TWE.get('ajaxurl','/wp-admin/admin-ajax.php'), {action: 'trick_or_treat', id: id, request_nonce: request_nonce, fingerprint: fingerprint}, function(data, status, xhr) {
  230. //console.dir(data);
  231. $chance.removeClass('loading-response').data('chances-remaining', data.claimable).data('chances-claimed', data.claimed);
  232.  
  233. if (true === data.treat && data.claim_nonce) {
  234. track('treat_response', id);
  235. // Received offer nonce
  236. $chance.data('claim-nonce', data.claim_nonce).on('click', show_treat_notification).addClass('treat');
  237. window.setTimeout(function() {
  238. $chance.click();
  239. }, 500);
  240. } else {
  241. // Prize already claimed/offered.
  242. track('trick_response', id);
  243. get_trick_defs().always(function(){
  244. get_tricks().always(function() {
  245. //console.log("get_tricks");
  246. var trick;
  247. /*
  248. if (TWE.hasFunction('get_trick') && (trick = TWE.get_trick($chance))) {
  249. console.log("inside if");
  250. //console.dir(trick);
  251. trick.prepare(function() {
  252. $chance.on('click', show_treat_notification).addClass('trickme');
  253.  
  254. window.setTimeout(function() {
  255. console.log("chance click");
  256. console.dir($chance);
  257. //$chance.trigger('click');
  258. $chance.click();
  259. }, 500);
  260. });
  261. } else {*/
  262. //console.log("inside else failed to load");
  263. // Failed to load tricks for some reason, use the simple fallback
  264. $chance.on('click', show_trick_notification).addClass('trickme');
  265. window.setTimeout(function() {
  266. $chance.click();
  267. }, 500);
  268. //}
  269. });
  270. });
  271. }
  272. });
  273. }
  274. }
  275.  
  276. function show_treat_notification(e)
  277. {
  278. var $chance = $(this), data = $chance.data(), $notification = data.hasOwnProperty('notification') ? data.notification : false, options, message, url;
  279.  
  280. if (data.claimNonce && data.chanceId && data.fingerprint && ($notification === false || $notification.closest('body').length === 0)) {
  281. if (CONFIG.claim_page) {
  282. url = CONFIG.claim_page
  283. } else {
  284. url = TWE.get('siteurl','') + '/claim-treat/';
  285. }
  286. options = get_chance_options(data.chanceId);
  287. url += '?' + $.param({claim_id : data.chanceId, claim_nonce: data.claimNonce, fingerprint: data.fingerprint});
  288. message = replace_variables(options.notification_copy, {'url' : url, 'remaining' : ''+data.chancesRemaining, 'remaining_words' : ''+english_number(data.chancesRemaining), 'claimed' : ''+data.chancesClaimed, 'claimed_words' : ''+english_number(data.chancesClaimed)});
  289.  
  290. $notification = TWE.notify({
  291. position: {
  292. relY: 'top',
  293. el: $chance
  294. },
  295. time:0,
  296. direction:'down',
  297. scroll:false,
  298. message:'<span class="close-notification icon-cancel"></span>' + message,
  299. wrapperClass: 'trick-or-treat-notification-wrapper trick-or-treat-notification-wrapper_treat trick-or-treat-notification-wrapper-' + data.chanceId
  300. });
  301.  
  302. setup_notification_close($notification);
  303.  
  304. $chance.data('notification', $notification);
  305. }
  306.  
  307. e.preventDefault();
  308. }
  309.  
  310. function show_trick_notification(e)
  311. {
  312. var $chance = $(this), data = $chance.data(), message = '<span class="close-notification icon-cancel"></span>', options = get_chance_options(data.chanceId), $notification;
  313. e.preventDefault();
  314.  
  315. //console.log("here");
  316. // If the last shown notification has been removed
  317. if (!data.notification || (data.notification.closest('body').length === 0)) {
  318. if (1 == CONFIG.trick_sound && $trick_audio.length > 0 && $trick_audio[0].readyState === 4) {
  319. $trick_audio[0].play();
  320. }
  321.  
  322. if (data.chancesRemaining > 0) {
  323. message += replace_variables(options.trick_notification_copy, {'remaining' : ''+data.chancesRemaining, 'remaining_words' : ''+english_number(data.chancesRemaining), 'claimed' : ''+data.chancesClaimed, 'claimed_words' : ''+english_number(data.chancesClaimed)});
  324. } else {
  325. message += options.trick_notification_none_copy;
  326. }
  327.  
  328. $notification = TWE.notify({
  329. position: {
  330. relY: 'top',
  331. el: $chance
  332. },
  333. time:0,
  334. direction:'down',
  335. scroll:false,
  336. message:message,
  337. wrapperClass: 'trick-or-treat-notification-wrapper trick-or-treat-notification-wrapper_trick trick-or-treat-notification-wrapper-' + data.chanceId
  338. });
  339.  
  340. setup_notification_close($notification);
  341.  
  342. $chance.data('notification', $notification);
  343. }
  344.  
  345. }
  346.  
  347. function setup_notification_close($notification)
  348. {
  349. if (TWE.get('is_mobile')) {
  350. $notification.on('notification_visible.twe', function(){
  351. $notification.next('.notification-message').addClass('with-close').on('click', '.close-notification', function(){$notification.trigger('notification_hide.twe');})
  352. });
  353. } else {
  354. $notification.on('click', '.close-notification', function(){$notification.trigger('notification_hide.twe');});
  355. }
  356. }
  357.  
  358. function track(action, id, fingerprint)
  359. {
  360. if (TWE.hasFunction('trackEvent')) {
  361. if (fingerprint) {
  362. TWE.trackEvent('trick_or_treat', id ? action + '_' + id : action, fingerprint);
  363. } else {
  364. get_fingerprint().always(function(){
  365. if ($.isFunction(Fingerprint2)) {
  366. new Fingerprint2().get(function(){
  367. if (!fingerprint) {
  368. fingerprint = 'no fingerprint';
  369. }
  370. TWE.trackEvent('trick_or_treat', id ? action + '_' + id : action, fingerprint);
  371. });
  372. } else {
  373. TWE.trackEvent('trick_or_treat', id ? action + '_' + id : action, 'failed_fingerprint');
  374. }
  375. });
  376. }
  377. }
  378. }
  379.  
  380. function english_number(num)
  381. {
  382. var ZERO_TO_NINETEEN = [
  383. "zero",
  384. "one",
  385. "two",
  386. "three",
  387. "four",
  388. "five",
  389. "six",
  390. "seven",
  391. "eight",
  392. "nine",
  393. "ten",
  394. "eleven",
  395. "twelve",
  396. "thirteen",
  397. "fourteen",
  398. "fifteen",
  399. "sixteen",
  400. "seventeen",
  401. "eighteen",
  402. "nineteen"
  403. ], TENS = [
  404. "",
  405. "ten",
  406. "twenty",
  407. "thirty",
  408. "fourty",
  409. "fifty",
  410. "sixty",
  411. "seventy",
  412. "eighty",
  413. "ninety"
  414. ], HUNDRED = "hundred", floored = Math.floor(num), hundreds, tens, remainder;
  415.  
  416. if (floored < 0 || isNaN(floored)) {
  417. floored = 0;
  418. }
  419.  
  420. if (floored < 20) {
  421. return ZERO_TO_NINETEEN[floored];
  422. }
  423.  
  424.  
  425. if (floored < 100) {
  426. remainder = floored % 10;
  427. tens = Math.floor(floored / 10);
  428. if (0 === remainder) {
  429. return TENS[tens];
  430. }
  431. return TENS[tens] + ' ' + ZERO_TO_NINETEEN[remainder];
  432. }
  433.  
  434. hundreds = Math.floor(floored / 100);
  435. remainder = floored % 100;
  436. if (0 === remainder) {
  437. return ZERO_TO_NINETEEN[hundreds] + ' ' + HUNDRED;
  438. }
  439.  
  440. return ZERO_TO_NINETEEN[hundreds] + ' ' + HUNDRED + ' and ' + english_number(remainder);
  441. }
  442. });
  443. })(jQuery)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement