Gennadysht hace 2 años
padre
commit
4973d7edc9

+ 1 - 1
js/14 FOOP/hw14_05_login_form .html

@@ -1,4 +1,4 @@
-<header>password</header>
+<header>login form</header>
 
 <body>
     <script>

+ 75 - 4
js/14 FOOP/hw14_06_login_form_constructor.html

@@ -1,10 +1,81 @@
-<header>
-
-</header>
+<header>login_form_constructor</header>
 
 <body>
     <script>
+        function Login(parent) {
+            this.input = document.createElement("input");
+            parent.append(this.input);
+            this.setValue = function (value) {
+                this.input.value = value;
+            }
+            this.getValue = function () {
+                return this.input.value;
+            }
+            this.input.onchange = function () {
+                if (this.onChange)
+                    this.onChange(this.getValue());
+            }.bind(this);
+        }
+
+        function Password(parent, open) {
+            this.input = document.createElement("input");
+            parent.append(this.input);
+            this.setValue = function (value) {
+                this.input.value = value;
+            }
+            this.getValue = function () {
+                return this.input.value;
+            }
+            this.input.onchange = function () {
+                if (this.onChange)
+                    this.onChange(this.getValue());
+            }.bind(this);
+
+
+            this.setOpen = function (open) {
+                this.input.type = open ? "text" : "password";
+            }
+            this.getOpen = function () {
+                return this.input.type == "text";
+            }
+            let check = document.createElement("input");
+            parent.append(check);
+            check.onchange = function () {
+                this.setOpen(check.checked);
+                if (this.onOpenChange)
+                    this.onOpenChange(this.getOpen());
+            }.bind(this);
+
+            check.checked = open;
+            check.value = "open";
+            check.type = "checkbox";
+        }
+        function LoginForm(parent, open) {
+
+            let form = document.createElement("form");
+            parent.append(form);
+            this.login = new Login(form);
+            this.password = new Password(form, open);
+            this.button = document.createElement("button");
+            this.button.innerText = "OK";
+            form.append(this.button);
+
+            let setButtonStateCallback = function setButtonState() {
+                this.button.disabled = !this.login.getValue() || !this.password.getValue();
+            }.bind(this);
+            this.login.onChange = setButtonStateCallback;
+            this.password.onChange = setButtonStateCallback;
+            this.button.disabled = true;
+            this.button.onclick = function () {
+                if (this.onLogin)
+                    this.onLogin(this.login.getValue(), this.password.getValue());
+            }.bind(this);
+        }
+
+        let loginForm = new LoginForm(document.body, true);
+        loginForm.onLogin = function (login, password) {
+            console.log(`${login}:${password}`)
+        }
 
-        
     </script>
 </body>

+ 69 - 1
js/14 FOOP/hw14_07_password_verify.html

@@ -2,7 +2,75 @@
 
 <body>
     <script>
+        function Password(parent, open) {
+            this.input = document.createElement("input");
+            parent.append(this.input);
+            this.setValue = function (value) {
+                this.input.value = value;
+            }
+            this.getValue = function () {
+                return this.input.value;
+            }
+            this.setOpen = function (open) {
+                this.open = open;
+                this.check.style.display = this.open === undefined ? "none" : "block";
+                this.input.type = this.open === true ? "text" : "password";
+            }
+            this.getOpen = function () {
+                return this.input.type == "text";
+            }
+            this.input.onchange = function () {
+                if (this.onChange)
+                    this.onChange(this.getValue());
+            }.bind(this);
+
+            this.show = function (isShow) {
+                let displayStyle = isShow ? "block" : "none";
+                this.input.style.display = displayStyle;
+                if (this.open !== undefined)
+                    this.check.style.display = displayStyle;
+            }.bind(this);
+
+            this.setErrorState = function (isError) {
+                let borderColor = isError ? "#FF0000" : "#00FF00";
+                this.input.style.borderColor = borderColor;
+            }.bind(this)
+
+
+            this.check = document.createElement("input");
+            parent.append(this.check);
+            this.check.onchange = function () {
+                this.setOpen(this.check.checked);
+                if (this.onOpenChange)
+                    this.onOpenChange(this.getOpen());
+            }.bind(this);
+
+            this.check.checked = open;
+            this.check.value = "open";
+            this.check.type = "checkbox";
+            this.setOpen(open);
+        }
+        let passw1 = new Password(document.body, true);
+        let passw2 = new Password(document.body);
+        passw2.show(!passw1.getOpen());
+        passw1.onOpenChange = function (isOpen) {
+            passw2.show(!isOpen);
+            if (passw1.getOpen())
+                passw1.setErrorState(false);
+            else
+              passw1.onChange(passw1.getValue());  
+        }
+        const checkPasswordsEqual = (val) => {
+            if (!passw1.getOpen()) {
+                var areEqual = passw1.getValue() == passw2.getValue();
+                passw1.setErrorState(!areEqual);
+                passw2.setErrorState(!areEqual);
+            }
+        }
+        passw1.onChange = checkPasswordsEqual;
+        passw2.onChange = checkPasswordsEqual;
+
+
 
-        
     </script>
 </body>