This repository has been archived by the owner on Jan 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.github-voice.js
161 lines (134 loc) · 4.73 KB
/
jquery.github-voice.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
(function($) {
$.fn.githubVoice = function(user, repository, options) {
var githubVoice = $.fn.githubVoice,
options = $.extend(true, {}, githubVoice.defaults, options || {}),
setup = options.setup || githubVoice.base;
options.path = user + '/' + repository;
for (property in setup) {
var methods = setup[property];
for (var i = 0, length = methods.length; i < length; i++) {
methods[i].call(this, options);
}
}
return this.trigger('github-voice-initialize');
};
$.fn.githubVoice.base = {
initialize: [function(options) {
this.bind('github-voice-initialize', function() {
$(this).trigger('github-voice-bindEventListeners');
});
}],
bindEventListeners: [function(options) {
var path = 'https://api.github.com/repos/' + options.path + '/issues';
this.bind('github-voice-bindEventListeners', function() {
var
element = $(this);
element.click(function() {
var cache = element.data('cache');
if (options.overlay) {
element.trigger('github-voice-createOverlay');
}
element.trigger('github-voice-createElement')
.trigger('github-voice-updatePosition');
if (cache) {
element.trigger('github-voice-update', [cache]);
} else {
$.getJSON(path + '?callback=?', options.query, function(response) {
var issues = response.data;
element.data('cache', issues)
.trigger('github-voice-update', [issues]);
});
}
return false;
});
});
}],
createElement: [function(options) {
this.bind('github-voice-createElement', function() {
var text = options.text;
$('body').append(options.html);
$('#github-voice')
.find('p.description').html(text.description).end()
.find('li.loading').html(text.loading).end()
.find('p.call-to-action a')
.html(text.callToAction)
.attr('href', 'http://github.com/' + options.path + '/issues');
$('#github-voice-wrapper .close').click(function() {
$('#github-voice-overlay, #github-voice-wrapper').remove();
});
});
}],
createOverlay: [function(options) {
this.bind('github-voice-createOverlay', function() {
$('body')
.append('<div id="github-voice-overlay"></div>')
.find('#github-voice-overlay')
.css({
width : $(window).width(),
height : $(document).height(),
opacity : 0.75
})
.click(function() {
$('#github-voice-overlay, #github-voice-wrapper').remove();
});
});
}],
update: [function(options) {
this.bind('github-voice-update', function(event, issues) {
var list = $('#github-voice ol').empty(),
valid;
$.each(issues, function(index, issue) {
if (options.filter) {
valid = true;
$.each(options.filter, function(key, value) {
if (!issue[key].match(value)) {
valid = false;
return false;
}
});
if (!valid) {
return;
}
}
list.append('<li>' +
'<p class="votes">' +
'<em>' + issue.comments + '</em> comments' +
'</p>' +
'<h3><a href="http://github.com/' + options.path + '/issues#issue/' + issue.number + '">' + issue.title + '</a></h3>' +
'</li>');
});
$('#github-voice p.call-to-action a').append('<span> (' + issues.length + ' ideas)</span>')
$(this).trigger('github-voice-updatePosition');
});
}],
updatePosition: [function(options) {
this.bind('github-voice-updatePosition', function() {
var
element = $('#github-voice-wrapper');
element.css('margin-top', -1 * (element.height() / 2));
});
}]
};
$.fn.githubVoice.defaults = {
overlay : true,
filter : null,
query : { 'sort' : 'comments', 'per_page' : 5 },
text : {
loading : "Loading...",
description : "We've setup a feedback forum so you can tell us what's on your mind. Please go there and be heard!",
callToAction: "» Go to our Feedback Forum"
},
html : '' +
'<div id="github-voice-wrapper">' +
'<div class="close" />' +
'<h1>Feedback</h1>' +
'<div id="github-voice">' +
'<p class="description"></p>' +
'<ol>' +
'<li class="loading"></li>' +
'</ol>' +
'<p class="call-to-action"><a href="#"></a></p>' +
'</div>' +
'</div>'
};
})(jQuery);