Alyona Brytvina il y a 2 ans
Parent
commit
e223af3c4a
3 fichiers modifiés avec 234 ajouts et 0 suppressions
  1. 12 0
      HW09/index.html
  2. 173 0
      HW09/main.js
  3. 49 0
      HW09/style.css

+ 12 - 0
HW09/index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>HW09</title>
+    <link rel="stylesheet" href="style.css">
+</head>
+<body>
+<div id="formContainer"></div>
+<script src="main.js"></script>
+</body>
+</html>

+ 173 - 0
HW09/main.js

@@ -0,0 +1,173 @@
+function Form(el, data, okCallback, cancelCallback) {
+    let table = document.createElement('table');
+    table.className = 'table';
+    for (let key in data) {
+        let tr = document.createElement('tr');
+        tr.className = 'tr';
+        let th = document.createElement('th');
+        th.className = 'th';
+        let td = document.createElement('td');
+        td.className = 'td';
+
+        th.innerHTML = key;
+
+        if (key === 'name' || key === 'password') {
+            let fieldForStar = document.createElement('span');
+            fieldForStar.textContent = '*';
+            fieldForStar.style.color = 'red';
+            th.appendChild(fieldForStar);
+        }
+
+        tr.appendChild(th);
+
+        let type = data[key].constructor.name;
+
+        let inputCreators = {
+            String(key, value, oninput) {
+                let input = document.createElement('input');
+                let label = document.createElement('label');
+                let errorField = document.createElement('div');
+                errorField.className = 'errorField';
+                label.htmlFor = 'input';
+                label.textContent = `Enter ${key}`;
+                if (`${key}` === 'password') {
+                    input.type = 'password';
+                } else {
+                    input.type = 'text';
+                }
+                input.placeholder = key;
+                input.value = value;
+                input.oninput = (event) => oninput(event.target.value, input, errorField);
+                td.appendChild(label);
+                label.appendChild(input);
+                label.appendChild(errorField);
+                return input;
+            },
+            Boolean(key, checked, onclick) {
+                let input = document.createElement('input');
+                let label = document.createElement('label');
+                label.htmlFor = 'input';
+                label.textContent = 'Are you married?';
+                input.type = 'checkbox';
+                input.checked = checked;
+                input.value = type;
+                input.onclick = (event) => onclick(event.target.checked);
+                label.appendChild(input);
+                td.appendChild(label);
+                return input;
+            },
+            Date(key, value, oninput) {
+                let input = document.createElement('input');
+                let label = document.createElement('label');
+                label.htmlFor = 'input';
+                label.textContent = 'Select your birth date';
+                input.type = 'datetime-local';
+                input.value = (new Date()).toISOString().slice(0, -1);
+                input.oninput = (event) => oninput(event.target.value);
+                label.appendChild(input);
+                td.appendChild(label);
+                return input;
+            },
+        };
+
+        const creatorFunc = inputCreators[type];
+
+        if (type === 'String') {
+            creatorFunc(key, data[key], (value, input, errorField) => {
+                const validationResult = this.validators[key](value);
+                this.data[key] = value;
+                // console.log(this.data);
+                if (validationResult !== true) {
+                    errorField.textContent = validationResult;
+                    input.style.borderColor = 'red';
+                } else {
+                    errorField.textContent = '';
+                    input.style.borderColor = 'black';
+                }
+            });
+        } else if (type !== 'String') {
+            creatorFunc(key, data[key], (value) => console.log(value));
+        }
+
+
+        table.appendChild(tr);
+        tr.appendChild(td);
+    }
+
+    formContainer.appendChild(table);
+    let formBody = document.createElement('div');
+    let okButton = document.createElement('button');
+    okButton.innerHTML = 'OK';
+
+    let cancelButton = document.createElement('button');
+    cancelButton.innerHTML = 'Cancel';
+
+    if (typeof okCallback === 'function') {
+        formBody.appendChild(okButton);
+        okButton.onclick = (e) => {
+            let result = false;
+            for (let key in this.validators) {
+                let func = this.validators[key];
+                let value = this.data[key];
+                result = func(value) === true;
+            }
+            console.log(result);
+            if (result) {
+                this.okCallback(e);
+            }
+        };
+    }
+
+
+    if (typeof cancelCallback === 'function') {
+        formBody.appendChild(cancelButton);
+        cancelButton.onclick = (e) => {
+            console.log(data);
+            cancelCallback(e);
+        };
+    }
+
+
+    el.appendChild(formBody);
+
+    this.okCallback = okCallback;
+    this.cancelCallback = cancelCallback;
+    this.data = {...data};
+    this.validators = {};
+}
+
+let formContainer = document.getElementById('formContainer');
+
+let form = new Form(formContainer, {
+    name: 'Anakin',
+    surname: 'Skywalker',
+    married: true,
+    birthday: new Date((new Date).getTime() - 86400000 * 30 * 365),
+    password: '',
+}, () => console.log('ok'), () => console.log('cancel'));
+form.okCallback = () => console.log('ok2');
+
+form.validators.name = (value) => {
+    const result = value.length > 2
+        && value[0].toUpperCase() === value[0]
+        && !value.includes(' ');
+
+    return result || 'Wrong name';
+};
+
+form.validators.surname = (value) => {
+    const result = value.length > 2
+        && value[0].toUpperCase() === value[0]
+        && !value.includes(' ');
+
+    return result || 'Wrong surname';
+};
+
+form.validators.password = (value) => {
+    const result = value.length > 8
+        && !value.includes(' ');
+
+    return result || 'Wrong password';
+};
+
+

+ 49 - 0
HW09/style.css

@@ -0,0 +1,49 @@
+.table{
+    font-family: Georgia, sans-serif;
+    font-size: 16px;
+    display: flex;
+    justify-content: center;
+    flex-direction: column;
+}
+
+label{
+    display: flex;
+    flex-direction: column;
+    text-align: center;
+    align-items: center;
+    margin: 10px;
+}
+
+.th{
+    border: 1px solid black;
+    border-radius: 5px;
+    width: 100px;
+}
+
+.td{
+    border: 1px solid black;
+    border-radius: 5px;
+    min-width: 300px;
+}
+
+.tr{
+    padding: 10px;
+}
+
+input{
+    margin: 10px;
+    height:20px;
+    border: 1px solid black;
+    border-radius: 2px;
+    width: 220px;
+}
+
+input:hover{
+    border: 1px solid gray;
+    background-color: lightgrey;
+}
+
+.errorField{
+    font-size: 12px;
+    color: red;
+}