forked from platzi/javascript-profesional
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<html> | ||
<head> | ||
<title>Decorator</title> | ||
</head> | ||
|
||
<body> | ||
<a href="/ejercicios/">Go back</a> | ||
|
||
<div style="margin-top: 3rem;"> | ||
<label for="email">Email</label> | ||
<input id="email" /> | ||
</div> | ||
|
||
<script src="/ejercicios/decorator/index.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Field { | ||
errors: string[]; | ||
input: HTMLInputElement; | ||
|
||
constructor(input: HTMLInputElement) { | ||
this.input = input; | ||
this.errors = []; | ||
|
||
let errorMessage = document.createElement('p'); | ||
errorMessage.className = 'text-danger'; | ||
this.input.parentNode.insertBefore(errorMessage, this.input.nextSibling); | ||
|
||
this.input.addEventListener('input', () => { | ||
this.errors = []; | ||
this.validate(); | ||
errorMessage.innerText = this.errors[0] || ''; | ||
}); | ||
} | ||
|
||
validate() {} | ||
} | ||
|
||
function RequiredFieldDecorator(field: Field) { | ||
return field; | ||
} | ||
|
||
let field = new Field(document.querySelector('#email')); | ||
RequiredFieldDecorator(field); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
interface Observer { | ||
update: (data: any) => void; | ||
} | ||
|
||
interface Subject { | ||
subscribe: (observer: Observer) => void; | ||
unsubscribe: (observer: Observer) => void; | ||
} | ||
|
||
class BitcoinPrice implements Subject { | ||
observers: Observer[] = []; | ||
|
||
constructor() { | ||
const el: HTMLInputElement = document.querySelector('#value'); | ||
el.addEventListener('input', () => { | ||
this.notify(el.value); | ||
}); | ||
} | ||
|
||
subscribe(observer: Observer) { | ||
this.observers.push(observer); | ||
} | ||
|
||
unsubscribe(observer: Observer) { | ||
const index = this.observers.findIndex(obs => { | ||
return obs === observer; | ||
}); | ||
|
||
this.observers.splice(index, 1); | ||
} | ||
|
||
notify(data: any) { | ||
this.observers.forEach(observer => observer.update(data)); | ||
} | ||
} | ||
|
||
class PriceDisplay implements Observer { | ||
private el: HTMLElement; | ||
|
||
constructor() { | ||
this.el = document.querySelector('#price'); | ||
} | ||
|
||
update(data: any) { | ||
this.el.innerText = data; | ||
} | ||
} | ||
|
||
const value = new BitcoinPrice(); | ||
const display = new PriceDisplay(); | ||
|
||
value.subscribe(display); | ||
|
||
setTimeout(() => value.unsubscribe(display), 5000); |