Commit 46a41fff81ab407234091a1b0ceeb81faca77b49

Authored by Evandro Junior
1 parent 61d1a008

Refactored limit proposal voting using cookie persistence

to another object to allow unit testing with Jasmine
@@ -7,33 +7,58 @@ @@ -7,33 +7,58 @@
7 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <meta name="viewport" content="width=device-width, initial-scale=1">
8 8
9 <script type="text/javascript"> 9 <script type="text/javascript">
10 - function removeUrlParameters(full_url){  
11 - var url_base = full_url.split('#').shift();  
12 - return url_base;  
13 - }  
14 - function addBaseUrl(filename){  
15 - var base = window.location.href;  
16 - if(base.match(/file:/) || filename.match(/http:/)){  
17 - base = '';  
18 - }else{  
19 - base = removeUrlParameters(base) + '/'; 10 + /**
  11 + * The Url created with module pattern
  12 + *
  13 + * @todo Moves the module to another single js file
  14 + */
  15 + var Url = (function(){
  16 +
  17 + return {
  18 +
  19 + initBase: function(){
  20 + var base = window.location.href;
  21 + var host = window.location.host;
  22 + var regex = new RegExp(".*" + host + '/', "g");
  23 +
  24 + if(host){
  25 + base = base.replace(regex,'');
  26 + }else{
  27 + base = base.replace('index.html','');
  28 + }
  29 + return base
  30 + },
  31 + removeUrlParameters: function(full_url){
  32 + var url_base = full_url.split('#').shift();
  33 + return url_base;
  34 + },
  35 + addBaseUrl: function(filename){
  36 + var base = window.location.href;
  37 + if(base.match(/file:/) || filename.match(/http:/)){
  38 + base = '';
  39 + }else{
  40 + base = this.removeUrlParameters(base) + '/';
  41 + }
  42 + return base + filename;
  43 + }
20 } 44 }
21 - return base + filename;  
22 - } 45 +
  46 + })();
  47 +
