script.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. let btn = document.createElement('button')
  9. btn.innerText = 'Hide/Show'
  10. this.getHTMLElements = function() {
  11. return {
  12. Password_wrapper: wrapper,
  13. Password_input: input,
  14. Password_btn: btn
  15. }
  16. }
  17. this.getValue = function() {
  18. return _value
  19. }
  20. this.setValue = function(newValue) {
  21. _value = newValue
  22. input.value = _value
  23. }
  24. this.setOpen = (bool) => {
  25. _isVisible = bool
  26. let _type = _isVisible? 'text' : 'password'
  27. input.setAttribute('type', `${_type}`)
  28. this.onOpenChange()
  29. }
  30. this.getOpen = function() {
  31. return _isVisible
  32. }
  33. this.onChange = function(event) {
  34. _value = event.target.value
  35. }
  36. let closed = () => {}
  37. this.onOpenChange = (func=null, ...args) => {
  38. let lockfunc = func
  39. if(func) {
  40. (function(){
  41. closed = lockfunc
  42. closed(...args)
  43. })()
  44. } else {
  45. closed(...args)
  46. }
  47. }
  48. input.addEventListener('input', this.onChange)
  49. btn.addEventListener('click', () => {
  50. this.setOpen(!_isVisible)
  51. })
  52. wrapper.setAttribute('class', 'Password')
  53. this.setOpen(open)
  54. wrapper.append(input, btn)
  55. parent.append(wrapper)
  56. }
  57. let pass = new Password(document.body, true)
  58. // check if Password constructor works
  59. // pass.setValue('hello world')
  60. // pass.setOpen(false)
  61. // console.log(pass.getOpen())
  62. // console.log(pass.getValue())
  63. // window.setTimeout(()=> {
  64. // console.log('old value:', pass.getValue())
  65. // pass.setValue('YOU CAN SEE ME!')
  66. // pass.setOpen(true)
  67. // }, 5000)
  68. // login form
  69. function Login (parent) {
  70. let _value = ''
  71. let input = document.createElement('input')
  72. input.type = 'text'
  73. input.placeholder = 'login'
  74. input.style.display = 'block'
  75. parent.append(input)
  76. this.getHTMLElement = function() {
  77. return input
  78. }
  79. this.getValue = function() {
  80. return _value
  81. }
  82. this.setValue = function(newValue) {
  83. _value = newValue
  84. input.value = _value
  85. }
  86. this.onChange = function(event) {
  87. _value = event.target.value
  88. }
  89. input.addEventListener('input', this.onChange)
  90. }
  91. // loginForm Constructor
  92. function loginForm(parent, heading='loginForm Constructor') {
  93. let form = document.createElement('div')
  94. form.id = 'loginForm'
  95. form.style.border = '5px double black'
  96. form.style.width = 'fit-content'
  97. form.style.textAlign = 'center'
  98. form.style.padding = '15px'
  99. form.style.margin = '15px'
  100. let h2 = document.createElement('h2')
  101. h2.innerText = heading
  102. form.append(heading)
  103. let _login = new Login(form)
  104. let _password = new Password(form)
  105. let login_btn = document.createElement('button')
  106. login_btn.innerText = 'Log in'
  107. login_btn.disabled = true
  108. form.append(login_btn)
  109. for(let item of form.children) {
  110. item.style.margin = '10px'
  111. }
  112. this.getLogin = function() {
  113. return _login.getValue()
  114. }
  115. this.getPassword = function() {
  116. return _password.getValue()
  117. }
  118. this.isPasswordOpened = function() {
  119. return _password.getOpen()
  120. }
  121. this.setPasswordOpened = function(bool) {
  122. _password.setOpen(bool)
  123. }
  124. this.setLogin = function(newLogin) {
  125. _login.setValue(newLogin)
  126. checkFields()
  127. }
  128. this.setPass = function(newPass) {
  129. _password.setValue(newPass)
  130. checkFields()
  131. }
  132. function checkFields() {
  133. if (_login.getValue() && _password.getValue()) {
  134. login_btn.disabled = false
  135. } else {
  136. login_btn.disabled = true
  137. }
  138. }
  139. _login.getHTMLElement().addEventListener('input', checkFields)
  140. _password.getHTMLElements().Password_input.addEventListener('input', checkFields)
  141. parent.append(form)
  142. }
  143. let login_form = new loginForm(document.body)
  144. // login_form.setLogin('default_user')
  145. // login_form.setPass('pass example', true)
  146. // window.setTimeout(()=> {
  147. // console.log(login_form.getPassword())
  148. // console.log('before open', login_form.isPasswordOpened())
  149. // login_form.setPasswordOpened(true)
  150. // console.log('after open', login_form.isPasswordOpened())
  151. // console.log(login_form.getLogin())
  152. // }, 5000)
  153. // Password Verify
  154. class PasswordVerify extends Password {
  155. constructor(parent, open=false) {
  156. super(parent, open);
  157. let _repeatWrapper = document.createElement('div')
  158. let _repeatField = document.createElement('input')
  159. let _successBtn = document.createElement('button')
  160. let displayRepeat = () => {
  161. if(this.getOpen() === false) {
  162. _repeatField.style.marginTop = '10px'
  163. _repeatField.type = 'password'
  164. _repeatField.placeholder = 'repeat password'
  165. _repeatField.value = ''
  166. _successBtn.innerText = 'no match!'
  167. _successBtn.disabled = true
  168. _successBtn.style.display = 'inline-block'
  169. _successBtn.style.backgroundColor = 'transparent'
  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. if(this.getHTMLElements().Password_input.value && _repeatField.value) {
  184. if(this.getHTMLElements().Password_input.value === _repeatField.value) {
  185. return true
  186. }
  187. }
  188. return false
  189. }
  190. let btnActivate = (bool) => {
  191. if(bool) {
  192. _successBtn.disabled = false
  193. _successBtn.style.backgroundColor = 'chartreuse'
  194. _successBtn.innerText = 'passwords match'
  195. } else {
  196. _successBtn.disabled = true
  197. _successBtn.style.backgroundColor = 'transparent'
  198. _successBtn.innerText = 'no match!'
  199. }
  200. }
  201. this.getHTMLElements().Password_input.addEventListener('input', () => {
  202. btnActivate(isMatch())
  203. })
  204. _repeatField.addEventListener('input',() => {
  205. btnActivate(isMatch())
  206. })
  207. }
  208. }
  209. document.body.insertAdjacentHTML('beforeend', `
  210. <div id=verif></div>
  211. `)
  212. let verif = document.getElementById('verif')
  213. let passverif = new PasswordVerify(verif, true)
  214. // window.setTimeout(()=> {
  215. // console.log('bang')
  216. // passverif.setOpen(false)
  217. // }, 5000)
  218. // login form
  219. function Form(el, data, okCallback, cancelCallback){
  220. let formBody = document.createElement('div')
  221. let okButton = document.createElement('button')
  222. okButton.innerHTML = 'OK'
  223. this.validators = {isValid:{}}
  224. let cancelButton = document.createElement('button')
  225. cancelButton.innerHTML = 'Cancel'
  226. formBody.innerHTML = '<h2>Form</h2>'
  227. if (typeof okCallback === 'function'){
  228. formBody.appendChild(okButton);
  229. okButton.onclick = (e) => {
  230. console.log(this)
  231. this.okCallback(e)
  232. }
  233. }
  234. if (typeof cancelCallback === 'function'){
  235. formBody.appendChild(cancelButton);
  236. cancelButton.onclick = cancelCallback
  237. }
  238. el.appendChild(formBody)
  239. this.checkInfo = (value, key, data, input, err) => {
  240. let validCheck
  241. let keyFormated = key
  242. okButton.disabled = true
  243. try {
  244. console.log('valid', this.validators.isValid, data)
  245. if(key[0] === "*") {
  246. keyFormated = key.substring(1)
  247. if(this.validators.mandatoryErr(value, key) === true) {
  248. validCheck = this.validators[keyFormated]? this.validators[keyFormated](value, keyFormated) : true
  249. input.style.backgroundColor = 'transparent'
  250. input.style.color = 'black'
  251. console.log(input)
  252. err.innerText = ''
  253. } else {
  254. err.innerText = this.validators.mandatoryErr(value, keyFormated)
  255. this.validators.isValid[key] = false
  256. }
  257. } else {
  258. validCheck = this.validators[keyFormated]? this.validators[keyFormated](value, keyFormated) : true
  259. }
  260. if(typeof validCheck === 'string') {
  261. input.style.backgroundColor = 'firebrick'
  262. input.style.color = 'white'
  263. err.innerText = validCheck
  264. this.validators.isValid[key] = validCheck
  265. } else if(typeof validCheck === 'boolean' && validCheck === true) {
  266. input.style.backgroundColor = 'transparent'
  267. input.style.color = 'black'
  268. err.innerText = ''
  269. this.validators.isValid[key] = validCheck
  270. }
  271. console.log('valid', this.validators.isValid)
  272. } catch(e){/*sample text*/}
  273. try {
  274. if(Object.values(this.validators.isValid).every((item) => item === true)) {
  275. okButton.disabled = false
  276. } else {
  277. okButton.disabled = true
  278. }
  279. } catch(e) {}
  280. }
  281. let inputCreators = {
  282. string: (key, value, func ) => {
  283. let wrap = document.createElement('div')
  284. let input
  285. let small = document.createElement('small')
  286. small.style.color = 'red'
  287. small.innerText = ''
  288. if(/^[*]+$/.test(data[key])) {
  289. let div = document.createElement('div')
  290. input = new Password(div, true)
  291. input.getHTMLElements().Password_input.value = ''
  292. console.log('iz valid', this.validators.isValid)
  293. this.validators.isValid[key] = false
  294. wrap.append(input.getHTMLElements().Password_wrapper, small)
  295. input.getHTMLElements().Password_input.oninput = (e) => {
  296. func(input.getHTMLElements().Password_input.value)
  297. this.checkInfo(input.getHTMLElements().Password_input.value, key, {}, input.getHTMLElements().Password_input, small)
  298. }
  299. } else {
  300. input = document.createElement('input')
  301. input.type = 'text'
  302. input.style.display = 'block'
  303. input.placeholder = key
  304. input.value = value
  305. wrap.append(input, small)
  306. input.oninput = () => {
  307. func(input.value)
  308. this.checkInfo(input.value, key, {}, input, small)
  309. }
  310. }
  311. return wrap
  312. },
  313. boolean: (key, value, func) => {
  314. let wrap = document.createElement('div')
  315. let input = document.createElement('input')
  316. input.type = 'checkbox'
  317. input.placeholder = key
  318. input.checked = value
  319. let small = document.createElement('small')
  320. small.style.color = 'red'
  321. small.innerText = ''
  322. wrap.append(input, small)
  323. input.onchange = (e) => {
  324. func(input.checked)
  325. this.checkInfo(input.checked, key, {}, input, small)
  326. }
  327. return wrap
  328. },
  329. date: (key, value, func) => {
  330. let wrap = document.createElement('div')
  331. let input = document.createElement('input')
  332. input.type = 'datetime-local'
  333. input.style.display = 'block'
  334. input.placeholder = key
  335. input.value = new Date(value).toISOString().slice(0, -1)
  336. let small = document.createElement('small')
  337. small.style.color = 'red'
  338. small.innerText = ''
  339. wrap.append(input, small)
  340. input.onchange = () => {
  341. func(new Date(input.value))
  342. this.checkInfo(input.value, key, {}, input, small)
  343. }
  344. return wrap
  345. },
  346. }
  347. let formTable = document.createElement('table')
  348. formTable.border = 1
  349. for(let key in data) {
  350. let tr = document.createElement('tr')
  351. let tdKey = document.createElement('td')
  352. let tdInput = document.createElement('td')
  353. let input = inputCreators[(data[key].constructor.name).toLowerCase()](key, data[key],(value) => {
  354. data[key] = value
  355. })
  356. tdKey.innerHTML = key[0] ==='*'? `<span><span style="color: red;">*&nbsp</span>${key.substring(1)}</span>`:`<span>${key}</span>`
  357. tdInput.append(input)
  358. tr.append(tdKey, tdInput)
  359. formTable.append(tr)
  360. }
  361. okButton.before(formTable)
  362. this.okCallback = okCallback
  363. this.cancelCallback = cancelCallback
  364. this.data = data
  365. }
  366. let formContainer = document.createElement('div')
  367. formContainer.id = 'formContainer'
  368. document.body.append(formContainer)
  369. let form = new Form(formContainer, {
  370. "*name": 'Anakin',
  371. surname: 'Skywalker',
  372. "*married": true,
  373. birthday: new Date((new Date).getTime() - 86400000 * 30*365),
  374. "*password": '**'
  375. }, () => console.log('ok'),() => console.log('cancel') )
  376. form.okCallback = () => console.log('ok2')
  377. form.validators.surname = (value, key, data, input) => value !== '' && value.length > 2 &&
  378. value[0].toUpperCase() == value[0] &&
  379. !/[0-9 \W]/.test(value) ? true : `Wrong ${key}`
  380. form.validators.name = form.validators.surname
  381. form.validators.mandatoryErr = (value, key, data, input) => {
  382. if(typeof value === 'boolean') {
  383. return value === true? value : `${key} is mandatory`
  384. } else {
  385. return value.length > 0 && value !== ''? true : `${key} is mandatory`
  386. }
  387. }
  388. form.validators.birthday = (value, key, data, input) => {
  389. let today = new Date()
  390. let given = new Date(value)
  391. let lowest = new Date()
  392. lowest.setFullYear(1900)
  393. lowest.setHours(0,0,0,0)
  394. today.setHours(0,0,0,0)
  395. given.setHours(0,0,0,0)
  396. if(given >= today || given < lowest || value === '') {
  397. return `Wrong ${key}`
  398. }else {
  399. return true
  400. }
  401. }
  402. for(let key in form) {
  403. //проверка изначальных данных
  404. form.checkInfo(form[key], key,)
  405. }
  406. console.log(form)