123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- // password
- function Password(parent, open=false) {
- let _isVisible = open
- let _value = ''
- let input = document.createElement('input')
- input.placeholder = 'password'
- let wrapper = document.createElement('div')
- let btn = document.createElement('button')
- btn.innerText = 'Hide/Show'
- this.getHTMLElements = function() {
- return {
- Password_wrapper: wrapper,
- Password_input: input,
- Password_btn: btn
- }
- }
- this.getValue = function() {
- return _value
- }
- this.setValue = function(newValue) {
- _value = newValue
- input.value = _value
- }
- this.setOpen = (bool) => {
- _isVisible = bool
- let _type = _isVisible? 'text' : 'password'
- input.setAttribute('type', `${_type}`)
- this.onOpenChange()
- }
- this.getOpen = function() {
- return _isVisible
- }
- this.onChange = function(event) {
- _value = event.target.value
- }
- let closed = () => {}
- this.onOpenChange = (func=null, ...args) => {
- let lockfunc = func
- if(func) {
- (function(){
- closed = lockfunc
- closed(...args)
- })()
- } else {
- closed(...args)
- }
- }
- input.addEventListener('input', this.onChange)
- btn.addEventListener('click', () => {
- this.setOpen(!_isVisible)
- })
- wrapper.setAttribute('class', 'Password')
- this.setOpen(open)
- wrapper.append(input, btn)
- parent.append(wrapper)
- }
- let pass = new Password(document.body, true)
- // check if Password constructor works
- // pass.setValue('hello world')
- // pass.setOpen(false)
- // console.log(pass.getOpen())
- // console.log(pass.getValue())
- // window.setTimeout(()=> {
- // console.log('old value:', pass.getValue())
- // pass.setValue('YOU CAN SEE ME!')
- // pass.setOpen(true)
- // }, 5000)
- // login form
- function Login (parent) {
- let _value = ''
- let input = document.createElement('input')
- input.type = 'text'
- input.placeholder = 'login'
- input.style.display = 'block'
- parent.append(input)
- this.getHTMLElement = function() {
- return input
- }
- this.getValue = function() {
- return _value
- }
- this.setValue = function(newValue) {
- _value = newValue
- input.value = _value
- }
- this.onChange = function(event) {
- _value = event.target.value
- }
- input.addEventListener('input', this.onChange)
- }
- // loginForm Constructor
- function loginForm(parent, heading='loginForm Constructor') {
- let form = document.createElement('div')
- form.id = 'loginForm'
- form.style.border = '5px double black'
- form.style.width = 'fit-content'
- form.style.textAlign = 'center'
- form.style.padding = '15px'
- form.style.margin = '15px'
- let h2 = document.createElement('h2')
- h2.innerText = heading
- form.append(heading)
- let _login = new Login(form)
- let _password = new Password(form)
- let login_btn = document.createElement('button')
- login_btn.innerText = 'Log in'
- login_btn.disabled = true
- form.append(login_btn)
- for(let item of form.children) {
- item.style.margin = '10px'
- }
- this.getLogin = function() {
- return _login.getValue()
- }
- this.getPassword = function() {
- return _password.getValue()
- }
- this.isPasswordOpened = function() {
- return _password.getOpen()
- }
- this.setPasswordOpened = function(bool) {
- _password.setOpen(bool)
- }
- this.setLogin = function(newLogin) {
- _login.setValue(newLogin)
- checkFields()
- }
- this.setPass = function(newPass) {
- _password.setValue(newPass)
- checkFields()
- }
- function checkFields() {
- if (_login.getValue() && _password.getValue()) {
- login_btn.disabled = false
- } else {
- login_btn.disabled = true
- }
- }
- _login.getHTMLElement().addEventListener('input', checkFields)
- _password.getHTMLElements().Password_input.addEventListener('input', checkFields)
- parent.append(form)
- }
- let login_form = new loginForm(document.body)
- // login_form.setLogin('default_user')
- // login_form.setPass('pass example', true)
- // window.setTimeout(()=> {
- // console.log(login_form.getPassword())
- // console.log('before open', login_form.isPasswordOpened())
- // login_form.setPasswordOpened(true)
- // console.log('after open', login_form.isPasswordOpened())
- // console.log(login_form.getLogin())
- // }, 5000)
- // Password Verify
- class PasswordVerify extends Password {
- constructor(parent, open=false) {
- super(parent, open);
- let _repeatWrapper = document.createElement('div')
- let _repeatField = document.createElement('input')
- let _successBtn = document.createElement('button')
- let displayRepeat = () => {
- if(this.getOpen() === false) {
- _repeatField.style.marginTop = '10px'
- _repeatField.type = 'password'
- _repeatField.placeholder = 'repeat password'
- _repeatField.value = ''
- _successBtn.innerText = 'no match!'
- _successBtn.disabled = true
- _successBtn.style.display = 'inline-block'
- _successBtn.style.backgroundColor = 'transparent'
- _repeatWrapper.append(_repeatField)
- _repeatWrapper.append(_successBtn)
-
- this.getHTMLElements().Password_wrapper.append(_repeatWrapper)
- } else {
- try {
- for(let child of _repeatWrapper.children) {
- _repeatWrapper.remove(child)
- }
- } catch(e){}
- }
- }
- this.onOpenChange(displayRepeat)
- let isMatch= () => {
- if(this.getHTMLElements().Password_input.value && _repeatField.value) {
- if(this.getHTMLElements().Password_input.value === _repeatField.value) {
- return true
- }
- }
- return false
- }
- let btnActivate = (bool) => {
- if(bool) {
- _successBtn.disabled = false
- _successBtn.style.backgroundColor = 'chartreuse'
- _successBtn.innerText = 'passwords match'
- } else {
- _successBtn.disabled = true
- _successBtn.style.backgroundColor = 'transparent'
- _successBtn.innerText = 'no match!'
- }
- }
- this.getHTMLElements().Password_input.addEventListener('input', () => {
- btnActivate(isMatch())
- })
- _repeatField.addEventListener('input',() => {
- btnActivate(isMatch())
- })
- }
- }
- document.body.insertAdjacentHTML('beforeend', `
- <div id=verif></div>
- `)
- let verif = document.getElementById('verif')
- let passverif = new PasswordVerify(verif, true)
- // window.setTimeout(()=> {
- // console.log('bang')
- // passverif.setOpen(false)
- // }, 5000)
- // login form
- 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 = '<h2>Form</h2>'
- 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)
- let inputCreators = {
- string: (key, value, func ) => {
- let input = document.createElement('input')
- input.type = 'text'
- input.placeholder = key
- input.value = value
- input.oninput = () => {
- func(input.value)
- try {
- if(this.validators[key]) {
- this.validators[key](value, key, this.data, input)
- console.log('RESSSS', this.validators[key](value, key, this.data, input))
- }
- } catch(e){console.warn(`no validator for ${key} property`)}
- }
- return input
- },
- boolean: (key, value, func) => {
- let input = document.createElement('input')
- input.type = 'checkbox'
- input.placeholder = key
- input.checked = value
- input.onchange = () => {
- func(input.value)
- try {
- if(this.validators[key]) {
- this.validators[key](value, key, this.data, e.target)
- }
- } catch(e){console.warn(`no validator for ${key} property`)}
- }
- return input
- },
- date: (key, value, func) => {
- let input = document.createElement('input')
- input.type = 'datetime-local'
- input.placeholder = key
- input.value = new Date(value).toISOString().slice(0, -1)
- console.log('inp val', input.value)
- input.onchange = () => {
- func(new Date(input.value))
- try {
- if(this.validators[key]) {
- this.validators[key](value, key, this.data, e.target)
- }
- } catch(e){console.warn(`no validator for ${key} property`)}
- }
- return input
- },
- }
- let formTable = document.createElement('table')
- formTable.border = 1
- for(let key in data) {
- let tr = document.createElement('tr')
- let tdKey = document.createElement('td')
- let tdInput = document.createElement('td')
- let input = inputCreators[(data[key].constructor.name).toLowerCase()](key, data[key],(value) => {
- data[key] = value
- })
- tdKey.innerText = key
- tdInput.append(input)
- tr.append(tdKey, tdInput)
- formTable.append(tr)
- }
- okButton.before(formTable)
- this.okCallback = okCallback
- this.cancelCallback = cancelCallback
- this.data = data
- this.validators = {}
- }
- let formContainer = document.createElement('div')
- formContainer.id = 'formContainer'
- document.body.append(formContainer)
- let form = new Form(formContainer, {
- name: 'Anakin',
- surname: 'Skywalker',
- married: true,
- 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) => {
- console.log('includes whitespace?', value.includes(' '))
- console.log('lenz',value.length)
- return value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(' ') ? true : 'Wrong name'
- }
- console.log(form)
|