Pavel hace 7 años
commit
b151667f40
Se han modificado 5 ficheros con 285 adiciones y 0 borrados
  1. 50 0
      hw1/Weather/css/normalize.min.css
  2. 50 0
      hw1/Weather/css/styles.css
  3. 17 0
      hw1/Weather/index.html
  4. 67 0
      hw1/Weather/js/index.js
  5. 101 0
      hw1/index.md

+ 50 - 0
hw1/Weather/css/normalize.min.css

@@ -0,0 +1,50 @@
+/*! normalize.css v1.0.1 | MIT License | git.io/normalize */
+article,aside,details,figcaption,figure,footer,header,hgroup,nav,section,summary{display:block}
+audio,canvas,video{display:inline-block;*display:inline;*zoom:1}
+audio:not([controls]){display:none;height:0}
+[hidden]{display:none}
+html{font-size:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}
+html,button,input,select,textarea{font-family:sans-serif}
+body{margin:0}
+a:focus{outline:thin dotted}
+a:active,a:hover{outline:0}
+h1{font-size:2em;margin:.67em 0}
+h2{font-size:1.5em;margin:.83em 0}
+h3{font-size:1.17em;margin:1em 0}
+h4{font-size:1em;margin:1.33em 0}
+h5{font-size:.83em;margin:1.67em 0}
+h6{font-size:.75em;margin:2.33em 0}
+abbr[title]{border-bottom:1px dotted}
+b,strong{font-weight:bold}
+blockquote{margin:1em 40px}
+dfn{font-style:italic}
+mark{background:#ff0;color:#000}
+p,pre{margin:1em 0}
+code,kbd,pre,samp{font-family:monospace,serif;_font-family:'courier new',monospace;font-size:1em}
+pre{white-space:pre;white-space:pre-wrap;word-wrap:break-word}
+q{quotes:none}
+q:before,q:after{content:'';content:none}
+small{font-size:80%}
+sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}
+sup{top:-0.5em}
+sub{bottom:-0.25em}
+dl,menu,ol,ul{margin:1em 0}
+dd{margin:0 0 0 40px}
+menu,ol,ul{padding:0 0 0 40px}
+nav ul,nav ol{list-style:none;list-style-image:none}
+img{border:0;-ms-interpolation-mode:bicubic}
+svg:not(:root){overflow:hidden}
+figure{margin:0}
+form{margin:0}
+fieldset{border:1px solid #c0c0c0;margin:0 2px;padding:.35em .625em .75em}
+legend{border:0;padding:0;white-space:normal;*margin-left:-7px}
+button,input,select,textarea{font-size:100%;margin:0;vertical-align:baseline;*vertical-align:middle}
+button,input{line-height:normal}
+button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer;*overflow:visible}
+button[disabled],input[disabled]{cursor:default}
+input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0;*height:13px;*width:13px}
+input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}
+input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none}
+button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}
+textarea{overflow:auto;vertical-align:top}
+table{border-collapse:collapse;border-spacing:0}

+ 50 - 0
hw1/Weather/css/styles.css

@@ -0,0 +1,50 @@
+.weather {
+  background: #d6eeff;
+  display: inline-block;
+  text-align: center;
+  width: 100%;
+  border-bottom-left-radius: 15px;
+  border-bottom-right-radius: 15px;
+  border: 1px solid #e8e8e8;
+  margin-bottom: 20px;
+}
+.selects select{
+  display: block;
+  width: 100%;
+  height: 50px;
+  font-size: 20px;
+  border: 1px solid #e8e8e8;
+  padding: 10px;
+}
+
+.selects select:first-child {
+  border-top-left-radius: 15px;
+  border-top-right-radius: 15px;
+  margin-top: 40px;
+}
+.forecast {
+
+  width: 37%;
+  margin: 0 auto;
+  font-size: 20px;
+
+}
+
+body {
+  font-family: arial, sans-serif;
+  line-height: 30px;
+  margin: 0;
+  padding: 0;
+}
+
+body * {
+  box-sizing: border-box;
+  outline: none;
+}
+
+.forecast img {
+  width: 70px;
+  margin-bottom: 15px;
+  margin-top: 10px;
+
+}

+ 17 - 0
hw1/Weather/index.html

@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Document</title>
+	<link rel="stylesheet" href="css/normalize.min.css">
+	<link rel="stylesheet" href="css/styles.css">
+</head>
+<body>
+	<div class="forecast">
+		<div class="selects"></div>
+		<div class="weather"></div>
+	</div>
+
+	<script src="js/index.js"></script>
+</body>
+</html>

+ 67 - 0
hw1/Weather/js/index.js

