YavorJS

1.Forecaster

Jul 31st, 2017
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. function attachEvents() {
  2. let baseURL='https://judgetests.firebaseio.com/locations.json';
  3.  
  4. let userInput='';
  5.  
  6.  
  7. $('#submit').click(function () {
  8. userInput=$('#location').val();
  9. $.get(baseURL)
  10. .then(codeValue)
  11. .catch(weatherError);
  12. });
  13.  
  14. function codeValue(data) {
  15. let townCode='';
  16. for (let obj of data) {
  17. let code=obj.code;
  18. let name=obj.name;
  19. if(userInput===name){
  20. townCode=code;
  21. break;
  22. }
  23. }
  24.  
  25. let currentConditionsUrl=`https://judgetests.firebaseio.com/forecast/today/${townCode}.json`;
  26.  
  27. $.get(currentConditionsUrl).then(currentConditions);
  28.  
  29. function currentConditions(data) {
  30. $('#current').empty();
  31. let current=$('#current');
  32. $('#forecast').css('display','inline-block');
  33. // console.log(data.forecast.condition,data.forecast.high,data.forecast.low);
  34.  
  35. let label=$('<div>').attr('class','label').text('Current conditions');
  36. current.append(label);
  37.  
  38. let icon=getIcon(data.forecast.condition);
  39. let span=$('<span>').attr('class','condition symbol').html(icon);
  40. current.append(span);
  41.  
  42. let conditions=$('<span>').attr('class','condition');
  43.  
  44. let span1=$('<span>').attr('class','forecast-data').text(data.name);
  45. conditions.append(span1);
  46. let span2=$('<span>').attr('class','forecast-data').html(data.forecast.low+'\&#176;/'+data.forecast.high+'\&#176;');
  47. conditions.append(span2);
  48.  
  49. let span3=$('<span>').attr('class','forecast-data').text(data.forecast.condition);
  50. conditions.append(span3);
  51. current.append(conditions);
  52. } //end of currentConditions
  53.  
  54. let upcomingConditionsUrl=`https://judgetests.firebaseio.com/forecast/upcoming/${townCode}.json`;
  55.  
  56. $.get(upcomingConditionsUrl).then(upcomingConditions);
  57.  
  58. function upcomingConditions(data) {
  59. $('.upcoming').detach();
  60. let upcoming=$('#upcoming');
  61.  
  62. for (let obj of data.forecast) {
  63. // console.log(obj.condition,obj.log,obj.high);
  64.  
  65. let icon=getIcon(obj.condition);
  66.  
  67. let span=$('<span>').attr('class','upcoming');
  68. upcoming.append(span);
  69.  
  70. let line= $('<span>').attr('class','symbol').html(icon);
  71. span.append(line);
  72.  
  73. let span1=$('<span>').attr('class','forecast-data').html(obj.low+'\&#176;/'+obj.high+'\&#176;');
  74. span.append(span1);
  75.  
  76. let span2=$('<span>').attr('class','forecast-data').html(obj.condition);
  77. span.append(span2);
  78. }
  79.  
  80.  
  81. }
  82.  
  83. function getIcon(x){
  84. switch(x){
  85. case 'Sunny':
  86. return '\&#x2600;';
  87. break;
  88. case 'Partly sunny':
  89. return '\&#x26C5;';
  90. break;
  91. case 'Rain':
  92. return '\&#x2614;';
  93. break;
  94. case 'Overcast':
  95. return '\&#x2601;';
  96. break;
  97. case 'Degrees':
  98. return '\&#176;';
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104.  
  105. } //end of codeValue
  106.  
  107.  
  108. function weatherError() {
  109. $('#forecast').text('Error');
  110.  
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment