123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- function Form(el, data, okCallback, cancelCallback){
- let formBody = document.createElement('div')
- let okButton = document.createElement('button')
- okButton.innerHTML = 'OK'
- let cancelButton = document.createElement('button')
- cancelButton.innerHTML = 'Cancel'
-
- formBody.innerHTML = '<h1>тут будет форма после перервы</h1>'
-
- let inputCreators = {
- String(key, value, oninput){
- let label = document.createElement('label');
- let input = document.createElement('input');
- if(value == '***'){
- input.type = 'password'
- } else {
- input.type = 'text'
- }
- let keyslicer;
- if(key[0] == '*'){
- keyslicer =key.slice(1);
- console.log(keyslicer);
- input.type = 'text';
- /* input.style.cssText = `:after{content:'*',color:red}`; */
- label.classList.add('required');
- label.innerText = `${keyslicer}`
-
- input.onblur = function(){
- if(input.value.includes('')){
- input.style.borderColor = 'red';
- }
- }
- input.onfocus = function(){
- input.style.borderColor = '';
- }
-
- }else{
- label.innerText = `${key}`
- }
- input.placeholder = keyslicer
- input.value = value
- input.oninput = () => oninput(input.value)
- label.appendChild(input);
- return label
- },
- Boolean(key, value, oninput){
- //label for с input type='checkbox' внутри
- /* */
- let label = document.createElement('label');
- let input = document.createElement('input');
- input.type = 'checkbox'
- input.checked= value
- input.oninput = () => oninput(input.checked);
- label.innerText = `${key}`
- label.appendChild(input);
- return label
- },
- Date(key, value, oninput){
- //используйте datetime-local
- let input = document.createElement('input');
- input.type = "datetime-local"
- let timeStamp = value.getTime();
- input.value = ((new Date(timeStamp - value.getTimezoneOffset()*60*1000)).toISOString()).slice(0,-1);
-
- input.oninput= () => oninput(new Date(input.value));
- return input
- }
-
- //и др. по вкусу, например textarea для длинных строк
- }
-
-
- for (let [key,value] of Object.entries(data)){
- console.log(key+":"+data[key]);
- let inputCreations = inputCreators[value.constructor.name];
- let createdInput = inputCreations(key,value,(value)=>{
- if(typeof this.validators[key] === "function"){
- let result = this.validators[key](value,key,data,createdInput)
- if(result === true){
- createdInput.style.backgroundColor = "";
- data[key] = value;
- } else {
- createdInput.style.backgroundColor = "red";
- }
-
- }
- })
-
- formBody.appendChild(createdInput);
- inputCreations.oninput = () => {data[input.placeholder] = input.value;
- }
-
- }
-
- if (typeof okCallback === 'function'){
- formBody.appendChild(okButton);
- okButton.onclick = (e) => {
- console.log(this)
- this.okCallback(e)
- }
- }
- if (typeof cancelCallback === 'function'){
- formBody.appendChild(cancelButton);
- cancelButton.onclick = cancelCallback
- }
- el.appendChild(formBody)
-
- this.okCallback = okCallback
- this.cancelCallback = cancelCallback
- this.data = data
- this.validators = {}
- }
- let form = new Form(formContainer, {
- name: 'Anakin',
- surname: 'Skywalker',
- married: true,
- password: '***',
- '*something': 'SOME thing',
- birthday: new Date((new Date).getTime() - 86400000 * 30*365)
- }, () => console.log('ok'),() => console.log('cancel') )
- form.okCallback = () => console.log('ok2')
- form.validators.surname = (value, key, data, input) => value.length > 2 &&
- value[0].toUpperCase() == value[0] &&
- !value.includes(' ') ? true : 'Wrong name'
- form.validators.password = (value,key,data,input) => value.length >= 3 && value.length <=10 ? true : 'password not full'
-
- console.log(form);
|