hw16.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // fetch basic + fetch improved ===========================================================================================================
  2. function jsonToTable (parent, json) {
  3. const jsonTable = document.createElement('table')
  4. parent.append(jsonTable)
  5. Object.assign(jsonTable.style, {border: 'dotted', borderLeft: 'solid'})
  6. for (let key in json) {
  7. const jsonTableTr = document.createElement('tr')
  8. jsonTable.appendChild(jsonTableTr)
  9. const jsonTableTdTittle = document.createElement('td')
  10. jsonTableTr.appendChild(jsonTableTdTittle).innerText = key
  11. Object.assign(jsonTableTdTittle.style, {background: 'black', color: 'white'})
  12. const jsonTableTdContent = document.createElement('td')
  13. jsonTableTr.appendChild(jsonTableTdContent)
  14. Object.assign(jsonTableTdContent.style, {borderBottom: 'dotted'})
  15. if (typeof json[key] === 'string' && json[key].includes('https://swapi.dev/api/')) {
  16. const tdButton = document.createElement('button')
  17. jsonTableTdContent.appendChild(tdButton).innerText = json[key]
  18. Object.assign(tdButton.style, {background: 'black', color: 'yellow', borderColor: 'yellow'})
  19. tdButton.onclick = () => {
  20. fetch(`${json[key]}`)
  21. .then(res => res.json())
  22. .then(data => jsonToTable(jsonTableTdContent, data))
  23. }
  24. }
  25. else if (typeof json[key] === 'object') {
  26. for (let i = 0; i < json[key].length; i ++) {
  27. const tdButton = document.createElement('button')
  28. jsonTableTdContent.appendChild(tdButton).innerText = json[key][i]
  29. Object.assign(tdButton.style, {background: 'black', color: 'yellow', borderColor: 'yellow'})
  30. tdButton.onclick = () => {
  31. fetch(`${json[key][i]}`)
  32. .then(res => res.json())
  33. .then(data => jsonToTable(jsonTableTdContent, data))
  34. }
  35. }
  36. }
  37. else {
  38. jsonTableTdContent.innerText = json[key]
  39. }
  40. }
  41. }
  42. fetch('https://swapi.dev/api/people/1/')
  43. .then(res => res.json())
  44. .then(luke => jsonToTable(document.body, luke))
  45. // race ===========================================================================================================
  46. function delay (ms) {
  47. return new Promise ( (resolve, reject) => {
  48. setTimeout( () => resolve('myDelay'), Math.random()*ms)
  49. })
  50. }
  51. const myDelay = delay(1000)
  52. const myFetch = fetch('https://swapi.dev/api/people/1/')
  53. Promise.race([myDelay,myFetch]).then((who) => console.log(who))
  54. // Promisify: confirm ===========================================================================================================
  55. function confirmPromise(text){
  56. return new Promise( (resolve, reject) => {
  57. confirm(text) ? resolve() : reject()
  58. })
  59. }
  60. confirmPromise('Промисы это сложно?').then(() => console.log('не так уже и сложно'),
  61. () => console.log('respect за усидчивость и внимательность'))
  62. // Promisify: prompt ===========================================================================================================
  63. function promptPromise(text){
  64. const promptVal = prompt(text)
  65. return new Promise ( (resolve, reject) => {
  66. promptVal ? resolve(promptVal) : reject()
  67. })
  68. }
  69. promptPromise("Как тебя зовут?").then(name => console.log(`Тебя зовут ${name}`),
  70. () => console.log('Ну зачем морозиться, нормально же общались'))
  71. // Promisify: LoginForm ===========================================================================================================
  72. function Form (parent) {
  73. this.mainForm = document.createElement('form')
  74. parent.append(this.mainForm)
  75. Object.assign(this.mainForm.style, {display: 'flex', flexDirection: 'column', alignItems: 'center', width: '150px'})
  76. this.formElementsPlace = document.createElement('div')
  77. this.mainForm.append(this.formElementsPlace)
  78. Object.assign(this.formElementsPlace.style, {display: 'flex', flexDirection: 'column', width: '100%'})
  79. const p = new Password (this.formElementsPlace, false)
  80. const l = new Login (this.formElementsPlace)
  81. this.formButton = document.createElement('button')
  82. this.mainForm.append(this.formButton)
  83. this.formButton.innerText = 'Login'
  84. this.formButton.type = 'button'
  85. Object.assign(this.formButton.style, {width: '50px', marginBottom: '5px'})
  86. this.formButton.disabled = p.passwordInput.value === '' && l.loginInput.value === '' ? true : false
  87. p.onChange = data => this.formButton.disabled = data === ''? true : false
  88. l.onChange = data => this.formButton.disabled = data === ''? true : false
  89. this.getData = () => {
  90. return {
  91. login: l.getValue(),
  92. password: p.getValue()
  93. }
  94. }
  95. this.formButton.onclick = () => this.getData()
  96. console.log(this)
  97. }
  98. function Password (parent,open) {
  99. this.passwordInput = document.createElement('input')
  100. parent.appendChild(this.passwordInput)
  101. this.passwordInput.placeholder = 'Password'
  102. this.passwordInput.type = open ? 'text' : 'password'
  103. this.openInput = document.createElement('input')
  104. parent.appendChild(this.openInput)
  105. this.openInput.type = 'checkBox'
  106. this.setValue = newValue => this.passwordInput.value = newValue
  107. this.getValue = () => {
  108. return this.passwordInput.value
  109. }
  110. this.setOpen = newOpen => {
  111. newOpen = this.openInput.checked
  112. this.passwordInput.type = newOpen ? 'text' : 'password'
  113. }
  114. this.getOpen = () => {
  115. return this.openInput.checked
  116. }
  117. this.passwordInput.oninput = () => {
  118. if (typeof this.onChange === 'function') {
  119. this.onChange(this.getValue())
  120. }
  121. }
  122. this.openInput.onchange = () => {
  123. if (typeof this.onOpenChange === 'function') {
  124. this.onOpenChange(this.getOpen())
  125. }
  126. this.getOpen(this.setOpen())
  127. }
  128. }
  129. function Login (parent) {
  130. this.loginInput = document.createElement('input')
  131. parent.prepend(this.loginInput)
  132. this.loginInput.placeholder = 'Login'
  133. Object.assign(this.loginInput.style, {marginBottom: '5px'})
  134. this.setValue = newValue => this.loginInput.value = newValue
  135. this.getValue = () => {
  136. return this.loginInput.value
  137. }
  138. this.loginInput.oninput = () => {
  139. if (typeof this.onChange === 'function') {
  140. this.onChange(this.getValue())
  141. }
  142. }
  143. }
  144. function loginPromise(parent){
  145. const f = new Form(parent, true)
  146. return new Promise ( (resolve, reject) => {
  147. f.formButton.onclick = () => resolve(f.getData())
  148. })
  149. }
  150. loginPromise(document.body).then(({login, password}) => console.log(`Вы ввели ${login} и ${password}`))