-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunny-ui.js
77 lines (54 loc) · 1.87 KB
/
bunny-ui.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Save SomeBunny Bunny Page BunnyUI Class
* Author: [email protected]
*/
class BunnyUI {
static _SEX_SELECTOR_ID() { return 'bunny-sex-select'; }
static _SIZE_SELECTOR_ID() { return 'bunny-size-select'; }
constructor(
containerElement, // DOMElement
status // String: adoptable or pending
) {
if (!containerElement) { throw Error('Falsey container'); }
if (!status) { throw Error('No status provided'); }
const Self = BunnyUI;
const self = this;
this._containerElement = containerElement;
this._status = status;
this._bunnies = null; // Optional[Bunnies]
this._sexSelector = new BunnyDropdown(
document.getElementById(Self._SEX_SELECTOR_ID()),
this._updateBunnyDisplay.bind(self)
);
this._sizeSelector = new BunnyDropdown(
document.getElementById(Self._SIZE_SELECTOR_ID()),
this._updateBunnyDisplay.bind(self)
);
Bunnies.retrieve(this._receiveBunnies.bind(self));
return;
}
_receiveBunnies(error, bunnies) {
if (error) { throw Error('handle me'); return; }
this._bunnies = bunnies;
this._updateBunnyDisplay();
return;
}
_updateBunnyDisplay() {
const bunniesElement = this._bunnies.createBunniesDomElement(
this._sexSelector.value,
this._sizeSelector.value,
this._status
);
while (this._containerElement.firstChild) {
this._containerElement.removeChild(
this._containerElement.lastChild
);
continue;
}
this._containerElement.appendChild(bunniesElement);
return;
}
static fromElementId(elementId, status) {
return new BunnyUI(document.getElementById(elementId), status);
}
}