@@ -33,46 +33,83 @@ trait Author extends js.Object:
33
33
trait CommitTop extends js.Object :
34
34
def commit : CommitBottom
35
35
def author : Author
36
+ def url : String
36
37
37
38
trait Commits extends js.Array [CommitTop ]
38
39
40
+ trait CommitDescription extends js.Object :
41
+ def files : js.Array [FileChange ]
42
+
43
+ trait FileChange extends js.Object :
44
+ def filename : String
45
+ def status : String
46
+ def previous_filename : String
47
+
39
48
class ContentContributors :
49
+ val indenticonsUrl = " https://github.com/identicons"
50
+ def linkForFilename (filename : String ) = Globals .githubContributorsUrl + s " /commits?path= $filename"
51
+ def getAuthorsForFilename (filename : String ): Future [List [FullAuthor ]] = {
52
+ val link = linkForFilename(filename)
53
+ Ajax .get(link).map(_.responseText).flatMap { json =>
54
+ val res = JSON .parse(json).asInstanceOf [Commits ]
55
+ val authors = res.map { commit =>
56
+ commit.author match
57
+ case null =>
58
+ FullAuthor (commit.commit.author.name, " " , s " $indenticonsUrl/ ${commit.commit.author.name}.png " )
59
+ case author =>
60
+ FullAuthor (author.login, author.html_url, author.avatar_url)
61
+ }
62
+ val lastCommit = res.lastOption
63
+ val lastCommitDescriptionLink = lastCommit.map(_.url)
64
+ val previousFilename = lastCommitDescriptionLink
65
+ .fold(Future .successful(None )) { link =>
66
+ findRename(link, filename)
67
+ }
68
+ val previousAuthors = previousFilename.flatMap {
69
+ case Some (filename) => getAuthorsForFilename(filename)
70
+ case None => Future .successful(List .empty)
71
+ }.fallbackTo(Future .successful(List .empty))
72
+
73
+ previousAuthors.map(_ ++ authors).map(_.distinct)
74
+ }
75
+ }
76
+ def findRename (link : String , filename : String ): Future [Option [String ]] = {
77
+ Ajax .get(link).map(_.responseText).map { json =>
78
+ val res = JSON .parse(json).asInstanceOf [CommitDescription ]
79
+ val files = res.files
80
+ files
81
+ .find(_.filename == filename)
82
+ .filter(_.status == " renamed" )
83
+ .map(_.previous_filename)
84
+ }
85
+ }
40
86
document.addEventListener(" DOMContentLoaded" , (e : Event ) => {
41
- val indenticonsUrl = " https://github.com/identicons"
42
- js.typeOf(Globals .githubContributorsUrl) match
43
- case " undefined" =>
44
- // don't do anything
45
- case url =>
46
- val request : Future [String ] = Ajax .get(Globals .githubContributorsUrl).map(_.responseText)
47
- request.onComplete {
48
- case Success (json : String ) =>
49
- val res = JSON .parse(json).asInstanceOf [Commits ]
50
- val authors = res.map { commit =>
51
- commit.author match
52
- case null =>
53
- FullAuthor (commit.commit.author.name, " " , s " $indenticonsUrl/ ${commit.commit.author.name}.png " )
54
- case author =>
55
- FullAuthor (author.login, author.html_url, author.avatar_url)
56
- }.distinct
57
- val maybeDiv = Option (document.getElementById(" documentation-contributors" ))
58
- maybeDiv.foreach { div =>
59
- authors.foreach { case FullAuthor (name, url, img) =>
60
- val divN = document.createElement(" div" )
61
- val imgN = document.createElement(" img" ).asInstanceOf [html.Image ]
62
- imgN.src = img
63
- val autN = document.createElement(" a" ).asInstanceOf [html.Anchor ]
64
- autN.href = url
65
- autN.text = name
66
- divN.appendChild(imgN)
67
- divN.appendChild(autN)
68
- div.appendChild(divN)
69
- }
70
-
71
- if authors.nonEmpty then
72
- div.asInstanceOf [html.Div ].parentElement.classList.toggle(" hidden" )
73
- }
74
- case Failure (_) =>
75
- println(s " Couldn't fetch contributors for ${Globals .githubContributorsUrl}" )
76
- }
87
+ if js.typeOf(Globals .githubContributorsUrl) != " undefined" &&
88
+ js.typeOf(Globals .githubContributorsFilename) != " undefined"
89
+ then {
90
+ getAuthorsForFilename(Globals .githubContributorsFilename.stripPrefix(" /" )).onComplete {
91
+ case Success (authors) =>
92
+ val maybeDiv = Option (document.getElementById(" documentation-contributors" ))
93
+ maybeDiv.foreach { div =>
94
+ authors.foreach { case FullAuthor (name, url, img) =>
95
+ val divN = document.createElement(" div" )
96
+ val imgN = document.createElement(" img" ).asInstanceOf [html.Image ]
97
+ imgN.src = img
98
+ val autN = document.createElement(" a" ).asInstanceOf [html.Anchor ]
99
+ autN.href = url
100
+ autN.text = name
101
+ divN.appendChild(imgN)
102
+ divN.appendChild(autN)
103
+ div.appendChild(divN)
104
+ }
105
+
106
+ if authors.nonEmpty then
107
+ div.asInstanceOf [html.Div ].parentElement.classList.toggle(" hidden" )
108
+ }
109
+ case Failure (err) =>
110
+ println(s " Couldn't fetch contributors. $err" )
111
+ None
112
+ }
113
+ }
77
114
})
78
115
0 commit comments