@@ -0,0 +1,67 @@
+var selects = document.querySelector('.selects');
+var weatherDiv = document.querySelector('.weather');
+
+function get(url){
+  return new Promise(function(resolve,reject){
+    var xhr = new XMLHttpRequest();
+    xhr.open('GET',url,true);
+    xhr.onload = function(){
+      if(xhr.status == 200){
+        resolve(JSON.parse(xhr.responseText));
+      }
+      else{
+        reject(xhr.statusText);
+      }
+    }
+    xhr.onerror = function(){
+      reject('not found');
+    }
+    xhr.send();
+  });
+}
+function show(obj){
+  var weather = obj.query.results.channel.item.description;
+  var resWeather = weather.replace(/href|CDATA|[\!]|[\[]|[\]]/g,"");
+  var resWeather2 = resWeather.slice(1,resWeather.length - 1);
+  var resWeather3 = resWeather2.replace(/Full Forecast at Yahoo Weather/g,"");
+  var resWeather4 = resWeather3.slice(0,resWeather3.length - 28);
+  weatherDiv.innerHTML = resWeather4;
+}
+get('https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json').then(function(object){
+  var keys = Object.keys(object);
+  var select = document.createElement("select");
+  for (var i = 0; i < keys.length; i++) {
+      var option = document.createElement("option");
+      option.innerHTML = keys[i];
+      option.value = keys[i];
+      select.appendChild(option);
+  }
+  select.onchange = function(e){
+      var select = document.createElement("select");
+      for (var i = 0; i < object[this.value].length; i++) {
+          var option = document.createElement("option")
+          option.innerHTML = object[this.value][i];
+          select.appendChild(option);
+      }
+      if(selects.children.length > 1){
+          selects.replaceChild(select, document.getElementById("main").nextElementSibling);
+      }
+      else
+          selects.appendChild(select);
+        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').then(function(obj){
+        show(obj);
+      }).catch(function(){
+        weatherDiv.setAttribute("heigt", "400px")
+        weatherDiv.innerHTML = "loading...";
+      });
+      select.onchange = function(){
+        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){
+          show(obj);
+        })
+      }
+  }
+  selects.appendChild(select);
+  select.id = "main"
+}).catch(function(error){
+  console.log(error);
+})

+ 101 - 0
hw1/index.md

@@ -0,0 +1,101 @@
+##### Задание №1. Promise
+
+```javascript
+function createPromiseRandom(min, max, delay) {
+   return new Promise(function (resolve, reject) {
+     setTimeout(function(){
+       if(min < max){
+          var randNum = min + Math.random() * (max - min);
+          resolve(randNum);
+        }
+        else{
+          reject('min cant be greater than max')
+        }
+      },delay);
+   });
+}
+let p1 = createPromiseRandom(1, 100, 2000);
+p1.then(function(result){
+  console.log(result);
+});
+let p2 = createPromiseRandom(1000, 100, 3000);
+p2.then(function(){},function(error){
+  console.log(error);
+});
+```
+
+##### Задание №2. Promise
+
+```javascript
+new Promise(function (resolve, reject) {
+   debugger;
+   var num = prompt('Введите число');
+   if(!isNaN(+num)){
+     resolve(num);
+   }
+   else{
+     reject('Вы ввели нечисловое значение, повторите ввод');
+   }
+}).catch(function (error) {
+   return new Promise(function (resolve, reject) {
+      do{
+        var num = prompt(error);
+      }
+      while(isNaN(+num))
+      resolve(num);
+   });
+}).then(function (result) {
+   return new Promise(function (resolve, reject) {
+      console.log(result);
+   });
+});
+```
+
+##### Задание №3. Генераторы
+
+```javascript
+function* generateFibonacci() {
+  var fibonacciNum = 1;
+  var fibonacciArr = [];
+  yield fibonacciNum;
+  fibonacciArr[0] = 0;
+  fibonacciArr[1] = fibonacciNum;
+  for (var i = 2;;i++) {
+    fibonacciArr[i] = fibonacciArr[i - 1] + fibonacciArr[i - 2];
+    yield fibonacciArr[i];
+  }
+}
+let gen = generateFibonacci();
+for (let i = 0; i < 10; i++) {
+   console.log(gen.next().value);
+}
+```
+
+##### Задание №4. Генераторы
+
+```javascript
+function* generateFibonacci() {
+  var fibonacciNum = 1;
+  var fibonacciArr = [];
+  var nextValType = yield fibonacciNum;
+  fibonacciArr[0] = 0;
+  fibonacciArr[1] = fibonacciNum;
+  for (var i = 2;;i++) {
+    var returnVal;
+    fibonacciArr[i] = fibonacciArr[i - 1] + fibonacciArr[i - 2];
+    returnVal = nextValType != 'string'? fibonacciArr[i]: fibonacciArr[i].toString();
+    nextValType = yield returnVal;
+  }
+}
+let gen = generateFibonacci();
+console.log(gen.next().value); // 1
+console.log(gen.next("string").value); // "1"
+console.log(gen.next("number").value); // 2
+console.log(gen.next().value); // 3
+console.log(gen.next("string").value); // "5"
+```
+
+##### Wheather(promises)
+
+###### http://homeworkReactJs.pavelefimen95.fe.a-level.com.ua/hw1/Weather
+