123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- /* Password */
- function Password(parent, open) {
- let value = ''
- let pass = document.createElement('input')
- let check = document.createElement('input')
- check.type = 'checkbox'
- parent.append(pass, check)
- check.onchange = () => {
- this.setOpen(check.checked)
- }
- //добавить pass oninput, который из pass забирает value, запускает setValue
- this.getValue = function () {
- return value
- }
- this.setValue = function(value) {
- // передает value в onchange если он есть
- }
- this.getOpen = function () {
- return open
- }
- this.setOpen = function(newOpen) {
- open = newOpen
- pass.type = open ? 'text' : 'password'
- check.checked = open
- if(typeof this.onOpenChange === 'function') {
- this.onOpenChange(open)
- }
- }
- this.setOpen(open)
- }
- 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 LoginForm(parent, open) {
- let value = ''
- let login = document.createElement('input')
- let submit = document.createElement('input')
- submit.type = 'submit'
- submit.disabled = true;
- login.placeholder = 'Введите логин'
-
- let pass = document.createElement('input')
- pass.placeholder = 'Введите пароль'
- let check = document.createElement('input')
- check.type = 'checkbox'
-
- parent.append(login, pass, check, submit)
- let passAndLogNotEmpty = function () {
- return login && login.value && pass && pass.value ? true : false;
- };
- let setSubmit = function () {
- if (passAndLogNotEmpty()) {
- submit.disabled = false;
- } else {
- submit.disabled = true;
- }
- }
- setSubmit();
- pass.onkeyup = () => {
- setSubmit();
- }
- login.onkeyup = () => {
- setSubmit();
- }
- check.onchange = () => {
- this.setOpen(check.checked)
- }
- //добавить pass oninput, который из pass забирает value, запускает setValue
- this.getValue = function () {
- return value
- }
- this.setValue = function (value) {
- // передает value в onchange если он есть
- }
- this.getOpen = function () {
- return open
- }
- this.setOpen = function (newOpen) {
- open = newOpen
- pass.type = open ? 'text' : 'password'
- check.checked = open
- if (typeof this.onOpenChange === 'function') {
- this.onOpenChange(open)
- }
- }
- this.setOpen(open)
- }
- let p = new LoginForm(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 Constructor */
- function LoginFormConstructor(parent, open) {
- let login = document.createElement('input')
- let submit = document.createElement('input')
- submit.type = 'submit'
- submit.disabled = true;
- login.placeholder = 'Введите логин'
-
- let pass = document.createElement('input')
- pass.placeholder = 'Введите пароль'
- let checkbox = document.createElement('input')
- checkbox.type = 'checkbox'
- parent.append(login, pass, checkbox, submit)
- let form = {
- login: login.value,
- password: pass.value,
- checkbox: checkbox.checked,
- submitDisabled: submit.disabled
- }
- let passAndLoginNotEmpty = function () {
- return login && login.value && pass && pass.value ? true : false;
- };
- let setSubmit = function () {
- if (passAndLoginNotEmpty()) {
- submit.disabled = false;
- } else {
- submit.disabled = true;
- }
- form.submitDisabled = submit.disabled;
- if (typeof this.onChange === 'function') {
- this.onChange(form);
- }
- }
- setSubmit();
- pass.onkeyup = () => {
- setSubmit();
- form.password = pass.value;
- if (typeof this.onChange === 'function') {
- this.onChange(pass.value);
- }
- if (typeof this.onChange === 'function') {
- this.onChange(form);
- }
- }
- login.onkeyup = () => {
- setSubmit();
- form.login = login.value;
- if (typeof this.onChange === 'function') {
- this.onChange(login.value);
- }
- if (typeof this.onChange === 'function') {
- this.onChange(form);
- }
- }
- checkbox.onchange = () => {
- form.checkbox = checkbox.checked;
- if (checkbox.checked) {
- pass.type = 'text';
- } else {
- pass.type = 'password';
- };
- if (typeof this.onChange === 'function') {
- this.onChange(checkbox.checked);
- }
- if (typeof this.onChange === 'function') {
- this.onChange(form);
- }
- }
-
- this.getValue = function () {
- return form;
- }
- this.setValue = function (newLoginValue, newPasswordValue, newCheckboxValue) {
-
- if (typeof newLoginValue === 'string') {
- login.value = newLoginValue;
- login.onkeyup();
- };
- if (typeof newPasswordValue === 'string') {
- pass.value = newPasswordValue;
- pass.onkeyup();
- };
- if (typeof newCheckboxValue === 'boolean') {
- checkbox.disabled = newCheckboxValue;
- checkbox.onchange();
- };
- }
- }
- let loginForm = new LoginFormConstructor(document.body, true)
- loginForm.setValue('AntonLevshin', 'qwerty', false);
|