Commit 3d10c1fb0bfb3bb7469bb5ddc0ae32c9f6ea7d19
1 parent
30e6b9f2
Exists in
master
and in
8 other branches
Changes to support automated testing
Showing
4 changed files
with
537 additions
and
358 deletions
Show diff stats
js/main.js
1 | 1 | define(['handlebars'], function(Handlebars){ |
2 | 2 | |
3 | + | |
3 | 4 | /* global Handlebars, $ */ |
4 | 5 | // The template code |
5 | 6 | var templateSource = document.getElementById('proposal-template').innerHTML; |
... | ... | @@ -41,7 +42,7 @@ define(['handlebars'], function(Handlebars){ |
41 | 42 | $('.login-container').html(loginTemplate()); |
42 | 43 | $('.countdown').maxlength({text: '%left caracteres restantes'}); |
43 | 44 | |
44 | - navigateTo(window.location.hash); | |
45 | + Main.navigateTo(window.location.hash); | |
45 | 46 | |
46 | 47 | //Actions for links |
47 | 48 | $( '#nav-proposal-categories a' ).on('click touchstart', function(e){ |
... | ... | @@ -50,7 +51,7 @@ define(['handlebars'], function(Handlebars){ |
50 | 51 | var $link = $(this); |
51 | 52 | |
52 | 53 | // Update URL and Navigate |
53 | - updateHash($link.attr('href')); | |
54 | + Main.updateHash($link.attr('href')); | |
54 | 55 | }); |
55 | 56 | |
56 | 57 | $( '#nav-proposal-group a' ).on('click touchstart', function(e){ |
... | ... | @@ -59,7 +60,7 @@ define(['handlebars'], function(Handlebars){ |
59 | 60 | var $link = $(this); |
60 | 61 | |
61 | 62 | // Update URL and Navigate |
62 | - updateHash($link.attr('href')); | |
63 | + Main.updateHash($link.attr('href')); | |
63 | 64 | }); |
64 | 65 | |
65 | 66 | $( '.proposal-item a' ).on('click touchstart', function(e){ |
... | ... | @@ -68,7 +69,7 @@ define(['handlebars'], function(Handlebars){ |
68 | 69 | var $link = $(this); |
69 | 70 | |
70 | 71 | // Update URL and Navigate |
71 | - updateHash($link.attr('href')); | |
72 | + Main.updateHash($link.attr('href')); | |
72 | 73 | }); |
73 | 74 | |
74 | 75 | $( '.proposal-category a' ).on('click touchstart', function(e){ |
... | ... | @@ -77,7 +78,7 @@ define(['handlebars'], function(Handlebars){ |
77 | 78 | var $link = $(this); |
78 | 79 | |
79 | 80 | // Update URL and Navigate |
80 | - updateHash($link.attr('href')); | |
81 | + Main.updateHash($link.attr('href')); | |
81 | 82 | }); |
82 | 83 | |
83 | 84 | $( '.proposal-category .go-back' ).on('click touchstart', function(e){ |
... | ... | @@ -94,7 +95,7 @@ define(['handlebars'], function(Handlebars){ |
94 | 95 | } |
95 | 96 | |
96 | 97 | // Update URL and Navigate |
97 | - updateHash(newHash); | |
98 | + Main.updateHash(newHash); | |
98 | 99 | }); |
99 | 100 | |
100 | 101 | $( '.send-button a' ).on('click touchstart', function(e){ |
... | ... | @@ -105,7 +106,7 @@ define(['handlebars'], function(Handlebars){ |
105 | 106 | loginButton = $this.parents('.send-button'); |
106 | 107 | loginButton.hide(); |
107 | 108 | $this.parents('.success-proposal-sent').hide(); |
108 | - loginCallback(logged_in); | |
109 | + Main.loginCallback(logged_in); | |
109 | 110 | }); |
110 | 111 | |
111 | 112 | $( '#display-contrast' ).on('click touchstart', function(e){ |
... | ... | @@ -119,7 +120,7 @@ define(['handlebars'], function(Handlebars){ |
119 | 120 | var $link = $(this); |
120 | 121 | |
121 | 122 | // Update URL and Navigate |
122 | - updateHash($link.attr('href')); | |
123 | + Main.updateHash($link.attr('href')); | |
123 | 124 | }); |
124 | 125 | |
125 | 126 | $( '.go-to-proposal-button a' ).on('click touchstart', function(e){ |
... | ... | @@ -128,13 +129,13 @@ define(['handlebars'], function(Handlebars){ |
128 | 129 | var $link = $(this); |
129 | 130 | |
130 | 131 | // Update URL and Navigate |
131 | - updateHash($link.attr('href')); | |
132 | + Main.updateHash($link.attr('href')); | |
132 | 133 | }); |
133 | 134 | |
134 | 135 | $( '.proposal-selection' ).change(function(e){ |
135 | 136 | e.preventDefault(); |
136 | 137 | |
137 | - display_proposal('proposal-item-' + this.value); | |
138 | + Main.display_proposal('proposal-item-' + this.value); | |
138 | 139 | }); |
139 | 140 | |
140 | 141 | var availableTags = [ ]; |
... | ... | @@ -189,96 +190,356 @@ define(['handlebars'], function(Handlebars){ |
189 | 190 | console.log( 'Request Failed: ' + err ); |
190 | 191 | }); |
191 | 192 | |
192 | - function loadRandomProposal(topic_id, private_token) { | |
193 | - var $noProposals = $('.no-proposals'); | |
194 | - var $loading = $('.loading'); | |
195 | - var $randomProposal = $('.random-proposal'); | |
196 | - var $body = $(document.body); | |
197 | - | |
198 | - // reset view | |
199 | - $noProposals.hide(); | |
200 | - $loading.show(); | |
201 | - $randomProposal.html(''); | |
202 | - | |
203 | - var url = host + '/api/v1/articles/' + topic_id + '/children' + '?private_token=' + private_token + '&limit=1&order=random()&_='+new Date().getTime()+'&fields=id,name,abstract,created_by&content_type=ProposalsDiscussionPlugin::Proposal'; | |
204 | - $.getJSON(url).done(function( data ) { | |
205 | - $loading.hide(); | |
206 | - | |
207 | - if(data.articles.length === 0) { | |
208 | - $noProposals.show(); | |
209 | - return; | |
193 | + var BARRA_ADDED = false; | |
194 | + var HIDE_BARRA_DO_GOVERNO = false; | |
195 | + | |
196 | + var Main = (function(){ | |
197 | + | |
198 | + return { | |
199 | + loadRandomProposal: function (topic_id, private_token) { | |
200 | + var $noProposals = $('.no-proposals'); | |
201 | + var $loading = $('.loading'); | |
202 | + var $randomProposal = $('.random-proposal'); | |
203 | + var $body = $(document.body); | |
204 | + var contextMain = this; | |
205 | + | |
206 | + // reset view | |
207 | + $noProposals.hide(); | |
208 | + $loading.show(); | |
209 | + $randomProposal.html(''); | |
210 | + | |
211 | + var url = host + '/api/v1/articles/' + topic_id + '/children' + '?private_token=' + private_token + '&limit=1&order=random()&_='+new Date().getTime()+'&fields=id,name,abstract,created_by&content_type=ProposalsDiscussionPlugin::Proposal'; | |
212 | + $.getJSON(url).done(function( data ) { | |
213 | + $loading.hide(); | |
214 | + | |
215 | + if(data.articles.length === 0) { | |
216 | + $noProposals.show(); | |
217 | + return; | |
218 | + } | |
219 | + | |
220 | + var article = data.articles[0]; | |
221 | + $randomProposal.html(supportProposalTemplate(article)); | |
222 | + $body.off('click', '.vote-actions .skip'); | |
223 | + $body.on('click', '.vote-actions .skip', function(e) { | |
224 | + contextMain.loadRandomProposal(topic_id, private_token); | |
225 | + e.preventDefault(); | |
226 | + }); | |
227 | + $body.off('click', '.vote-actions .like'); | |
228 | + $body.on('click', '.vote-actions .like', function(e) { | |
229 | + //Helps to prevent more than one vote per proposal | |
230 | + if(contextMain.hasProposalbeenVoted(article.id)){ | |
231 | + console.log("Proposta " + article.id + " já havia sido votada"); | |
232 | + contextMain.loadRandomProposal(topic_id, private_token); | |
233 | + e.preventDefault(); | |
234 | + return; | |
235 | + } | |
236 | + $.ajax({ | |
237 | + type: 'post', | |
238 | + url: host + '/api/v1/articles/' + article.id + '/vote', | |
239 | + data: { | |
240 | + value: $(this).data('vote-value'), | |
241 | + private_token: private_token | |
242 | + } | |
243 | + }).done(function( /*data*/ ) { | |
244 | + contextMain.addVotedProposal(article.id); | |
245 | + contextMain.loadRandomProposal(topic_id, private_token); | |
246 | + }); | |
247 | + e.preventDefault(); | |
248 | + }); | |
249 | + | |
250 | + $body.off('click', '.vote-result'); | |
251 | + $body.on('click', '.vote-result', function(e) { | |
252 | + | |
253 | + var $this = $(this); | |
254 | + var $proposalDetail = $this.parents('.proposal-detail'); | |
255 | + var $resultsContainer = $proposalDetail.find('.results-container'); | |
256 | + | |
257 | + // $resultsContainer.toggle(); | |
258 | + // $resultsContainer.toggleClass('hide'); | |
259 | + | |
260 | + if($resultsContainer.css('display') === 'none') { | |
261 | + | |
262 | + $resultsContainer.find('.loading').show(); | |
263 | + $resultsContainer.find('.results-content').hide(); | |
264 | + | |
265 | + var url = host + '/api/v1/articles/' + topic_id + '/children' + '?private_token=' + private_token + '&limit=10&order=votes_score&fields=id,name,abstract,votes_for,votes_against&content_type=ProposalsDiscussionPlugin::Proposal'; | |
266 | + $.getJSON(url).done(function( data ) { | |
267 | + | |
268 | + $resultsContainer.html(resultsTemplate(data)); | |
269 | + $resultsContainer.find('.loading').hide(); | |
270 | + $resultsContainer.find('.results-content').show(); | |
271 | + $resultsContainer.show(); | |
272 | + | |
273 | + // scroll to the end | |
274 | + $('html, body').animate({ | |
275 | + scrollTop: $(document).height() | |
276 | + }, 'fast'); | |
277 | + }); | |
278 | + $('.experience-proposal-container').hide(); | |
279 | + $('.talk-proposal-container').hide(); | |
280 | + } else { | |
281 | + $('.experience-proposal-container').show(); | |
282 | + $('.talk-proposal-container').show(); | |
283 | + $resultsContainer.hide(); | |
284 | + } | |
285 | + | |
286 | + e.preventDefault(); | |
287 | + }); | |
288 | + }); | |
289 | + }, | |
290 | + | |
291 | + loginCallback: function(loggedIn, token) { | |
292 | + logged_in = loggedIn; | |
293 | + $('.login .message').text(''); | |
294 | + | |
295 | + if(logged_in) { | |
296 | + if(token){ | |
297 | + private_token = token; | |
298 | + } | |
299 | + loginButton.siblings('.save-article-form').show(); | |
300 | + loginButton.siblings('.save-article-form .message').show(); | |
301 | + loginButton.siblings('.login-container').hide(); | |
302 | + $.cookie('_dialoga_session', private_token); | |
303 | + } else { | |
304 | + loginButton.siblings('.save-article-form').hide(); | |
305 | + loginButton.siblings('.login-container').show(); | |
306 | + } | |
307 | + }, | |
308 | + oauthPluginHandleLoginResult: function(loggedIn, token) { | |
309 | + loginCallback(loggedIn, token); | |
310 | + }, | |
311 | + guid: function() { | |
312 | + function s4() { | |
313 | + return Math.floor((1 + Math.random()) * 0x10000) | |
314 | + .toString(16) | |
315 | + .substring(1); | |
316 | + } | |
317 | + }, | |
318 | + display_category_tab: function(){ | |
319 | + $('#proposal-group').hide(); | |
320 | + $('#proposal-categories').show(); | |
321 | + $('#nav-proposal-categories a').addClass('active'); | |
322 | + $('#nav-proposal-group a').removeClass('active'); | |
323 | + $('.proposal-category-items').hide(); | |
324 | + $('.proposal-category .arrow-box').hide(); | |
325 | + $('.proposal-detail').hide(); | |
326 | + | |
327 | + $('#content').show(); | |
328 | + $('nav').show(); | |
329 | + }, | |
330 | + display_proposals_tab: function(){ | |
331 | + $('#proposal-categories').hide(); | |
332 | + $('#proposal-group').show(); | |
333 | + $('#nav-proposal-group a').addClass('active'); | |
334 | + $('#nav-proposal-categories a').removeClass('active'); | |
335 | + $('#content').show(); | |
336 | + $('nav').show(); | |
337 | + }, | |
338 | + display_proposal: function(proposal_id){ | |
339 | + $('#proposal-categories').hide(); | |
340 | + $('#proposal-group').hide(); | |
341 | + $('nav').hide(); | |
342 | + $('#content').hide(); | |
343 | + $('.make-proposal-form').hide(); | |
344 | + $('.login-container').hide(); | |
345 | + $('.proposal-detail').hide(); | |
346 | + $('.proposal-detail-base').hide(); | |
347 | + $('#' + proposal_id).show(); | |
348 | + $('.proposal-header').show(); | |
349 | + $('.make-proposal-container').show(); | |
350 | + $('.support-proposal-container').show(); | |
351 | + $('.results-container').hide(); | |
352 | + $('.results-container .loading').hide(); | |
353 | + $('.results-container .results-content').hide(); | |
354 | + $('.experience-proposal-container').show(); | |
355 | + $('.talk-proposal-container').show(); | |
356 | + var topic_id = proposal_id.split('-').pop(); | |
357 | + this.loadRandomProposal(topic_id, private_token); | |
358 | + }, | |
359 | + display_proposal_detail: function(proposal_id){ | |
360 | + $('#proposal-categories').hide(); | |
361 | + $('#proposal-group').hide(); | |
362 | + $('nav').hide(); | |
363 | + $('#content').hide(); | |
364 | + $('.make-proposal-form').hide(); | |
365 | + $('.proposal-header').hide(); | |
366 | + $('.make-proposal-container').hide(); | |
367 | + $('.support-proposal-container').hide(); | |
368 | + $('.results-container').hide(); | |
369 | + $('.experience-proposal-container').hide(); | |
370 | + $('.talk-proposal-container').hide(); | |
371 | + $('.body').show(); | |
372 | + | |
373 | + var url = host + '/api/v1/articles/' + proposal_id + '?private_token=' + private_token + '&fields=id,body&content_type=ProposalsDiscussionPlugin::Topic'; | |
374 | + $.getJSON(url).done(function( data ) { | |
375 | + $('.body-content').replaceWith(data.article.body); | |
376 | + }) | |
377 | + .fail(function( jqxhr, textStatus, error ) { | |
378 | + var err = textStatus + ', ' + error; | |
379 | + console.log( 'Request Failed: ' + err ); | |
380 | + }); | |
381 | + }, | |
382 | + display_proposal_by_category: function(item){ | |
383 | + var $item = $('#' + item); | |
384 | + | |
385 | + if($item.hasClass('proposal-category-items')){ | |
386 | + //Display Topics or Discussion by category | |
387 | + $('nav').show(); | |
388 | + $('#content').show(); | |
389 | + $('#proposal-categories').show(); | |
390 | + $('#nav-proposal-categories a').addClass('active'); | |
391 | + $('#nav-proposal-group a').removeClass('active'); | |
392 | + $('.proposal-category-items').hide(); | |
393 | + $('.proposal-detail').hide(); | |
394 | + $item.toggle( 'blind', 1000 ); | |
395 | + $('.proposal-category .arrow-box').hide(); | |
396 | + var categorySlug = $item.data('category'); | |
397 | + $('#proposal-category-' + categorySlug).find('.arrow-box').show(); | |
398 | + } | |
399 | + }, | |
400 | + addBarraDoGoverno: function(){ | |
401 | + | |
402 | + if( BARRA_ADDED ) { return; } | |
403 | + | |
404 | + var HTML_BODY_PREPEND = '' + | |
405 | + '<div id="barra-brasil" style="background:#7F7F7F; height: 20px; padding:0 0 0 10px;display:block;"> ' + | |
406 | + '<ul id="menu-barra-temp" style="list-style:none;">' + | |
407 | + '<li style="display:inline; float:left;padding-right:10px; margin-right:10px; border-right:1px solid #EDEDED"><a href="http://brasil.gov.br" style="font-family:sans,sans-serif; text-decoration:none; color:white;">Portal do Governo Brasileiro</a></li> ' + | |
408 | + '<li><a style="font-family:sans,sans-serif; text-decoration:none; color:white;" href="http://epwg.governoeletronico.gov.br/barra/atualize.html">Atualize sua Barra de Governo</a></li>' + | |
409 | + '</ul>' + | |
410 | + '</div>'; | |
411 | + | |
412 | + var HTML_BODY_APPEND = ''+ | |
413 | + '<div id="footer-brasil"></div>' + | |
414 | + '<script defer="defer" src="http://barra.brasil.gov.br/barra.js" type="text/javascript"></script>'; | |
415 | + | |
416 | + var STYLE_TEMA_AZUL = '' + | |
417 | + '<style>'+ | |
418 | + '#footer-brasil {'+ | |
419 | + 'background: none repeat scroll 0% 0% #0042b1;'+ | |
420 | + 'padding: 1em 0px;'+ | |
421 | + 'max-width: 100%;'+ | |
422 | + 'margin-top: 40px;'+ | |
423 | + '}'+ | |
424 | + '#barra-brasil ul {'+ | |
425 | + 'width: auto;'+ | |
426 | + '}'+ | |
427 | + '<style>'; | |
428 | + | |
429 | + var $body = $(document.body); | |
430 | + $body.prepend(HTML_BODY_PREPEND); | |
431 | + $body.append(HTML_BODY_APPEND); | |
432 | + $body.append(STYLE_TEMA_AZUL); | |
433 | + | |
434 | + BARRA_ADDED = true; | |
435 | + }, | |
436 | + updateHash: function(hash){ | |
437 | + var id = hash.replace(/^.*#/, ''); | |
438 | + var elem = document.getElementById(id); | |
439 | + | |
440 | + // preserve the query param | |
441 | + // if (HIDE_BARRA_DO_GOVERNO && (hash.indexOf('?barra=false') === -1)){ | |
442 | + // hash += '?barra=false'; | |
443 | + // } | |
444 | + | |
445 | + if ( !elem ) { | |
446 | + window.location.hash = hash; | |
447 | + return; | |
448 | + } | |
449 | + | |
450 | + elem.id = id+'-tmp'; | |
451 | + window.location.hash = hash; | |
452 | + elem.id = id; | |
453 | + }, | |
454 | + locationHashChanged: function(){ | |
455 | + var hash = window.location.hash; | |
456 | + this.navigateTo(hash); | |
457 | + }, | |
458 | + navigateTo: function(hash){ | |
459 | + var regexProposals = /#\/programas/; | |
460 | + var regexCategory = /#\/temas/; | |
461 | + var regexHideBarra = /barra=false$/; | |
462 | + | |
463 | + if( !(regexHideBarra.exec(hash) !== null) && !HIDE_BARRA_DO_GOVERNO ){ | |
464 | + this.addBarraDoGoverno(); | |
465 | + }else{ | |
466 | + HIDE_BARRA_DO_GOVERNO = true; | |
467 | + } | |
468 | + | |
469 | + // remove query params | |
470 | + hash = hash.split('?')[0]; | |
471 | + | |
472 | + var parts = hash.split('/'); | |
473 | + | |
474 | + var isProposal = regexProposals.exec(hash) !== null; | |
475 | + var isCategory = regexCategory.exec(hash) !== null; | |
476 | + | |
477 | + if( isProposal ){ | |
478 | + | |
479 | + // go to proposal | |
480 | + var proposalId = parts[2]; | |
481 | + this.navigateToProposal(proposalId); | |
482 | + } | |
483 | + | |
484 | + if( isCategory ){ | |
485 | + | |
486 | + // go to category | |
487 | + var categoryId = parts[3]; | |
488 | + this.navigateToCategory(categoryId); | |
489 | + } | |
490 | + | |
491 | + // default | |
492 | + if( !isProposal && !isCategory ){ | |
493 | + // show the 'index' -> category tab | |
494 | + this.display_category_tab(); | |
495 | + } | |
496 | + | |
497 | + $('html, body').animate({ scrollTop: 0 }, 'fast'); | |
498 | + }, | |
499 | + navigateToProposal: function(proposalId){ | |
500 | + if(proposalId === undefined){ | |
501 | + this.display_proposals_tab(); | |
502 | + }else{ | |
503 | + this.display_proposal('proposal-item-' + proposalId); | |
504 | + | |
505 | + // show sub-page | |
506 | + var regexSubpages = /sobre-o-programa$/; | |
507 | + var m; | |
508 | + if((m = regexSubpages.exec(window.location.hash)) !== null ){ | |
509 | + this.display_proposal_detail(proposalId); | |
510 | + } | |
511 | + } | |
512 | + }, | |
513 | + navigateToCategory: function(categoryId){ | |
514 | + if(categoryId === undefined){ | |
515 | + this.display_category_tab(); | |
516 | + }else{ | |
517 | + this.display_proposal_by_category('proposal-item-' + categoryId); | |
518 | + } | |
519 | + }, | |
520 | + addVotedProposal: function(id) { | |
521 | + var votedProposals; | |
522 | + if (typeof($.cookie("votedProposals")) == "undefined"){ | |
523 | + votedProposals = []; | |
524 | + } | |
525 | + else{ | |
526 | + votedProposals = JSON.parse($.cookie("votedProposals")); | |
527 | + } | |
528 | + if (votedProposals.indexOf(id)==-1){ | |
529 | + votedProposals.push(id); | |
530 | + } | |
531 | + $.cookie("votedProposals", JSON.stringify(votedProposals), {expires : 999 }) ; | |
532 | + }, | |
533 | + hasProposalbeenVoted: function(id) { | |
534 | + if (typeof($.cookie("votedProposals")) == "undefined") { | |
535 | + return false; | |
536 | + } | |
537 | + votedProposals = JSON.parse($.cookie("votedProposals")); | |
538 | + return votedProposals.indexOf(id)!=-1; | |
539 | + } | |
210 | 540 | } |
541 | + })(); | |
211 | 542 | |
212 | - var article = data.articles[0]; | |
213 | - $randomProposal.html(supportProposalTemplate(article)); | |
214 | - $body.off('click', '.vote-actions .skip'); | |
215 | - $body.on('click', '.vote-actions .skip', function(e) { | |
216 | - loadRandomProposal(topic_id, private_token); | |
217 | - e.preventDefault(); | |
218 | - }); | |
219 | - $body.off('click', '.vote-actions .like'); | |
220 | - $body.on('click', '.vote-actions .like', function(e) { | |
221 | - //Helps to prevent more than one vote per proposal | |
222 | - if(hasProposalbeenVoted(article.id)){ | |
223 | - console.log("Proposta " + article.id + " já havia sido votada"); | |
224 | - loadRandomProposal(topic_id, private_token); | |
225 | - e.preventDefault(); | |
226 | - return; | |
227 | - } | |
228 | - $.ajax({ | |
229 | - type: 'post', | |
230 | - url: host + '/api/v1/articles/' + article.id + '/vote', | |
231 | - data: { | |
232 | - value: $(this).data('vote-value'), | |
233 | - private_token: private_token | |
234 | - } | |
235 | - }).done(function( /*data*/ ) { | |
236 | - addVotedProposal(article.id); | |
237 | - loadRandomProposal(topic_id, private_token); | |
238 | - }); | |
239 | - e.preventDefault(); | |
240 | - }); | |
241 | - | |
242 | - $body.off('click', '.vote-result'); | |
243 | - $body.on('click', '.vote-result', function(e) { | |
244 | - | |
245 | - var $this = $(this); | |
246 | - var $proposalDetail = $this.parents('.proposal-detail'); | |
247 | - var $resultsContainer = $proposalDetail.find('.results-container'); | |
248 | - | |
249 | - // $resultsContainer.toggle(); | |
250 | - // $resultsContainer.toggleClass('hide'); | |
251 | - | |
252 | - if($resultsContainer.css('display') === 'none') { | |
253 | - | |
254 | - $resultsContainer.find('.loading').show(); | |
255 | - $resultsContainer.find('.results-content').hide(); | |
256 | - | |
257 | - var url = host + '/api/v1/articles/' + topic_id + '/children' + '?private_token=' + private_token + '&limit=10&order=votes_score&fields=id,name,abstract,votes_for,votes_against&content_type=ProposalsDiscussionPlugin::Proposal'; | |
258 | - $.getJSON(url).done(function( data ) { | |
259 | - | |
260 | - $resultsContainer.html(resultsTemplate(data)); | |
261 | - $resultsContainer.find('.loading').hide(); | |
262 | - $resultsContainer.find('.results-content').show(); | |
263 | - $resultsContainer.show(); | |
264 | - | |
265 | - // scroll to the end | |
266 | - $('html, body').animate({ | |
267 | - scrollTop: $(document).height() | |
268 | - }, 'fast'); | |
269 | - }); | |
270 | - $('.experience-proposal-container').hide(); | |
271 | - $('.talk-proposal-container').hide(); | |
272 | - } else { | |
273 | - $('.experience-proposal-container').show(); | |
274 | - $('.talk-proposal-container').show(); | |
275 | - $resultsContainer.hide(); | |
276 | - } | |
277 | - | |
278 | - e.preventDefault(); | |
279 | - }); | |
280 | - }); | |
281 | - } | |
282 | 543 | |
283 | 544 | $(document).ready(function($) { |
284 | 545 | if($.cookie('_dialoga_session')) { |
... | ... | @@ -310,278 +571,29 @@ define(['handlebars'], function(Handlebars){ |
310 | 571 | }); |
311 | 572 | }); |
312 | 573 | |
313 | - function loginCallback(loggedIn, token) { | |
314 | - logged_in = loggedIn; | |
315 | - $('.login .message').text(''); | |
316 | - | |
317 | - if(logged_in) { | |
318 | - if(token){ | |
319 | - private_token = token; | |
320 | - } | |
321 | - loginButton.siblings('.save-article-form').show(); | |
322 | - loginButton.siblings('.save-article-form .message').show(); | |
323 | - loginButton.siblings('.login-container').hide(); | |
324 | - $.cookie('_dialoga_session', private_token); | |
325 | - } else { | |
326 | - loginButton.siblings('.save-article-form').hide(); | |
327 | - loginButton.siblings('.login-container').show(); | |
328 | - } | |
329 | - } | |
330 | - | |
331 | - function oauthPluginHandleLoginResult(loggedIn, token) { | |
332 | - loginCallback(loggedIn, token); | |
333 | - } | |
334 | - | |
335 | - function guid() { | |
336 | - function s4() { | |
337 | - return Math.floor((1 + Math.random()) * 0x10000) | |
338 | - .toString(16) | |
339 | - .substring(1); | |
340 | - } | |
341 | - } | |
342 | - | |
343 | - function display_category_tab(){ | |
344 | - $('#proposal-group').hide(); | |
345 | - $('#proposal-categories').show(); | |
346 | - $('#nav-proposal-categories a').addClass('active'); | |
347 | - $('#nav-proposal-group a').removeClass('active'); | |
348 | - $('.proposal-category-items').hide(); | |
349 | - $('.proposal-category .arrow-box').hide(); | |
350 | - $('.proposal-detail').hide(); | |
351 | - | |
352 | - $('#content').show(); | |
353 | - $('nav').show(); | |
354 | - } | |
355 | - | |
356 | - function display_proposals_tab(){ | |
357 | - $('#proposal-categories').hide(); | |
358 | - $('#proposal-group').show(); | |
359 | - $('#nav-proposal-group a').addClass('active'); | |
360 | - $('#nav-proposal-categories a').removeClass('active'); | |
361 | - $('#content').show(); | |
362 | - $('nav').show(); | |
363 | - } | |
364 | - | |
365 | - function display_proposal(proposal_id){ | |
366 | - $('#proposal-categories').hide(); | |
367 | - $('#proposal-group').hide(); | |
368 | - $('nav').hide(); | |
369 | - $('#content').hide(); | |
370 | - $('.make-proposal-form').hide(); | |
371 | - $('.login-container').hide(); | |
372 | - $('.proposal-detail').hide(); | |
373 | - | |
374 | - $('.proposal-detail-base').hide(); | |
375 | - $('#' + proposal_id).show(); | |
376 | - $('.proposal-header').show(); | |
377 | - $('.make-proposal-container').show(); | |
378 | - $('.support-proposal-container').show(); | |
379 | - $('.results-container').hide(); | |
380 | - $('.results-container .loading').hide(); | |
381 | - $('.results-container .results-content').hide(); | |
382 | - $('.experience-proposal-container').show(); | |
383 | - $('.talk-proposal-container').show(); | |
384 | - | |
385 | - var topic_id = proposal_id.split('-').pop(); | |
386 | - loadRandomProposal(topic_id, private_token); | |
387 | - } | |
388 | - | |
389 | - function display_proposal_detail(proposal_id){ | |
390 | - $('#proposal-categories').hide(); | |
391 | - $('#proposal-group').hide(); | |
392 | - $('nav').hide(); | |
393 | - $('#content').hide(); | |
394 | - $('.make-proposal-form').hide(); | |
395 | - $('.proposal-header').hide(); | |
396 | - $('.make-proposal-container').hide(); | |
397 | - $('.support-proposal-container').hide(); | |
398 | - $('.results-container').hide(); | |
399 | - $('.experience-proposal-container').hide(); | |
400 | - $('.talk-proposal-container').hide(); | |
401 | - $('.body').show(); | |
402 | - | |
403 | - var url = host + '/api/v1/articles/' + proposal_id + '?private_token=' + private_token + '&fields=id,body&content_type=ProposalsDiscussionPlugin::Topic'; | |
404 | - $.getJSON(url).done(function( data ) { | |
405 | - $('.body-content').replaceWith(data.article.body); | |
406 | - }) | |
407 | - .fail(function( jqxhr, textStatus, error ) { | |
408 | - var err = textStatus + ', ' + error; | |
409 | - console.log( 'Request Failed: ' + err ); | |
410 | - }); | |
411 | - } | |
412 | - function display_proposal_by_category(item){ | |
413 | - var $item = $('#' + item); | |
414 | - | |
415 | - if($item.hasClass('proposal-category-items')){ | |
416 | - //Display Topics or Discussion by category | |
417 | - $('nav').show(); | |
418 | - $('#content').show(); | |
419 | - $('#proposal-categories').show(); | |
420 | - $('#nav-proposal-categories a').addClass('active'); | |
421 | - $('#nav-proposal-group a').removeClass('active'); | |
422 | - $('.proposal-category-items').hide(); | |
423 | - $('.proposal-detail').hide(); | |
424 | - $item.toggle( 'blind', 1000 ); | |
425 | - $('.proposal-category .arrow-box').hide(); | |
426 | - var categorySlug = $item.data('category'); | |
427 | - $('#proposal-category-' + categorySlug).find('.arrow-box').show(); | |
428 | - } | |
429 | - } | |
430 | - | |
431 | - var BARRA_ADDED = false; | |
432 | - function addBarraDoGoverno(){ | |
433 | - | |
434 | - if( BARRA_ADDED ) { return; } | |
435 | - | |
436 | - var HTML_BODY_PREPEND = '' + | |
437 | - '<div id="barra-brasil" style="background:#7F7F7F; height: 20px; padding:0 0 0 10px;display:block;"> ' + | |
438 | - '<ul id="menu-barra-temp" style="list-style:none;">' + | |
439 | - '<li style="display:inline; float:left;padding-right:10px; margin-right:10px; border-right:1px solid #EDEDED"><a href="http://brasil.gov.br" style="font-family:sans,sans-serif; text-decoration:none; color:white;">Portal do Governo Brasileiro</a></li> ' + | |
440 | - '<li><a style="font-family:sans,sans-serif; text-decoration:none; color:white;" href="http://epwg.governoeletronico.gov.br/barra/atualize.html">Atualize sua Barra de Governo</a></li>' + | |
441 | - '</ul>' + | |
442 | - '</div>'; | |
443 | - | |
444 | - var HTML_BODY_APPEND = ''+ | |
445 | - '<div id="footer-brasil"></div>' + | |
446 | - '<script defer="defer" src="http://barra.brasil.gov.br/barra.js" type="text/javascript"></script>'; | |
447 | - | |
448 | - var STYLE_TEMA_AZUL = '' + | |
449 | - '<style>'+ | |
450 | - '#footer-brasil {'+ | |
451 | - 'background: none repeat scroll 0% 0% #0042b1;'+ | |
452 | - 'padding: 1em 0px;'+ | |
453 | - 'max-width: 100%;'+ | |
454 | - 'margin-top: 40px;'+ | |
455 | - '}'+ | |
456 | - '#barra-brasil ul {'+ | |
457 | - 'width: auto;'+ | |
458 | - '}'+ | |
459 | - '<style>'; | |
460 | - | |
461 | - var $body = $(document.body); | |
462 | - $body.prepend(HTML_BODY_PREPEND); | |
463 | - $body.append(HTML_BODY_APPEND); | |
464 | - $body.append(STYLE_TEMA_AZUL); | |
465 | - | |
466 | - BARRA_ADDED = true; | |
467 | - } | |
468 | 574 | |
469 | - function updateHash(hash){ | |
470 | - var id = hash.replace(/^.*#/, ''); | |
471 | - var elem = document.getElementById(id); | |
472 | 575 | |
473 | - // preserve the query param | |
474 | - // if (HIDE_BARRA_DO_GOVERNO && (hash.indexOf('?barra=false') === -1)){ | |
475 | - // hash += '?barra=false'; | |
476 | - // } | |
477 | 576 | |
478 | - if ( !elem ) { | |
479 | - window.location.hash = hash; | |
480 | - return; | |
481 | - } | |
482 | 577 | |
483 | - elem.id = id+'-tmp'; | |
484 | - window.location.hash = hash; | |
485 | - elem.id = id; | |
486 | - } | |
487 | 578 | |
488 | - function locationHashChanged(){ | |
489 | - var hash = window.location.hash; | |
490 | - navigateTo(hash); | |
491 | - } | |
492 | 579 | |
493 | - var HIDE_BARRA_DO_GOVERNO = false; | |
494 | - function navigateTo(hash){ | |
495 | - var regexProposals = /#\/programas/; | |
496 | - var regexCategory = /#\/temas/; | |
497 | - var regexHideBarra = /barra=false$/; | |
498 | 580 | |
499 | - if( !(regexHideBarra.exec(hash) !== null) && !HIDE_BARRA_DO_GOVERNO ){ | |
500 | - addBarraDoGoverno(); | |
501 | - }else{ | |
502 | - HIDE_BARRA_DO_GOVERNO = true; | |
503 | - } | |
504 | 581 | |
505 | - // remove query params | |
506 | - hash = hash.split('?')[0]; | |
507 | 582 | |
508 | - var parts = hash.split('/'); | |
509 | 583 | |
510 | - var isProposal = regexProposals.exec(hash) !== null; | |
511 | - var isCategory = regexCategory.exec(hash) !== null; | |
512 | 584 | |
513 | - if( isProposal ){ | |
514 | - | |
515 | - // go to proposal | |
516 | - var proposalId = parts[2]; | |
517 | - navigateToProposal(proposalId); | |
518 | - } | |
519 | 585 | |
520 | - if( isCategory ){ | |
521 | - | |
522 | - // go to category | |
523 | - var categoryId = parts[3]; | |
524 | - navigateToCategory(categoryId); | |
525 | - } | |
526 | - | |
527 | - // default | |
528 | - if( !isProposal && !isCategory ){ | |
529 | - // show the 'index' -> category tab | |
530 | - display_category_tab(); | |
531 | - } | |
532 | - | |
533 | - $('html, body').animate({ scrollTop: 0 }, 'fast'); | |
534 | - } | |
586 | + if('onhashchange' in window){ | |
535 | 587 | |
536 | - function navigateToProposal(proposalId){ | |
537 | - if(proposalId === undefined){ | |
538 | - display_proposals_tab(); | |
539 | - }else{ | |
540 | - display_proposal('proposal-item-' + proposalId); | |
541 | - | |
542 | - // show sub-page | |
543 | - var regexSubpages = /sobre-o-programa$/; | |
544 | - var m; | |
545 | - if((m = regexSubpages.exec(window.location.hash)) !== null ){ | |
546 | - display_proposal_detail(proposalId); | |
547 | - } | |
588 | + window.onhashchange = function(){ | |
589 | + Main.locationHashChanged.apply(Main); | |
548 | 590 | } |
549 | - } | |
550 | 591 | |
551 | - function navigateToCategory(categoryId){ | |
552 | - if(categoryId === undefined){ | |
553 | - display_category_tab(); | |
554 | - }else{ | |
555 | - display_proposal_by_category('proposal-item-' + categoryId); | |
556 | - } | |
592 | + }else{ | |
593 | + console.log('The browser not supports the hashchange event!'); | |
557 | 594 | } |
558 | 595 | |
559 | - function addVotedProposal(id) { | |
560 | - var votedProposals; | |
561 | - if (typeof($.cookie("votedProposals")) == "undefined"){ | |
562 | - votedProposals = []; | |
563 | - } | |
564 | - else{ | |
565 | - votedProposals = JSON.parse($.cookie("votedProposals")); | |
566 | - } | |
567 | - if (votedProposals.indexOf(id)==-1){ | |
568 | - votedProposals.push(id); | |
569 | - } | |
570 | - $.cookie("votedProposals", JSON.stringify(votedProposals), {expires : 999 }) ; | |
571 | - } | |
572 | 596 | |
573 | - function hasProposalbeenVoted(id) { | |
574 | - if (typeof($.cookie("votedProposals")) == "undefined") { | |
575 | - return false; | |
576 | - } | |
577 | - votedProposals = JSON.parse($.cookie("votedProposals")); | |
578 | - return votedProposals.indexOf(id)!=-1; | |
579 | - } | |
580 | 597 | |
581 | - if('onhashchange' in window){ | |
582 | - window.onhashchange = locationHashChanged; | |
583 | - }else{ | |
584 | - console.log('The browser not supports the hashchange event!'); | |
585 | - } | |
586 | 598 | |
587 | 599 | }); | ... | ... |
test/SpecRunner.html
... | ... | @@ -14,12 +14,47 @@ |
14 | 14 | <script src="lib/jasmine-2.2.0/jasmine-html.js"></script> |
15 | 15 | <script src="lib/jasmine-2.2.0/boot.js"></script> |
16 | 16 | |
17 | - <!-- include source files here... --> | |
18 | - <script src="../js/main.js"></script> | |
17 | + <script type="text/javascript"> | |
18 | + | |
19 | + var require = { | |
20 | + paths: { | |
21 | + "domReady": "lib/requirejs-plugins/domReady", | |
22 | + "model": "../js" | |
23 | + } | |
24 | + }; | |
25 | + | |
26 | + </script> | |
19 | 27 | |
28 | + <!-- include source files here... --> | |
29 | + <!-- <script src="../js/main.js"></script> --> | |
20 | 30 | |
21 | 31 | <!-- include spec files here... --> |
22 | - <script src="spec/MainSpec.js"></script> | |
32 | + | |
33 | + <script type="text/javascript"> | |
34 | + | |
35 | + require( | |
36 | + [ | |
37 | + "domReady!", | |
38 | + "spec/MainSpec" | |
39 | + ], | |
40 | + function( document ){ | |
41 | + | |
42 | + // Set up the HTML reporter - this is reponsible for | |
43 | + // aggregating the results reported by Jasmine as the | |
44 | + // tests and suites are executed. | |
45 | + jasmine.getEnv().addReporter( | |
46 | + new jasmine.HtmlReporter() | |
47 | + ); | |
48 | + | |
49 | + // Run all the loaded test specs. | |
50 | + jasmine.getEnv().execute(); | |
51 | + | |
52 | + } | |
53 | + ); | |
54 | + | |
55 | + </script> | |
56 | + | |
57 | + <!-- // <script src="spec/MainSpec.js"></script> --> | |
23 | 58 | |
24 | 59 | </head> |
25 | 60 | ... | ... |
... | ... | @@ -0,0 +1,129 @@ |
1 | +/** | |
2 | + * @license RequireJS domReady 2.0.1 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved. | |
3 | + * Available via the MIT or new BSD license. | |
4 | + * see: http://github.com/requirejs/domReady for details | |
5 | + */ | |
6 | +/*jslint */ | |
7 | +/*global require: false, define: false, requirejs: false, | |
8 | + window: false, clearInterval: false, document: false, | |
9 | + self: false, setInterval: false */ | |
10 | + | |
11 | + | |
12 | +define(function () { | |
13 | + 'use strict'; | |
14 | + | |
15 | + var isTop, testDiv, scrollIntervalId, | |
16 | + isBrowser = typeof window !== "undefined" && window.document, | |
17 | + isPageLoaded = !isBrowser, | |
18 | + doc = isBrowser ? document : null, | |
19 | + readyCalls = []; | |
20 | + | |
21 | + function runCallbacks(callbacks) { | |
22 | + var i; | |
23 | + for (i = 0; i < callbacks.length; i += 1) { | |
24 | + callbacks[i](doc); | |
25 | + } | |
26 | + } | |
27 | + | |
28 | + function callReady() { | |
29 | + var callbacks = readyCalls; | |
30 | + | |
31 | + if (isPageLoaded) { | |
32 | + //Call the DOM ready callbacks | |
33 | + if (callbacks.length) { | |
34 | + readyCalls = []; | |
35 | + runCallbacks(callbacks); | |
36 | + } | |
37 | + } | |
38 | + } | |
39 | + | |
40 | + /** | |
41 | + * Sets the page as loaded. | |
42 | + */ | |
43 | + function pageLoaded() { | |
44 | + if (!isPageLoaded) { | |
45 | + isPageLoaded = true; | |
46 | + if (scrollIntervalId) { | |
47 | + clearInterval(scrollIntervalId); | |
48 | + } | |
49 | + | |
50 | + callReady(); | |
51 | + } | |
52 | + } | |
53 | + | |
54 | + if (isBrowser) { | |
55 | + if (document.addEventListener) { | |
56 | + //Standards. Hooray! Assumption here that if standards based, | |
57 | + //it knows about DOMContentLoaded. | |
58 | + document.addEventListener("DOMContentLoaded", pageLoaded, false); | |
59 | + window.addEventListener("load", pageLoaded, false); | |
60 | + } else if (window.attachEvent) { | |
61 | + window.attachEvent("onload", pageLoaded); | |
62 | + | |
63 | + testDiv = document.createElement('div'); | |
64 | + try { | |
65 | + isTop = window.frameElement === null; | |
66 | + } catch (e) {} | |
67 | + | |
68 | + //DOMContentLoaded approximation that uses a doScroll, as found by | |
69 | + //Diego Perini: http://javascript.nwbox.com/IEContentLoaded/, | |
70 | + //but modified by other contributors, including jdalton | |
71 | + if (testDiv.doScroll && isTop && window.external) { | |
72 | + scrollIntervalId = setInterval(function () { | |
73 | + try { | |
74 | + testDiv.doScroll(); | |
75 | + pageLoaded(); | |
76 | + } catch (e) {} | |
77 | + }, 30); | |
78 | + } | |
79 | + } | |
80 | + | |
81 | + //Check if document already complete, and if so, just trigger page load | |
82 | + //listeners. Latest webkit browsers also use "interactive", and | |
83 | + //will fire the onDOMContentLoaded before "interactive" but not after | |
84 | + //entering "interactive" or "complete". More details: | |
85 | + //http://dev.w3.org/html5/spec/the-end.html#the-end | |
86 | + //http://stackoverflow.com/questions/3665561/document-readystate-of-interactive-vs-ondomcontentloaded | |
87 | + //Hmm, this is more complicated on further use, see "firing too early" | |
88 | + //bug: https://github.com/requirejs/domReady/issues/1 | |
89 | + //so removing the || document.readyState === "interactive" test. | |
90 | + //There is still a window.onload binding that should get fired if | |
91 | + //DOMContentLoaded is missed. | |
92 | + if (document.readyState === "complete") { | |
93 | + pageLoaded(); | |
94 | + } | |
95 | + } | |
96 | + | |
97 | + /** START OF PUBLIC API **/ | |
98 | + | |
99 | + /** | |
100 | + * Registers a callback for DOM ready. If DOM is already ready, the | |
101 | + * callback is called immediately. | |
102 | + * @param {Function} callback | |
103 | + */ | |
104 | + function domReady(callback) { | |
105 | + if (isPageLoaded) { | |
106 | + callback(doc); | |
107 | + } else { | |
108 | + readyCalls.push(callback); | |
109 | + } | |
110 | + return domReady; | |
111 | + } | |
112 | + | |
113 | + domReady.version = '2.0.1'; | |
114 | + | |
115 | + /** | |
116 | + * Loader Plugin API method | |
117 | + */ | |
118 | + domReady.load = function (name, req, onLoad, config) { | |
119 | + if (config.isBuild) { | |
120 | + onLoad(null); | |
121 | + } else { | |
122 | + domReady(onLoad); | |
123 | + } | |
124 | + }; | |
125 | + | |
126 | + /** END OF PUBLIC API **/ | |
127 | + | |
128 | + return domReady; | |
129 | +}); | ... | ... |
test/spec/MainSpec.js
1 | -describe("addVotedProposal", function(){ | |
2 | - it("Should register a voted id", function(){ | |
3 | - addVotedProposal(1).toEqual(true); | |
1 | +define(["model/main"],function(){ | |
2 | + | |
3 | + describe("addVotedProposal", function(){ | |
4 | + it("Should register a voted id", function(){ | |
5 | + addVotedProposal(1).toEqual(true); | |
6 | + }); | |
4 | 7 | }); |
5 | 8 | }); | ... | ... |