-
Notifications
You must be signed in to change notification settings - Fork 25
/
dom.js
61 lines (49 loc) · 2.35 KB
/
dom.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
var PropertyChanges = require("collections/listen/property-changes");
// for whatever reason, HTMLInputElement is not the same as the global of the
// same name, at least in Chrome
function changeChecked(event) {
PropertyChanges.dispatchOwnPropertyChange(event.target, "checked", event.target.checked);
}
function changeValue(event) {
PropertyChanges.dispatchOwnPropertyChange(event.target, "value", event.target.value);
}
function changeInnerHTML(event) {
PropertyChanges.dispatchOwnPropertyChange(event.target, "innerHTML", event.target.innerHTML);
PropertyChanges.dispatchOwnPropertyChange(event.target, "innerText", event.target.innerText);
PropertyChanges.dispatchOwnPropertyChange(event.target, "value", event.target.innerText);
}
function makeObservable(key) {
if (key === "checked") {
this.addEventListener("change", changeChecked);
} else if (key === "value") {
this.addEventListener("change", changeValue);
if (this.type === "text" || this.nodeName === "TEXTAREA") {
this.addEventListener("keyup", changeValue);
} else if (this.contentEditable) {
this.innerText = this.innerText ? this.innerText : this.value;
this.addEventListener("keyup", changeInnerHTML);
}
}
}
function makeUnobservable(key) {
if (key === "checked") {
this.removeEventListener("change", changeChecked);
} else if (key === "value") {
this.removeEventListener("change", changeValue);
if (this.type === "text" || this.nodeName === "TEXTAREA") {
this.removeEventListener("keyup", changeValue);
} else if (this.contentEditable) {
this.removeEventListener("keyup", changeInnerHTML);
}
}
}
var HTMLInputElement = Object.getPrototypeOf(document.createElement("input"));
HTMLInputElement.makePropertyObservable = makeObservable;
HTMLInputElement.makePropertyUnobservable = makeUnobservable;
var HTMLTextAreaElement = Object.getPrototypeOf(document.createElement("textarea"));
HTMLTextAreaElement.makePropertyObservable = makeObservable;
HTMLTextAreaElement.makePropertyUnobservable = makeUnobservable;
var HTMLSpanElement = Object.getPrototypeOf(document.createElement("span"));
HTMLSpanElement.makePropertyObservable = makeObservable;
HTMLSpanElement.makePropertyUnobservable = makeUnobservable;
// TODO make window.history state observable