script.js 14 KB

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