23 function makeCSSLink(filename){ 48 function makeCSSLink(filename){
24 var fileref=document.createElement('link'); 49 var fileref=document.createElement('link');
25 fileref.setAttribute('rel', 'stylesheet'); 50 fileref.setAttribute('rel', 'stylesheet');
26 fileref.setAttribute('type', 'text/css'); 51 fileref.setAttribute('type', 'text/css');
27 - fileref.setAttribute('href', addBaseUrl(filename)); 52 + fileref.setAttribute('href', Url.addBaseUrl(filename));
28 return fileref; 53 return fileref;
29 } 54 }
30 function loadCSSFiles(){ 55 function loadCSSFiles(){
31 var css_files = [ 56 var css_files = [
32 - 'css/bootstrap.min.css',  
33 - 'css/hover.custom.css',  
34 - 'http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css',  
35 - 'http://fonts.googleapis.com/css?family=Open+Sans:400,300,700',  
36 - 'http://fonts.googleapis.com/css?family=Asap:400,700', 57 + 'css/bootstrap.min.css',
  58 + 'css/hover.custom.css',
  59 + 'http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css',
  60 + 'http://fonts.googleapis.com/css?family=Open+Sans:400,300,700',
  61 + 'http://fonts.googleapis.com/css?family=Asap:400,700',
37 'style.css' 62 'style.css'
38 ]; 63 ];
39 head = document.getElementsByTagName('head')[0] 64 head = document.getElementsByTagName('head')[0]
@@ -42,8 +67,8 @@ @@ -42,8 +67,8 @@
42 } 67 }
43 } 68 }
44 function makeJSURL(filename){ 69 function makeJSURL(filename){
45 - var url = addBaseUrl(filename);  
46 - 70 + var url = Url.addBaseUrl(filename);
  71 +
47 return url; 72 return url;
48 } 73 }
49 74
@@ -66,17 +91,17 @@ @@ -66,17 +91,17 @@
66 91
67 <div id="proposal-result"></div> 92 <div id="proposal-result"></div>
68 <div class="clearfix"></div> 93 <div class="clearfix"></div>
69 - 94 +
70 <script id="proposal-template" type="text/x-handlebars-template"> 95 <script id="proposal-template" type="text/x-handlebars-template">
71 <header> 96 <header>
72 <h1><a href="#">{{article.title}}</a></h1> 97 <h1><a href="#">{{article.title}}</a></h1>
73 <a id="display-contrast" href="#">Alto Contraste</a> 98 <a id="display-contrast" href="#">Alto Contraste</a>
74 </header> 99 </header>
75 - 100 +
76 <div id="content"> 101 <div id="content">
77 {{{article.body}}} 102 {{{article.body}}}
78 </div> 103 </div>
79 - 104 +
80 <nav role="tabpanel"> 105 <nav role="tabpanel">
81 <ul role="tablist"> 106 <ul role="tablist">
82 <li id="nav-proposal-categories" role="presentation"> 107 <li id="nav-proposal-categories" role="presentation">
@@ -112,7 +137,7 @@ @@ -112,7 +137,7 @@
112 </div> 137 </div>
113 </div> 138 </div>
114 139
115 - <div id="proposal-group" class="hide"> 140 + <div id="proposal-group" class="hide">
116 <label for="search-input">Selecione um programa para fazer propostas e contar sua experiência.</label> 141 <label for="search-input">Selecione um programa para fazer propostas e contar sua experiência.</label>
117 <div id="search-input-container"> 142 <div id="search-input-container">
118 <input placeholder="palavra chave" id="search-input" type="text" name="search" /> 143 <input placeholder="palavra chave" id="search-input" type="text" name="search" />
@@ -160,7 +185,7 @@ @@ -160,7 +185,7 @@
160 <span class="fa fa-reply"></span> 185 <span class="fa fa-reply"></span>
161 Agora contribua para a melhoria desse programa 186 Agora contribua para a melhoria desse programa
162 </a> 187 </a>
163 - </div> 188 + </div>
164 </div> 189 </div>
165 190
166 <div class="make-proposal-container"> 191 <div class="make-proposal-container">
@@ -303,7 +328,7 @@ @@ -303,7 +328,7 @@
303 </script> 328 </script>
304 329
305 <script type='text/javascript' > 330 <script type='text/javascript' >
306 -loadRequireJS(); 331 + loadRequireJS();
307 </script> 332 </script>
308 333
309 334
1 -define(['handlebars','handlebars_helpers','jquery'], function(Handlebars){ 1 +define(['handlebars','handlebars_helpers'], function(Handlebars){
2 2
3 3
4 /* global Handlebars, $ */ 4 /* global Handlebars, $ */
@@ -227,7 +227,7 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;,&#39;jquery&#39;], function(Handlebars){ @@ -227,7 +227,7 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;,&#39;jquery&#39;], function(Handlebars){
227 $body.off('click', '.vote-actions .like'); 227 $body.off('click', '.vote-actions .like');
228 $body.on('click', '.vote-actions .like', function(e) { 228 $body.on('click', '.vote-actions .like', function(e) {
229 //Helps to prevent more than one vote per proposal 229 //Helps to prevent more than one vote per proposal
230 - if(contextMain.hasProposalbeenVoted(article.id)){ 230 + if(ProposalApp.hasProposalbeenVoted(article.id)){
231 console.log("Proposta " + article.id + " já havia sido votada"); 231 console.log("Proposta " + article.id + " já havia sido votada");
232 contextMain.loadRandomProposal(topic_id, private_token); 232 contextMain.loadRandomProposal(topic_id, private_token);
233 e.preventDefault(); 233 e.preventDefault();
@@ -241,7 +241,7 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;,&#39;jquery&#39;], function(Handlebars){ @@ -241,7 +241,7 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;,&#39;jquery&#39;], function(Handlebars){
241 private_token: private_token 241 private_token: private_token
242 } 242 }
243 }).done(function( /*data*/ ) { 243 }).done(function( /*data*/ ) {
244 - contextMain.addVotedProposal(article.id); 244 + ProposalApp.addVotedProposal(article.id);
245 contextMain.loadRandomProposal(topic_id, private_token); 245 contextMain.loadRandomProposal(topic_id, private_token);
246 }); 246 });
247 e.preventDefault(); 247 e.preventDefault();
@@ -516,28 +516,7 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;,&#39;jquery&#39;], function(Handlebars){ @@ -516,28 +516,7 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;,&#39;jquery&#39;], function(Handlebars){
516 }else{ 516 }else{
517 this.display_proposal_by_category('proposal-item-' + categoryId); 517 this.display_proposal_by_category('proposal-item-' + categoryId);
518 } 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 - return votedProposals;  
533 - },  
534 - hasProposalbeenVoted: function(id) {  
535 - if (typeof($.cookie("votedProposals")) == "undefined") {  
536 - return false;  
537 - }  
538 - votedProposals = JSON.parse($.cookie("votedProposals"));  
539 - return votedProposals.indexOf(id)!=-1;  
540 - } 519 + }
541 } 520 }
542 })(); 521 })();
543 522
js/proposal-app.js 0 → 100644
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
  1 +define(['jquery','jquery_cookie'],function($){
  2 +
  3 +
  4 + ProposalApp = (function (){
  5 +
  6 + return {
  7 + addVotedProposal: function(id) {
  8 + var votedProposals;
  9 + if (typeof($.cookie("votedProposals")) == "undefined"){
  10 + votedProposals = [];
  11 + }
  12 + else{
  13 + votedProposals = JSON.parse($.cookie("votedProposals"));
  14 + }
  15 + if (votedProposals.indexOf(id)==-1){
  16 + votedProposals.push(id);
  17 + }
  18 + $.cookie("votedProposals", JSON.stringify(votedProposals), {expires : 999 }) ;
  19 + return votedProposals;
  20 + },
  21 +
  22 + hasProposalbeenVoted: function(id) {
  23 + if (typeof($.cookie("votedProposals")) == "undefined") {
  24 + return false;
  25 + }
  26 + votedProposals = JSON.parse($.cookie("votedProposals"));
  27 + return votedProposals.indexOf(id)!=-1;
  28 + }
  29 + };
  30 +
  31 + })();
  32 +
  33 + return ProposalApp;
  34 +});
