Commit 944cecb5a77f08e66f8e49960bf43613a9cc6097

Authored by Victor Costa
1 parent 23846083

Display random proposal

Showing 2 changed files with 66 additions and 40 deletions   Show diff stats
index.html
... ... @@ -65,16 +65,23 @@
65 65 <input type='submit' id='make-proposal-button' name='make-proposal-button' value='Submit'>
66 66 </form>
67 67 </div>
68   - <div class='support-proposal'>
69   - <div class='title'> Apoie outras propostas</div>
70   - <div class='author'>{{author.name}}</div>
71   - <div class='abstract'>{{abstract}}</div>
72   - </div>
  68 + <div class='support-proposal-container'></div>
73 69 <div class='experience-proposal'>Conte sua experiência</div>
74 70 <div class='talk-proposal'>Fale com o ministro</div>
75 71 </div>
76 72 {{/each}}
77   -
  73 + </script>
  74 +
  75 + <script id='support-proposal-template' type='text/x-handlebars-template'>
  76 + <div class='support-proposal'>
  77 + <div class='title'> Apoie outras propostas</div>
  78 + <div class='author'>{{author.name}}</div>
  79 + <div class='abstract'>{{abstract}}</div>
  80 + <div class="vote-actions">
  81 + <a href="#" class="like" data-vote-value="1">Gostei</a>
  82 + <a href="#" class="like" data-vote-value="-1">Não Gostei</a>
  83 + </div>
  84 + </div>
78 85 </script>
79 86  
80 87 <div id='proposal-result'></div>
... ...
js/main.js
1 1 // The template code
2 2 var templateSource = document.getElementById('proposal-template').innerHTML;
3   -
  3 +
4 4 // compile the template
5 5 var template = Handlebars.compile(templateSource);
6   -
  6 +
  7 +var supportProposalTemplate = Handlebars.compile(document.getElementById('support-proposal-template').innerHTML);
  8 +
7 9 // The div/container that we are going to display the results in
8 10 var resultsPlaceholder = document.getElementById('proposal-result');
9 11  
... ... @@ -15,10 +17,10 @@ if(participa){
15 17 var private_token = '9350c1488fcae884ad955091a3d2d960'; //participa
16 18 var proposal_discussion = '92856'; //participa
17 19 }else{
18   - var host = 'http://localhost:3000';
  20 + var host = 'http://noosfero.com:3000';
19 21 //var private_token = 'bd8996155f5ea4354e42fee50b4b6891'; //casa
20   - var private_token = '89419a2d331a17e815c3ecc53b303aac'; //local serpro
21   - var proposal_discussion = '377'; //local serpro
  22 + var private_token = '04c9b36cf0afba52915fe86f182e741c'; //local serpro
  23 + var proposal_discussion = '632'; //local serpro
22 24 //var proposal_discussion = '401'; //casa
23 25 }
24 26  
... ... @@ -30,35 +32,53 @@ $.getJSON(noosferoAPI)
30 32 data['private_token'] = private_token;
31 33 resultsPlaceholder.innerHTML = template(data);
32 34 //Actions for links
33   - $( 'a' ).click(function(event){
  35 + $( '#nav-proposal-categories a' ).click(function(event){
  36 + //Display the category tab
  37 + $('#proposal-group').hide();
  38 + $('#proposal-categories').show();
  39 + $('#nav-proposal-categories a').addClass('active');
  40 + $('#nav-proposal-group a').removeClass('active');
  41 + $('.proposal-category-item').hide();
  42 + $('.proposal-detail').hide();
  43 + event.preventDefault();
  44 + });
  45 + $( '#nav-proposal-group a' ).click(function(event){
  46 + //Display the Topics or Discussions tab
  47 + $('#proposal-categories').hide();
  48 + $('#proposal-group').show();
  49 + $('#nav-proposal-group a').addClass('active');
  50 + $('#nav-proposal-categories a').removeClass('active');
  51 + event.preventDefault();
  52 + });
  53 + $( '.proposal-item a' ).click(function(event){
34 54 var item = this.href.split('#').pop();
35   - if(item == 'proposal-categories'){
36   - //Display the category tab
37   - $('#proposal-group').hide();
38   - $('#proposal-categories').show();
39   - $('#nav-proposal-categories a').addClass('active');
40   - $('#nav-proposal-group a').removeClass('active');
  55 + //Display Proposal
  56 + $('#proposal-categories').hide();
  57 + $('#proposal-group').hide();
  58 + $('.proposal-detail').hide();
  59 + $('#' + item).show();
  60 +
  61 + var topic_id = this.id.replace('\#','');
  62 + var url = host + '/api/v1/articles/' + topic_id + '/children' + '?private_token=' + private_token + '&limit=1&order=random()';
  63 + $.getJSON(url).done(function( data ) {
  64 + var article = data.articles.length > 0 ? data.articles[0] : null;
  65 + $('.support-proposal-container').html(supportProposalTemplate(article));
  66 + $(document.body).on('click', '.vote-actions .like', function(e) {
  67 + $.ajax({
  68 + type: 'post',
  69 + url: host + '/api/v1/articles/' + article.id + '/vote',
  70 + data: {value: $(this).data('vote-value'), private_token: private_token}
  71 + });
  72 + e.preventDefault();
  73 + });
  74 + });
  75 + });
  76 + $( '.proposal-category a' ).click(function(event){
  77 + var item = this.href.split('#').pop();
  78 + if($('#' + item).hasClass('proposal-category-item')){
  79 + //Display Topics or Discussion by category
41 80 $('.proposal-category-item').hide();
42   - $('.proposal-detail').hide();
43   - }else if(item == 'proposal-group'){
44   - //Display the Topics or Discussions tab
45   - $('#proposal-categories').hide();
46   - $('#proposal-group').show();
47   - $('#nav-proposal-group a').addClass('active');
48   - $('#nav-proposal-categories a').removeClass('active');
49   - }else{
50   - if($('#' + item).hasClass('proposal-category-item')){
51   - //Display Topics or Discussion by category
52   - $('.proposal-category-item').hide();
53   - $('#' + item).show();
54   -
55   - }else{
56   - //Display Proposal
57   - $('#proposal-categories').hide();
58   - $('#proposal-group').hide();
59   - $('.proposal-detail').hide();
60   - $('#' + item).show();
61   - }
  81 + $('#' + item).show();
62 82 }
63 83 event.preventDefault();
64 84 });
... ... @@ -66,7 +86,7 @@ $.getJSON(noosferoAPI)
66 86 $('.make-proposal-form').submit(function (e) {
67 87 e.preventDefault();
68 88 var proposal_id = this.id.split('-').pop();
69   - $.ajax({
  89 + $.ajax({
70 90 type: 'post',
71 91 url: host + '/api/v1/articles/' + proposal_id + '/children',
72 92 data: $('#'+this.id).serialize()
... ... @@ -84,4 +104,3 @@ $.getJSON(noosferoAPI)
84 104 var err = textStatus + ", " + error;
85 105 console.log( "Request Failed: " + err );
86 106 });
87   -
... ...