Commit 1ac5135310a32febbe0b83236b2e1adfea13898b

Authored by Michel Felipe
1 parent 13e18eaf
Exists in jasmine-testing

Refactory Url object and require.js deps to another paths configurations

@@ -14,32 +14,46 @@ @@ -14,32 +14,46 @@
14 */ 14 */
15 var Url = (function(){ 15 var Url = (function(){
16 16
  17 + var _base = '';
  18 +
17 return { 19 return {
18 20
19 initBase: function(){ 21 initBase: function(){
20 - var base = window.location.href; 22 + _base = window.location.href;
21 var host = window.location.host; 23 var host = window.location.host;
22 var regex = new RegExp(".*" + host + '/', "g"); 24 var regex = new RegExp(".*" + host + '/', "g");
23 25
24 if(host){ 26 if(host){
25 - base = base.replace(regex,''); 27 + _base = _base.replace(regex,'');
26 }else{ 28 }else{
27 - base = base.replace('index.html',''); 29 + _base = _base.replace('index.html','');
  30 + }
  31 + return this;
  32 + },
  33 + getBase: function(){
  34 +
  35 + if(!_base){
  36 + this.initBase();
28 } 37 }
29 - return base 38 +
  39 + return _base;
30 }, 40 },
31 removeUrlParameters: function(full_url){ 41 removeUrlParameters: function(full_url){
  42 + full_url = full_url || _base;
32 var url_base = full_url.split('#').shift(); 43 var url_base = full_url.split('#').shift();
33 return url_base; 44 return url_base;
34 }, 45 },
35 addBaseUrl: function(filename){ 46 addBaseUrl: function(filename){
36 - var base = window.location.href; 47 + base = window.location.href;
37 if(base.match(/file:/) || filename.match(/http:/)){ 48 if(base.match(/file:/) || filename.match(/http:/)){
38 base = ''; 49 base = '';
39 }else{ 50 }else{
40 base = this.removeUrlParameters(base) + '/'; 51 base = this.removeUrlParameters(base) + '/';
41 } 52 }
42 return base + filename; 53 return base + filename;
  54 + },
  55 + toString: function(){
  56 + return this.getBase();
43 } 57 }
44 } 58 }
45 59
@@ -75,12 +89,21 @@ @@ -75,12 +89,21 @@
75 function loadJavaScriptAsync(file_path){ 89 function loadJavaScriptAsync(file_path){
76 } 90 }
77 91
78 - function loadRequireJS(){ 92 + function loadRequireJS(onLoad){
79 var head = document.getElementsByTagName('head')[0]; 93 var head = document.getElementsByTagName('head')[0];
80 var script_element = document.createElement('script'); 94 var script_element = document.createElement('script');
81 script_element.type = 'text/javascript'; 95 script_element.type = 'text/javascript';
82 script_element.src = makeJSURL('js/require.js'); 96 script_element.src = makeJSURL('js/require.js');
83 - script_element.setAttribute('data-main', makeJSURL('js/requirejs-config.js')); 97 + // script_element.setAttribute('data-main', makeJSURL('js/requirejs-config.js'));
  98 + if(onLoad instanceof Function){
  99 + script_element.onreadystatechange = function(){
  100 + if (this.readyState == 'complete'){
  101 + onload();
  102 + }
  103 + }
  104 + script_element.addEventListener('load', onLoad);
  105 + }
  106 +
84 head.appendChild(script_element); 107 head.appendChild(script_element);
85 } 108 }
86 loadCSSFiles(); 109 loadCSSFiles();
@@ -197,7 +220,7 @@ @@ -197,7 +220,7 @@
197 <div class="info">Ela se tornará pública em até 24 horas após o envio e poderá ser apoiada por outros participantes.</div> 220 <div class="info">Ela se tornará pública em até 24 horas após o envio e poderá ser apoiada por outros participantes.</div>
198 <div class="send-proposal-button send-button"><a href="#"><span>Envie Sua Proposta</span></a></div> 221 <div class="send-proposal-button send-button"><a href="#"><span>Envie Sua Proposta</span></a></div>
199 <div class="login-container hide">Login</div> 222 <div class="login-container hide">Login</div>
200 - <form class="make-proposal-form save-article-form hide" id="make-proposal-form-{{id}}" action="{{proposal_action ../article . }}" method="post"> 223 + <form class="make-proposal-form save-article-form hide" id="make-proposal-form-{{id}}" action="{{proposal_action ../article . }}">
201 <div class="message hide"></div> 224 <div class="message hide"></div>
202 <div> 225 <div>
203 <div><label for="article_abstract">Descrição</label></div> 226 <div><label for="article_abstract">Descrição</label></div>
@@ -230,7 +253,7 @@ @@ -230,7 +253,7 @@
230 <p>Esta história pode nos ajudar a melhorar a nossa ação e não será divulgada.</p> 253 <p>Esta história pode nos ajudar a melhorar a nossa ação e não será divulgada.</p>
231 <div class="send-experience-button send-button"><a href="#"><span>Envie Sua Experiência</span></a></div> 254 <div class="send-experience-button send-button"><a href="#"><span>Envie Sua Experiência</span></a></div>
232 <div class="login-container hide">Login</div> 255 <div class="login-container hide">Login</div>
233 - <form class="make-experience-form save-article-form hide" id="make-experience-form-{{id}}" action="/api/v1/articles/{{id}}/children" method="post"> 256 + <form class="make-experience-form save-article-form hide" id="make-experience-form-{{id}}" action="/api/v1/articles/{{id}}/children">
234 <div class="message hide"></div> 257 <div class="message hide"></div>
235 <div> 258 <div>
236 <div> 259 <div>
@@ -258,7 +281,7 @@ @@ -258,7 +281,7 @@
258 <div class="date"><i class="fa fa-calendar"></i> 17/06/2015</div><div class="time"><i class="fa fa-clock-o"></i> 19:00</div> 281 <div class="date"><i class="fa fa-calendar"></i> 17/06/2015</div><div class="time"><i class="fa fa-clock-o"></i> 19:00</div>
259 </li> 282 </li>
260 <li> 283 <li>
261 - <span>Tereza Campello</span> 284 + <span>Teresa Campello</span>
262 <span>Desenvolvimento Social</span> 285 <span>Desenvolvimento Social</span>
263 <div class="date"><i class="fa fa-calendar"></i> 18/06/2015</div><div class="time"><i class="fa fa-clock-o"></i> 19:00</div> 286 <div class="date"><i class="fa fa-calendar"></i> 18/06/2015</div><div class="time"><i class="fa fa-clock-o"></i> 19:00</div>
264 </li> 287 </li>
@@ -333,17 +356,22 @@ @@ -333,17 +356,22 @@
333 </div> 356 </div>
334 <div class="oauth"> 357 <div class="oauth">
335 <div class="label">Acessar com:</div> 358 <div class="label">Acessar com:</div>
336 - <a href="http://www.participa.br/plugin/oauth_client/facebook?oauth_client_popup=true&id=1" target="_blank" class="facebook oauth-login">Facebook</a>  
337 - <a href="http://www.participa.br/plugin/oauth_client/google_oauth2?oauth_client_popup=true&id=2" target="_blank" class="google oauth-login">Google +</a> 359 + <a href="/plugin/oauth_client/facebook?oauth_client_popup=true&id=1" target="_blank" class="facebook">Facebook</a>
  360 + <a href="/plugin/oauth_client/google_oauth2?oauth_client_popup=true&id=2" target="_blank" class="google">Google +</a>
338 <a href="http://www.participa.br/account/signup" target="_blank" class="new-user">Cadastrar</a> 361 <a href="http://www.participa.br/account/signup" target="_blank" class="new-user">Cadastrar</a>
339 </div> 362 </div>
340 </form> 363 </form>
341 </script> 364 </script>
342 365
343 - <script type='text/javascript' >  
344 - loadRequireJS();  
345 - </script> 366 + <script type='text/javascript'>
  367 + loadRequireJS(function(){
  368 +
  369 + require([ Url.getBase()+'/js/requirejs-config' ], function(){
  370 + requirejs(['proposal_app','main']);
  371 + });
346 372
  373 + });
  374 + </script>
347 375
348 </body> 376 </body>
349 377
1 -define(['handlebars','handlebars_helpers'], function(Handlebars){ 1 +define(['handlebars_helpers','jquery_ui','jquery_slick', 'jquery_maxlength', 'layout'], function(Handlebars){
2 2
3 /* global Handlebars, $ */ 3 /* global Handlebars, $ */
4 // The template code 4 // The template code
@@ -43,11 +43,6 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){ @@ -43,11 +43,6 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){
43 43
44 Main.navigateTo(window.location.hash); 44 Main.navigateTo(window.location.hash);
45 45
46 - $('.oauth-login').on('click touchstart', function(e) {  
47 - Main.oauthClientAction($(this).attr('href'));  
48 - e.preventDefault();  
49 - });  
50 -  
51 //Actions for links 46 //Actions for links
52 $( '#nav-proposal-categories a' ).on('click touchstart', function(e){ 47 $( '#nav-proposal-categories a' ).on('click touchstart', function(e){
53 e.preventDefault(); 48 e.preventDefault();
@@ -372,7 +367,6 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){ @@ -372,7 +367,6 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){
372 $('.results-container').hide(); 367 $('.results-container').hide();
373 $('.experience-proposal-container').hide(); 368 $('.experience-proposal-container').hide();
374 $('.talk-proposal-container').hide(); 369 $('.talk-proposal-container').hide();
375 - $('#proposal-item-' + proposal_id + '.proposal-detail').show();  
376 $('#proposal-item-' + proposal_id + ' .body').show(); 370 $('#proposal-item-' + proposal_id + ' .body').show();
377 371
378 var url = host + '/api/v1/articles/' + proposal_id + '?private_token=' + private_token + '&fields=id,body&content_type=ProposalsDiscussionPlugin::Topic'; 372 var url = host + '/api/v1/articles/' + proposal_id + '?private_token=' + private_token + '&fields=id,body&content_type=ProposalsDiscussionPlugin::Topic';
@@ -502,13 +496,17 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){ @@ -502,13 +496,17 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){
502 $('html, body').animate({ scrollTop: 0 }, 'fast'); 496 $('html, body').animate({ scrollTop: 0 }, 'fast');
503 }, 497 },
504 navigateToProposal: function(proposalId){ 498 navigateToProposal: function(proposalId){
505 - var regexSubpages = /sobre-o-programa$/;  
506 if(proposalId === undefined){ 499 if(proposalId === undefined){
507 this.display_proposals_tab(); 500 this.display_proposals_tab();
508 - }else if(regexSubpages.exec(window.location.hash) == null){  
509 - this.display_proposal('proposal-item-' + proposalId);  
510 }else{ 501 }else{
511 - this.display_proposal_detail(proposalId); 502 + this.display_proposal('proposal-item-' + proposalId);
  503 +
  504 + // show sub-page
  505 + var regexSubpages = /sobre-o-programa$/;
  506 + var m;
  507 + if((m = regexSubpages.exec(window.location.hash)) !== null ){
  508 + this.display_proposal_detail(proposalId);
  509 + }
512 } 510 }
513 }, 511 },
514 navigateToCategory: function(categoryId){ 512 navigateToCategory: function(categoryId){
@@ -517,23 +515,6 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){ @@ -517,23 +515,6 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){
517 }else{ 515 }else{
518 this.display_proposal_by_category('proposal-item-' + categoryId); 516 this.display_proposal_by_category('proposal-item-' + categoryId);
519 } 517 }
520 - },  
521 - oauthClientAction: function(url) {  
522 - var child = window.open(url, "_blank");  
523 - var interval = setInterval(function() {  
524 - try {  
525 - if(!child.closed) {  
526 - child.postMessage({ message: "requestOauthClientPluginResult" }, "*");  
527 - }  
528 - }  
529 - catch(e) {  
530 - // we're here when the child window has been navigated away or closed  
531 - if (child.closed) {  
532 - clearInterval(interval);  
533 - return;  
534 - }  
535 - }  
536 - }, 300);  
537 } 518 }
538 } 519 }
539 })(); 520 })();
@@ -569,12 +550,9 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){ @@ -569,12 +550,9 @@ define([&#39;handlebars&#39;,&#39;handlebars_helpers&#39;], function(Handlebars){
569 }); 550 });
570 }); 551 });
571 552
572 - window.addEventListener("message", function(ev) {  
573 - if (ev.data.message === "oauthClientPluginResult") {  
574 - Main.loginCallback(ev.data.logged_in, ev.data.private_token);  
575 - ev.source.close();  
576 - }  
577 - }); 553 + window.oauthPluginHandleLoginResult = function(loggedIn, token) {
  554 + Main.loginCallback(loggedIn, token);
  555 + }
