-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmain.js
234 lines (204 loc) · 7.87 KB
/
main.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
229
230
231
232
233
234
(function ($) {
var projectsSvc = new ProjectsService(projects),
compiledtemplateFn = null,
projectsPanel = null;
var getFilterUrl = function() {
return location.href.indexOf('/#/filters') > -1 ? location.href : location.href+ 'filters';
}
var renderProjects = function (tags, names, labels) {
projectsPanel.html(compiledtemplateFn({
"projects": projectsSvc.get(tags, names, labels),
"tags": projectsSvc.getTags(),
"popularTags": projectsSvc.getPopularTags(6),
"selectedTags": tags,
"names": projectsSvc.getNames(),
"selectedNames": names,
"labels": projectsSvc.getLabels(),
"selectedLabels": labels
}));
projectsPanel.find("select.tags-filter").chosen({
no_results_text: "No tags found by that name.",
width: "95%"
}).val(tags).trigger('chosen:updated').change(function (e) {
location.href = updateQueryStringParameter(getFilterUrl(), 'tags', encodeURIComponent(($(this).val() || "")));
});
projectsPanel.find("select.names-filter").chosen({
search_contains: true,
no_results_text: "No project found by that name.",
width: "95%"
}).val(names).trigger('chosen:updated').change(function (e) {
location.href = updateQueryStringParameter(getFilterUrl(), 'names', encodeURIComponent(($(this).val() || "")))
});
projectsPanel.find("select.labels-filter").chosen({
no_results_text: "No project found by that label.",
width: "95%"
}).val(labels).trigger('chosen:updated').change(function (e) {
location.href = updateQueryStringParameter(getFilterUrl(), 'labels', encodeURIComponent(($(this).val() || "")));
});
projectsPanel.find("ul.popular-tags").children().each(function(i, elem){
//elem.firstElementChild.innerText.toLowerCase()
console.log(elem);
$(elem).on("click", function(){
location.href = updateQueryStringParameter(
getFilterUrl(), 'tags', encodeURIComponent((preparePopTagName($(this).text()) || "")));
});
});
};
/*
This is a utility method to help update a list items Name parameter to make
it fit URL specification
@return string - The value of the Name
*/
var preparePopTagName = function(name) {
return name.toLowerCase().split(" ")[0];
}
/**
* This is a utility method to help update URL Query Parameters
* @return string - The value of the URL when adding/removing values to it.
*/
var updateQueryStringParameter = function(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
/**
* This function help getting all params in url queryString
* Taken from here
* https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
*
* @return string - value of url params
*/
var getParameterByName = function(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
};
/*
* This is a helper method that prepares the chosen labels/tags/names
* For HTML and helps display the selected values of each
* @params String text - The text given, indices or names. As long as it is a string
* @return Array - Returns an array of splitted values if given a text. Otherwise undefined
*/
var prepareForHTML = function(text) {
return text ? text.toLowerCase().split(',') : text;
}
var app = $.sammy(function () {
/*
* This is the route used to filter by tags/names/labels
* It ensures to read values from the URI query param and perform actions
* based on that. NOTE: It has major side effects on the browser.
*/
this.get('#/filters', function() {
var labels = prepareForHTML(getParameterByName('labels'));
var names = prepareForHTML(getParameterByName('names'));
var tags = prepareForHTML(getParameterByName('tags'));
renderProjects(tags, names, labels)
});
this.get("#/", function (context) {
renderProjects();
});
});
var storage = (function (global) {
function set(name, value) {
try {
if (typeof (global.localStorage) !== "undefined") {
global.localStorage.setItem(name, JSON.stringify(value));
}
} catch (exception) {
if ((exception != QUOTA_EXCEEDED_ERR) &&
(exception != NS_ERROR_DOM_QUOTA_REACHED)) {
throw exception;
}
}
};
function get(name) {
if (typeof (global.localStorage) !== "undefined") {
return JSON.parse(global.localStorage.getItem(name));
}
return undefined;
};
return {
set: set,
get: get
};
})(window);
var issueCount = function (project) {
var a = $(project).find('.label a'),
gh = a.attr('href').match(/github.com(\/[^\/]+\/[^\/]+\/)(?:issues\/)?labels\/([^\/]+)$/),
url = gh && ('https://api.github.com/repos' + gh[1] + 'issues?labels=' + gh[2]),
count = a.find('.count');
if (!!count.length) {
return;
}
if (!gh) {
count = $('<span class="count" title="Issue count is only available for projects on GitHub.">?</span>').appendTo(a);
return;
}
count = $('<span class="count"><img src="images/octocat-spinner-32.gif" /></span>').appendTo(a);
var cached = storage.get(gh[1]);
if (cached && cached.date && new Date(cached.date) >= (new Date() - 1000 * 60 * 60 * 24)) {
count.html(cached.count);
return;
}
$.ajax(url)
.done(function (data, textStatus, jqXHR) {
var resultCount = data && typeof data.length === 'number' ? data.length.toString() : '?';
count.html(resultCount);
storage.set(gh[1], {
"count": resultCount,
"date": new Date()
});
})
.fail(function (jqXHR, textStatus, errorThrown) {
var rateLimited = jqXHR.getResponseHeader('X-RateLimit-Remaining') === '0',
rateLimitReset = rateLimited && new Date(1000 * +jqXHR.getResponseHeader('X-RateLimit-Reset')),
message = rateLimitReset ? 'GitHub rate limit met. Reset at ' + rateLimitReset.toLocaleTimeString() + '.' :
'Could not get issue count from GitHub: ' + ((jqXHR.responseJSON && jqXHR.responseJSON.message) || errorThrown) + '.';
count.html('?!');
count.attr('title', message);
});
};
$(function () {
var $window = $(window),
onScreen = function onScreen($elem) {
var docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
return (docViewTop <= elemTop && elemTop <= docViewBottom) ||
(docViewTop <= elemBottom && elemBottom <= docViewBottom);
};
$window.on('scroll chosen:updated', function () {
$('.projects tbody:not(.counted)')
.each(function () {
var project = $(this);
if (onScreen(project)) {
issueCount(project);
project.addClass('counted');
}
});
});
compiledtemplateFn = _.template($("#projects-panel-template").html());
projectsPanel = $("#projects-panel");
projectsPanel.on("click", "a.remove-tag", function (e) {
e.preventDefault();
var tags = [];
projectsPanel.find("a.remove-tag").not(this).each(function () {
tags.push($(this).data("tag"));
});
var tagsString = tags.join(",");
window.location.href = "#/tags/" + tagsString;
});
app.raise_errors = true;
app.run("#/");
});
})(jQuery);