forked from hotwired/stimulus
/
action.ts
146 lines (122 loc) · 4.35 KB
/
action.ts
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
import { ActionDescriptor, parseActionDescriptorString } from "./action_descriptor"
import { Token } from "../mutation-observers"
import { Context } from "./context"
import { camelize } from "./string_helpers"
import { hasProperty } from "./utils"
const allModifiers = ["meta", "ctrl", "alt", "shift"]
export class Action {
readonly element: Element
readonly index: number
private readonly eventTargetName: string | undefined
readonly eventName: string
readonly eventOptions: AddEventListenerOptions
readonly identifier: string
readonly methodName: string
readonly keyFilter: string
readonly context: Context
static forToken(token: Token, context: Context) {
return new this(token.element, token.index, parseActionDescriptorString(token.content), context)
}
constructor(element: Element, index: number, descriptor: Partial<ActionDescriptor>, context: Context) {
this.element = element
this.index = index
this.eventTargetName = descriptor.eventTargetName
this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name")
this.eventOptions = descriptor.eventOptions || {}
this.identifier = descriptor.identifier || error("missing identifier")
this.methodName = descriptor.methodName || error("missing method name")
this.keyFilter = descriptor.keyFilter || ""
this.context = context
}
toString() {
const eventFilter = this.keyFilter ? `.${this.keyFilter}` : ""
const eventTarget = this.eventTargetName ? `@${this.eventTargetName}` : ""
return `${this.eventName}${eventFilter}${eventTarget}->${this.identifier}#${this.methodName}`
}
shouldIgnoreKeyboardEvent(event: KeyboardEvent): boolean {
if (!this.keyFilter) {
return false
}
const filters = this.keyFilter.split("+")
if (this.keyFilterDissatisfied(event, filters)) {
return true
}
const standardFilter = filters.filter((key) => !allModifiers.includes(key))[0]
if (!standardFilter) {
// missing non modifier key
return false
}
if (!hasProperty(this.keyMappings, standardFilter)) {
error(`contains unknown key filter: ${this.keyFilter}`)
}
return this.keyMappings[standardFilter].toLowerCase() !== event.key.toLowerCase()
}
shouldIgnoreMouseEvent(event: MouseEvent): boolean {
if (!this.keyFilter) {
return false
}
const filters = [this.keyFilter]
if (this.keyFilterDissatisfied(event, filters)) {
return true
}
return false
}
get params() {
const params: { [key: string]: any } = {}
const pattern = new RegExp(`^data-${this.identifier}-(.+)-param$`, "i")
for (const { name, value } of Array.from(this.element.attributes)) {
const match = name.match(pattern)
const key = match && match[1]
if (key) {
params[camelize(key)] = typecast(value)
}
}
return params
}
get schema() {
return this.context.schema
}
get eventTargets(): EventTarget[] {
if (this.eventTargetName == "window") {
return [window]
} else if (this.eventTargetName == "document") {
return [document]
} else if (typeof this.eventTargetName == "string") {
return this.context.controller.outlets.findAll(this.eventTargetName)
} else {
return [this.element]
}
}
private get keyMappings() {
return this.schema.keyMappings
}
private keyFilterDissatisfied(event: KeyboardEvent | MouseEvent, filters: Array<string>): boolean {
const [meta, ctrl, alt, shift] = allModifiers.map((modifier) => filters.includes(modifier))
return event.metaKey !== meta || event.ctrlKey !== ctrl || event.altKey !== alt || event.shiftKey !== shift
}
}
const defaultEventNames: { [tagName: string]: (element: Element) => string } = {
a: () => "click",
button: () => "click",
form: () => "submit",
details: () => "toggle",
input: (e) => (e.getAttribute("type") == "submit" ? "click" : "input"),
select: () => "change",
textarea: () => "input",
}
export function getDefaultEventNameForElement(element: Element): string | undefined {
const tagName = element.tagName.toLowerCase()
if (tagName in defaultEventNames) {
return defaultEventNames[tagName](element)
}
}
function error(message: string): never {
throw new Error(message)
}
function typecast(value: any): any {
try {
return JSON.parse(value)
} catch (o_O) {
return value
}
}