main.js
4.11 KB
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
// The template code
var templateSource = document.getElementById('proposal-template').innerHTML;
// compile the template
var template = Handlebars.compile(templateSource);
var supportProposalTemplate = Handlebars.compile(document.getElementById('support-proposal-template').innerHTML);
// The div/container that we are going to display the results in
var resultsPlaceholder = document.getElementById('proposal-result');
var topics;
var participa = true;
if(participa){
var host = 'http://www.participa.br';
var private_token = '9350c1488fcae884ad955091a3d2d960'; //participa
var proposal_discussion = '92856'; //participa
}else{
var host = 'http://noosfero.com:3000';
//var private_token = 'bd8996155f5ea4354e42fee50b4b6891'; //casa
var private_token = '04c9b36cf0afba52915fe86f182e741c'; //local serpro
var proposal_discussion = '632'; //local serpro
//var proposal_discussion = '401'; //casa
}
var noosferoAPI = host + '/api/v1/articles/' + proposal_discussion + '?private_token=' + private_token + '&callback=?';
$.getJSON(noosferoAPI)
.done(function( data ) {
data['host'] = host;
data['private_token'] = private_token;
resultsPlaceholder.innerHTML = template(data);
//Actions for links
$( '#nav-proposal-categories a' ).click(function(event){
//Display the category tab
$('#proposal-group').hide();
$('#proposal-categories').show();
$('#nav-proposal-categories a').addClass('active');
$('#nav-proposal-group a').removeClass('active');
$('.proposal-category-items').hide();
$('.proposal-detail').hide();
event.preventDefault();
});
$( '#nav-proposal-group a' ).click(function(event){
//Display the Topics or Discussions tab
$('#proposal-categories').hide();
$('#proposal-group').show();
$('#nav-proposal-group a').addClass('active');
$('#nav-proposal-categories a').removeClass('active');
event.preventDefault();
});
$( '.proposal-item a' ).click(function(event){
var item = this.href.split('#').pop();
//Display Proposal
$('#proposal-categories').hide();
$('#proposal-group').hide();
$('.proposal-detail').hide();
$('#' + item).show();
var topic_id = this.id.replace('\#','');
loadRandomProposal(topic_id, private_token);
});
$( '.proposal-category a' ).click(function(event){
var item = this.href.split('#').pop();
if($('#' + item).hasClass('proposal-category-items')){
//Display Topics or Discussion by category
$('.proposal-category-items').hide();
$('#' + item).show();
}
event.preventDefault();
});
$('.make-proposal-form').submit(function (e) {
e.preventDefault();
var proposal_id = this.id.split('-').pop();
$.ajax({
type: 'post',
url: host + '/api/v1/articles/' + proposal_id + '/children',
data: $('#'+this.id).serialize()
})
.done(function( data ) {
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
});
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
function loadRandomProposal(topic_id, private_token) {
var url = host + '/api/v1/articles/' + topic_id + '/children' + '?private_token=' + private_token + '&limit=1&order=random()';
$.getJSON(url).done(function( data ) {
var article = data.articles.length > 0 ? data.articles[0] : null;
$('.support-proposal-container').html(supportProposalTemplate(article));
$(document.body).off('click', '.vote-actions .like');
$(document.body).on('click', '.vote-actions .like', function(e) {
$.ajax({
type: 'post',
url: host + '/api/v1/articles/' + article.id + '/vote',
data: {value: $(this).data('vote-value'), private_token: private_token}
}).done(function( data ) {
loadRandomProposal(topic_id, private_token);
});
e.preventDefault();
});
});
}
function oauthPluginHandleLoginResult(loggedIn, token) {
private_token = token;
}