Matt23

Mirkoplusy code

Jun 1st, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function () {
  2.     $('.windows8').hide();
  3.     $('#stats').html('');
  4.  
  5.     $.ajax({
  6.         type: 'GET',
  7.         url: '/mirkoplusy/api/v2/init'
  8.     });
  9.  
  10.     //Checking whether ids are passed in url
  11.     if (/wpis\/(\d+)\/?(\d+)?/g.exec(window.location.href)) {
  12.         if (isParameterValid(RegExp.$1) && isParameterValid(RegExp.$2)) fetchData('comment/' + RegExp.$1 + '/' + RegExp.$2);
  13.         if (isParameterValid(RegExp.$1) && !isParameterValid(RegExp.$2)) fetchData('entry/' + RegExp.$1);
  14.     }
  15.  
  16.     //When user presses enter we call click action
  17.     $("body").keyup(function (event) {
  18.         if (event.keyCode == 13) $("#btn").click();
  19.     });
  20.     //On click proper action should be taken
  21.     $("#btn").click(function () {
  22.         takeAction($("#url").val().trim());
  23.     });
  24. });
  25.  
  26. //Checking whether given Ids from url are valid
  27. function isParameterValid(param) {
  28.     return null !== param && param.length > 0 && !isNaN(param);
  29. }
  30.  
  31. //Deciding whether url points to a comment or to an entry
  32. function takeAction(url) {
  33.     if (/wpis\/(\d+)(\/(?!#comment)|$)/g.exec(url)) //whether url points to a comment
  34.         return fetchData('comment/' + RegExp.$1 + '/' + RegExp.$2);
  35.  
  36.     if (/wpis\/(\d+)\/#comment-(\d+)$/g.exec(url))  //whether url points to an entry
  37.         return fetchData('entry/' + RegExp.$1);
  38.  
  39.     if (!isNaN(url) && url.length != 0) //when user enters id
  40.         return fetchData('entry/' + url);
  41.  
  42.     //If not matches any of above we send following message
  43.     $('#stats').html("Wprowadź adres lub identyfiktor");
  44.     $('.nicks').html('');
  45. }
  46.  
  47. //Clearing outputs and showing loading spinner
  48. function prepareToFetch() {
  49.     $('.windows8').show();
  50.     $('#stats').html('');
  51.     $('.nicks').html('');
  52. }
  53.  
  54. //Main function, calls api
  55. function fetchData(param) {
  56.     prepareToFetch();
  57.  
  58.     $.ajax({
  59.         type: 'GET',
  60.         url: '/mirkoplusy/api/v2/' + param,
  61.         success: function (response) {
  62.             var data = JSON.parse(response);
  63.  
  64.             if (data.success == "ok") showStats(data); //data is valid
  65.             else $('#stats').html(data.message); //showing errors
  66.  
  67.             $('.windows8').hide(); //hiding loading spinner
  68.         },
  69.         error: function () {
  70.             $('#stats').html("Wystąpił nieprzewidziany błąd");
  71.             $('.windows8').hide(); //hiding loading spinner
  72.         },
  73.         timeout: 15000
  74.     });
  75. }
  76.  
  77. function listVotersNames(voters) {
  78.     var list = '';
  79.     voters.forEach(function (name) {
  80.         list += "<a href=\"http://wykop.pl/ludzie/" + name + "/\">" + name + "</a>, ";
  81.     });
  82.     return list;
  83. }
  84.  
  85. function calcPercent(amount, votes) {
  86.     if (votes === 0)
  87.         return "0%";
  88.     else
  89.         return (Math.round(amount / votes * 100 * 100) / 100) + "%";
  90. }
  91.  
  92. //Displaying received data in readable format
  93. function showStats(data) {
  94.     //setting new url address
  95.     if (/^(.+)mirkoplusy/g.exec(window.location.href))
  96.         window.history.pushState('', '', RegExp.$1 + 'mirkoplusy/wpis/' + data['urlToSet']);
  97.  
  98.     //setting returned url to the input; e.g. when user enters id it will change to url
  99.     $("#url").val(data['entryUrl']);
  100.  
  101.     var voters = data['voters'];
  102.     var votesCount = {
  103.         'men': voters.men.length,
  104.         'women': voters.women.length,
  105.         'unknown': voters.unknown.length
  106.     };
  107.     votesCount.total = votesCount.men + votesCount.women + votesCount.unknown;
  108.  
  109.     var stats = "<p>" + data.author + "</p>";
  110.     stats += "<p>Liczba plusów: " + votesCount.total + "</p>";
  111.     stats += "<p>Niebieskich pasków: " + votesCount.men + ", " + calcPercent(votesCount.men, votesCount.total) + "</p>";
  112.     stats += "<p>Różowych pasków: " + votesCount.women + ", " + calcPercent(votesCount.women, votesCount.total) + "</p>";
  113.     stats += "<p>Niezdefiniowanych: " + votesCount.unknown + ", " + calcPercent(votesCount.unknown, votesCount.total) + "</p>";
  114.     $("#stats").html(stats);
  115.  
  116.     if (voters.men.length > 0) {
  117.         var men = "<p>Niebiescy:<br>" + listVotersNames(voters.men) + "</p>";
  118.         $("#men").html(men);
  119.     }
  120.     if (voters.women.length > 0) {
  121.         var women = "<p>Różowe:<br>" + listVotersNames(voters.women) + "</p>";
  122.         $("#women").html(women);
  123.     }
  124.     if (voters.unknown.length > 0) {
  125.         var unknown = "<p>Niezdefiniowani:<br>" + listVotersNames(voters.unknown) + "</p>";
  126.         $("#unknown").html(unknown);
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment