Merge pull request #41 from danharibo/master

Web view for polls
This commit is contained in:
Luke Slater 2012-06-15 08:24:28 -07:00
commit 81a7c5eb64
5 changed files with 108 additions and 0 deletions

View File

@ -1,4 +1,7 @@
var poll = function(dbot) { var poll = function(dbot) {
if(!dbot.db.hasOwnProperty('polls')) {
dbot.db.polls = {};
}
var polls = dbot.db.polls; var polls = dbot.db.polls;
var commands = { var commands = {
'~newpoll': function(event) { '~newpoll': function(event) {

View File

@ -34,6 +34,29 @@ var webInterface = function(dbot) {
res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } }); res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } });
}); });
// Lists all of the polls
app.get('/polls', function(req, res) {
res.render('polllist', { 'name': dbot.name, 'polllist': Object.keys(dbot.db.polls) });
});
// Shows the results of a poll
app.get('/polls/:key', function(req, res) {
var key = req.params.key.toLowerCase();
if(dbot.db.polls.hasOwnProperty(key)) {
// tally the votes
var totalVotes = 0;
for( var v in dbot.db.polls[key].votes ) {
var N = Number(dbot.db.polls[key].votes[v]);
if( !isNaN(N) ) {
totalVotes += N;
}
}
res.render('polls', { 'name': dbot.name, 'description': dbot.db.polls[key].description, 'votees': dbot.db.polls[key].votees, 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } });
} else {
res.render('error', { 'name': dbot.name, 'message': 'No polls under that key.' });
}
});
app.listen(dbot.webPort); app.listen(dbot.webPort);
return { return {

View File

@ -21,6 +21,15 @@ body {
text-shadow: 1px 1px 2px #2B2B2B; text-shadow: 1px 1px 2px #2B2B2B;
} }
h1,h2 {
margin: 0;
padding: 0;
}
p {
margin: 15px 5px;
}
div#page { div#page {
width: 90%; width: 90%;
margin: 0 auto 0 auto; margin: 0 auto 0 auto;
@ -105,3 +114,34 @@ li.quote {
img { img {
max-width: 100%; max-width: 100%;
} }
/* Polls */
#votelist {
margin: 10px 0;
padding: 0 40px;
}
li.option {
list-style: none;
text-align: left;
font-size: 1.2em;
}
li.option-votes {
list-style: none;
text-align: left;
margin-bottom: 10px;
}
.vote-percentage {
height: 20px;
background: #444;
}
.vote-track {
height: 20px;
width: 100%;
background: #EEE;
margin: 2px 0;
box-shadow: inset 0px 0px 3px #444;
}

7
views/polllist.jade Normal file
View File

@ -0,0 +1,7 @@
div#controls
input(type="text", name="search", id="search-text", oninput="search(this.value)")
ul#quotelist
-each poll in polllist
a(href='/polls/'+poll)
li.quotes #{poll}

35
views/polls.jade Normal file
View File

@ -0,0 +1,35 @@
h2 #{description}
p Voters (#{locals.totalVotes}):
-each voter in votees
| #{voter}
ul#votelist
-var hasYouTubeVids=false
-each votes,option in options
-var percentage = votes/locals.totalVotes*100
-if(options.hasOwnProperty(option))
-if(option.match(locals.url_regex))
li.option
-if(option.match(/(jpg|png|gif|jpeg|tiff)$/))
a(href=option)
img(src=option)
-else if(option.match(/youtube.com\/watch/))
-hasYouTubeVids = true
span(class='ytplaceholder')
=option
-else
a(href=option)
=option
-else
li.option #{option}
li.option-votes
.vote-track
-if(!isNaN(percentage))
.vote-percentage(style="width: #{percentage}%")
-if(votes == 1)
|#{votes} vote
-else
|#{votes} votes
-if(!isNaN(percentage))
|(#{percentage.toFixed(2)}%)
-if(hasYouTubeVids)
script(src='/ytembed.js')