index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. try{
  31. var object = yield get('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json');
  32. var keys = Object.keys(object);
  33. var select = document.createElement("select");
  34. for (var i = 0; i < keys.length; i++) {
  35. var option = document.createElement("option");
  36. option.innerHTML = keys[i];
  37. option.value = keys[i];
  38. select.appendChild(option);
  39. }
  40. select.onchange = function(){
  41. run(onchange1);
  42. }
  43. function* onchange1(){
  44. try{
  45. var mainSelect = document.getElementById("main")
  46. var select = document.createElement("select");
  47. for (var i = 0; i < object[mainSelect.value].length; i++) {
  48. var option = document.createElement("option")
  49. option.innerHTML = object[mainSelect.value][i];
  50. select.appendChild(option);
  51. }
  52. if(selects.children.length > 1){
  53. selects.replaceChild(select,mainSelect.nextElementSibling);
  54. }
  55. else
  56. selects.appendChild(select);
  57. 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');
  58. show(obj);
  59. select.onchange = function(){
  60. run(onchange2);
  61. }
  62. function* onchange2(){
  63. try{
  64. var obj2 = 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');
  65. show(obj2);
  66. }catch(error){
  67. throw error;
  68. }
  69. }
  70. }catch(error){
  71. throw error;
  72. }
  73. }
  74. selects.appendChild(select);
  75. select.id = "main";
  76. }catch(error){
  77. throw error;
  78. }
  79. }
  80. function run(g){
  81. var gen = g();
  82. function handle(result){
  83. if(!result.done){
  84. result.value.then(function(data){
  85. handle(gen.next(data));
  86. }).catch(function(error){
  87. throw(error);
  88. handle(gen.next());
  89. })
  90. }
  91. }
  92. handle(gen.next());
  93. }
  94. run(myGenerator);