2012-08-10 19:44:04 +02:00
|
|
|
/**
|
|
|
|
* @provides javelin-behavior-ponder-votebox
|
|
|
|
* @requires javelin-behavior
|
|
|
|
* javelin-dom
|
|
|
|
* javelin-util
|
2012-10-01 05:12:35 +02:00
|
|
|
* javelin-stratcom
|
|
|
|
* javelin-request
|
2012-08-10 19:44:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
JX.behavior('ponder-votebox', function(config) {
|
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
function handle_vote(e, vote) {
|
|
|
|
e.kill();
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
var root = e.getNode('ponder-votable');
|
|
|
|
var data = e.getNodeData('ponder-votable');
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
if (data.vote != vote) {
|
|
|
|
data.vote = vote;
|
|
|
|
data.count += vote;
|
|
|
|
} else {
|
|
|
|
// User is undoing their vote.
|
|
|
|
data.vote = 0;
|
|
|
|
data.count -= vote;
|
|
|
|
}
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
var upv = JX.DOM.find(root, 'a', 'upvote');
|
|
|
|
JX.DOM.alterClass(upv, 'ponder-vote-active', (data.vote > 0));
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
var downv = JX.DOM.find(root, 'a', 'downvote');
|
|
|
|
JX.DOM.alterClass(downv, 'ponder-vote-active', (data.vote < 0))
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
JX.DOM.setContent(
|
|
|
|
JX.DOM.find(root, 'div', 'ponder-vote-count'),
|
|
|
|
data.count);
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
new JX.Request(e.getTarget().href, JX.bag)
|
|
|
|
.setData({vote: data.vote})
|
|
|
|
.send()
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
|
|
|
|
2012-10-01 05:12:35 +02:00
|
|
|
JX.Stratcom.listen(
|
|
|
|
'click',
|
|
|
|
'downvote',
|
|
|
|
function(e) {
|
|
|
|
handle_vote(e, -1);
|
|
|
|
});
|
|
|
|
|
|
|
|
JX.Stratcom.listen(
|
|
|
|
'click',
|
|
|
|
'upvote',
|
|
|
|
function(e) {
|
|
|
|
handle_vote(e, 1);
|
|
|
|
});
|
2012-08-10 19:44:04 +02:00
|
|
|
});
|