script.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. <<<<<<< HEAD
  240. let checkInfo = (value, key, data, input, err) => {
  241. console.log(this.initalData)
  242. console.log(this.data)
  243. for(let key in this.validators) {
  244. if(key === 'isValid') continue;
  245. this.validators.isValid[key] = false
  246. }
  247. console.dir(this.validators)
  248. =======
  249. this.checkInfo = (value, key, data, input, err) => {
  250. >>>>>>> 4a6ddce12df7d82608b2ab2f1082894f85ada9c2
  251. let validCheck
  252. let keyFormated = key
  253. okButton.disabled = true
  254. try {
  255. console.log('valid', this.validators.isValid, data)
  256. if(key[0] === "*") {
  257. keyFormated = key.substring(1)
  258. if(this.validators.mandatoryErr(value, key) === true) {
  259. validCheck = this.validators[keyFormated]? this.validators[keyFormated](value, keyFormated) : true
  260. input.style.backgroundColor = 'transparent'
  261. input.style.color = 'black'
  262. console.log(input)
  263. err.innerText = ''
  264. } else {
  265. err.innerText = this.validators.mandatoryErr(value, keyFormated)
  266. this.validators.isValid[key] = false
  267. }
  268. } else {
  269. validCheck = this.validators[keyFormated]? this.validators[keyFormated](value, keyFormated) : true
  270. }
  271. if(typeof validCheck === 'string') {
  272. input.style.backgroundColor = 'firebrick'
  273. input.style.color = 'white'
  274. err.innerText = validCheck
  275. this.validators.isValid[key] = validCheck
  276. } else if(typeof validCheck === 'boolean' && validCheck === true) {
  277. input.style.backgroundColor = 'transparent'
  278. input.style.color = 'black'
  279. err.innerText = ''
  280. this.validators.isValid[key] = validCheck
  281. }
  282. console.log('valid', this.validators.isValid)
  283. } catch(e){/*sample text*/}
  284. try {
  285. if(Object.values(this.validators.isValid).every((item) => item === true)) {
  286. okButton.disabled = false
  287. } else {
  288. okButton.disabled = true
  289. }
  290. } catch(e) {}
  291. }
  292. let inputCreators = {
  293. string: (key, value, func ) => {
  294. let wrap = document.createElement('div')
  295. let input
  296. let small = document.createElement('small')
  297. small.style.color = 'red'
  298. small.innerText = ''
  299. if(/^[*]+$/.test(data[key])) {
  300. let div = document.createElement('div')
  301. input = new Password(div, true)
  302. input.getHTMLElements().Password_input.value = ''
  303. console.log('iz valid', this.validators.isValid)
  304. this.validators.isValid[key] = false
  305. wrap.append(input.getHTMLElements().Password_wrapper, small)
  306. input.getHTMLElements().Password_input.oninput = (e) => {
  307. func(input.getHTMLElements().Password_input.value)
  308. this.checkInfo(input.getHTMLElements().Password_input.value, key, {}, input.getHTMLElements().Password_input, small)
  309. }
  310. } else {
  311. input = document.createElement('input')
  312. input.type = 'text'
  313. input.style.display = 'block'
  314. input.placeholder = key
  315. input.value = value
  316. wrap.append(input, small)
  317. input.oninput = () => {
  318. func(input.value)
  319. this.checkInfo(input.value, key, {}, input, small)
  320. }
  321. }
  322. return wrap
  323. },
  324. boolean: (key, value, func) => {
  325. let wrap = document.createElement('div')
  326. let input = document.createElement('input')
  327. input.type = 'checkbox'
  328. input.placeholder = key
  329. input.checked = value
  330. let small = document.createElement('small')
  331. small.style.color = 'red'
  332. small.innerText = ''
  333. wrap.append(input, small)
  334. input.onchange = (e) => {
  335. func(input.checked)
  336. this.checkInfo(input.checked, key, {}, input, small)
  337. }
  338. return wrap
  339. },
  340. date: (key, value, func) => {
  341. let wrap = document.createElement('div')
  342. let input = document.createElement('input')
  343. input.type = 'datetime-local'
  344. input.style.display = 'block'
  345. input.placeholder = key
  346. input.value = new Date(value).toISOString().slice(0, -1)
  347. let small = document.createElement('small')
  348. small.style.color = 'red'
  349. small.innerText = ''
  350. wrap.append(input, small)
  351. input.onchange = () => {
  352. func(new Date(input.value))
  353. this.checkInfo(input.value, key, {}, input, small)
  354. }
  355. return wrap
  356. },
  357. }
  358. let formTable = document.createElement('table')
  359. formTable.border = 1
  360. for(let key in data) {
  361. let tr = document.createElement('tr')
  362. let tdKey = document.createElement('td')
  363. let tdInput = document.createElement('td')
  364. let input = inputCreators[(data[key].constructor.name).toLowerCase()](key, data[key],(value) => {
  365. data[key] = value
  366. })
  367. tdKey.innerHTML = key[0] ==='*'? `<span><span style="color: red;">*&nbsp</span>${key.substring(1)}</span>`:`<span>${key}</span>`
  368. tdInput.append(input)
  369. tr.append(tdKey, tdInput)
  370. formTable.append(tr)
  371. }
  372. okButton.before(formTable)
  373. this.okCallback = okCallback
  374. this.cancelCallback = cancelCallback
  375. this.data = data
  376. <<<<<<< HEAD
  377. this.initalData = this.data
  378. this.validators = {isValid:{}}
  379. =======
  380. >>>>>>> 4a6ddce12df7d82608b2ab2f1082894f85ada9c2
  381. }
  382. let formContainer = document.createElement('div')
  383. formContainer.id = 'formContainer'
  384. document.body.append(formContainer)
  385. let form = new Form(formContainer, {
  386. "*name": 'Anakin',
  387. surname: 'Skywalker',
  388. "*married": true,
  389. birthday: new Date((new Date).getTime() - 86400000 * 30*365),
  390. "*password": '**'
  391. }, () => console.log('ok'),() => console.log('cancel') )
  392. form.okCallback = () => console.log('ok2')
  393. form.validators.surname = (value, key, data, input) => value !== '' && value.length > 2 &&
  394. value[0].toUpperCase() == value[0] &&
  395. !/[0-9 \W]/.test(value) ? true : `Wrong ${key}`
  396. form.validators.name = form.validators.surname
  397. form.validators.mandatoryErr = (value, key, data, input) => {
  398. if(typeof value === 'boolean') {
  399. return value === true? value : `${key} is mandatory`
  400. } else {
  401. return value.length > 0 && value !== ''? true : `${key} is mandatory`
  402. }
  403. }
  404. form.validators.birthday = (value, key, data, input) => {
  405. let today = new Date()
  406. let given = new Date(value)
  407. let lowest = new Date()
  408. lowest.setFullYear(1900)
  409. lowest.setHours(0,0,0,0)
  410. today.setHours(0,0,0,0)
  411. given.setHours(0,0,0,0)
  412. if(given >= today || given < lowest || value === '') {
  413. return `Wrong ${key}`
  414. }else {
  415. return true
  416. }
  417. }
  418. for(let key in form) {
  419. //проверка изначальных данных
  420. form.checkInfo(form[key], key,)
  421. }
  422. console.log(form)