js/requirejs-config.js
1 -var base = window.location.href;  
2 -var host = window.location.host;  
3 -var regex = new RegExp(".*" + host + '/', "g");  
4 -  
5 -if(host){  
6 - base = base.replace(regex,''); 1 +if(window.Url){
  2 + var base = Url.initBase();
  3 + base = Url.removeUrlParameters(base) + '/js/';
7 }else{ 4 }else{
8 - base = base.replace('index.html',''); 5 + base = '../js'
9 } 6 }
10 -base = removeUrlParameters(base);  
11 7
12 requirejs.config({ 8 requirejs.config({
13 - baseUrl: ( base + '/js/'), 9 + baseUrl: base,
14 waitSeconds: 0, 10 waitSeconds: 0,
15 paths: { 11 paths: {
16 jquery: 'jquery-2.1.3.min', 12 jquery: 'jquery-2.1.3.min',
17 - jquery_ui: 'jquery-ui-1.11.4.custom/jquery-ui.min',  
18 - jquery_cookie: 'jquery.cookie', 13 + jquery_ui: 'jquery-ui-1.11.4.custom/jquery-ui.min',
  14 + jquery_cookie: 'jquery.cookie',
19 handlebars: 'handlebars-v3.0.1', 15 handlebars: 'handlebars-v3.0.1',
20 handlebars_helpers: 'handlebars-helpers', 16 handlebars_helpers: 'handlebars-helpers',
21 jquery_maxlength: 'jquery.maxlength.min', 17 jquery_maxlength: 'jquery.maxlength.min',
22 layout: 'layout', 18 layout: 'layout',
23 - main: 'main' 19 + main: 'main',
  20 + proposal_app: 'proposal-app'
24 }, 21 },
25 shim: { 22 shim: {
26 'handlebars':{ 23 'handlebars':{
@@ -38,9 +35,13 @@ requirejs.config({ @@ -38,9 +35,13 @@ requirejs.config({
38 }, 35 },
39 'handlebars_helpers':{ 36 'handlebars_helpers':{
40 deps: ['handlebars'] 37 deps: ['handlebars']
  38 + },
  39 + 'proposal_app' :{
  40 + deps: ['jquery'],
  41 + exports: 'ProposalApp'
41 } 42 }
42 } 43 }
43 }); 44 });
44 45
45 -requirejs(['jquery', 'jquery_ui', 'jquery_cookie', 'handlebars', 'handlebars_helpers']);  
46 -requirejs(['jquery_maxlength', 'layout', 'main']); 46 +requirejs(['jquery', 'proposal_app', 'jquery_ui','handlebars_helpers']);
  47 +requirejs(['jquery_maxlength', 'layout','main']);