index.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. // passwordWrapp()
  2. function passwordWrapp() {
  3. // в классе ниже коллбэки расположены в ивентлистенерах, что неправильно,
  4. // их нужно размещать в сеттерах, а в ивентлистеннерах вызывать сеттеры
  5. // function Password(parent=document.body, open=false) {
  6. // const passField = document.createElement('input')
  7. // const checkBox = document.createElement('input')
  8. // checkBox.type = 'checkbox'
  9. // parent.append(passField)
  10. // parent.append(checkBox)
  11. // this.setValue = (newValue) => {
  12. // passField.value = newValue
  13. // }
  14. // this.getValue = () => passField.value
  15. // this.setOpen = (newOpen) => {
  16. // open = newOpen
  17. // if (open === true) {
  18. // checkBox.checked = true
  19. // passField.type = 'text'
  20. // } else if (open === false) {
  21. // checkBox.checked = false
  22. // passField.type = 'password'
  23. // }
  24. // }
  25. // this.setOpen(open)
  26. // this.getOpen = () => checkBox.checked
  27. // passField.oninput = () => {
  28. // if (typeof this.onChange === 'function') {
  29. // this.onChange(this.getValue())
  30. // }
  31. // }
  32. // checkBox.oninput = () => {
  33. // if (this.getOpen() === true) {
  34. // this.setOpen(true)
  35. // } else {
  36. // this.setOpen(false)
  37. // }
  38. // if (typeof this.onOpenChange === 'function') {
  39. // this.onOpenChange(this.getOpen())
  40. // }
  41. // }
  42. // }
  43. // здесь коллбэки расположены правильно
  44. function Password(parent=document.body, open=false) {
  45. const passField = document.createElement('input')
  46. const checkBox = document.createElement('input')
  47. checkBox.type = 'checkbox'
  48. parent.append(passField)
  49. parent.append(checkBox)
  50. this.setValue = (newValue) => {
  51. passField.value = newValue
  52. if (typeof this.onChange === 'function') {
  53. this.onChange(newValue)
  54. }
  55. }
  56. this.getValue = () => passField.value
  57. this.setOpen = (newOpen) => {
  58. open = newOpen
  59. if (open === true) {
  60. checkBox.checked = true
  61. passField.type = 'text'
  62. } else if (open === false) {
  63. checkBox.checked = false
  64. passField.type = 'password'
  65. }
  66. if (typeof this.onOpenChange === 'function') {
  67. this.onOpenChange(open)
  68. }
  69. }
  70. this.setOpen(open)
  71. this.getOpen = () => checkBox.checked
  72. passField.oninput = () => {
  73. this.setValue(this.getValue())
  74. }
  75. checkBox.oninput = () => {
  76. if (this.getOpen() === true) {
  77. this.setOpen(true)
  78. } else {
  79. this.setOpen(false)
  80. }
  81. }
  82. }
  83. let p = new Password(document.body, true)
  84. p.onChange = data => console.log(data)
  85. p.onOpenChange = open => console.log(open)
  86. // p.setValue('qwerty')
  87. // console.log(p.getValue())
  88. // p.setOpen(false)
  89. // console.log(p.getOpen())
  90. }
  91. // loginFormWrapp()
  92. function loginFormWrapp() {
  93. function Password(parent=document.body, open=false) {
  94. const passField = document.createElement('input')
  95. const checkBox = document.createElement('input')
  96. checkBox.type = 'checkbox'
  97. parent.append(passField)
  98. parent.append(checkBox)
  99. this.setValue = (newValue) => {
  100. passField.value = newValue
  101. if (typeof this.onChange === 'function') {
  102. this.onChange(newValue)
  103. }
  104. }
  105. this.getValue = () => passField.value
  106. this.setOpen = (newOpen) => {
  107. open = newOpen
  108. if (open === true) {
  109. checkBox.checked = true
  110. passField.type = 'text'
  111. } else if (open === false) {
  112. checkBox.checked = false
  113. passField.type = 'password'
  114. }
  115. if (typeof this.onOpenChange === 'function') {
  116. this.onOpenChange(open)
  117. }
  118. }
  119. this.setOpen(open)
  120. this.getOpen = () => checkBox.checked
  121. passField.oninput = () => {
  122. this.setValue(this.getValue())
  123. }
  124. checkBox.oninput = () => {
  125. if (this.getOpen() === true) {
  126. this.setOpen(true)
  127. } else {
  128. this.setOpen(false)
  129. }
  130. }
  131. }
  132. let loginInput = document.createElement('input')
  133. formContainer.append(loginInput)
  134. let passwordInput = new Password(formContainer, false)
  135. let loginButton = document.createElement('button')
  136. loginButton.innerText = 'Подтвердить'
  137. loginButton.disabled = true
  138. formContainer.append(loginButton)
  139. function changeBtnStatus() {
  140. if (loginInput.value && passwordInput.getValue()) {
  141. loginButton.disabled = false
  142. } else {
  143. loginButton.disabled = true
  144. }
  145. }
  146. passwordInput.onChange = changeBtnStatus
  147. loginInput.oninput = changeBtnStatus
  148. }
  149. // loginFormConstructorWrapp()
  150. function loginFormConstructorWrapp() {
  151. function Password(parent=document.body, open=false) {
  152. const passField = document.createElement('input')
  153. const checkBox = document.createElement('input')
  154. checkBox.type = 'checkbox'
  155. parent.append(passField)
  156. parent.append(checkBox)
  157. this.setValue = (newValue) => {
  158. passField.value = newValue
  159. if (typeof this.onChange === 'function') {
  160. this.onChange(newValue)
  161. }
  162. }
  163. this.getValue = () => passField.value
  164. this.setOpen = (newOpen) => {
  165. open = newOpen
  166. if (open === true) {
  167. checkBox.checked = true
  168. passField.type = 'text'
  169. } else if (open === false) {
  170. checkBox.checked = false
  171. passField.type = 'password'
  172. }
  173. if (typeof this.onOpenChange === 'function') {
  174. this.onOpenChange(open)
  175. }
  176. }
  177. this.setOpen(open)
  178. this.getOpen = () => checkBox.checked
  179. passField.oninput = () => {
  180. this.setValue(this.getValue())
  181. }
  182. checkBox.oninput = () => {
  183. if (this.getOpen() === true) {
  184. this.setOpen(true)
  185. } else {
  186. this.setOpen(false)
  187. }
  188. }
  189. }
  190. function LoginForm(parent=document.body, open=false) {
  191. // let currThis = this
  192. const loginField = document.createElement('input')
  193. loginField.type = 'text'
  194. const button = document.createElement('button')
  195. button.innerText = 'Подтвердить'
  196. parent.append(loginField)
  197. let passField = new Password(parent, open)
  198. parent.append(button)
  199. this.setLoginValue = (newValue) => {
  200. loginField.value = newValue
  201. this.makeCallback(this.onLoginChange, newValue)
  202. }
  203. this.getLoginValue = () => loginField.value
  204. let btnState = true
  205. this.setBtn = (newValue=btnState) => {
  206. btnState = newValue
  207. if (this.getLoginValue() && passField.getValue()) {
  208. btnState = false
  209. } else {
  210. btnState = true
  211. }
  212. button.disabled = btnState
  213. }
  214. this.setBtn()
  215. this.getBtn = () => button.disabled
  216. this.makeCallback = (name, content) => {
  217. if (typeof name === 'function') {
  218. name(content)
  219. }
  220. }
  221. loginField.oninput = () => {
  222. this.setBtn()
  223. this.setLoginValue(this.getLoginValue())
  224. }
  225. passField.onChange = () => {
  226. this.setBtn()
  227. }
  228. button.onclick = () => {
  229. this.makeCallback(this.onBtnClick, 'click')
  230. }
  231. }
  232. let loginForm = new LoginForm(document.body)
  233. loginForm.onLoginChange = (data) => console.log(data)
  234. loginForm.onBtnClick = (data) => console.log(data)
  235. }
  236. passwordVerifyWrapp()
  237. function passwordVerifyWrapp() {
  238. function Password(parent=document.body, open=false) {
  239. const passField = document.createElement('input')
  240. const checkBox = document.createElement('input')
  241. checkBox.type = 'checkbox'
  242. parent.append(passField)
  243. parent.append(checkBox)
  244. this.setValue = (newValue) => {
  245. passField.value = newValue
  246. if (typeof this.onChange === 'function') {
  247. this.onChange(newValue)
  248. }
  249. }
  250. this.getValue = () => passField.value
  251. this.setOpen = (newOpen) => {
  252. open = newOpen
  253. if (open === true) {
  254. checkBox.checked = true
  255. passField.type = 'text'
  256. } else if (open === false) {
  257. checkBox.checked = false
  258. passField.type = 'password'
  259. }
  260. if (typeof this.onOpenChange === 'function') {
  261. this.onOpenChange(open)
  262. }
  263. }
  264. this.setOpen(open)
  265. this.getOpen = () => checkBox.checked
  266. passField.oninput = () => {
  267. this.setValue(this.getValue())
  268. }
  269. checkBox.oninput = () => {
  270. if (this.getOpen() === true) {
  271. this.setOpen(true)
  272. } else {
  273. this.setOpen(false)
  274. }
  275. }
  276. }
  277. function RegForm(parent=document.body, open=false) {
  278. const loginField = document.createElement('input')
  279. loginField.type = 'text'
  280. const passField2 = document.createElement('input')
  281. passField2.type = 'password'
  282. const button = document.createElement('button')
  283. button.innerText = 'Подтвердить'
  284. parent.append(loginField)
  285. parent.append(passField2)
  286. let passField = new Password(parent, open)
  287. parent.append(button)
  288. this.setLoginValue = (newValue) => {
  289. loginField.value = newValue
  290. this.makeCallback(this.onLoginChange, newValue)
  291. }
  292. this.getLoginValue = () => loginField.value
  293. this.setPassValue2 = (newValue) => {
  294. passField2.value = newValue
  295. this.makeCallback(this.onPass2Change, newValue)
  296. if (passField.getOpen()) {
  297. passField2.style.display = 'none'
  298. } else {
  299. passField2.style.display = 'inline'
  300. }
  301. }
  302. this.getPassValue2 = () => passField2.value
  303. this.setPassState2 = (state=open) => {
  304. if (state) {
  305. passField2.style.display = 'none'
  306. } else {
  307. passField2.style.display = 'inline'
  308. }
  309. }
  310. this.getPassState2 = () => {
  311. if (passField2.style.display = 'none') {
  312. return true
  313. } else {
  314. return false
  315. }
  316. }
  317. let btnState = true
  318. this.setBtn = (newValue=btnState) => {
  319. btnState = newValue
  320. if (passField.getOpen() ? this.getLoginValue() && passField.getValue() :
  321. this.getLoginValue() && passField.getValue() && passField.getValue() === this.getPassValue2()) {
  322. btnState = false
  323. } else {
  324. btnState = true
  325. }
  326. button.disabled = btnState
  327. }
  328. this.setBtn()
  329. this.getBtn = () => button.disabled
  330. this.makeCallback = (name, content) => {
  331. if (typeof name === 'function') {
  332. name(content)
  333. }
  334. }
  335. loginField.oninput = () => {
  336. this.setBtn()
  337. this.setLoginValue(this.getLoginValue())
  338. }
  339. passField.onChange = () => {
  340. this.setBtn()
  341. }
  342. passField.onOpenChange = () => {
  343. this.setPassValue2(this.getPassValue2())
  344. this.setBtn()
  345. }
  346. passField2.oninput = () => {
  347. this.setBtn()
  348. this.setPassState2(passField.getOpen())
  349. }
  350. button.onclick = () => {
  351. this.makeCallback(this.onBtnClick, 'click')
  352. }
  353. }
  354. let regForm = new RegForm()
  355. }
  356. // В коде ниже не используется класс Password, все написано общими классами
  357. // loginFormWrapp2()
  358. // function loginFormWrapp2() {
  359. // function LoginForm(parent=document.body, open=false) {
  360. // const loginField = document.createElement('input')
  361. // loginField.type = 'text'
  362. // const passField = document.createElement('input')
  363. // const checkBox = document.createElement('input')
  364. // checkBox.type = 'checkbox'
  365. // const button = document.createElement('button')
  366. // button.innerText = 'Подтвердить'
  367. // button.disabled = true
  368. // parent.append(loginField)
  369. // parent.append(passField)
  370. // parent.append(checkBox)
  371. // parent.append(button)
  372. // this.setLoginValue = (newValue) => {
  373. // loginField.value = newValue
  374. // }
  375. // this.getLoginValue = () => loginField.value
  376. // this.setPassValue = (newValue) => {
  377. // passField.value = newValue
  378. // }
  379. // this.getPassValue = () => passField.value
  380. // this.setOpen = (newOpen) => {
  381. // open = newOpen
  382. // if (open === true) {
  383. // checkBox.checked = true
  384. // passField.type = 'text'
  385. // } else if (open === false) {
  386. // checkBox.checked = false
  387. // passField.type = 'password'
  388. // }
  389. // }
  390. // this.setOpen(open)
  391. // this.getOpen = () => checkBox.checked
  392. // loginField.oninput = () => {
  393. // if (this.getLoginValue() && this.getPassValue()) {
  394. // button.disabled = false
  395. // } else {
  396. // button.disabled = true
  397. // }
  398. // if (typeof this.onLoginChange === 'function') {
  399. // this.onLoginChange(this.getLoginValue())
  400. // }
  401. // }
  402. // passField.oninput = () => {
  403. // if (this.getLoginValue() && this.getPassValue()) {
  404. // button.disabled = false
  405. // } else {
  406. // button.disabled = true
  407. // }
  408. // if (typeof this.onPassChange === 'function') {
  409. // this.onPassChange(this.getPassValue())
  410. // }
  411. // }
  412. // checkBox.oninput = () => {
  413. // if (this.getOpen() === true) {
  414. // this.setOpen(true)
  415. // } else {
  416. // this.setOpen(false)
  417. // }
  418. // if (typeof this.onOpenChange === 'function') {
  419. // this.onOpenChange(this.getOpen())
  420. // }
  421. // }
  422. // }
  423. // let loginForm = new LoginForm(document.body)
  424. // }
  425. // loginFormConstructorWrapp2()
  426. // function loginFormConstructorWrapp2() {
  427. // function LoginForm(parent=document.body, open=false) {
  428. // const loginField = document.createElement('input')
  429. // loginField.type = 'text'
  430. // const passField = document.createElement('input')
  431. // const checkBox = document.createElement('input')
  432. // checkBox.type = 'checkbox'
  433. // const button = document.createElement('button')
  434. // button.innerText = 'Подтвердить'
  435. // parent.append(loginField)
  436. // parent.append(passField)
  437. // parent.append(checkBox)
  438. // parent.append(button)
  439. // this.setLoginValue = (newValue) => {
  440. // loginField.value = newValue
  441. // }
  442. // this.getLoginValue = () => loginField.value
  443. // this.setPassValue = (newValue) => {
  444. // passField.value = newValue
  445. // }
  446. // this.getPassValue = () => passField.value
  447. // this.setOpen = (newOpen) => {
  448. // open = newOpen
  449. // if (open === true) {
  450. // checkBox.checked = true
  451. // passField.type = 'text'
  452. // } else if (open === false) {
  453. // checkBox.checked = false
  454. // passField.type = 'password'
  455. // }
  456. // }
  457. // this.setOpen(open)
  458. // this.getOpen = () => checkBox.checked
  459. // let btnState = true
  460. // this.setBtn = (newValue=btnState) => {
  461. // btnState = newValue
  462. // if (this.getLoginValue() && this.getPassValue()) {
  463. // btnState = false
  464. // } else {
  465. // btnState = true
  466. // }
  467. // button.disabled = btnState
  468. // }
  469. // this.setBtn()
  470. // this.getBtn = () => button.disabled
  471. // this.makeCallback = (name, content) => {
  472. // if (typeof name === 'function') {
  473. // name(content)
  474. // }
  475. // }
  476. // loginField.oninput = () => {
  477. // this.setBtn()
  478. // this.makeCallback(this.onLoginChange, this.getLoginValue())
  479. // }
  480. // passField.oninput = () => {
  481. // this.setBtn()
  482. // this.makeCallback(this.onPassChange, this.getPassValue())
  483. // }
  484. // checkBox.oninput = () => {
  485. // if (this.getOpen() === true) {
  486. // this.setOpen(true)
  487. // } else {
  488. // this.setOpen(false)
  489. // }
  490. // this.setBtn()
  491. // this.makeCallback(this.onOpenChange, this.getOpen())
  492. // }
  493. // button.onclick = () => {
  494. // this.makeCallback(this.onBtnClick, 'click')
  495. // }
  496. // }
  497. // let loginForm = new LoginForm(document.body)
  498. // loginForm.onLoginChange = data => console.log(data)
  499. // loginForm.onPassChange = data => console.log(data)
  500. // loginForm.onBtnClick = click => console.log(click)
  501. // }
  502. // passwordVerifyWrapp2()
  503. // function passwordVerifyWrapp2() {
  504. // function LoginForm(parent=document.body, open=false) {
  505. // const loginField = document.createElement('input')
  506. // loginField.type = 'text'
  507. // const passField = document.createElement('input')
  508. // const passField2 = document.createElement('input')
  509. // const checkBox = document.createElement('input')
  510. // checkBox.type = 'checkbox'
  511. // const button = document.createElement('button')
  512. // button.innerText = 'Подтвердить'
  513. // parent.append(loginField)
  514. // parent.append(passField)
  515. // parent.append(passField2)
  516. // parent.append(checkBox)
  517. // parent.append(button)
  518. // this.setLoginValue = (newValue) => {
  519. // loginField.value = newValue
  520. // }
  521. // this.getLoginValue = () => loginField.value
  522. // this.setPassValue = (newValue) => {
  523. // passField.value = newValue
  524. // }
  525. // this.getPassValue = () => passField.value
  526. // this.setPassValue2 = (newValue) => {
  527. // passField2.value = newValue
  528. // }
  529. // this.getPassValue2 = () => passField2.value
  530. // this.setOpen = (newOpen) => {
  531. // open = newOpen
  532. // checkBox.checked = open
  533. // if (open === true) {
  534. // passField.type = 'text'
  535. // passField2.style.display = 'none'
  536. // } else if (open === false) {
  537. // passField.type = 'password'
  538. // passField2.style.display = 'inline'
  539. // passField2.type = 'password'
  540. // }
  541. // }
  542. // this.setOpen(open)
  543. // this.getOpen = () => checkBox.checked
  544. // let btnState = true
  545. // this.setBtn = (newValue=btnState) => {
  546. // btnState = newValue
  547. // if (this.getOpen() ? this.getLoginValue() && this.getPassValue() :
  548. // this.getLoginValue() && this.getPassValue() && this.getPassValue() === this.getPassValue2()) {
  549. // btnState = false
  550. // } else {
  551. // btnState = true
  552. // }
  553. // button.disabled = btnState
  554. // }
  555. // this.setBtn()
  556. // this.getBtn = () => button.disabled
  557. // this.makeCallback = (name, content) => {
  558. // if (typeof name === 'function') {
  559. // name(content)
  560. // }
  561. // }
  562. // loginField.oninput = () => {
  563. // this.setBtn()
  564. // this.makeCallback(this.onLoginChange, this.getLoginValue())
  565. // }
  566. // passField.oninput = () => {
  567. // this.setBtn()
  568. // this.makeCallback(this.onPassChange, this.getPassValue())
  569. // }
  570. // passField2.oninput = () => {
  571. // this.setBtn()
  572. // this.makeCallback(this.onPass2Change, this.getPassValue2())
  573. // }
  574. // checkBox.oninput = () => {
  575. // if (this.getOpen() === true) {
  576. // this.setOpen(true)
  577. // } else {
  578. // this.setOpen(false)
  579. // }
  580. // this.setBtn()
  581. // this.makeCallback(this.onOpenChange, this.getOpen())
  582. // }
  583. // button.onclick = () => {
  584. // this.makeCallback(this.onBtnClick, 'click')
  585. // }
  586. // }
  587. // let loginForm = new LoginForm()
  588. // }
  589. // formWrapp()
  590. function formWrapp() {
  591. function Form(el, data, okCallback, cancelCallback){
  592. let formBody = document.createElement('div')
  593. let okButton = document.createElement('button')
  594. okButton.innerHTML = 'OK'
  595. let cancelButton = document.createElement('button')
  596. cancelButton.innerHTML = 'Cancel'
  597. formBody.innerHTML = '<h1>Форма</h1>'
  598. const table = document.createElement('table')
  599. formBody.append(table)
  600. let callBackValidator = {}
  601. let checkObj = (obj) => {
  602. for (let key in obj) {
  603. if (obj[key] === false) {
  604. return true
  605. }
  606. }
  607. return false
  608. }
  609. if (typeof okCallback === 'function'){
  610. formBody.appendChild(okButton)
  611. okButton.onclick = () => {
  612. // ??
  613. // console.log(this)
  614. this.okCallback(data)
  615. }
  616. }
  617. if (typeof cancelCallback === 'function'){
  618. formBody.appendChild(cancelButton)
  619. cancelButton.onclick = () => {
  620. table.innerHTML = ''
  621. createTable(this.dataBackup)
  622. this.cancelCallback()
  623. }
  624. }
  625. let inputCreators = {
  626. String(key, value, oninput){
  627. let input = document.createElement('input')
  628. // console.log(value.match(/\**/)[0])
  629. if (value.match(/\**/)[0] === value && value !== '') {
  630. input.type = 'password'
  631. input.value = ''
  632. input.oninput = () => oninput(input.value)
  633. setTimeout(() => {
  634. oninput(input.value)
  635. }, 0)
  636. } else {
  637. input.type = 'text'
  638. input.placeholder = key
  639. input.value = value
  640. input.oninput = () => oninput(input.value)
  641. setTimeout(() => {
  642. oninput(input.value)
  643. }, 0)
  644. }
  645. return input
  646. },
  647. Boolean(key, value, oninput){
  648. let input = document.createElement('input')
  649. input.type = 'checkbox'
  650. input.checked = value
  651. input.oninput = () => oninput(input.checked)
  652. setTimeout(() => {
  653. oninput(input.checked)
  654. }, 0)
  655. return input
  656. },
  657. Date(key, value, oninput){
  658. let input = document.createElement('input')
  659. input.type = 'datetime-local'
  660. // при преобразовании объекта к строке смещается часовой пояс на нулевой, выражение ниже восстанавливает текущий
  661. input.value = new Date(value - (new Date()).getTimezoneOffset() * 60 * 1000).toISOString().slice(0,-1)
  662. input.oninput = () => oninput(new Date(input.value))
  663. setTimeout(() => {
  664. oninput(new Date(input.value))
  665. }, 0)
  666. return input
  667. },
  668. Number(key, value, oninput){
  669. let input = document.createElement('input')
  670. input.type = 'text'
  671. input.placeholder = key
  672. input.value = value
  673. input.oninput = () => oninput(input.value)
  674. setTimeout(() => {
  675. oninput(input.value)
  676. }, 0)
  677. return input
  678. },
  679. }
  680. this.okCallback = okCallback
  681. this.cancelCallback = cancelCallback
  682. this.data = data
  683. this.dataBackup = Object.assign({}, data)
  684. this.validators = {}
  685. // код ниже дает возможность выполнять что-либо при модификации объекта
  686. // this.validators = new Proxy(this.validators , {
  687. // set(target, prop, val) {
  688. // if (typeof val == 'function') {
  689. // target[prop] = val
  690. // тут можно написать функцию,
  691. // которая будет выполняться при добавлении в
  692. // объект функции в качестве свойства
  693. // return true
  694. // } else {
  695. // return false
  696. // }
  697. // }
  698. // })
  699. let formThis = this
  700. el.appendChild(formBody)
  701. function createTable(inputData) {
  702. for (const [key, value] of Object.entries(inputData)) {
  703. let dataUpdateValidation = (valueOfInput) => {
  704. // console.log(valueOfInput)
  705. if (rowMandatory && !valueOfInput) {
  706. input.style.border = '4px solid red'
  707. callBackValidator[key] = false
  708. if (formThis.validators && key in formThis.validators) {
  709. let valid = formThis.validators[key](valueOfInput,key,data,input)
  710. if (valid === true) {
  711. data[key] = valueOfInput
  712. tdErr.innerText = ''
  713. input.style.backgroundColor = 'white'
  714. } else if (typeof valid === 'string') {
  715. tdErr.innerText = valid
  716. input.style.backgroundColor = '#800000'
  717. }
  718. } else {
  719. data[key] = valueOfInput
  720. }
  721. } else {
  722. input.style.border = '1px solid rgb(118, 118, 118)'
  723. if (formThis.validators && key in formThis.validators) {
  724. let valid = formThis.validators[key](valueOfInput,key,data,input)
  725. if (valid === true) {
  726. callBackValidator[key] = true
  727. data[key] = valueOfInput
  728. tdErr.innerText = ''
  729. input.style.backgroundColor = 'white'
  730. } else if (typeof valid === 'string') {
  731. callBackValidator[key] = false
  732. tdErr.innerText = valid
  733. input.style.backgroundColor = '#800000'
  734. }
  735. } else {
  736. callBackValidator[key] = true
  737. data[key] = valueOfInput
  738. }
  739. }
  740. okButton.disabled = checkObj(callBackValidator)
  741. }
  742. const tr = document.createElement('tr')
  743. table.append(tr)
  744. let rowMandatory = false
  745. const th = document.createElement('th')
  746. if (key[0] === '*') {
  747. const thPre = document.createElement('span')
  748. thPre.innerText = '* '
  749. thPre.style.color = 'red'
  750. th.append(thPre)
  751. const thCont = document.createElement('span')
  752. thCont.innerText = key.slice(1)
  753. th.append(thCont)
  754. rowMandatory = true
  755. } else {
  756. th.innerText = key
  757. }
  758. tr.append(th)
  759. const td = document.createElement('td')
  760. tr.append(td)
  761. const input = inputCreators[value.constructor.name](key, value, dataUpdateValidation)
  762. td.append(input)
  763. const tdErr = document.createElement('td')
  764. tr.append(tdErr)
  765. }
  766. }
  767. createTable(this.data)
  768. }
  769. let form = new Form(formContainer, {
  770. '*name': '',
  771. '*surname': 'AAA',
  772. fathername: 'batkovch',
  773. married: true,
  774. password: '***',
  775. birthday: new Date((new Date).getTime() - 86400000 * 30*365)
  776. }, () => console.log('ok'),() => console.log('cancel') )
  777. form.okCallback = (data) => console.log(data)
  778. form.cancelCallback = () => console.log('aaaa')
  779. form.validators['*surname'] = (value, key, data, input) => value.length > 2 &&
  780. value[0].toUpperCase() == value[0] &&
  781. !value.includes(' ') ? true : 'Wrong surname'
  782. form.validators.fathername = (value, key, data, input) => value.length > 2 &&
  783. value[0].toUpperCase() == value[0] &&
  784. !value.includes(' ') ? true : 'Wrong fathername'
  785. console.log(form)
  786. // fetch('https://open.er-api.com/v6/latest/USD')
  787. // .then(res => res.json())
  788. // .then(data => {
  789. // currencyRates = data.rates
  790. // let rates = new Form(formContainer, currencyRates, () => console.log('ok'),() => console.log('cancel') )
  791. // })
  792. }