Advertisement
DmitriyKim

Stats script

May 20th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.90 KB | None | 0 0
  1.  
  2.  
  3. (function($) {
  4.  
  5. $(document).ready (
  6.    function() {
  7.    
  8.       //-----------------------------------------
  9.       var tab = $('#stats_table');
  10.      
  11.       tab.data('hidden',true);
  12.       tab.data('stats',true);
  13.       tab.find('tr:not(:eq(0))').each(
  14.          function() {
  15.          
  16.             var pos = 0;
  17.             var green_val=230;
  18.             var red_val = 230;
  19.             var prev_val = -1;
  20.             var els=$(this).find('td:gt(1):not(:last):not(.dop_stat)');
  21.             var len =els.length;
  22.            
  23.             els.filter(function(index) {
  24.                return index<len-2;
  25.             })
  26.            
  27.             .each(
  28.                function () {
  29.                
  30.                   var value =parseInt($(this).html());
  31.                   pos += value;
  32.                  
  33.                   if (prev_val!=-1) {
  34.                      if (((value<prev_val) && (value!=0)) || ((value>prev_val) && (prev_val==0))) {
  35.                         $(this).css('background-color', 'rgb(210,'+green_val+',210)');
  36.                         green_val +=15;
  37.                      }         
  38.                      
  39.                      if (((value>prev_val) && (prev_val!=0)) || ((value==0) && (prev_val!=0))) {
  40.                         $(this).css('background-color','rgb('+red_val+',210,210)');
  41.                         red_val +=15;
  42.                      }
  43.                      
  44.                   }
  45.                   prev_val =value;
  46.                  
  47.                   if ((value<20) && (value>0)) {
  48.                      $(this).css({color:'#c00',fontWeight:'bold'});
  49.                      $(this).parent().children(':eq(1)').css({fontWeight:600})
  50.                      
  51.                   }
  52.                }
  53.             );         
  54.            
  55.             if (pos==0) {
  56.                $(this).attr('data-zero','zero')
  57.                $(this).hide();
  58.             }
  59.          }
  60.       )
  61.       //-----------------------------------------
  62.       //          show / hide zeros
  63.       //-----------------------------------------
  64.       $('#show_hide_zeros').click (
  65.          function() {
  66.             if (tab.data('hidden')) {
  67.                tab.data('hidden',false);
  68.                tab.find('tr[data-zero="zero"]').show();
  69.                $(this).html('Hide zeros');
  70.                
  71.             } else {
  72.                tab.data('hidden',true);
  73.                tab.find('tr[data-zero="zero"]').hide();
  74.                $(this).html('Show zeros');
  75.             }
  76.          }
  77.       )
  78.      
  79.       var  not_sorting_columns = {};
  80.       tab.find('tr:eq(0) th').each (
  81.          function(index) {
  82.             if ($(this).hasClass('dop_stat')) {
  83.                not_sorting_columns[index] = {sorter:false};
  84.             }
  85.          }
  86.       )
  87.      
  88.       //-----------------------------------------    
  89.       //          show / hide stats
  90.       //-----------------------------------------
  91.       $('#show_hide_stats').click (
  92.          function() {
  93.             if (tab.data('stats')) {
  94.                tab.data('stats',false);
  95.                
  96.                tab.find('.dop_stat').hide();
  97.                tab.find('thead tr:lt(3)').hide();
  98.                $(this).html('Show stats');
  99.             } else {
  100.                
  101.                tab.data('stats',true);
  102.                tab.find('.dop_stat').show();
  103.                tab.find('thead tr:lt(3)').show();
  104.                $(this).html('Hide stats');
  105.             }
  106.          }
  107.       )
  108.      
  109.       //-----------------------------------------
  110.       //     switch  yandex/google positions
  111.       //-----------------------------------------    
  112.       $('#yandex_google').change(
  113.          function () {
  114.             var proto = location.protocol;
  115.             var hostname = location.hostname;
  116.             var pathname = location.pathname;
  117.             var search = location.search;
  118.            
  119.             var re=/&se=.*?($|&)/g;
  120.             search = search.replace(re,'');
  121.            
  122.             var new_location = proto+'//'+hostname+pathname+search+'&se='+this.value;
  123.             location.replace(new_location);
  124.            
  125.            
  126.          }
  127.       )
  128.                  
  129.       //-----------------------------------------
  130.       //                  sort
  131.       //-----------------------------------------
  132.       tab.tablesorter({
  133.          headers: {
  134.             0: {sorter:false}
  135.          }
  136.          
  137.       });
  138.      
  139.       //-----------------------------------------
  140.       //                resizing
  141.       //-----------------------------------------
  142.       tab.find("th").resizable({
  143.          handles: "e",
  144.          helper: "ui-resizable-helper",
  145.      
  146.          start: function(event, ui) {          
  147.          },
  148.  
  149.         stop: function(event, ui) {    
  150.                            
  151.            if ($(this).index()!=1)
  152.               return;
  153.                          
  154.            var els = tab.find('tr td:nth-child(2) .td_out_query');                 
  155.          
  156.            els.css('width',ui.size.width).parent().css('width',ui.size.width);
  157.        
  158.            // height must be set in order to prevent IE9 to set wrong height
  159.            $(this).css("height", "auto");
  160.         }
  161.       });
  162.      
  163.       //-----------------------------------------
  164.       //                 tooltips
  165.       //-----------------------------------------
  166.       tab.tooltip({
  167.          items: 'td:not(:first-child):not([title^="none"])',
  168.          
  169.          show: {
  170.             effect: "fade",
  171.             duration: 100,
  172.             easing: 'easeOutCirc'
  173.          },
  174.          
  175.          hide: {       
  176.             delay: 0,
  177.             duration: 0
  178.          },
  179.          
  180.          content: function() {
  181.          
  182.             var element = $(this);
  183.             return element.attr('title');
  184.            
  185.          },
  186.          
  187.          position: {         
  188.            my: "center bottom-40",
  189.            at: "center top",
  190.            
  191.             using: function( position, feedback ) {
  192.              $( this ).css( position );
  193.              $( "<div>" )
  194.                .addClass( "arrow" )
  195.                .addClass( feedback.vertical )
  196.                .addClass( feedback.horizontal )
  197.                .appendTo( this );
  198.                }
  199.            }
  200.       });
  201.       //-------------------------------  
  202.       tab.find('tr:eq(0)').tooltip({
  203.          items: 'th.dop_stat',
  204.          
  205.          show: {
  206.             effect: "fade",
  207.             duration: 100,
  208.             easing: "easeOutCirc"
  209.          },
  210.          
  211.          hide: {
  212.             delay: 0,
  213.             duration: 0
  214.          },
  215.          
  216.          content: function() {
  217.            
  218.             return $(this).children('.th_title').text();
  219.          },
  220.          
  221.          position: {
  222.             my: "center bottom-20",
  223.             at: "center top"
  224.          }
  225.       })
  226.      
  227.      
  228.       //-----------------------------------------
  229.       //       extra rows in header
  230.       //-----------------------------------------
  231.       var spancols = tab.find('th.dop_stat').length+2-3;
  232.       var poscols =tab.find('th').length - spancols-3-3;
  233.       var header=tab.children('thead');
  234.      
  235.       for (var j=0; j<3; j++) {
  236.          var tr = $('<tr/>');
  237.          if (j==2) {
  238.             tr.append($('<th/>').attr('colspan',spancols).attr('rowspan',3));
  239.          }
  240.          tr.append($('<th/>').attr('colspan',3));
  241.          for (var i=0;i<poscols;i++) {
  242.             tr.append($('<th/>'));
  243.          }
  244.          if (j==2)
  245.             tr.append($('<th/>').attr('colspan',3).attr('rowspan',3));
  246.            
  247.          header.prepend(tr);
  248.       }
  249.      
  250.       var head = header.get(0);
  251.       head.rows[0].cells[1].innerHTML = 'TOP 10';
  252.       head.rows[1].cells[0].innerHTML = 'TOP 100';
  253.       head.rows[2].cells[0].innerHTML = 'Видимость';
  254.      
  255.       var top10;
  256.       var top100;
  257.       var all;
  258.       var value;
  259.       for (i=0; i<poscols; i++) {
  260.                
  261.          top10 = 0;
  262.          top100 = 0;
  263.          all = 0;
  264.          
  265.          tab.find('td:nth-child('+(spancols+3+i+1)+')').each(
  266.             function() {
  267.                value = parseInt($(this).html());
  268.                if ((value>0) && (value<=10)) {
  269.                   top10++;
  270.                }
  271.                if ((value>0) && (value<=100)) {
  272.                   top100++;
  273.                }
  274.                all++;
  275.             }
  276.          );
  277.          head.rows[0].cells[i+2].innerHTML = top10;
  278.          head.rows[1].cells[i+1].innerHTML = top100;
  279.          head.rows[2].cells[i+1].innerHTML = Math.round(top100/all*100)+' %';
  280.       }
  281.      
  282.      
  283.    }
  284. )
  285.  
  286. })(jQuery)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement