-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathauto-submit.js
112 lines (101 loc) · 3.49 KB
/
auto-submit.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
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
import "../../core/jquery-ext";
import $ from "jquery";
import Base from "../../core/base";
import input_change_events from "../../lib/input-change-events";
import logging from "../../core/logging";
import Parser from "../../core/parser";
import utils from "../../core/utils";
const log = logging.getLogger("autosubmit");
export const parser = new Parser("autosubmit");
// - 400ms -> 400
// - 400 -> 400
// - defocus
parser.addArgument("delay", "400ms");
export default Base.extend({
name: "autosubmit",
trigger: ".pat-autosubmit, .pat-auto-submit",
parser: {
parse($el, opts) {
const cfg = parser.parse($el, opts);
if (cfg.delay !== "defocus") {
cfg.delay = parseInt(cfg.delay.replace(/[^\d]*/g, ""), 10);
}
return cfg;
},
},
init() {
this.options = this.parser.parse(this.$el, this.options);
input_change_events.setup(this.$el, "autosubmit");
this.registerListeners();
this.registerTriggers();
return this.$el;
},
registerListeners() {
this.$el.on("input-change-delayed.pat-autosubmit", this.onInputChange);
this.registerSubformListeners();
this.$el.on("patterns-injected", this.refreshListeners.bind(this));
},
registerSubformListeners(ev) {
/* If there are subforms, we need to listen on them as well, so
* that only the subform gets submitted if an element inside it
* changes.
*/
const $el = typeof ev !== "undefined" ? $(ev.target) : this.$el;
$el.find(".pat-subform")
.not(".pat-autosubmit")
.each((idx, el) => {
$(el).on(
"input-change-delayed.pat-autosubmit",
this.onInputChange
);
});
},
refreshListeners(ev, cfg, el, injected) {
this.registerSubformListeners();
// Register change event handlers for new inputs injected into this form
input_change_events.setup($(injected), "autosubmit");
},
registerTriggers() {
const isText = this.$el.is("input:text, input[type=search], textarea");
if (this.options.delay === "defocus" && !isText) {
log.error(
"The defocus delay value makes only sense on text input elements."
);
return this.$el;
}
function trigger_event(ev) {
if ($(ev.target).closest(".pat-autosubmit")[0] !== this) {
return;
}
$(ev.target).trigger("input-change-delayed");
}
if (this.options.delay === "defocus") {
this.$el.on("input-defocus.pat-autosubmit", trigger_event);
} else if (this.options.delay > 0) {
this.$el.on(
"input-change.pat-autosubmit",
utils.debounce(trigger_event, this.options.delay)
);
} else {
this.$el.on("input-change.pat-autosubmit", trigger_event);
}
},
destroy($el) {
input_change_events.remove($el, "autosubmit");
if (this.$el.is("form")) {
this.$el
.find(".pat-subform")
.addBack(this.$el)
.each((idx, el) => {
$(el).off(".pat-autosubmit");
});
} else {
$el.off(".pat-autosubmit");
}
},
onInputChange(ev) {
ev.stopPropagation();
$(this).submit();
log.debug("triggered by " + ev.type);
},
});