handlebars-helpers.js
5.39 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
define(['handlebars'], function(Handlebars){
Handlebars.registerHelper('link', function(text, url) {
text = Handlebars.Utils.escapeExpression(text);
url = Handlebars.Utils.escapeExpression(url);
// Exemplo: <a href="#/programas/{{id}}" class="proposal-link" data-target="">{{name}}</a>
var result = '<a href="#/programas/' + url + '" data-target="proposal-item-' + url + '" class="proposal-link">' + text + '</a>';
return new Handlebars.SafeString(result);
});
Handlebars.registerHelper('list_proposal', function(proposals, options) {
proposals = proposals.sort(function(p1, p2) {
return p1.position - p2.position;
});
var ret = "";
for(var i=0, j=proposals.length; i<j; i++) {
var proposal = proposals[i];
element = '<li class="proposal-item col-sm-6">' +
'<a href="#/programas/' + proposal.id + '" data-target="proposal-item-' + proposal.id + '" class="proposal-link box">' +
'<div class="box-header item">';
category = "<div class='category box-category'>";
for (var x = 0, y = proposal.categories.length; x < y; x++) {
if ((options.hash['category'] != null) && (options.hash['category'] != proposal.categories[x].slug)) {
element = '';
continue;
}
category = category + '<div class="category-' + proposal.categories[x].slug + '">' + proposal.categories[x].name + '</div>';
}
if (element == '') {
continue;
}
if (proposal.image) {
category = category + '</div>' +
'<div class="box-body">' +
// '<div class="box__image">' + '<img src="' + options.hash['host'] + proposal.image.url + '" alt="Imagem de apresentação do programa."/>' + '</div>' +
'<div class="box__image" style="background-image:url(' + (options.hash['host'] + proposal.image.url) + ')"></div>' +
'<div class="box__title">' + proposal.title + '</div>' +
(proposal.abstract ? '<div class="box__abstract">' + proposal.abstract + '</div>' : '') +
'<div class="box__footer">' + '<button type="button" class="button button-block participe">OPINE NESTE PROGRAMA</button>' + '</div>' +
'</div>';
}
// element = element + options.fn(proposal);
element = element + category;
ret = ret + element + '</div></a></li>';
}
return ret;
});
Handlebars.registerHelper('proposal_detail', function(proposals, options) {
var ret = "";
for(var i=0, j=proposals.length; i<j; i++) {
ret = ret + "<div class='proposal-detail hide' id='proposal-item-" + proposals[i].id + "'>";
ret = ret + "<div class='title'>" + proposals[i].title + "</div>";
ret = ret + "<span>" + proposals[i].body + "</span>";
ret = ret + '</div>';
}
return ret;
});
Handlebars.registerHelper('replace', function(string, to_replace, replacement) {
return (string || '').replace(new RegExp(to_replace, 'g'), replacement);
});
Handlebars.registerHelper('select_proposal', function(proposals, category_slug, selected_id) {
var ret = '<label for="proposal-selection" class="sr-only">Selecione o programa</label>'
ret = ret + '<select id="proposal-selection" name="proposal-selection" data-proposal="'+selected_id+'" title="Selecione o programa" class="proposal-selection">';
for(var i=0; i<proposals.length; i++) {
if(!proposal_has_category(proposals[i], category_slug)) continue;
var selected = proposals[i].id===selected_id ? "selected" : "";
ret += '<option value="'+proposals[i].id+'" '+selected+'>'+proposals[i].title+'</option>';
}
ret += '</select>';
return ret;
});
Handlebars.registerHelper('trimString', function(passedString, endstring) {
return passedString.substring(0, endstring);
});
Handlebars.registerHelper('stripTags', function(passedString) {
return $("<div/>").html(passedString).text();
});
Handlebars.registerHelper('proposal_action', function(discussion, target) {
if(discussion.setting && discussion.setting.moderate_proposals) {
return '/api/v1/proposals_discussion_plugin/'+target.id+'/propose';
} else {
return '/api/v1/articles/'+target.id+'/children';
}
});
Handlebars.registerHelper('round', function(num) {
return +(Math.round(num + "e+2") + "e-2");
});
Handlebars.registerHelper('social_share', function(title, description, url) {
var template = Handlebars.compile($('#social-share').html());
if(url==='#') {
url = '';
}
url = 'http:'+Url.addBaseUrl(url);
return template({title: title, description: description, url: url});
});
Handlebars.registerHelper('proposal_url', function(parent_id, id) {
return "#/programas/"+parent_id+"/propostas/"+id;
});
Handlebars.registerHelper('program_detail_url', function(id) {
return "#/programas/"+id+"/sobre-o-programa";
});
Handlebars.registerHelper('encodeURI', function(uri) {
return encodeURIComponent(uri);
});
Handlebars.registerHelper('calcPosition', function(index, per_page, page) {
return index + 1 + per_page * (page - 1);
});
function proposal_has_category(proposal, category_slug) {
for(var i=0; i<proposal.categories.length; i++) {
if(proposal.categories[i].slug == category_slug)
return true;
}
return false;
}
});