Advertisement
arowls

Add Google Event Tracking to Monarch Social Sharing Plugin

Mar 14th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.     $(document).ready(function() {
  3.         var all_networks_opened = 0;
  4.  
  5.         $('body').on('click', '.et_social_share', function() {
  6.             var $this_el = $(this),
  7.                 social_network = $this_el.data('social_name'),
  8.                 social_type = $this_el.data('social_type'),
  9.                 media_url = 'media' == social_type ? $this_el.closest('.et_social_media_wrapper').find('img').attr('src') : '',
  10.                 post_id = $this_el.data('post_id'),
  11.                 share_link = 'media' == social_type ? $this_el.data('social_link') : $this_el.prop('href');
  12.  
  13.             update_stats_table(social_type, social_network, media_url, post_id, $this_el);
  14.  
  15.  
  16.  
  17.  
  18.             if ('like' === social_network) {
  19.                 return false;
  20.             }
  21.  
  22.             var left = ($(window).width() / 2) - (550 / 2);
  23.             var top = ($(window).height() / 2) - (450 / 2);
  24.             var new_window = window.open(share_link, '', 'scrollbars=1, height=450, width=550, left=' + left + ', top=' + top);
  25.  
  26.             if (window.focus) {
  27.                 new_window.focus();
  28.             }
  29.  
  30.  
  31.             if (__gaTracker)
  32.             // using Yoast Analytics with Universal GA;
  33.                 __gaTracker('send', 'event', 'Social_Share', social_network, document.title);
  34.             else if (_gaq)
  35.             // using classic GA;
  36.                 _gaq('send', 'event', 'Social_Share', social_network, document.title);
  37.             else if (_ga)
  38.             // using Universal GA;
  39.                 _ga('send', 'event', 'Social_Share', social_network, document.title);
  40.  
  41.  
  42.             return false;
  43.         });
  44.  
  45.         $('.et_social_follow').click(function() {
  46.             var $this_el = $(this),
  47.                 social_network = $this_el.data('social_name'),
  48.                 social_type = $this_el.data('social_type'),
  49.                 media_url = 'media' == social_type ? $this_el.closest('.et_social_media_wrapper').find('img').attr('src') : '',
  50.                 post_id = $this_el.data('post_id');
  51.  
  52.             update_stats_table(social_type, social_network, media_url, post_id, $this_el);
  53.             if (__gaTracker)
  54.             // using Yoast Analytics with Universal GA;
  55.                 __gaTracker('send', 'event', 'Social_Follow', social_network, document.title);
  56.             else if (_gaq)
  57.             // using classic GA;
  58.                 _gaq('send', 'event', 'Social_Follow', social_network, document.title);
  59.             else if (_ga)
  60.             // using Universal GA;
  61.                 _ga('send', 'event', 'Social_Follow', social_network, document.title);
  62.  
  63.             if ('like' === social_network) {
  64.                 return false;
  65.             }
  66.         });
  67.  
  68.         $('body').on('click', '.et_social_share_pinterest', function() {
  69.             if ($(this).hasClass('et_social_pin_all')) {
  70.                 var left = ($(window).width() / 2) - (550 / 2),
  71.                     top = ($(window).height() / 2) - (450 / 2),
  72.                     share_link = $(this).attr('href'),
  73.                     new_window = window.open(share_link, '', 'scrollbars=1, height=450, width=550, left=' + left + ', top=' + top);
  74.  
  75.                 if (window.focus) {
  76.                     new_window.focus();
  77.                 }
  78.             } else {
  79.                 $('.et_social_pin_images_outer').fadeToggle(400);
  80.             }
  81.  
  82.             return false;
  83.         });
  84.  
  85.         function get_url_parameter(param_name) {
  86.             var page_url = window.location.search.substring(1);
  87.             var url_variables = page_url.split('&');
  88.             for (var i = 0; i < url_variables.length; i++) {
  89.                 var curr_param_name = url_variables[i].split('=');
  90.                 if (curr_param_name[0] == param_name) {
  91.                     return curr_param_name[1];
  92.                 }
  93.             }
  94.         }
  95.  
  96.         function update_stats_table($action, $social_network, $media_url, $post_id, $this_el) {
  97.             $stats_data = JSON.stringify({
  98.                 'action': $action,
  99.                 'network': $social_network,
  100.                 'media_url': $media_url,
  101.                 'post_id': $post_id
  102.             });
  103.             $.ajax({
  104.                 type: 'POST',
  105.                 url: monarchSettings.ajaxurl,
  106.                 data: {
  107.                     action: 'add_stats_record_db',
  108.                     stats_data_array: $stats_data,
  109.                     add_stats_nonce: monarchSettings.stats_nonce
  110.                 },
  111.                 success: function(data) {
  112.                     if (true == data) {
  113.                         if ('like' === $action) {
  114.                             update_single_shares($this_el, '', $post_id, $social_network, 'like');
  115.                         }
  116.                         if ('media' == $action) {
  117.                             update_total_media_shares($this_el.closest('.et_social_media_wrapper'));
  118.                             update_single_shares($this_el, $media_url, $post_id, $social_network, 'media');
  119.                         }
  120.                     }
  121.                 }
  122.             });
  123.         }
  124.  
  125.  
  126.         function append_share_counts($current_network) {
  127.             var network = $current_network.data('social_name'),
  128.                 min_count = $current_network.data('min_count'),
  129.                 post_id = $current_network.data('post_id'),
  130.                 url = monarchSettings.pageurl !== '' ? monarchSettings.pageurl : window.location.href,
  131.                 label_div = $current_network.find('.et_social_network_label'),
  132.                 append_to = (0 != (label_div.length)) ? label_div : $current_network;
  133.  
  134.             $share_count_data = JSON.stringify({
  135.                 'network': network,
  136.                 'min_count': min_count,
  137.                 'post_id': post_id,
  138.                 'url': url
  139.             });
  140.  
  141.             $.ajax({
  142.                 type: 'POST',
  143.                 url: monarchSettings.ajaxurl,
  144.                 data: {
  145.                     action: 'get_shares_count',
  146.                     share_count_array: $share_count_data,
  147.                     get_share_counts_nonce: monarchSettings.share_counts
  148.                 },
  149.                 beforeSend: function(data) {
  150.                     append_to.append('<span class="et_social_placeholder"></span>');
  151.                 },
  152.                 success: function(data) {
  153.                     $current_network.find('span.et_social_placeholder').remove();
  154.                     append_to.append(data);
  155.                 }
  156.             });
  157.         }
  158.  
  159.         function append_total_shares($current_area) {
  160.             var post_id = $current_area.data('post_id'),
  161.                 url = monarchSettings.pageurl !== '' ? monarchSettings.pageurl : window.location.href,
  162.                 append_to = $current_area;
  163.  
  164.             $share_total_count_data = JSON.stringify({
  165.                 'post_id': post_id,
  166.                 'url': url
  167.             });
  168.  
  169.             $.ajax({
  170.                 type: 'POST',
  171.                 url: monarchSettings.ajaxurl,
  172.                 data: {
  173.                     action: 'get_total_shares',
  174.                     share_total_count_array: $share_total_count_data,
  175.                     get_total_counts_nonce: monarchSettings.total_counts
  176.                 },
  177.                 beforeSend: function(data) {
  178.                     append_to.append('<span class="et_social_placeholder"></span>');
  179.                 },
  180.                 success: function(data) {
  181.                     append_to.find('span.et_social_placeholder').remove();
  182.                     append_to.append(data);
  183.                 }
  184.             });
  185.         }
  186.  
  187.         function append_follow_counts($current_area) {
  188.             var network = $current_area.data('network'),
  189.                 min_count = $current_area.data('min_count'),
  190.                 index = $current_area.data('index'),
  191.                 append_to = $current_area;
  192.  
  193.             $follow_count_data = JSON.stringify({
  194.                 'network': network,
  195.                 'min_count': min_count,
  196.                 'index': index
  197.             });
  198.             $.ajax({
  199.                 type: 'POST',
  200.                 url: monarchSettings.ajaxurl,
  201.                 data: {
  202.                     action: 'get_follow_counts',
  203.                     follow_count_array: $follow_count_data,
  204.                     get_follow_counts_nonce: monarchSettings.follow_counts
  205.                 },
  206.                 beforeSend: function(data) {
  207.                     append_to.append('<span class="et_social_placeholder"></span>');
  208.                 },
  209.                 success: function(data) {
  210.                     $current_area.find('span.et_social_placeholder').remove();
  211.                     append_to.append(data);
  212.                 }
  213.             });
  214.         }
  215.  
  216.         function append_total_follows($current_area) {
  217.             var append_to = $current_area;
  218.             $.ajax({
  219.                 type: 'POST',
  220.                 url: monarchSettings.ajaxurl,
  221.                 data: {
  222.                     action: 'get_follow_total',
  223.                     get_total_counts_nonce: monarchSettings.total_counts
  224.                 },
  225.                 beforeSend: function(data) {
  226.                     append_to.append('<span class="et_social_placeholder"></span>');
  227.                 },
  228.                 success: function(data) {
  229.                     append_to.find('span.et_social_placeholder').remove();
  230.                     append_to.append(data);
  231.                 }
  232.             });
  233.         }
  234.  
  235.         if ($('.et_social_display_follow_counts').length) {
  236.             $('.et_social_display_follow_counts').each(function() {
  237.                 append_follow_counts($(this));
  238.             });
  239.         }
  240.  
  241.         if ($('.et_social_follow_total').length) {
  242.             $('.et_social_follow_total').each(function() {
  243.                 append_total_follows($(this));
  244.             });
  245.         }
  246.  
  247.  
  248.         if ($('.et_social_total_share').length) {
  249.             $('.et_social_total_share').each(function() {
  250.                 append_total_shares($(this));
  251.             });
  252.         }
  253.  
  254.         if ($('.et_social_display_count').length) {
  255.             $('.et_social_display_count').each(function() {
  256.                 append_share_counts($(this));
  257.             });
  258.         }
  259.  
  260.         if ($('.et_social_media_wrapper').length && $('.et_social_media_wrapper .et_social_totalcount').length) {
  261.  
  262.             $('.et_social_media_wrapper').each(function() {
  263.                 update_total_media_shares($(this));
  264.             });
  265.         }
  266.  
  267.         if ($('.et_social_media_wrapper').length && $('.et_social_media_wrapper .et_social_withcounts').length) {
  268.  
  269.             $('.et_social_media_wrapper .et_social_share').each(function() {
  270.                 var this_el = $(this),
  271.                     media_url = this_el.closest('.et_social_media_wrapper').find('img').attr('src'),
  272.                     post_id = this_el.data('post_id'),
  273.                     social_network = this_el.data('social_name');
  274.  
  275.                 update_single_shares(this_el, media_url, post_id, social_network, 'media');
  276.  
  277.             });
  278.         }
  279.  
  280.         function update_total_media_shares($element) {
  281.             if ($('.et_social_totalcount').length) {
  282.                 var this_el = $element,
  283.                     media_url = this_el.find('img').attr('src'),
  284.                     post_id = this_el.find('.et_social_share').first().data('post_id'),
  285.                     media_data = JSON.stringify({
  286.                         'media_url': media_url,
  287.                         'post_id': post_id
  288.                     });
  289.  
  290.                 $.ajax({
  291.                     type: 'POST',
  292.                     url: monarchSettings.ajaxurl,
  293.                     data: {
  294.                         action: 'get_media_shares_total',
  295.                         media_total: media_data,
  296.                         get_media_shares_total_nonce: monarchSettings.media_total
  297.                     },
  298.                     success: function(data) {
  299.                         this_el.find('.et_social_totalcount_count').empty().append(data);
  300.                     }
  301.                 });
  302.             }
  303.         }
  304.  
  305.         function update_single_shares($this_el, $media_url, $post_id, $network, $action) {
  306.             if ($('.et_social_withcounts').length) {
  307.                 var media_data = JSON.stringify({
  308.                     'media_url': $media_url,
  309.                     'post_id': $post_id,
  310.                     'network': $network,
  311.                     'action': $action
  312.                 });
  313.                 $.ajax({
  314.                     type: 'POST',
  315.                     url: monarchSettings.ajaxurl,
  316.                     data: {
  317.                         action: 'get_shares_single',
  318.                         media_single: media_data,
  319.                         get_media_shares_nonce: monarchSettings.media_single
  320.                     },
  321.                     success: function(data) {
  322.                         $this_el.find('.et_social_count span').not('.et_social_count_label').empty().append(data);
  323.                     }
  324.                 });
  325.             }
  326.         }
  327.  
  328.         function setCookieExpire(days) {
  329.  
  330.             var ms = days * 24 * 60 * 60 * 1000;
  331.  
  332.             var date = new Date();
  333.             date.setTime(date.getTime() + ms);
  334.  
  335.             return "; expires=" + date.toUTCString();
  336.         }
  337.  
  338.         function checkCookieValue(cookieName, value) {
  339.             return parseCookies()[cookieName] === value;
  340.         }
  341.  
  342.         function parseCookies() {
  343.             var cookies = document.cookie.split('; ');
  344.  
  345.             var ret = {};
  346.             for (var i = cookies.length - 1; i >= 0; i--) {
  347.                 var el = cookies[i].split('=');
  348.                 ret[el[0]] = el[1];
  349.             }
  350.             return ret;
  351.         }
  352.  
  353.         function set_cookie($expire) {
  354.             cookieExpire = setCookieExpire($expire);
  355.             document.cookie = 'etSocialCookie=true' + cookieExpire;
  356.         }
  357.  
  358.         //separate function for the setTimeout to make it work properly within the loop.
  359.         function make_popup_visible($popup, $delay) {
  360.             setTimeout(function() {
  361.                 $popup.addClass('et_social_visible et_social_animated');
  362.                 if ($('.et_social_resize').length) {
  363.                     $('.et_social_resize').each(function() {
  364.                         define_popup_position($(this));
  365.                     });
  366.                 }
  367.             }, $delay);
  368.         }
  369.  
  370.         function auto_popup(this_el, delay) {
  371.             var $current_popup_auto = this_el;
  372.             if (!$current_popup_auto.hasClass('et_social_animated')) {
  373.                 var $cookies_expire_auto = $current_popup_auto.data('cookie_duration') ? $current_popup_auto.data('cookie_duration') : false,
  374.                     $delay = delay;
  375.  
  376.                 if ((false !== $cookies_expire_auto && !checkCookieValue('etSocialCookie', 'true')) || false == $cookies_expire_auto) {
  377.  
  378.                     make_popup_visible($current_popup_auto, $delay);
  379.  
  380.                     if (false !== $cookies_expire_auto) {
  381.                         set_cookie($cookies_expire_auto);
  382.                     }
  383.                 }
  384.             }
  385.         }
  386.  
  387.         if ('true' == get_url_parameter('et_monarch_popup')) {
  388.             $('.et_social_after_comment').each(function() {
  389.                 var $current_popup = $(this);
  390.                 auto_popup($current_popup, 0);
  391.             });
  392.         }
  393.  
  394.         if ($('.et_social_auto_popup').length) {
  395.             $('.et_social_auto_popup').each(function() {
  396.                 var $current_popup_auto = $(this);
  397.                 auto_popup($current_popup_auto, '' !== $current_popup_auto.data('delay') ? $current_popup_auto.data('delay') * 1000 : 0);
  398.             });
  399.         }
  400.  
  401.         $('.et_social_pinterest_window .et_social_close').on('click', function() {
  402.             $('.et_social_pin_images_outer').fadeToggle(400);
  403.         });
  404.  
  405.         $('body').on('click', '.et_social_icon_cancel', function() {
  406.             var this_el = $(this);
  407.             if (this_el.parent().hasClass('et_social_flyin')) {
  408.                 $popup = this_el.parent();
  409.             } else {
  410.                 $popup = this_el.parent().parent();
  411.  
  412.                 if ($popup.hasClass('et_social_all_networks_popup')) {
  413.                     all_networks_opened = 0;
  414.                 }
  415.             }
  416.  
  417.             $popup.addClass('et_social_fadeout');
  418.  
  419.             setTimeout(function() {
  420.                 $popup.remove();
  421.             }, 800);
  422.             return false;
  423.         });
  424.  
  425.         if ($('.et_monarch_after_order').length) {
  426.             $('.et_social_after_purchase').each(function() {
  427.                 var $current_popup = $(this);
  428.                 if (!$current_popup.hasClass('et_social_animated')) {
  429.                     var $cookies_expire = $current_popup.data('cookie_duration') ? $current_popup.data('cookie_duration') : false,
  430.                         $delay = 0;
  431.  
  432.                     if ((false !== $cookies_expire && !checkCookieValue('etSocialCookie', 'true')) || false == $cookies_expire) {
  433.  
  434.                         make_popup_visible($current_popup, $delay);
  435.  
  436.                         if (false !== $cookies_expire) {
  437.                             set_cookie($cookies_expire);
  438.                         }
  439.                     }
  440.                 }
  441.             });
  442.         }
  443.  
  444.         if ($('.et_social_trigger_bottom').length) {
  445.  
  446.             $('.et_social_trigger_bottom').each(function() {
  447.                 scroll_trigger($(this), true);
  448.             });
  449.  
  450.         }
  451.  
  452.         if ($('.et_social_scroll').length) {
  453.  
  454.             $('.et_social_scroll').each(function() {
  455.                 scroll_trigger($(this), false);
  456.             });
  457.  
  458.         }
  459.  
  460.         function scroll_trigger(this_el, is_bottom_trigger) {
  461.             var current_popup_bottom = this_el;
  462.             if (!current_popup_bottom.hasClass('et_social_animated')) {
  463.                 var cookies_expire_bottom = current_popup_bottom.data('cookie_duration') ? current_popup_bottom.data('cookie_duration') : false;
  464.  
  465.                 if (true == is_bottom_trigger) {
  466.                     var scroll_trigger = $('.et_social_bottom_trigger').length ? $('.et_social_bottom_trigger').offset().top : $(document).height() - 500;
  467.                 } else {
  468.                     var scroll_pos = this_el.data('scroll_pos') > 100 ? 100 : this_el.data('scroll_pos'),
  469.                         scroll_trigger = 100 == scroll_pos ? $(document).height() - 10 : $(document).height() * scroll_pos / 100;
  470.                 }
  471.  
  472.                 $(window).scroll(function() {
  473.                     if ((false !== cookies_expire_bottom && !checkCookieValue('etSocialCookie', 'true')) || false == cookies_expire_bottom) {
  474.                         if ($(window).scrollTop() + $(window).height() > scroll_trigger) {
  475.                             current_popup_bottom.addClass('et_social_visible et_social_animated');
  476.                             if ($('.et_social_resize').length) {
  477.                                 $('.et_social_resize').each(function() {
  478.                                     define_popup_position($(this));
  479.                                 });
  480.                             }
  481.                             if (false !== cookies_expire_bottom) {
  482.                                 set_cookie(cookies_expire_bottom);
  483.                             }
  484.                         }
  485.                     }
  486.                 });
  487.             }
  488.         }
  489.  
  490.         if ($('.et_social_trigger_idle').length) {
  491.             $('.et_social_trigger_idle').each(function() {
  492.                 var this_el = $(this);
  493.  
  494.                 if (!this_el.hasClass('et_social_animated')) {
  495.                     var $cookies_expire_idle = this_el.data('cookie_duration') ? this_el.data('cookie_duration') : false,
  496.                         $idle_timeout = '' !== this_el.data('idle_timeout') ? this_el.data('idle_timeout') * 1000 : 30000;
  497.  
  498.                     if ((false !== $cookies_expire_idle && !checkCookieValue('etSocialCookie', 'true')) || false == $cookies_expire_idle) {
  499.                         $(document).idleTimer($idle_timeout);
  500.  
  501.                         $(document).on("idle.idleTimer", function() {
  502.                             make_popup_visible(this_el, 0);
  503.                         });
  504.  
  505.                         if (false !== $cookies_expire_idle) {
  506.                             set_cookie($cookies_expire_idle);
  507.                         }
  508.                     }
  509.                 }
  510.             });
  511.         }
  512.  
  513.         // open close the mobile sideabr on header click
  514.         $('.et_social_heading, .et_social_mobile_button').click(function() {
  515.             $this_mobile_div = $('.et_social_mobile');
  516.  
  517.             $this_mobile_div.css({
  518.                 'display': 'block'
  519.             });
  520.             $('.et_social_mobile_button').removeClass('et_social_active_button');
  521.  
  522.             if ($this_mobile_div.hasClass('et_social_opened')) {
  523.                 $this_mobile_div.find('.et_social_networks').slideToggle(600);
  524.                 $this_mobile_div.removeClass('et_social_opened').addClass('et_social_closed');
  525.                 $('.et_social_mobile_overlay').removeClass('et_social_visible_overlay');
  526.                 $('.et_social_mobile_overlay').fadeToggle(600);
  527.             } else {
  528.                 $this_mobile_div.removeClass('et_social_closed').addClass('et_social_opened');
  529.                 $this_mobile_div.find('.et_social_networks').slideToggle(600);
  530.                 $('.et_social_mobile_overlay').addClass('et_social_visible_overlay').css({
  531.                     'display': 'block'
  532.                 });
  533.             }
  534.         });
  535.  
  536.         //if close button clicked - hide the mobile sidebar from screen
  537.         $('.et_social_mobile .et_social_close').click(function() {
  538.             $mobile_div = $('.et_social_mobile');
  539.             $mobile_div.fadeToggle(600);
  540.             $('.et_social_mobile_button').addClass('et_social_active_button');
  541.  
  542.             if ($mobile_div.hasClass('et_social_opened')) {
  543.                 $('.et_social_mobile_overlay').fadeToggle(600);
  544.                 $mobile_div.removeClass('et_social_opened');
  545.                 $mobile_div.find('.et_social_networks').fadeToggle(600);
  546.             }
  547.  
  548.         });
  549.  
  550.         // Move inline icons into appropriate sections in Divi theme
  551.         if ($('.et_social_inline').length) {
  552.             if ($('body').hasClass('et_pb_pagebuilder_layout')) {
  553.                 var top_inline = $('.et_social_inline_top'),
  554.                     bottom_inline = $('.et_social_inline_bottom'),
  555.                     divi_container = '<div class="et_pb_row"><div class="et_pb_column et_pb_column_4_4"></div></div>';
  556.  
  557.                 if (top_inline.length) {
  558.                     $('.et_pb_section').not('.et_pb_fullwidth_section').first().prepend(divi_container).find('.et_pb_row').first().find('.et_pb_column').append(top_inline);
  559.                 }
  560.  
  561.                 if (bottom_inline.length) {
  562.                     $('.et_pb_section').not('.et_pb_fullwidth_section').last().append(divi_container).find('.et_pb_row').last().find('.et_pb_column').append(bottom_inline);
  563.                 }
  564.             }
  565.         }
  566.  
  567.         function define_popup_position($this_popup) {
  568.             setTimeout(function() { // make sure all css transitions are finished to calculate the heights correctly
  569.                 var this_popup = $this_popup,
  570.                     networks_div = this_popup.find('.et_social_networks'),
  571.                     header_height = this_popup.find('.et_social_header').outerHeight(),
  572.                     total_count_height = this_popup.find('.et_social_totalcount').height(),
  573.                     extra_height = 0 < total_count_height ? 20 : 0;
  574.  
  575.                 this_popup.height(this_popup.find('.et_social_icons_container').innerHeight() + header_height + total_count_height + 20 + extra_height);
  576.  
  577.                 var popup_max_height = this_popup.hasClass('et_social_popup_content') ? $(window).height() : $(window).height() - 60;
  578.  
  579.                 if (this_popup.hasClass('et_social_popup_content') && 768 < $(window).width()) {
  580.                     popup_max_height = popup_max_height - 50;
  581.                 }
  582.  
  583.                 this_popup.css({
  584.                     'max-height': popup_max_height
  585.                 });
  586.  
  587.                 if (this_popup.hasClass('et_social_popup_content')) {
  588.                     var top_position = $(window).height() / 2 - this_popup.innerHeight() / 2;
  589.                     this_popup.css({
  590.                         'top': top_position + 'px'
  591.                     });
  592.                 }
  593.  
  594.                 var networks_div_height = this_popup.height() - header_height + total_count_height - extra_height;
  595.                 networks_div.height(networks_div_height);
  596.             }, 400);
  597.         }
  598.  
  599.         function set_mobile_sidebar_height() {
  600.             setTimeout(function() { // make sure all css transitions are finished to calculate the heights correctly
  601.                 var mobile_div = $('.et_social_mobile');
  602.  
  603.                 if (!mobile_div.hasClass('et_social_opened')) {
  604.                     $('.et_social_mobile .et_social_networks').css({
  605.                         'display': 'block'
  606.                     });
  607.                 }
  608.                 if ($('.et_social_active_button').length) {
  609.                     mobile_div.css({
  610.                         'display': 'block'
  611.                     });
  612.                 }
  613.  
  614.                 var inner_contatiner_height = mobile_div.find('.et_social_icons_container').innerHeight() + 45;
  615.  
  616.                 if (!mobile_div.hasClass('et_social_opened')) {
  617.                     $('.et_social_mobile .et_social_networks').css({
  618.                         'display': 'none'
  619.                     });
  620.                 }
  621.                 if ($('.et_social_active_button').length) {
  622.                     mobile_div.css({
  623.                         'display': 'none'
  624.                     });
  625.                 }
  626.  
  627.                 mobile_div.find('.et_social_networks').css({
  628.                     'max-height': inner_contatiner_height,
  629.                     'height': inner_contatiner_height
  630.                 });
  631.                 if ($(window).height() < inner_contatiner_height) {
  632.                     var inner_height = $(window).height() - mobile_div.find('.et_social_heading').innerHeight() + 10;
  633.                     mobile_div.find('.et_social_networks').css({
  634.                         'height': inner_height
  635.                     });
  636.                 }
  637.             }, 400);
  638.         }
  639.  
  640.         function set_sidebar_position() {
  641.             if ($('.et_social_sidebar_networks').length) {
  642.                 var this_sidebar = $('.et_social_sidebar_networks'),
  643.                     top_position = $(window).height() / 2 - this_sidebar.innerHeight() / 2;
  644.                 top_position = 0 > top_position ? 0 : top_position;
  645.                 this_sidebar.css({
  646.                     'top': top_position + 'px'
  647.                 });
  648.             }
  649.         }
  650.  
  651.         function set_media_wrapper_size() {
  652.             if ($('.et_social_media_wrapper').length) {
  653.                 $('.et_social_media_wrapper').each(function() {
  654.                     var this_wrapper = $(this),
  655.                         this_wrapper_media = this_wrapper.find('.et_social_media'),
  656.                         this_image = this_wrapper.find('img'),
  657.                         this_image_height = this_image.height(),
  658.                         this_image_width = this_image.width();
  659.  
  660.                     this_wrapper.addClass(this_image.attr('class'));
  661.                     this_wrapper_media.height(this_image_height);
  662.                     this_wrapper_media.width(this_image_width - 80);
  663.                 });
  664.             }
  665.         }
  666.  
  667.         $('body').on('click', '.et_social_open_all', function() {
  668.             all_networks_opened++;
  669.             if (1 == all_networks_opened) {
  670.                 var this_button = $(this),
  671.                     page_id = this_button.data('page_id'),
  672.                     permalink = this_button.data('permalink'),
  673.                     title = this_button.data('title'),
  674.                     media = typeof this_button.data('media') !== 'undefined' ? this_button.data('media') : '',
  675.                     is_popup = 'popup' == this_button.data('location') ? 'true' : 'false';
  676.  
  677.                 $.ajax({
  678.                     type: 'POST',
  679.                     url: monarchSettings.ajaxurl,
  680.                     data: {
  681.                         action: 'generate_all_networks_popup',
  682.                         all_networks_page_id: page_id,
  683.                         all_networks_link: permalink,
  684.                         all_networks_title: title,
  685.                         all_networks_media: media,
  686.                         is_popup: is_popup,
  687.                         generate_all_window_nonce: monarchSettings.generate_all_window_nonce
  688.                     },
  689.                     success: function(data) {
  690.                         if ('false' == is_popup) {
  691.                             $('body').append(data);
  692.                             make_popup_visible($('.et_social_all_networks_popup'), 1);
  693.                         } else {
  694.                             var popup_container = this_button.parent().closest('.et_social_popup_content');
  695.  
  696.                             this_button.parent().replaceWith(data);
  697.                             define_popup_position(popup_container);
  698.                             all_networks_opened = 0;
  699.                         }
  700.                     }
  701.                 });
  702.             }
  703.  
  704.             return false;
  705.         });
  706.  
  707.         set_mobile_sidebar_height();
  708.  
  709.         set_sidebar_position();
  710.  
  711.         $(window).resize(function() {
  712.             if ($('.et_social_resize').length) {
  713.                 $('.et_social_resize').each(function() {
  714.                     define_popup_position($(this));
  715.                 });
  716.             }
  717.             if ($('.et_social_mobile')) {
  718.                 set_mobile_sidebar_height();
  719.             }
  720.             if ($('.et_social_sidebar_networks').length) {
  721.                 set_sidebar_position();
  722.             }
  723.  
  724.             set_media_wrapper_size();
  725.         });
  726.  
  727.         $('.et_social_hide_sidebar').click(function() {
  728.             $('.et_social_hide_sidebar').toggleClass('et_social_hidden_sidebar');
  729.             $('.et_social_sidebar_networks').toggleClass('et_social_hidden_sidebar et_social_visible_sidebar');
  730.             if (__gaTracker)
  731.             // using Yoast Analytics with Universal GA;
  732.                 __gaTracker('send', 'event', 'Social_Sidebar', 'Hide_Sidebar', document.title);
  733.             else if (_gaq)
  734.             // using classic GA;
  735.                 _gaq('send', 'event', 'Social_Sidebar', 'Hide_Sidebar', document.title);
  736.             else if (_ga)
  737.             // using Universal GA;
  738.                 _ga('send', 'event', 'Social_Sidebar', 'Hide_Sidebar', document.title);
  739.         });
  740.  
  741.         $(window).load(function() {
  742.             set_media_wrapper_size();
  743.  
  744.             if ($('.et_social_pin_images').length && ($('.et_social_all_button').length || $('.et_social_pinterest').length)) {
  745.                 var pin_container = $('.et_social_pin_images'),
  746.                     permalink = pin_container.data('permalink'),
  747.                     title = pin_container.data('title'),
  748.                     post_id = pin_container.data('post_id'),
  749.                     $i = 0;
  750.  
  751.                 $('img').each(function() {
  752.                     //do not include comment avatars into the Modal
  753.                     if (!$(this).hasClass('avatar')) {
  754.                         var this_img = $(this).attr('src'),
  755.                             this_alt = $(this).attr('alt');
  756.  
  757.                         if ('' != this_img) {
  758.                             var pin_link = 'http://www.pinterest.com/pin/create/button/?url=' + permalink + '&media=' + this_img + '&description=' + title,
  759.                                 this_img_container = '<div class="et_social_pin_image"><a href="' + pin_link + '" rel="nofollow" class="et_social_icon et_social_share" data-social_name="pinterest" data-post_id="' + post_id + '" data-social_type="share"><img src="' + this_img + '" alt="' + this_alt + '"/><span class="et_social_pin_overlay et_social_icon"></span></a></div>';
  760.                             $('.et_social_pin_images').append(this_img_container);
  761.                             $i++;
  762.                         }
  763.                     }
  764.                 });
  765.  
  766.                 //Append error message if no images found on page
  767.                 if (0 == $i) {
  768.                     $('.et_social_pin_images').append(monarchSettings.no_img_message);
  769.                 }
  770.             }
  771.         });
  772.     });
  773. })(jQuery)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement