Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function () {
- $('.windows8').hide();
- $('#stats').html('');
- $.ajax({
- type: 'GET',
- url: '/mirkoplusy/api/v2/init'
- });
- //Checking whether ids are passed in url
- if (/wpis\/(\d+)\/?(\d+)?/g.exec(window.location.href)) {
- if (isParameterValid(RegExp.$1) && isParameterValid(RegExp.$2)) fetchData('comment/' + RegExp.$1 + '/' + RegExp.$2);
- if (isParameterValid(RegExp.$1) && !isParameterValid(RegExp.$2)) fetchData('entry/' + RegExp.$1);
- }
- //When user presses enter we call click action
- $("body").keyup(function (event) {
- if (event.keyCode == 13) $("#btn").click();
- });
- //On click proper action should be taken
- $("#btn").click(function () {
- takeAction($("#url").val().trim());
- });
- });
- //Checking whether given Ids from url are valid
- function isParameterValid(param) {
- return null !== param && param.length > 0 && !isNaN(param);
- }
- //Deciding whether url points to a comment or to an entry
- function takeAction(url) {
- if (/wpis\/(\d+)(\/(?!#comment)|$)/g.exec(url)) //whether url points to a comment
- return fetchData('comment/' + RegExp.$1 + '/' + RegExp.$2);
- if (/wpis\/(\d+)\/#comment-(\d+)$/g.exec(url)) //whether url points to an entry
- return fetchData('entry/' + RegExp.$1);
- if (!isNaN(url) && url.length != 0) //when user enters id
- return fetchData('entry/' + url);
- //If not matches any of above we send following message
- $('#stats').html("Wprowadź adres lub identyfiktor");
- $('.nicks').html('');
- }
- //Clearing outputs and showing loading spinner
- function prepareToFetch() {
- $('.windows8').show();
- $('#stats').html('');
- $('.nicks').html('');
- }
- //Main function, calls api
- function fetchData(param) {
- prepareToFetch();
- $.ajax({
- type: 'GET',
- url: '/mirkoplusy/api/v2/' + param,
- success: function (response) {
- var data = JSON.parse(response);
- if (data.success == "ok") showStats(data); //data is valid
- else $('#stats').html(data.message); //showing errors
- $('.windows8').hide(); //hiding loading spinner
- },
- error: function () {
- $('#stats').html("Wystąpił nieprzewidziany błąd");
- $('.windows8').hide(); //hiding loading spinner
- },
- timeout: 15000
- });
- }
- function listVotersNames(voters) {
- var list = '';
- voters.forEach(function (name) {
- list += "<a href=\"http://wykop.pl/ludzie/" + name + "/\">" + name + "</a>, ";
- });
- return list;
- }
- function calcPercent(amount, votes) {
- if (votes === 0)
- return "0%";
- else
- return (Math.round(amount / votes * 100 * 100) / 100) + "%";
- }
- //Displaying received data in readable format
- function showStats(data) {
- //setting new url address
- if (/^(.+)mirkoplusy/g.exec(window.location.href))
- window.history.pushState('', '', RegExp.$1 + 'mirkoplusy/wpis/' + data['urlToSet']);
- //setting returned url to the input; e.g. when user enters id it will change to url
- $("#url").val(data['entryUrl']);
- var voters = data['voters'];
- var votesCount = {
- 'men': voters.men.length,
- 'women': voters.women.length,
- 'unknown': voters.unknown.length
- };
- votesCount.total = votesCount.men + votesCount.women + votesCount.unknown;
- var stats = "<p>" + data.author + "</p>";
- stats += "<p>Liczba plusów: " + votesCount.total + "</p>";
- stats += "<p>Niebieskich pasków: " + votesCount.men + ", " + calcPercent(votesCount.men, votesCount.total) + "</p>";
- stats += "<p>Różowych pasków: " + votesCount.women + ", " + calcPercent(votesCount.women, votesCount.total) + "</p>";
- stats += "<p>Niezdefiniowanych: " + votesCount.unknown + ", " + calcPercent(votesCount.unknown, votesCount.total) + "</p>";
- $("#stats").html(stats);
- if (voters.men.length > 0) {
- var men = "<p>Niebiescy:<br>" + listVotersNames(voters.men) + "</p>";
- $("#men").html(men);
- }
- if (voters.women.length > 0) {
- var women = "<p>Różowe:<br>" + listVotersNames(voters.women) + "</p>";
- $("#women").html(women);
- }
- if (voters.unknown.length > 0) {
- var unknown = "<p>Niezdefiniowani:<br>" + listVotersNames(voters.unknown) + "</p>";
- $("#unknown").html(unknown);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment