123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- //Password
- // function Password(parent, open){
- // let input = document.createElement('input');
- // input.placeholder = 'your password';
- // input.type = 'password';
- // let btn= document.createElement('button');
- // btn.type = 'checkbox';
- // parent.append(input);
- // parent.append(btn);
- // let value = '';
- // let innerTextForBtn = open;
- // btn.innerText = innerTextForBtn ? 'show password' : 'hide password';
- // this.setValue = newValue => {
- // if (newValue != ' ' && newValue && newValue !== value){
- // value = newValue;
- // }
- // };
- // this.setOpen = newOpen => {
- // if (newOpen != innerTextForBtn && newOpen != undefined){
- // innerTextForBtn = newOpen;
- // }
- // };
- // input.oninput = this.onChange = (input) => {
- // value = input.data;
- // };
- // btn.onclick = this.onOpenChange = (newOpen) => {
- // innerTextForBtn = !innerTextForBtn;
- // if(input.getAttribute('type') === 'password'){
- // input.type = 'text';
- // btn.innerHTML = 'show password';
- // } else if (input.getAttribute('type') === 'text') {
- // input.type = 'password';
- // btn.innerHTML = 'hide password';
- // }
- // };
- // this.getValue = () => value;
- // this.getOpen = () => innerTextForBtn;
- // }
- // let p = new Password(document.body, true);
- // p.onChange = data => console.log(data);
- // p.onOpenChange = open => console.log(open);
- // p.setValue('qwerty');
- // console.log(p.getValue());
- // p.setOpen(false);
- // console.log(p.getOpen());
- //LoginForm
- // function Login(parent){
- // let form = document.createElement('form');
- // let inputLogin = document.createElement('input');
- // inputLogin.placeholder = 'login';
- // inputLogin.type = 'text';
- // let inputPaswd = document.createElement('input');
- // inputPaswd.placeholder = 'password';
- // inputPaswd.type = 'password';
- // let btn= document.createElement('button');
- // btn.innerHTML = 'First, write login and password';
- // parent.append(form);
- // form.append(inputLogin);
- // form.append(inputPaswd);
- // form.append(btn);
- // let valueLogin = '';
- // let valuePaswd = '';
- // inputLogin.oninput = () =>{
- // valueLogin = inputLogin.value;
- // };
- // inputPaswd.oninput = () =>{
- // valuePaswd = inputPaswd.value;
- // };
- // btn.onclick = (event) =>{
- // event.preventDefault();
- // if(valueLogin === '' && valuePaswd === ''){
- // btn.innerHTML = 'Write login and password';
- // btn.style.backgroundColor = 'red';
- // } else {
- // btn.disabled = true;
- // btn.innerHTML = 'Sent';
- // btn.style.backgroundColor = 'green';
- // }
- // };
- // }
- // let p = new Login(document.body, true);
- //Password Verify
- // function Password (parent){
- // let pswd = document.createElement('input');
- // let pswdVerify = document.createElement('input');
- // let btn = document.createElement('button');
- // pswd.type = 'password';
- // pswdVerify.type = 'password';
- // btn.innerHTML = 'Check';
- // parent.append(pswd);
- // parent.append(pswdVerify);
- // parent.append(btn);
- // let valuePswd = '';
- // let valuePswdVerify = '';
- // pswdVerify.style.display = 'none';
- // pswd.oninput = () =>{
- // valuePswd = pswd.value;
- // };
-
- // pswdVerify.oninput = () =>{
- // valuePswdVerify = pswdVerify.value;
- // };
- // let p = document.createElement('p');
- // parent.append(p);
- // btn.onclick = () => {
- // if(valuePswd === ''){
- // p.innerHTML = 'Write password';
- // p.style.color = 'red';
- // } else if (pswdVerify.style.display === 'block') {
- // if(valuePswd === valuePswdVerify){
- // p.innerHTML = 'Right password';
- // p.style.color = 'green';
- // } else{
- // p.innerHTML = 'You made mistake';
- // p.style.color = 'red';
- // }
- // } else {
- // pswdVerify.style.display = 'block';
- // p.innerHTML = '';
- // }
- // };
- // }
- // let p = new Password(document.body);
- //Form
- const dateToDataTimeLocal = date => (new Date(date.getTime() - date.getTimezoneOffset() * 60000)).toISOString().slice(0, -1);
- dateToDataTimeLocal(new Date());
- 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';
- let inputCreators = {
- String(key, value, oninput){
- let div = document.createElement('div');
- let label = document.createElement('label');
- let input = document.createElement('input');
- label.innerHTML = key;
- input.required = true;
- input.placeholder = key;
- input.value = value;
- if(key === 'password'){
- input.type = 'password';
- }
- let p = document.createElement('p');
- p.innerHTML = '';
- input.oninput = () => {
- (function() {
- if (input.placeholder in this.validators) {
- let x = this.validators[input.placeholder](input.value, input.placeholder, this.data, input);
- if(x !== true){
- p.style.color = 'red';
- p.innerHTML = 'Error!';
- input.style.backgroundColor = 'red';
- label.appendChild(p);
- } else {
- p.innerHTML = '';
- input.style.backgroundColor = '';
- console.log(x);
- }
-
- } else {
- oninput(input.value);
- }
- }).call(this);
- }
- formBody.appendChild(div);
- div.appendChild(label,input);
- let abbr = document.createElement('abbr');
- abbr.title = 'Обязательно';
- abbr.className = 'importantInput';
- abbr.innerHTML = '*';
- label.appendChild(abbr);
-
- return input;
- },
-
- Boolean(key, value, oninput){
- let div = document.createElement('div');
- let label = document.createElement('label');
- let input = document.createElement('input');
- label.innerHTML = key;
- input.required = true;
- input.type = 'checkbox';
- input.checked = value;
- input.appendChild(label);
- input.oninput = () => oninput(input.checked);
- formBody.appendChild(div);
- div.appendChild(label,input);
- let abbr = document.createElement('abbr');
- abbr.title = 'Обязательно';
- abbr.className = 'importantInput';
- abbr.innerHTML = '*';
- label.appendChild(abbr);
- return input;
- },
-
- Date(key, value, oninput){
- let div = document.createElement('div');
- let label = document.createElement('label');
- let input = document.createElement('input');
- label.innerHTML = key;
- input.required = true;
- input.type = 'datetime-local';
- input.value = dateToDataTimeLocal(value);
- input.onchange = () =>{
- oninput(new Date(input.value));
- };
- formBody.appendChild(div);
- div.appendChild(label,input);
- let abbr = document.createElement('abbr');
- abbr.title = 'Обязательно';
- abbr.className = 'importantInput';
- abbr.innerHTML = '*';
- label.appendChild(abbr);
- return input;
- }
- }
- for(const [key, value] of Object.entries(data)){
- let input = inputCreators[value.constructor.name].call(this, key, value, newValue => {
- data[key] = newValue;
- });
- formBody.appendChild(input);
- }
-
- if (typeof okCallback === 'function'){
- formBody.appendChild(okButton);
- okButton.onclick = (e) => {
- this.okCallback(e)
- // console.log(this)
- }
- }
- if (typeof cancelCallback === 'function'){
- formBody.appendChild(cancelButton);
- cancelButton.onclick = () => window.location.reload();
- }
- el.appendChild(formBody);
- this.okCallback = okCallback;
- this.cancelCallback = cancelCallback;
- this.data = data;
- this.validators = {};
- };
- let form = new Form(formContainer, {
- name: 'Anakin',
- surname: 'Skywalker',
- password: 'qwerty',
- married: true,
- birthday: new Date((new Date).getTime() - 86400000 * 30*365)
- }, () => console.log('ok'),() => console.log('cancel') );
- form.okCallback = () => console.log('ok')
- form.validators.name = (value, key, data, input) => value.length > 2 &&
- value[0].toUpperCase() == value[0] &&
- !value.includes(' ') ? true : 'Wrong name';
- 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 > 5 &&
- !value.includes(' ') ? true : 'Wrong name';
- console.log(form);
|