-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathinput-change-events.js
More file actions
117 lines (105 loc) · 3.96 KB
/
input-change-events.js
File metadata and controls
117 lines (105 loc) · 3.96 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// helper functions to make all input elements
import $ from "jquery";
import dom from "../core/dom";
import logging from "../core/logging";
const namespace = "input-change-events";
const log = logging.getLogger(namespace);
const _ = {
setup($el, pat) {
if (!pat) {
log.error("The name of the calling pattern has to be set.");
return;
}
// list of patterns that installed input-change-event handlers
const patterns = $el.data(namespace) || [];
log.debug("setup handlers for " + pat);
const el = $el[0];
if (!patterns.length) {
log.debug("installing handlers");
this.setupInputHandlers(el);
$el.on("patterns-injected." + namespace, (event) => {
this.setupInputHandlers(event.target);
});
}
if (patterns.indexOf(pat) === -1) {
patterns.push(pat);
$el.data(namespace, patterns);
}
},
setupInputHandlers(el) {
if (dom.is_input(el)) {
// The element itself is an input, se we simply register a
// handler fot it.
console.log("1");
this.registerHandlersForElement({ trigger_source: el, trigger_target: el });
} else {
// We've been given an element that is not a form input. We
// therefore assume that it's a container of form inputs and
// register handlers for its children.
console.log("2");
const form = el.closest("form");
for (const _el of form.elements) {
console.log("3", _el);
// Search for all form elements, also those outside the form
// container.
if (!dom.is_input(_el)) {
// form.elements also catches fieldsets, object, output,
// which we do not want to handle here.
continue;
}
this.registerHandlersForElement({
trigger_source: _el,
trigger_target: form,
});
}
}
},
registerHandlersForElement({ trigger_source, trigger_target }) {
const $trigger_source = $(trigger_source);
const $trigger_target = $(trigger_target);
const isNumber = trigger_source.matches("input[type=number]");
const isText = trigger_source.matches(
"input:not(type), input[type=text], input[type=search], textarea"
);
if (isNumber) {
// for number inputs we want to trigger the change on keyup
$trigger_source.on("keyup." + namespace, function () {
log.debug("translating keyup");
$trigger_target.trigger("input-change");
});
}
if (isText || isNumber) {
$trigger_source.on("input." + namespace, function () {
log.debug("translating input");
$trigger_target.trigger("input-change");
});
} else {
$trigger_source.on("change." + namespace, function () {
log.debug("translating change");
$trigger_target.trigger("input-change");
});
}
$trigger_source.on("blur", function () {
$trigger_target.trigger("input-defocus");
});
},
remove($el, pat) {
let patterns = $el.data(namespace) || [];
if (patterns.indexOf(pat) === -1) {
log.warn("input-change-events were never installed for " + pat);
} else {
patterns = patterns.filter(function (e) {
return e !== pat;
});
if (patterns.length) {
$el.data(namespace, patterns);
} else {
log.debug("remove handlers");
$el.removeData(namespace);
$el.find(":input").off("." + namespace);
$el.off("." + namespace);
}
}
},
};
export default _;