script.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // password
  2. function Password(parent, open=false) {
  3. let _isVisible = open
  4. let _value = ''
  5. let input = document.createElement('input')
  6. input.placeholder = 'password'
  7. let wrapper = document.createElement('div')
  8. wrapper.style.border = '1px solid red'
  9. let btn = document.createElement('button')
  10. btn.innerText = 'Hide/Show'
  11. this.getHTMLElements = function() {
  12. return {
  13. Password_wrapper: wrapper,
  14. Password_input: input,
  15. Password_btn: btn
  16. }
  17. }
  18. this.getValue = function() {
  19. return _value
  20. }
  21. this.setValue = function(newValue) {
  22. _value = newValue
  23. input.value = _value
  24. }
  25. this.setOpen = (bool) => {
  26. _isVisible = bool
  27. let _type = _isVisible? 'text' : 'password'
  28. input.setAttribute('type', `${_type}`)
  29. this.onOpenChange()
  30. }
  31. this.getOpen = function() {
  32. return _isVisible
  33. }
  34. this.onChange = function(event) {
  35. _value = event.target.value
  36. }
  37. let closed = () => {}
  38. this.onOpenChange = (func=null, ...args) => {
  39. let lockfunc = func
  40. if(func) {
  41. (function(){
  42. closed = lockfunc
  43. closed(...args)
  44. })()
  45. } else {
  46. closed(...args)
  47. }
  48. }
  49. input.addEventListener('input', this.onChange)
  50. btn.addEventListener('click', () => {
  51. this.setOpen(!_isVisible)
  52. })
  53. wrapper.setAttribute('class', 'Password')
  54. this.setOpen(open)
  55. wrapper.append(input, btn)
  56. parent.append(wrapper)
  57. }
  58. let pass = new Password(document.body, true)
  59. // check if Password constructor works
  60. pass.setValue('hello world')
  61. pass.setOpen(false)
  62. console.log(pass.getOpen())
  63. console.log(pass.getValue())
  64. window.setTimeout(()=> {
  65. console.log('old value:', pass.getValue())
  66. pass.setValue('YOU CAN SEE ME!')
  67. pass.setOpen(true)
  68. }, 5000)
  69. // login form
  70. function Login (parent) {
  71. let _value = ''
  72. let input = document.createElement('input')
  73. input.type = 'text'
  74. input.placeholder = 'login'
  75. input.style.display = 'block'
  76. parent.append(input)
  77. this.getHTMLElement = function() {
  78. return input
  79. }
  80. this.getValue = function() {
  81. return _value
  82. }
  83. this.setValue = function(newValue) {
  84. _value = newValue
  85. input.value = _value
  86. }
  87. this.onChange = function(event) {
  88. _value = event.target.value
  89. }
  90. input.addEventListener('input', this.onChange)
  91. }
  92. // loginForm Constructor
  93. function loginForm(parent, heading='loginForm Constructor') {
  94. let form = document.createElement('div')
  95. form.id = 'loginForm'
  96. form.style.border = '5px double black'
  97. form.style.width = 'fit-content'
  98. form.style.textAlign = 'center'
  99. form.style.padding = '15px'
  100. form.style.margin = '15px'
  101. let h2 = document.createElement('h2')
  102. h2.innerText = heading
  103. form.append(heading)
  104. let _login = new Login(form)
  105. let _password = new Password(form)
  106. let login_btn = document.createElement('button')
  107. login_btn.innerText = 'Log in'
  108. login_btn.disabled = true
  109. form.append(login_btn)
  110. for(let item of form.children) {
  111. item.style.margin = '10px'
  112. }
  113. this.getLogin = function() {
  114. return _login.getValue()
  115. }
  116. this.getPassword = function() {
  117. return _password.getValue()
  118. }
  119. this.isPasswordOpened = function() {
  120. return _password.getOpen()
  121. }
  122. this.setPasswordOpened = function(bool) {
  123. _password.setOpen(bool)
  124. }
  125. this.setLogin = function(newLogin) {
  126. _login.setValue(newLogin)
  127. checkFields()
  128. }
  129. this.setPass = function(newPass) {
  130. _password.setValue(newPass)
  131. checkFields()
  132. }
  133. function checkFields() {
  134. if (_login.getValue() && _password.getValue()) {
  135. login_btn.disabled = false
  136. } else {
  137. login_btn.disabled = true
  138. }
  139. }
  140. _login.getHTMLElement().addEventListener('input', checkFields)
  141. _password.getHTMLElements().Password_input.addEventListener('input', checkFields)
  142. parent.append(form)
  143. }
  144. let form = new loginForm(document.body)
  145. form.setLogin('default_user')
  146. form.setPass('pass example', true)
  147. window.setTimeout(()=> {
  148. console.log(form.getPassword())
  149. console.log('before open', form.isPasswordOpened())
  150. form.setPasswordOpened(true)
  151. console.log('after open', form.isPasswordOpened())
  152. console.log(form.getLogin())
  153. }, 5000)
  154. // Password Verify
  155. class PasswordVerify extends Password {
  156. constructor(parent, open=false) {
  157. super(parent, open);
  158. let _repeatWrapper = document.createElement('div')
  159. let _repeatField = document.createElement('input')
  160. let _successBtn = document.createElement('button')
  161. let displayRepeat = () => {
  162. if(this.getOpen() === false) {
  163. _repeatField.style.marginTop = '10px'
  164. _repeatField.type = 'password'
  165. _repeatField.placeholder = 'repeat password'
  166. _repeatField.value = ''
  167. _successBtn.innerText = 'no match!'
  168. _successBtn.disabled = true
  169. _successBtn.style.display = 'inline-block'
  170. _repeatWrapper.append(_repeatField)
  171. _repeatWrapper.append(_successBtn)
  172. this.getHTMLElements().Password_wrapper.append(_repeatWrapper)
  173. } else {
  174. try {
  175. for(let child of _repeatWrapper.children) {
  176. _repeatWrapper.remove(child)
  177. }
  178. } catch(e){}
  179. }
  180. }
  181. this.onOpenChange(displayRepeat)
  182. let isMatch= () => {
  183. console.log(this.getHTMLElements())
  184. if(this.getHTMLElements().Password_input.value === _repeatField.value) {
  185. return true
  186. } else {
  187. return false
  188. }
  189. }
  190. let btnActivate = (bool) => {
  191. if(bool) {
  192. _successBtn.disabled = false
  193. } else {
  194. _successBtn.disabled = true
  195. }
  196. }
  197. this.getHTMLElements().Password_input.addEventListener('input', () => {
  198. btnActivate(isMatch())
  199. })
  200. _repeatField.addEventListener('input',() => {
  201. btnActivate(isMatch())
  202. })
  203. }
  204. }
  205. document.body.insertAdjacentHTML('beforeend', `
  206. <div id=verif></div>
  207. `)
  208. let verif = document.getElementById('verif')
  209. let passverif = new PasswordVerify(verif, true)
  210. window.setTimeout(()=> {
  211. console.log('bang')
  212. passverif.setOpen(false)
  213. }, 5000)
  214. //login form
  215. function Form(el, data, okCallback, cancelCallback){
  216. let formBody = document.createElement('div')
  217. let okButton = document.createElement('button')
  218. okButton.innerHTML = 'OK'
  219. let cancelButton = document.createElement('button')
  220. cancelButton.innerHTML = 'Cancel'
  221. formBody.innerHTML = '<h1>тут будет форма после перервы</h1>'
  222. if (typeof okCallback === 'function'){
  223. formBody.appendChild(okButton);
  224. okButton.onclick = (e) => {
  225. console.log(this)
  226. this.okCallback(e)
  227. }
  228. }
  229. if (typeof cancelCallback === 'function'){
  230. formBody.appendChild(cancelButton);
  231. cancelButton.onclick = cancelCallback
  232. }
  233. el.appendChild(formBody)
  234. let inputCreators = {
  235. String(key, value, func){
  236. let input = document.createElement('input')
  237. input.type = 'text'
  238. input.placeholder = key
  239. input.value = value
  240. input.oninput = () => func(input.value)
  241. return input
  242. },
  243. Boolean(key, value, func){
  244. let input = document.createElement('input')
  245. input.type = 'checkbox'
  246. input.placeholder = key
  247. input.checked = value
  248. input.onchecked = () => func(input.value)
  249. return input
  250. },
  251. Date(key, value, func){
  252. //используйте datetime-local
  253. let input = document.createElement('input')
  254. input.type = 'datetime-local'
  255. input.placeholder = key
  256. input.value = Date.toISOstring(value).substring(0,10)
  257. input.onchange = () => func(input.value)
  258. return input
  259. },
  260. //и др. по вкусу, например textarea для длинных строк
  261. }
  262. let formTable = document.createElement('table')
  263. formTable.border = 1
  264. for(let key in data) {
  265. let tr = document.createElement('tr')
  266. let tdKey = document.createElement('td')
  267. let tdInput = document.createElement('td')
  268. let input = inputCreators[data[key].constructor.name](key, data[key],(value) => {
  269. data[key] = value
  270. })
  271. tdKey.innerText = key
  272. tdInput.append(input)
  273. tr.append(tdKey, tdInput)
  274. formTable.append(tr)
  275. }
  276. okButton.before(formTable)
  277. this.okCallback = okCallback
  278. this.cancelCallback = cancelCallback
  279. this.data = data
  280. this.validators = {}
  281. }
  282. let formContainer = document.createElement('div')
  283. formContainer.id = 'formContainer'
  284. document.body.append(formContainer)
  285. let form = new Form(formContainer, {
  286. name: 'Anakin',
  287. surname: 'Skywalker',
  288. married: true,
  289. birthday: new Date((new Date).getTime() - 86400000 * 30*365)
  290. }, () => console.log('ok'),() => console.log('cancel') )
  291. form.okCallback = () => console.log('ok2')
  292. form.validators.surname = (value, key, data, input) => value.length > 2 &&
  293. value[0].toUpperCase() == value[0] &&
  294. !value.includes(' ') ? true : 'Wrong name'
  295. console.log(form)