main.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. function Control(element, {value=0, min=0, max=100, minAngle=0, maxAngle=360, wheelSpeed=0.1, step=1}={}) {
  2. const container = document.createElement('div')
  3. container.style.padding = '15px'
  4. const img = document.createElement('img')
  5. const regulatorValue = document.createElement('div')
  6. regulatorValue.style.textAlign = 'center'
  7. img.src = '1@3x.png'
  8. container.append(img)
  9. container.append(regulatorValue)
  10. element.append(container)
  11. const ratio = (maxAngle - minAngle) / (max - min)
  12. const getAngle = () => (ratio * (value - min)) + minAngle
  13. this.setValue = newValue => {
  14. if (newValue > max) {
  15. newValue = max
  16. }
  17. if (newValue < min) {
  18. newValue = min
  19. }
  20. value = newValue
  21. img.style.transform = `rotate(${getAngle()}deg)`
  22. regulatorValue.innerText = value
  23. if (typeof this.onChange === 'function') {
  24. this.onChange(value)
  25. }
  26. }
  27. this.getValue = () => {
  28. return value
  29. }
  30. img.onmousewheel = (e) => {
  31. e.preventDefault()
  32. let {deltaY} = e
  33. this.setValue(value + deltaY * wheelSpeed)
  34. }
  35. img.onclick = (e) => {
  36. let {layerX} = e
  37. if (layerX < img.width / 2) {
  38. this.setValue(value - step)
  39. }
  40. if (layerX > img.width / 2) {
  41. this.setValue(value + step)
  42. }
  43. }
  44. this.setValue(value)
  45. }
  46. //крутилки цвета
  47. const rControl = new Control(regulators, {value: 0, min: 0, max: 255, minAngle: -90, maxAngle: +90})
  48. const gControl = new Control(regulators, {value: 0, min: 0, max: 255, minAngle: -90, maxAngle: +90})
  49. const bControl = new Control(regulators, {value: 0, min: 0, max: 255, minAngle: -90, maxAngle: +90})
  50. function changeBackgroundColor() {
  51. this.style.backgroundColor = `rgb(${rControl.getValue()}, ${gControl.getValue()}, ${bControl.getValue()})`
  52. }
  53. const changeDivBackground = changeBackgroundColor.bind(background)
  54. changeDivBackground()
  55. rControl.onChange = changeDivBackground
  56. gControl.onChange = changeDivBackground
  57. bControl.onChange = changeDivBackground
  58. //крутилка звука
  59. //я прошарила, что тут можно мин макс поменять и скорость колесика, и тогда можно не делить на 100, но если так сделать, то число некрасивое отображается, мне не нравится......
  60. const volumeControl = new Control(regulatorContainer, {value: 50, min: 0, max: 100, minAngle: -90, maxAngle: +90, wheelSpeed: 0.05})
  61. function changeVolume () {
  62. audio.volume = volumeControl.getValue() / 100
  63. }
  64. volumeControl.onChange = changeVolume
  65. btn.onclick = () => audio.play()
  66. //Password
  67. function Password(parent, open){
  68. let container = document.createElement('div')
  69. container.style.display = 'flex'
  70. container.style.alignItems = 'center'
  71. let passwordInput = document.createElement('input')
  72. passwordInput.type = open ? 'text' : 'password'
  73. let openBtn = document.createElement('span')
  74. openBtn.style.lineHeight = '0'
  75. openBtn.innerHTML = '&#128065;'
  76. openBtn.style.fontSize = '25px'
  77. openBtn.style.cursor = 'pointer'
  78. container.append(passwordInput)
  79. container.append(openBtn)
  80. parent.appendChild(container)
  81. let value = passwordInput.value
  82. this.setValue = (newValue) => {
  83. value = newValue
  84. passwordInput.value = value;
  85. return value
  86. }
  87. this.getValue = () => {
  88. return value
  89. }
  90. this.setOpen = (newValue) => {
  91. if (newValue === true || newValue === false) {
  92. open = newValue
  93. passwordInput.type = open ? 'text' : 'password'
  94. }
  95. return open
  96. }
  97. this.getOpen = () => {
  98. return open
  99. }
  100. passwordInput.oninput = () => {
  101. this.setValue(passwordInput.value)
  102. if (typeof this.onChange === 'function') {
  103. this.onChange(value)
  104. }
  105. }
  106. openBtn.onclick = () => {
  107. open = !open
  108. this.setOpen(open)
  109. if (typeof this.onOpenChange === 'function') {
  110. this.onOpenChange(open)
  111. }
  112. }
  113. }
  114. //LoginForm
  115. // let container = document.createElement('div')
  116. // let loginInput = document.createElement('input')
  117. // container.append(loginInput)
  118. // formContainer.append(container)
  119. // let passwordInput = new Password(formContainer, true)
  120. // let logInBtn = document.createElement('button')
  121. // logInBtn.innerText = 'Log in'
  122. // formContainer.append(logInBtn)
  123. // loginInput.oninput = inputCheck
  124. // passwordInput.onChange = inputCheck
  125. // function inputCheck() {
  126. // if (loginInput.value !== '' && passwordInput.getValue() !== '') {
  127. // logInBtn.disabled = false
  128. // } else {
  129. // logInBtn.disabled = true
  130. // }
  131. // }
  132. // inputCheck()
  133. //LoginForm Constructor
  134. function LoginForm (parent) {
  135. let container = document.createElement('div')
  136. let loginInput = document.createElement('input')
  137. container.append(loginInput)
  138. parent.append(container)
  139. let passwordInput = new Password(parent, true)
  140. let logInBtn = document.createElement('button')
  141. logInBtn.innerText = 'Log in'
  142. formContainer.append(logInBtn)
  143. let value = loginInput.value
  144. this.setValue = (newValue) => {
  145. value = newValue
  146. loginInput.value = value;
  147. return value
  148. }
  149. this.getValue = () => {
  150. return value
  151. }
  152. this.getPasswordInput = () => {
  153. return passwordInput
  154. }
  155. this.inputCheck = () => {
  156. if (value !== '' && passwordInput.getValue() !== '') {
  157. logInBtn.disabled = false
  158. } else {
  159. logInBtn.disabled = true
  160. }
  161. }
  162. this.inputCheck()
  163. loginInput.oninput = () => {
  164. this.setValue(loginInput.value)
  165. this.inputCheck()
  166. if (typeof this.onChange === 'function') {
  167. this.onChange(value)
  168. }
  169. }
  170. passwordInput.onChange = this.inputCheck
  171. }
  172. //Password Verify
  173. //надеюсь, у вас не случится сердечный приступ от количества костылей в этом коде
  174. let password = new Password(formContainer, true)
  175. let passwordVerify
  176. let logInBtn = document.createElement('button')
  177. logInBtn.innerText = 'Verify'
  178. formContainer.append(logInBtn)
  179. password.onOpenChange = (open) => {
  180. password.onChange()
  181. if (!open) {
  182. passwordVerify = new Password(formContainer, false)
  183. passwordVerify.onChange = password.onChange
  184. passwordVerify.onChange()
  185. formContainer.append(logInBtn)
  186. } else if (formContainer.children.length === 3){
  187. passwordVerify = undefined
  188. formContainer.removeChild(formContainer.children[formContainer.children.length - 2])
  189. }
  190. }
  191. password.onChange = () => {
  192. if (passwordVerify !== undefined) {
  193. if (password.getValue() === passwordVerify.getValue() && password.getValue() !== '') {
  194. logInBtn.disabled = false
  195. } else {
  196. logInBtn.disabled = true
  197. }
  198. } else {
  199. if (password.getValue() !== '') {
  200. logInBtn.disabled = false
  201. } else {
  202. logInBtn.disabled = true
  203. }
  204. }
  205. }
  206. password.onOpenChange(password.getOpen())