|
1 |
| -require(['gitbook'], function(gitbook) { |
2 |
| - |
3 |
| - var githubOwner = ""; |
4 |
| - var backgroundColor; |
5 |
| - var githubRepository = ""; |
6 |
| - var spinner = "<div class='spinner'><div class='bounce1'></div><div class='bounce2'></div><div class='bounce3'></div></div>"; |
7 |
| - |
8 |
| - |
9 |
| - var loadAndShowContributors = function(contributorsSection){ |
10 |
| - var settings = { "async": true, |
11 |
| - "crossDomain": true, |
12 |
| - "url": "https://api.github.com/repos/sta-szek/pojo-tester/stats/contributors", |
13 |
| - "method": "GET" |
14 |
| - } |
15 |
| - |
16 |
| - $.ajax(settings).done(function (response) { |
17 |
| - removeSpinner(contributorsSection); |
18 |
| - |
19 |
| - var users = $(contributorsSection); |
20 |
| - $.each(response, function(index,value){ |
21 |
| - var userContent = createUserContent(value); |
22 |
| - $(users).append(userContent); |
23 |
| - }); |
24 |
| - $(users).children().sort(function(a,b) { |
25 |
| - return a.children[0].dataset.weight > b.children[0].dataset.weight; |
26 |
| - }).appendTo(contributorsSection); |
27 |
| - }); |
| 1 | +require(['gitbook'], function (gitbook) { |
28 | 2 |
|
29 |
| - } |
| 3 | + var githubOwner = ""; |
| 4 | + var backgroundColor = $(".book-summary").css("background-color"); |
| 5 | + |
| 6 | + var githubRepository = ""; |
| 7 | + var spinner = "<div class='spinner'><div class='bounce1'></div><div class='bounce2'></div><div class='bounce3'></div></div>"; |
| 8 | + |
| 9 | + |
| 10 | + var changeBackgroundForContributors = function () { |
| 11 | + $(".contributor").each(function (index, element) { |
| 12 | + $(element).css('background-color', backgroundColor); |
| 13 | + }); |
| 14 | + }; |
| 15 | + |
| 16 | + function listenForThemeChanges() { |
| 17 | + var $div = $(".book"); |
| 18 | + var observer = new MutationObserver(function (mutations) { |
| 19 | + mutations.forEach(function (mutation) { |
| 20 | + if (mutation.attributeName === "class") { |
| 21 | + backgroundColor = $(".book-summary").css("background-color"); |
| 22 | + changeBackgroundForContributors(); |
| 23 | + } |
| 24 | + }); |
| 25 | + }); |
| 26 | + observer.observe($div[0], { |
| 27 | + attributes: true |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + var loadAndShowContributors = function (contributorsSection) { |
| 32 | + var settings = { |
| 33 | + "async": true, |
| 34 | + "crossDomain": true, |
| 35 | + "url": "https://api.github.com/repos/sta-szek/pojo-tester/stats/contributors", |
| 36 | + "method": "GET" |
| 37 | + } |
| 38 | + |
| 39 | + $.ajax(settings).done(function (response) { |
| 40 | + resetContributorsContent(contributorsSection); |
| 41 | + |
| 42 | + var users = $(contributorsSection); |
| 43 | + $.each(response, function (index, value) { |
| 44 | + var userContent = createUserContent(value); |
| 45 | + $(users).append(userContent); |
| 46 | + }); |
| 47 | + $(users).children().sort(function (a, b) { |
| 48 | + return a.children[0].dataset.weight > b.children[0].dataset.weight; |
| 49 | + }).appendTo(contributorsSection); |
| 50 | + }); |
| 51 | + |
| 52 | + listenForThemeChanges(); |
30 | 53 |
|
31 |
| - var createUserContent = function(githubContribution){ |
32 |
| - var additions = 0; |
33 |
| - var deletions = 0; |
34 |
| - $.each(githubContribution.weeks, function(){ |
35 |
| - additions+=this.a; |
36 |
| - deletions+=this.d; |
37 |
| - }); |
38 |
| - return $("<div class='contributor'></div>") |
39 |
| - .css('background-color', backgroundColor) |
40 |
| - .append("<div class='contributor-avatar' data-weight="+additions+"><img src='"+githubContribution.author.avatar_url+"'/></div>") |
41 |
| - .append("<div class='contributor-data'><a href=''"+githubContribution.author.url+"'>"+githubContribution.author.login+"</a></br>additions "+additions+"</br>deletions "+deletions+"</div>"); |
42 |
| - } |
43 |
| - |
44 |
| - var removeSpinner = function(contributorsSection){ |
45 |
| - $(contributorsSection).html(""); |
46 |
| - } |
47 |
| - |
48 |
| - var showSpinner = function(contributorsSection){ |
49 |
| - $(contributorsSection).html(spinner); |
50 |
| - } |
51 |
| - |
52 |
| - gitbook.events.bind("page.change", function(event){ |
53 |
| - var contributorsSection = $("#GitHubContributors"); |
54 |
| - if(contributorsSection.length != 0){ |
55 |
| - backgroundColor= $($('.book-summary')[0]).css('backgroundColor'); |
56 |
| - showSpinner(contributorsSection); |
57 |
| - loadAndShowContributors(contributorsSection); |
58 | 54 | }
|
59 |
| - }); |
60 | 55 |
|
61 |
| - gitbook.events.bind('start', function(e, config) { |
62 |
| - githubOwner = config.githubcontributors.githubOwner; |
63 |
| - githubRepository = config.githubcontributors.githubRepository; |
64 |
| - }); |
| 56 | + var createUserContent = function (githubContribution) { |
| 57 | + var additions = 0; |
| 58 | + var deletions = 0; |
| 59 | + $.each(githubContribution.weeks, function () { |
| 60 | + additions += this.a; |
| 61 | + deletions += this.d; |
| 62 | + }); |
| 63 | + return $("<div class='contributor'></div>").css('background-color', backgroundColor) |
| 64 | + .append("<div class='contributor-avatar' data-weight=" + additions + "><img src='" + githubContribution.author.avatar_url + "'/></div>") |
| 65 | + .append("<div class='contributor-data'><a href='" + githubContribution.author.html_url + "'>" + githubContribution.author.login + "</a><div class='contributor-additions'> " + format(additions) + " ++</div><div class='contributor-deletions'>" + format(deletions) + " --</div></div>"); |
| 66 | + } |
| 67 | + |
| 68 | + var resetContributorsContent = function (contributorsSection) { |
| 69 | + $(contributorsSection).html(""); |
| 70 | + } |
| 71 | + |
| 72 | + var showSpinner = function (contributorsSection) { |
| 73 | + $(contributorsSection).html(spinner); |
| 74 | + } |
| 75 | + |
| 76 | + var format = function (number) { |
| 77 | + var fixed = number.toFixed(1).replace(/(\d)(?=(\d{3})+\.)/g, "$1,"); |
| 78 | + return fixed.substring(0, fixed.length - 2); |
| 79 | + } |
| 80 | + |
| 81 | + gitbook.events.bind("page.change", function (event) { |
| 82 | + var contributorsSection = $("#GitHubContributors"); |
| 83 | + if (contributorsSection.length != 0) { |
| 84 | + backgroundColor = $($('.book-summary')[0]).css('backgroundColor'); |
| 85 | + showSpinner(contributorsSection); |
| 86 | + loadAndShowContributors(contributorsSection); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + gitbook.events.bind('start', function (e, config) { |
| 91 | + githubOwner = config.githubcontributors.githubOwner; |
| 92 | + githubRepository = config.githubcontributors.githubRepository; |
| 93 | + }); |
65 | 94 |
|
66 | 95 | });
|
0 commit comments