-
Notifications
You must be signed in to change notification settings - Fork 418
/
action.ts
126 lines (108 loc) · 3.92 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
import { ActionDescriptor, parseActionDescriptorString } from "./action_descriptor"
import { Token } from "../mutation-observers"
import { Context } from "./context"
import { camelize } from "./string_helpers"
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}`
}
isFilterTarget(event: KeyboardEvent): boolean {
if (!this.keyFilter) {
return false
}
const filteres = this.keyFilter.split("+")
const modifiers = ["meta", "ctrl", "alt", "shift"]
const [meta, ctrl, alt, shift] = modifiers.map((modifier) => filteres.includes(modifier))
if (event.metaKey !== meta || event.ctrlKey !== ctrl || event.altKey !== alt || event.shiftKey !== shift) {
return true
}
const standardFilter = filteres.filter((key) => !modifiers.includes(key))[0]
if (!standardFilter) {
// missing non modifier key
return false
}
if (!Object.prototype.hasOwnProperty.call(this.keyMappings, standardFilter)) {
error(`contains unknown key filter: ${this.keyFilter}`)
}
return this.keyMappings[standardFilter].toLowerCase() !== event.key.toLowerCase()
}
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
}
}
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
}
}