-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-timeline.js
152 lines (123 loc) · 4.53 KB
/
github-timeline.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
// ==UserScript==
// @name Github Timeline
// @namespace https://github.com/romqin/github-timeline
// @description Filter your github timeline
// @include https://github.com/
// @version 1.2
// @updateURL https://rawgit.com/romqin/github-timeline/master/github-timeline.js
// @run-at document-end
// @grant none
// ==/UserScript==
// Main class
githubTimeline = {
css: {
dashboard: 'div.container div#dashboard div.news',
menu: {
base: 'div#github-timeline-menu',
child: 'a'
},
getMenu: function() {
'use strict';
return this.dashboard + ' ' + this.menu.base;
},
getItems: function() {
'use strict';
return this.getMenu() + ' ' + this.menu.child;
},
getItemSelected: function() {
'use strict';
return this.getItems() + '.selected';
}
},
template: {
menu: '' +
'<div id="github-timeline-menu" class="issues-list-options">' +
' <div class="btn-group">' +
' <a class="btn btn-sm selected">All</a>' +
' <a class="btn btn-sm">Pull Requests</a>' +
' <a class="btn btn-sm">Issues</a>' +
' <a class="btn btn-sm">Stars</a>' +
' <a class="btn btn-sm">Others</a>' +
' </div>' +
'</div>'
},
menu: {
select: function(child) {
'use strict';
$(githubTimeline.css.getItems() + '.selected').removeClass('selected');
child.addClass('selected');
setCookie("ghtl-menu", child.html());
githubTimeline.dashboard.refresh();
}
},
dashboard: {
refresh: function() {
'use strict';
var $button = $(githubTimeline.css.getItemSelected());
$dashboard.find('.alert').each(function(index, line) {
var $line = $(line);
$line.css('display', 'none');
var issueRegexp = new RegExp(' issue ', 'g');
var pullRequestRegexp = new RegExp(' pull request ', 'g');
switch ($button.html()) {
case 'All':
$line.css('display', 'block');
break;
case 'Pull Requests':
if (pullRequestRegexp.test($line.find('div.title').html())) {
$line.css('display', 'block');
}
break;
case 'Issues':
if (issueRegexp.test($line.find('div.title').html())) {
$line.css('display', 'block');
}
break;
case 'Stars':
if ($line.hasClass('watch_started')) {
$line.css('display', 'block');
}
break;
case 'Others':
if (!pullRequestRegexp.test($line.find('div.title').html()) && !issueRegexp.test($line.find('div.title').html()) && !$line.hasClass('watch_started')) {
$line.css('display', 'block');
}
break;
}
});
}
}
};
// Cookie
function setCookie(sName, sValue)
{
var today = new Date(), expires = new Date();
expires.setTime(today.getTime() + (365*24*60*60*1000));
document.cookie = sName + "=" + encodeURIComponent(sValue) + ";expires=" + expires.toGMTString();
}
function getCookie(sName) {
var oRegex = new RegExp("(?:; )?" + sName + "=([^;]*);?");
if (oRegex.test(document.cookie)) {
return decodeURIComponent(RegExp["$1"]);
}
return null;
}
// Workflow
window.addEventListener('load', function() {
$dashboard = $(githubTimeline.css.dashboard);
$(githubTimeline.template.menu).prependTo($dashboard);
$menu = $(githubTimeline.css.getMenu());
$(document).on('click', githubTimeline.css.getItems(), function() {
githubTimeline.menu.select($(this));
});
var menuSelected = getCookie("ghtl-menu");
if (menuSelected != null) {
$(githubTimeline.css.getItems()).parent().find('a:contains("' + menuSelected + '")').trigger('click');
}
$(document).ajaxComplete(function(event, request, options) {
var urlMore = /\/dashboard\/index\/\d+/g
if (urlMore.test(options.url)) {
githubTimeline.dashboard.refresh();
}
});
});