Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($){
- $(function($){
- var $claim_treat_container = $('.claim-treat-container'),
- PRELOADS = {},
- FINGERPRINT = false,
- CONFIG = TWE.get('trick_or_treat_config', {}),
- CHANCE_OPTIONS = TWE.get('trick_or_treat_chances', {}),
- $trick_audio;
- // Setup individual chances
- if (!$.isEmptyObject(CHANCE_OPTIONS)) {
- // 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.
- chances_initialization();
- }
- // Setup claim form page
- if (1 === $claim_treat_container.length) {
- claim_treat_initialization();
- }
- function preload_script(id, url, check, inTWE, maxAttempts, checkDelay)
- {
- var attempts,
- loading = PRELOADS.hasOwnProperty(id); // Are we already loading it?
- // Defaults
- if (!(maxAttempts > 0)) {
- maxAttempts = 30;
- }
- if (!(checkDelay > 0)) {
- checkDelay = 100;
- }
- if (!loading) {
- PRELOADS[id] = $.Deferred();
- attempts = 0;
- $.ajax({
- url: url,
- dataType: 'script',
- cache:true
- }).done(check_loaded).fail(PRELOADS[id].reject);
- }
- function check_loaded()
- {
- var loaded, result;
- if (true === inTWE) {
- result = TWE.get(check, false);
- loaded = (false !== result);
- } else {
- loaded = (check in window);
- result = (loaded) ? window[check] : false;
- }
- if (loaded) {
- PRELOADS[id].resolve(result);
- } else if (++attempts > maxAttempts) {
- PRELOADS[id].reject(result);
- } else {
- window.setTimeout(check_loaded, checkDelay);
- }
- }
- return PRELOADS[id];
- }
- function get_fingerprint()
- {
- return preload_script('FINGERPRINT_LIB', TWE.get('themeurl', '/wp-content/themes/theweekendedition-v2') + '/js/lib/fingerprint2.min.js', 'Fingerprint2');
- }
- function get_tricks()
- {
- return preload_script('TRICK', TWE.get('themeurl', '/wp-content/themes/theweekendedition-v2') + '/trick-or-treat/trick.min.js', 'get_trick', true);
- }
- function get_trick_defs()
- {
- 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);
- }
- function get_chance_options(id)
- {
- return $.extend({}, {
- init_chance: halloween_chance_init,
- init_claim: $.noop,
- name: 'Prize',
- copy: 'You\'ve won a prize, thanks to The Weekend Edition and our valued partners'
- }, (id > 0 && CHANCE_OPTIONS.hasOwnProperty(id)) ? CHANCE_OPTIONS[id] : {});
- }
- //call the function on line85 to load in the halloween class images
- function halloween_chance_init($chance)
- {
- var classes = ['halloween'];
- $chance.addClass(classes[0]);
- }
- //call the function on line85 to load in the paniyiri class images
- function paniyiri_chance_init($chance)
- {
- var classes = ['baklava', 'gyro', 'olives'];
- $chance.addClass(classes[1]);
- }
- function replace_variables(content, variables)
- {
- var result = content;
- $.each(variables, function(name, data) {
- result = result.replace(new RegExp('%' + name + '%', 'g'), data);
- });
- return result;
- }
- function chances_initialization()
- {
- if (1 == CONFIG.trick_sound) {
- $trick_audio = $('<audio></audio>').attr({
- 'src' : TWE.get('themeurl', '/wp-content/themes/theweekendedition-v2') + '/trick-or-treat/trick-sound.mp3',
- 'preload' : 'auto'
- }).appendTo('body');
- }
- get_tricks();
- get_trick_defs();
- get_fingerprint();
- $.each(CHANCE_OPTIONS, chance_request)
- }
- function chance_request(id, data)
- {
- if (data.hasOwnProperty('nonce')) {
- $.getJSON(TWE.get('ajaxurl','/wp-admin/admin-ajax.php'), {action: 'get_chance', id: id, get_nonce: data.nonce}, function(data, status, xhr) {
- var $content = $(TWE.get('is_mobile', false) ? '.article-content' : '.content-area'),
- $paragraphs, $chance;
- if (!data.error && data.html && ($content.length === 1)) {
- $paragraphs = $content.find('p');
- $chance = $(data.html).insertAfter(1);
- chance_initialization($chance);
- }
- });
- }
- }
- function claim_treat_initialization()
- {
- var query = TWE.getQuery(),
- showForm = false,
- $title = $(TWE.get('is_mobile') ? 'article.article-content > h1.article-title' : '.mainarea > h1.article-header'),
- options,
- $message;
- if (query.claim_id > 0 && query.claim_nonce && query.fingerprint) {
- options = get_chance_options(query.claim_id);
- if (options.hasOwnProperty('id')) {
- showForm = true;
- }
- }
- if (showForm && options.img) {
- /*$('<img/>').attr({
- 'src': options.img,
- 'alt': options.name
- }).insertBefore($title).wrap('<div class="trick-or-treat-prize-img"></div>');*/
- }
- $message = $('<div class="claim-treat-message"></div>').html(showForm ? options.copy : 'But don\'t settle, keep searching.')
- if (showForm) {
- $title.html(options.name);
- track('claim_treat_init', query.claim_id);
- } else {
- $title.html('Sorry, you need to find a prize first in order to claim it!');
- $message.addClass('claim-treat-invalid');
- track('claim_treat_init_bad');
- }
- $message.insertBefore($claim_treat_container);
- if (showForm && options && $.isFunction(options.init_claim)) {
- options.init_claim($claim_treat_container, $message);
- }
- $claim_treat_container.toggle(showForm);
- }
- function chance_initialization($chance)
- {
- var id = $chance.data('chance-id'),
- request_nonce = $chance.data('request-nonce'),
- options;
- if (id && request_nonce) {
- track('init_chance', id);
- options = get_chance_options(id);
- if ($.isFunction(options.init_chance)) {
- // If there is extra initialization defined for this particular chance, do it now.
- options.init_chance($chance);
- }
- $chance.one('click', function(e){
- get_fingerprint().always(function(){
- if ($.isFunction(Fingerprint2)) {
- new Fingerprint2().get(function(fingerprint){
- $chance.data('fingerprint',fingerprint);
- trick_or_treat_request($chance, id, request_nonce, fingerprint);
- });
- } else {
- trick_or_treat_request($chance, id, request_nonce, false);
- }
- });
- e.preventDefault();
- }).addClass('ready');
- } else {
- // If for some reason a chance has ended up in the frontend without the required data, get rid of it.
- $chance.remove();
- track('broken_chance');
- }
- }
- function trick_or_treat_request($chance, id, request_nonce, fingerprint)
- {
- //console.log("trick or treat");<
- if (false === fingerprint) {
- track('failed_fingerprint', id);
- $chance.on('click', show_trick_notification).addClass('trick').click();
- } else {
- track('request', id);
- $chance.addClass('loading-response');
- $.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) {
- //console.dir(data);
- $chance.removeClass('loading-response').data('chances-remaining', data.claimable).data('chances-claimed', data.claimed);
- if (true === data.treat && data.claim_nonce) {
- track('treat_response', id);
- // Received offer nonce
- $chance.data('claim-nonce', data.claim_nonce).on('click', show_treat_notification).addClass('treat');
- window.setTimeout(function() {
- $chance.click();
- }, 500);
- } else {
- // Prize already claimed/offered.
- track('trick_response', id);
- get_trick_defs().always(function(){
- get_tricks().always(function() {
- //console.log("get_tricks");
- var trick;
- /*
- if (TWE.hasFunction('get_trick') && (trick = TWE.get_trick($chance))) {
- console.log("inside if");
- //console.dir(trick);
- trick.prepare(function() {
- $chance.on('click', show_treat_notification).addClass('trickme');
- window.setTimeout(function() {
- console.log("chance click");
- console.dir($chance);
- //$chance.trigger('click');
- $chance.click();
- }, 500);
- });
- } else {*/
- //console.log("inside else failed to load");
- // Failed to load tricks for some reason, use the simple fallback
- $chance.on('click', show_trick_notification).addClass('trickme');
- window.setTimeout(function() {
- $chance.click();
- }, 500);
- //}
- });
- });
- }
- });
- }
- }
- function show_treat_notification(e)
- {
- var $chance = $(this), data = $chance.data(), $notification = data.hasOwnProperty('notification') ? data.notification : false, options, message, url;
- if (data.claimNonce && data.chanceId && data.fingerprint && ($notification === false || $notification.closest('body').length === 0)) {
- if (CONFIG.claim_page) {
- url = CONFIG.claim_page
- } else {
- url = TWE.get('siteurl','') + '/claim-treat/';
- }
- options = get_chance_options(data.chanceId);
- url += '?' + $.param({claim_id : data.chanceId, claim_nonce: data.claimNonce, fingerprint: data.fingerprint});
- 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)});
- $notification = TWE.notify({
- position: {
- relY: 'top',
- el: $chance
- },
- time:0,
- direction:'down',
- scroll:false,
- message:'<span class="close-notification icon-cancel"></span>' + message,
- wrapperClass: 'trick-or-treat-notification-wrapper trick-or-treat-notification-wrapper_treat trick-or-treat-notification-wrapper-' + data.chanceId
- });
- setup_notification_close($notification);
- $chance.data('notification', $notification);
- }
- e.preventDefault();
- }
- function show_trick_notification(e)
- {
- var $chance = $(this), data = $chance.data(), message = '<span class="close-notification icon-cancel"></span>', options = get_chance_options(data.chanceId), $notification;
- e.preventDefault();
- //console.log("here");
- // If the last shown notification has been removed
- if (!data.notification || (data.notification.closest('body').length === 0)) {
- if (1 == CONFIG.trick_sound && $trick_audio.length > 0 && $trick_audio[0].readyState === 4) {
- $trick_audio[0].play();
- }
- if (data.chancesRemaining > 0) {
- 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)});
- } else {
- message += options.trick_notification_none_copy;
- }
- $notification = TWE.notify({
- position: {
- relY: 'top',
- el: $chance
- },
- time:0,
- direction:'down',
- scroll:false,
- message:message,
- wrapperClass: 'trick-or-treat-notification-wrapper trick-or-treat-notification-wrapper_trick trick-or-treat-notification-wrapper-' + data.chanceId
- });
- setup_notification_close($notification);
- $chance.data('notification', $notification);
- }
- }
- function setup_notification_close($notification)
- {
- if (TWE.get('is_mobile')) {
- $notification.on('notification_visible.twe', function(){
- $notification.next('.notification-message').addClass('with-close').on('click', '.close-notification', function(){$notification.trigger('notification_hide.twe');})
- });
- } else {
- $notification.on('click', '.close-notification', function(){$notification.trigger('notification_hide.twe');});
- }
- }
- function track(action, id, fingerprint)
- {
- if (TWE.hasFunction('trackEvent')) {
- if (fingerprint) {
- TWE.trackEvent('trick_or_treat', id ? action + '_' + id : action, fingerprint);
- } else {
- get_fingerprint().always(function(){
- if ($.isFunction(Fingerprint2)) {
- new Fingerprint2().get(function(){
- if (!fingerprint) {
- fingerprint = 'no fingerprint';
- }
- TWE.trackEvent('trick_or_treat', id ? action + '_' + id : action, fingerprint);
- });
- } else {
- TWE.trackEvent('trick_or_treat', id ? action + '_' + id : action, 'failed_fingerprint');
- }
- });
- }
- }
- }
- function english_number(num)
- {
- var ZERO_TO_NINETEEN = [
- "zero",
- "one",
- "two",
- "three",
- "four",
- "five",
- "six",
- "seven",
- "eight",
- "nine",
- "ten",
- "eleven",
- "twelve",
- "thirteen",
- "fourteen",
- "fifteen",
- "sixteen",
- "seventeen",
- "eighteen",
- "nineteen"
- ], TENS = [
- "",
- "ten",
- "twenty",
- "thirty",
- "fourty",
- "fifty",
- "sixty",
- "seventy",
- "eighty",
- "ninety"
- ], HUNDRED = "hundred", floored = Math.floor(num), hundreds, tens, remainder;
- if (floored < 0 || isNaN(floored)) {
- floored = 0;
- }
- if (floored < 20) {
- return ZERO_TO_NINETEEN[floored];
- }
- if (floored < 100) {
- remainder = floored % 10;
- tens = Math.floor(floored / 10);
- if (0 === remainder) {
- return TENS[tens];
- }
- return TENS[tens] + ' ' + ZERO_TO_NINETEEN[remainder];
- }
- hundreds = Math.floor(floored / 100);
- remainder = floored % 100;
- if (0 === remainder) {
- return ZERO_TO_NINETEEN[hundreds] + ' ' + HUNDRED;
- }
- return ZERO_TO_NINETEEN[hundreds] + ' ' + HUNDRED + ' and ' + english_number(remainder);
- }
- });
- })(jQuery)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement