script.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. let cancelButton = document.createElement('button')
  224. cancelButton.innerHTML = 'Cancel'
  225. formBody.innerHTML = '<h2>Form</h2>'
  226. if (typeof okCallback === 'function'){
  227. formBody.appendChild(okButton);
  228. okButton.onclick = (e) => {
  229. console.log(this)
  230. this.okCallback(e)
  231. }
  232. }
  233. if (typeof cancelCallback === 'function'){
  234. formBody.appendChild(cancelButton);
  235. cancelButton.onclick = cancelCallback
  236. }
  237. el.appendChild(formBody)
  238. let checkInfo = (value, key, data, input, err) => {
  239. console.log(this.initalData)
  240. console.log(this.data)
  241. for(let key in this.validators) {
  242. if(key === 'isValid') continue;
  243. this.validators.isValid[key] = false
  244. }
  245. console.dir(this.validators)
  246. let validCheck
  247. let keyFormated = key
  248. try {
  249. if(key[0] === "*") {
  250. keyFormated = key.substring(1)
  251. if(this.validators.mandatoryErr(value, key) === true) {
  252. validCheck = this.validators[keyFormated]? this.validators[keyFormated](value, keyFormated) : true
  253. input.style.backgroundColor = 'transparent'
  254. input.style.color = 'black'
  255. console.log(input)
  256. err.innerText = ''
  257. } else {
  258. err.innerText = this.validators.mandatoryErr(value, keyFormated)
  259. this.validators.isValid[key] = false
  260. }
  261. } else {
  262. validCheck = this.validators[keyFormated]? this.validators[keyFormated](value, keyFormated) : true
  263. }
  264. console.log('validators ', this.validators)
  265. if(typeof validCheck === 'string') {
  266. input.style.backgroundColor = 'firebrick'
  267. input.style.color = 'white'
  268. err.innerText = validCheck
  269. this.validators.isValid[key] = validCheck
  270. } else if(typeof validCheck === 'boolean' && validCheck === true) {
  271. input.style.backgroundColor = 'transparent'
  272. input.style.color = 'black'
  273. err.innerText = ''
  274. this.validators.isValid[key] = validCheck
  275. }
  276. console.log(Object.keys(this.validators.isValid))
  277. console.log(Object.values(this.validators.isValid))
  278. console.log(Object.values(this.validators.isValid).every((item) => item === true))
  279. if(Object.values(this.validators.isValid).every((item) => item === true)) {
  280. okButton.disabled = false
  281. } else {
  282. okButton.disabled = true
  283. }
  284. } catch(e){console.warn('whoopsie', e)}
  285. }
  286. let inputCreators = {
  287. string: (key, value, func ) => {
  288. let wrap = document.createElement('div')
  289. let input
  290. let small = document.createElement('small')
  291. small.style.color = 'red'
  292. small.innerText = ''
  293. if(/^[*]+$/.test(data[key])) {
  294. let div = document.createElement('div')
  295. input = new Password(div, true)
  296. input.getHTMLElements().Password_input.value = ''
  297. wrap.append(input.getHTMLElements().Password_wrapper, small)
  298. input.getHTMLElements().Password_input.oninput = (e) => {
  299. func(input.getHTMLElements().Password_input.value)
  300. checkInfo(input.getHTMLElements().Password_input.value, key, {}, input.getHTMLElements().Password_input, small)
  301. }
  302. func(input.getHTMLElements().Password_input.value)
  303. checkInfo(input.getHTMLElements().Password_input.value, key, {}, input.getHTMLElements().Password_input, small)
  304. } else {
  305. input = document.createElement('input')
  306. input.type = 'text'
  307. input.style.display = 'block'
  308. input.placeholder = key
  309. input.value = value
  310. wrap.append(input, small)
  311. input.oninput = () => {
  312. func(input.value)
  313. checkInfo(input.value, key, {}, input, small)
  314. }
  315. func(input.value)
  316. checkInfo(input.value, key, {}, input, small)
  317. }
  318. return wrap
  319. },
  320. boolean: (key, value, func) => {
  321. let wrap = document.createElement('div')
  322. let input = document.createElement('input')
  323. input.type = 'checkbox'
  324. input.placeholder = key
  325. input.checked = value
  326. let small = document.createElement('small')
  327. small.style.color = 'red'
  328. small.innerText = ''
  329. wrap.append(input, small)
  330. input.onchange = (e) => {
  331. func(input.checked)
  332. checkInfo(input.checked, key, {}, input, small)
  333. }
  334. func(input.checked)
  335. checkInfo(input.checked, key, {}, input, small)
  336. return wrap
  337. },
  338. date: (key, value, func) => {
  339. let wrap = document.createElement('div')
  340. let input = document.createElement('input')
  341. input.type = 'datetime-local'
  342. input.style.display = 'block'
  343. input.placeholder = key
  344. input.value = new Date(value).toISOString().slice(0, -1)
  345. let small = document.createElement('small')
  346. small.style.color = 'red'
  347. small.innerText = ''
  348. wrap.append(input, small)
  349. input.onchange = () => {
  350. func(new Date(input.value))
  351. checkInfo(input.value, key, {}, input, small)
  352. }
  353. func(new Date(input.value))
  354. checkInfo(input.value, key, {}, input, small)
  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. this.initalData = this.data
  377. this.validators = {isValid:{}}
  378. }
  379. let formContainer = document.createElement('div')
  380. formContainer.id = 'formContainer'
  381. document.body.append(formContainer)
  382. let form = new Form(formContainer, {
  383. "*name": 'Anakin',
  384. surname: 'Skywalker',
  385. "*married": true,
  386. birthday: new Date((new Date).getTime() - 86400000 * 30*365),
  387. "*password": '**'
  388. }, () => console.log('ok'),() => console.log('cancel') )
  389. form.okCallback = () => console.log('ok2')
  390. form.validators.surname = (value, key, data, input) => value !== '' && value.length > 2 &&
  391. value[0].toUpperCase() == value[0] &&
  392. !/[0-9 \W]/.test(value) ? true : `Wrong ${key}`
  393. form.validators.name = form.validators.surname
  394. form.validators.mandatoryErr = (value, key, data, input) => {
  395. if(typeof value === 'boolean') {
  396. return value === true? value : `${key} is mandatory`
  397. } else {
  398. return value.length > 0 && value !== ''? true : `${key} is mandatory`
  399. }
  400. }
  401. form.validators.birthday = (value, key, data, input) => {
  402. let today = new Date()
  403. let given = new Date(value)
  404. let lowest = new Date()
  405. lowest.setFullYear(1900)
  406. lowest.setHours(0,0,0,0)
  407. today.setHours(0,0,0,0)
  408. given.setHours(0,0,0,0)
  409. if(given >= today || given < lowest || value === '') {
  410. return `Wrong ${key}`
  411. }else {
  412. return true
  413. }
  414. }
  415. console.log(form)