index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var selects = document.querySelector('.selects');
  2. var weatherDiv = document.querySelector('.weather');
  3. function get(url){
  4. return new Promise(function(resolve,reject){
  5. var xhr = new XMLHttpRequest();
  6. xhr.open('GET',url,true);
  7. xhr.onload = function(){
  8. if(xhr.status == 200){
  9. resolve(JSON.parse(xhr.responseText));
  10. }
  11. else{
  12. reject(xhr.statusText);
  13. }
  14. }
  15. xhr.onerror = function(){
  16. reject('not found');
  17. }
  18. xhr.send();
  19. });
  20. }
  21. function show(obj){
  22. var weather = obj.query.results.channel.item.description;
  23. var resWeather = weather.replace(/href|CDATA|[\!]|[\[]|[\]]/g,"");
  24. var resWeather2 = resWeather.slice(1,resWeather.length - 1);
  25. var resWeather3 = resWeather2.replace(/Full Forecast at Yahoo Weather/g,"");
  26. var resWeather4 = resWeather3.slice(0,resWeather3.length - 28);
  27. weatherDiv.innerHTML = resWeather4;
  28. }
  29. function* myGenerator(){
  30. var object = yield get('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json');
  31. var keys = Object.keys(object);
  32. var select = document.createElement("select");
  33. for (var i = 0; i < keys.length; i++) {
  34. var option = document.createElement("option");
  35. option.innerHTML = keys[i];
  36. option.value = keys[i];
  37. select.appendChild(option);
  38. }
  39. select.onchange = function(){
  40. run(onchange1);
  41. }
  42. function* onchange1(e){
  43. var select = document.createElement("select");
  44. for (var i = 0; i < object[document.getElementById("main").value].length; i++) {
  45. var option = document.createElement("option")
  46. option.innerHTML = object[document.getElementById("main").value][i];
  47. select.appendChild(option);
  48. }
  49. if(selects.children.length > 1){
  50. selects.replaceChild(select, document.getElementById("main").nextElementSibling);
  51. }
  52. else
  53. selects.appendChild(select);
  54. var obj = yield get("https://query.yahooapis.com/v1/public/yql?" + 'q=' + encodeURIComponent("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\'" + select.value + "\') and u='c'") + '&format=json');
  55. show(obj);
  56. select.onchange = function(){
  57. get("https://query.yahooapis.com/v1/public/yql?" + 'q=' + encodeURIComponent("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\'" + this.value + "\') and u='c'") + '&format=json').then(function(obj){
  58. show(obj);
  59. })
  60. }
  61. }
  62. selects.appendChild(select);
  63. select.id = "main"
  64. }
  65. function run(g){
  66. var gen = g();
  67. function handle(result){
  68. if(!result.done){
  69. result.value.then(function(data){
  70. return handle(gen.next(data));
  71. })
  72. }
  73. }
  74. return handle(gen.next());
  75. }
  76. run(myGenerator);