forked from twitter/opensource-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojects.js
229 lines (190 loc) · 7.62 KB
/
projects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Put custom repo GitHub URLs in this object, keyed by nameWithOwner repo name.
var customGithubURL = {
"twitter/pants": "https://github.com/pantsbuild/pants",
}
var getGithubURL = function(project) {
return customGithubURL[project.nameWithOwner] || 'https://github.com/' + project.nameWithOwner
}
// Put custom repo Website URLs in this object, keyed by nameWithOwner repo name
var customWebsiteURL = {
"twitter/pants": "https://www.pantsbuild.org/",
}
var getHomepageURL = function(project) {
return customWebsiteURL[project.nameWithOwner] || project.homepageURL
}
/* Create project cards */
var renderProjects = function(projectsList, searchString="") {
// Parent div to hold all the project cards
var mainDiv = document.getElementsByClassName("all-projects")[0]
// Refer this for DOM manipulation with JS https://stackoverflow.com/questions/14094697/how-to-create-new-div-dynamically-change-it-move-it-modify-it-in-every-way-po
if (projectsList.length > 0) {
for (var project of projectsList) {
// Div for each project
var projectDiv = document.createElement('div')
projectDiv.className = "Grid-cell u-size1of3 project-card"
// Project Name
var nameDiv = document.createElement('h1')
nameDiv.className = "project-name small-margin"
nameDiv.innerHTML = project.name
projectDiv.appendChild(nameDiv)
// Color-coded border
var colorDiv = document.createElement('div')
colorDiv.className = "border small-margin"
colorDiv.style = "border-bottom-color: " + project.color
projectDiv.appendChild(colorDiv)
// Project Description (HTML version)
var descriptionDiv = document.createElement('div')
descriptionDiv.className = "project-description xsmall-margin"
descriptionDiv.innerHTML = project.description
projectDiv.appendChild(descriptionDiv)
// Primary Language
var languageDiv = document.createElement('p')
languageDiv.className = "project-language"
languageDiv.innerHTML = project.primaryLanguage
projectDiv.appendChild(languageDiv)
// Whitespace
var whitespaceDiv = document.createElement('div')
whitespaceDiv.className = "whitespace"
projectDiv.appendChild(whitespaceDiv)
// Project Links
var projectLinksDiv = document.createElement('div')
projectLinksDiv.className = "project-links"
// GitHub link
var githubLink = document.createElement('a')
githubLink.href = getGithubURL(project)
githubLink.innerHTML = "GitHub"
githubLink.target = "_blank"
projectLinksDiv.appendChild(githubLink)
// Website link (with clause)
var homepageURL = getHomepageURL(project)
if (homepageURL != "") {
var websiteLink = document.createElement('a')
websiteLink.href = homepageURL
websiteLink.innerHTML = "Website"
websiteLink.target = "_blank"
projectLinksDiv.appendChild(websiteLink)
}
projectDiv.appendChild(projectLinksDiv)
// Metrics button
var metricsButton = document.createElement('button')
metricsButton.setAttribute("onclick", "window.open('https://opensource.twitter.com/metrics/" + project.nameWithOwner + "/WEEKLY')")
metricsButton.type = "button"
metricsButton.className = "Button Button--tertiary"
metricsButton.innerHTML = "Metrics"
projectDiv.appendChild(metricsButton)
/* Finally Add the project card to the page */
mainDiv.appendChild(projectDiv)
}
} else {
var noResultDiv = document.createElement('div')
noResultDiv.className = 'no-results'
var noResultPara = document.createElement('p')
noResultPara.innerHTML = "No results for " + '<b>' + searchString + '</b>'
noResultDiv.appendChild(noResultPara)
var noResultContainer = document.getElementsByClassName("no-results-container")[0]
noResultContainer.appendChild(noResultDiv)
}
// Apply functions that determine how many columns
if (matchMedia) {
var mq3 = window.matchMedia("(min-width: 1236px)")
var mq2 = window.matchMedia("(max-width: 1236px) and (min-width: 850px)")
var mq1 = window.matchMedia("(max-width: 850px)")
threeColumn(mq3)
twoColumn(mq2)
oneColumn(mq1)
}
}
// Sort the projects
var sortFunction = function(a, b) {
// Sort by recently pushedAt
var deltaA = (new Date) - Date.parse(a.pushedAt)
var deltaB = (new Date) - Date.parse(b.pushedAt)
return deltaA>=deltaB?1:-1
}
// Sort and Render
allProjects.sort(sortFunction)
renderProjects(allProjects)
/* Search implementation starts */
var searchResult = allProjects // Search Result initialization
function findMatches(query, repos) {
if (query === '') {
return repos
} else {
var options = {
findAllMatches: true,
threshold: 0.2,
location: 0,
distance: 50,
maxPatternLength: 50,
minMatchCharLength: 1,
keys: [
"name",
"languages",
"description"
]
}
var fuse = new Fuse(repos, options)
var result = fuse.search(query)
// Sort
result.sort(sortFunction)
return result
}
}
var searchBox = document.getElementsByClassName('search-box')[0]
document.addEventListener('keyup', function(event) {
/* Update the list of results with the search results */
var newProjectsList = []
var searchString = searchBox.value.trim()
searchResult = findMatches(searchString, allProjects)
// Remove all the projects
var mainDiv = document.getElementsByClassName("all-projects")[0]
while (mainDiv.firstChild) {
mainDiv.removeChild(mainDiv.firstChild)
}
var noResultContainer = document.getElementsByClassName("no-results-container")[0]
while (noResultContainer.firstChild) {
noResultContainer.removeChild(noResultContainer.firstChild)
}
for (var item of searchResult) {
newProjectsList.push(item)
}
renderProjects(newProjectsList, searchString=searchBox.value)
})
/* Search implementation ends */
// Media queries for projects grid
if (matchMedia) {
var mediaQueryThreeColumn = window.matchMedia("(min-width: 1236px)")
threeColumn(mediaQueryThreeColumn)
mediaQueryThreeColumn.addListener(threeColumn)
var mediaQueryTwoColumn = window.matchMedia("(max-width: 1236px) and (min-width: 850px)")
twoColumn(mediaQueryTwoColumn)
mediaQueryTwoColumn.addListener(twoColumn)
var mediaQueryOneColumn = window.matchMedia("(max-width: 850px)")
oneColumn(mediaQueryOneColumn)
mediaQueryOneColumn.addListener(oneColumn)
}
// 3 columns
function threeColumn(mediaQuery) {
if (mediaQuery.matches) {
addClassByClass("project-card", "u-size1of3")
removeClassByClass("project-card", "u-size1of2")
} else {
removeClassByClass("project-card", "u-size1of3")
}
}
// 2 columns
function twoColumn(mediaQuery) {
if (mediaQuery.matches) {
addClassByClass("project-card", "u-size1of2")
removeClassByClass("project-card", "u-size1of3")
} else {
removeClassByClass("project-card", "u-size1of2")
}
}
// 1 column
function oneColumn(mediaQuery) {
if (mediaQuery.matches) {
removeClassByClass("project-card", "u-size1of3")
removeClassByClass("project-card", "u-size1of2")
}
}