578 556
579 if('onhashchange' in window){ 557 if('onhashchange' in window){
580 window.onhashchange = function(){ 558 window.onhashchange = function(){
js/proposal-app.js
1 -define(['jquery','jquery_cookie'],function($){ 1 +define(['jquery_cookie'],function(){
2 2
3 3
4 ProposalApp = (function (){ 4 ProposalApp = (function (){
js/requirejs-config.js
  1 +var base,test = '';
  2 +
1 if(window.Url){ 3 if(window.Url){
2 - var base = Url.initBase();  
3 - base = Url.removeUrlParameters(base) + '/js/'; 4 + var base = Url.initBase().removeUrlParameters()+'/js/';
4 }else{ 5 }else{
5 base = '../js' 6 base = '../js'
  7 + test = window.location.href.replace(/\/\w*\.html/g,'');
6 } 8 }
7 9
8 requirejs.config({ 10 requirejs.config({
@@ -15,30 +17,35 @@ requirejs.config({ @@ -15,30 +17,35 @@ requirejs.config({
15 handlebars: 'handlebars-v3.0.1', 17 handlebars: 'handlebars-v3.0.1',
16 handlebars_helpers: 'handlebars-helpers', 18 handlebars_helpers: 'handlebars-helpers',
17 jquery_maxlength: 'jquery.maxlength.min', 19 jquery_maxlength: 'jquery.maxlength.min',
18 - slick: 'slick.min', 20 + jquery_slick: 'slick.min',
19 layout: 'layout', 21 layout: 'layout',
20 main: 'main', 22 main: 'main',
21 - proposal_app: 'proposal-app' 23 + proposal_app: 'proposal-app',
  24 + test:test
22 }, 25 },
23 shim: { 26 shim: {
24 'handlebars':{ 27 'handlebars':{
25 deps: ['jquery'], 28 deps: ['jquery'],
26 exports: 'Handlebars' 29 exports: 'Handlebars'
27 }, 30 },
  31 + 'handlebars_helpers':{
  32 + deps: ['handlebars'],
  33 + exports: 'Handlebars'
  34 + },
28 'jquery_ui': { 35 'jquery_ui': {
29 deps: ['jquery'] 36 deps: ['jquery']
30 }, 37 },
31 'layout': { 38 'layout': {
32 deps: ['jquery'] 39 deps: ['jquery']
33 }, 40 },
34 - 'jquery_maxlength': { 41 + 'jquery_maxlength':{
35 deps: ['jquery'] 42 deps: ['jquery']
36 }, 43 },
37 - 'slick': { 44 + 'jquery_cookie':{
38 deps: ['jquery'] 45 deps: ['jquery']
39 }, 46 },
40 - 'handlebars_helpers':{  
41 - deps: ['handlebars'] 47 + 'jquery_slick': {
  48 + deps: ['jquery']
42 }, 49 },
43 'proposal_app' :{ 50 'proposal_app' :{
44 deps: ['jquery'], 51 deps: ['jquery'],
@@ -46,6 +53,3 @@ requirejs.config({ @@ -46,6 +53,3 @@ requirejs.config({
46 } 53 }
47 } 54 }
48 }); 55 });
49 -  
50 -requirejs(['jquery', 'proposal_app', 'jquery_ui','handlebars_helpers']);  
51 -requirejs(['slick', 'jquery_maxlength', 'layout','main']);