Commit e96555e7c360921350dd7cfc64c02c336f8a242f

Authored by Sergio Oliveira
1 parent 5562a304

Improving JS for voting

Showing 1 changed file with 28 additions and 45 deletions   Show diff stats
src/super_archives/templates/message-thread.html
... ... @@ -6,27 +6,21 @@
6 6 {% block head_js %}
7 7  
8 8 <script>
9   - function vote_done_callback(msg_id, step) {
  9 + function vote_done_callback(step) {
10 10 console.debug('(un)vote successfuly (step ' + step + ')');
11   - var $msg = $('#msg-' + msg_id)
  11 + var $btn = $(this);
  12 + var step;
12 13  
13   - $('.vote-count', $msg).text(function(self, count) {
  14 + if ($btn.hasClass('btn-default')) {
  15 + step = 1;
  16 + } else {
  17 + step = -1;
  18 + }
  19 +
  20 + $btn.prev('.vote-count').text(function(self, count) {
14 21 return parseInt(count) + step;
15 22 });
16 23  
17   - if (step == -1) {
18   - var $btn = $('.vote.btn-success', $msg);
19   - $btn.unbind('click');
20   - $btn.bind('click', function() {
21   - vote(msg_id);
22   - });
23   - } else {
24   - var $btn = $('.vote.btn-default', $msg);
25   - $btn.unbind('click');
26   - $btn.bind('click', function() {
27   - unvote(msg_id);
28   - });
29   - }
30 24 $btn.toggleClass('btn-success');
31 25 $btn.toggleClass('btn-default');
32 26 $btn.button('reset')
... ... @@ -46,35 +40,35 @@
46 40 scroll(0, 0);
47 41 }
48 42  
49   - function get_vote_ajax_dict(msg_id, method) {
  43 + function get_vote_ajax_dict(msg_id, method, context) {
50 44 var csrftoken = $.cookie('csrftoken');
51   - $('#msg-' + msg_id + ' .vote.btn-success').button('loading');
52 45  
53 46 return {
54 47 url: "/api/message/" + msg_id + "/vote",
55 48 type: method,
  49 + context: context,
56 50 beforeSend: function(xhr, settings) {
57 51 xhr.setRequestHeader("X-CSRFToken", csrftoken);
58 52 }
59 53 }
60 54 }
61 55  
62   - function vote(msg_id) {
63   - console.debug('trying to vote');
64   - $.ajax(get_vote_ajax_dict(msg_id, 'PUT'))
65   - .done(function(){
66   - vote_done_callback(msg_id, 1);
67   - })
68   - .fail(vote_fail_callback);
69   - }
  56 + function vote() {
  57 + var $ajax;
  58 + var $btn = $(this);
  59 + var $msg = $(this).parents('.email-message');
  60 +
  61 + var msg_id = $msg.attr('id').split('-')[1];
70 62  
71   - function unvote(msg_id) {
72   - console.debug('trying to remove vote');
73   - $.ajax(get_vote_ajax_dict(msg_id, 'DELETE'))
74   - .done(function(){
75   - vote_done_callback(msg_id, -1);
76   - })
77   - .fail(vote_fail_callback);
  63 + $btn.button('loading');
  64 + if($btn.hasClass('btn-default')) {
  65 + $ajax = $.ajax(get_vote_ajax_dict(msg_id, 'PUT', $btn[0]));
  66 + } else {
  67 + $ajax = $.ajax(get_vote_ajax_dict(msg_id, 'DELETE', $btn[0]));
  68 + }
  69 + console.debug('trying to vote');
  70 + $ajax.done(vote_done_callback);
  71 + $ajax.fail(vote_fail_callback);
78 72 }
79 73  
80 74 // Binding functions
... ... @@ -83,18 +77,7 @@
83 77 $(this).next('.panel-collapse').collapse('toggle');
84 78 });
85 79  
86   - $('.email-message').each(function() {
87   - var msg_id = this.getAttribute('id').split('-')[1];
88   -
89   - $('.vote.btn-default', this).bind('click', function() {
90   - vote(msg_id);
91   - });
92   -
93   - $('.vote.btn-success', this).bind('click', function() {
94   - unvote(msg_id);
95   - });
96   -
97   - });
  80 + $('.vote.btn', this).bind('click', vote);
98 81 });
99 82  
100 83 </script>
... ...