-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdepends.js
147 lines (132 loc) · 4.81 KB
/
depends.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import $ from "jquery";
import { BasePattern } from "@patternslib/patternslib/src/core/basepattern";
import events from "../../core/events";
import dom from "../../core/dom";
import logging from "../../core/logging";
import Parser from "@patternslib/patternslib/src/core/parser";
import registry from "@patternslib/patternslib/src/core/registry";
import utils from "../../core/utils";
const log = logging.getLogger("depends");
export const parser = new Parser("depends");
parser.addArgument("condition");
parser.addArgument("action", "both", ["show", "enable", "both"]);
parser.addArgument("transition", "none", ["none", "css", "fade", "slide"]);
parser.addArgument("effect-duration", "fast");
parser.addArgument("effect-easing", "swing");
class Pattern extends BasePattern {
static name = "depends";
static trigger = ".pat-depends";
static parser = parser;
async init() {
this.$el = $(this.el);
const DependsHandler = (await import("../../lib/dependshandler")).default; // prettier-ignore
try {
this.handler = new DependsHandler(this.el, this.options.condition);
} catch (e) {
log.error("Invalid condition: " + e.message, this.el);
return;
}
// Initialize
this.set_state();
for (const input of this.handler.getAllInputs()) {
events.add_event_listener(
input,
"change",
`pat-depends--change--${this.uuid}`, // We need to support multiple events per dependant ...
this.set_state.bind(this)
);
events.add_event_listener(
input,
"keyup",
`pat-depends--keyup--${this.uuid}`, // ... therefore we need to add a uuid to the event id ...
this.set_state.bind(this)
);
if (input.form) {
events.add_event_listener(
input.form,
"reset",
`pat-depends--reset--${this.uuid}`, // ... to not override previously set event handlers.
async () => {
// TODO: note sure, what this timeout is for.
await utils.timeout(50);
this.set_state.bind(this);
}
);
}
}
}
update_modal() {
const modal = this.el.closest(".pat-modal");
// If we're in a modal, make sure that it gets resized.
if (modal) {
$(document).trigger("pat-update", { pattern: "depends" });
}
}
enable() {
const inputs = dom.find_inputs(this.el);
for (const input of inputs) {
input.disabled = false;
// Trigger the change event after disabling so that any other bound actions can react on that.
input.dispatchEvent(events.change_event());
}
if (this.el.tagName === "A") {
events.remove_event_listener(this.el, "pat-depends--click");
}
this.el.classList.remove("disabled");
this.$el.trigger("pat-update", {
pattern: "depends",
action: "attribute-changed",
dom: this.el,
enabled: true,
});
}
disable() {
const inputs = dom.find_inputs(this.el);
for (const input of inputs) {
input.disabled = true;
// Trigger the change event after disabling so that any other bound actions can react on that.
input.dispatchEvent(events.change_event());
}
if (this.el.tagName === "A") {
events.add_event_listener(this.el, "click", "pat-depends--click", (e) =>
e.preventDefault()
);
}
this.el.classList.add("disabled");
this.$el.trigger("pat-update", {
pattern: "depends",
action: "attribute-changed",
dom: this.el,
enabled: false,
});
}
set_state() {
const state = this.handler.evaluate();
switch (this.options.action) {
case "show":
utils.hideOrShow(this.el, state, this.options, this.name);
this.update_modal();
break;
case "enable":
if (state) {
this.enable();
} else {
this.disable();
}
break;
case "both":
utils.hideOrShow(this.el, state, this.options, this.name);
this.update_modal();
if (state) {
this.enable();
} else {
this.disable();
}
break;
}
}
}
// Register Pattern class in the global pattern registry
registry.register(Pattern);
// Make it available
export default Pattern;