/
element.ts
174 lines (145 loc) · 4.24 KB
/
element.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* eslint-disable no-dupe-class-members */
import { isArray, isUndefined, Shortcuts, EMPTY_OBJ } from '@tarojs/shared'
import { TaroNode } from './node'
import { NodeType } from './node_types'
import { TaroEvent, eventSource } from './event'
import { isElement } from '../utils'
import { Style } from './style'
interface Attributes {
name: string;
value: string;
}
export class TaroElement extends TaroNode {
public props: Record<string, string> = {}
public dataset: Record<string, unknown> = EMPTY_OBJ
public tagName: string
public style: Style
public constructor (nodeType: NodeType, nodeName: string) {
super(nodeType || NodeType.ELEMENT_NODE, nodeName)
this.tagName = nodeName.toUpperCase()
this.style = new Style(this)
}
public get id () {
return this.getAttribute('id')!
}
public set id (val: string) {
this.setAttribute('id', val)
}
public get className () {
return this.getAttribute('class') || ''
}
public set className (val: string) {
this.setAttribute('class', val)
}
public get cssText () {
return this.getAttribute('style')
}
public get children () {
return this.childNodes.filter(isElement)
}
public hasAttribute (qualifiedName: string) {
return !isUndefined(this.props[qualifiedName])
}
public hasAttributes () {
return this.attributes.length > 0
}
public focus () {
this.setAttribute('focus', true)
}
public blur () {
this.setAttribute('focus', false)
}
public setAttribute (qualifiedName: string, value: string | boolean): void {
if (qualifiedName === 'style') {
this.style.cssText = value as string
qualifiedName = Shortcuts.Style
} else if (qualifiedName === 'id') {
eventSource.delete(this.uid)
this.uid = value as string
eventSource.set(value as string, this)
qualifiedName = 'uid'
} else {
this.props[qualifiedName] = value as string
if (qualifiedName === 'class') {
qualifiedName = Shortcuts.Class
}
if (qualifiedName.startsWith('data-')) {
if (this.dataset === EMPTY_OBJ) {
this.dataset = Object.create(null)
}
this.dataset[qualifiedName.replace(/^data-/, '')] = value
}
}
this.enqueueUpdate({
path: `${this._path}.${qualifiedName}`,
value
})
}
public removeAttribute (qualifiedName: string) {
if (qualifiedName === 'style') {
this.style.cssText = ''
} else {
delete this.props[qualifiedName]
}
this.enqueueUpdate({
path: `${this._path}.${qualifiedName}`,
value: ''
})
}
public getAttribute (qualifiedName: string): string | null {
const attr = qualifiedName === 'style' ? this.style.cssText : this.props[qualifiedName]
return attr || null
}
public get attributes (): Attributes[] {
const propKeys = Object.keys(this.props)
const style = this.style.cssText
const attrs = propKeys.map(p => ({ name: p, value: this.props[p] }))
return attrs.concat(style ? { name: 'style', value: style } : [])
}
public get parentElement () {
if (this.parentNode instanceof TaroElement) {
return this.parentNode
}
return null
}
public dispatchEvent (event: TaroEvent) {
const cancelable = event.cancelable
const listeners = this.__handlers[event.type]
if (!isArray(listeners)) {
return
}
for (let i = listeners.length; i--;) {
const listener = listeners[i]
let result: unknown
if (listener._stop) {
listener._stop = false
} else {
result = listener.call(this, event)
}
if ((result === false || event._end) && cancelable) {
event.defaultPrevented = true
}
}
if (event._stop) {
this._stopPropagation(event)
} else {
event._stop = true
}
return listeners != null
}
private _stopPropagation (event: TaroEvent) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let target = this
// eslint-disable-next-line no-cond-assign
while ((target = target.parentNode as this)) {
const listeners = target.__handlers[event.type]
if (!isArray(listeners)) {
continue
}
for (let i = listeners.length; i--;) {
const l = listeners[i]
l._stop = true
}
}
}
}