-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.ts
47 lines (37 loc) · 1.36 KB
/
controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Encapsulates a reusable UI element
Allows for element to be reused
*/
module Controller {
export class ProductOptions {
text: string;
constructor(text: string) {
this.text = text;
}
}
export class ProductController {
element: HTMLElement;
options: ProductOptions;
inputQuantity: HTMLInputElement;
constructor(element: HTMLElement, options: ProductOptions) {
this.element = element;
this.options = options;
this.render();
this.addEventHandlers();
}
private render() {
(<HTMLLabelElement>this.element.querySelector("label")).innerHTML = this.options.text;
}
private addEventHandlers() {
this.element.querySelector("input.addProductButton").addEventListener("click", () => {
this.addToBasket((<HTMLInputElement>this.element.querySelector("input.productName")).value);
});
}
addToBasket(val) {
if (val) {
(<HTMLUListElement>this.element.querySelector(".productList")).innerHTML += "<li>" + val + "</li>";
}
}
}
}
var product = new Controller.ProductController(document.getElementById("controllerElement"), new Controller.ProductOptions("Product: "));