Skip to content

Commit e5ac7d0

Browse files
committed
Refactor
1 parent 5ce7ffe commit e5ac7d0

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

src/decorators/bodyMode.js

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,65 @@
33
*/
44
import {checkStatus} from './customEvent'
55

6-
export default function (target) {
7-
target.prototype.isBodyMode = function () {
8-
return this.props.bodyMode
9-
}
10-
11-
const makeProxy = (e) => {
12-
const proxy = {}
13-
for (const key in e) {
14-
if (typeof e[key] === 'function') {
15-
proxy[key] = e[key].bind(e)
16-
} else {
17-
proxy[key] = e[key]
18-
}
6+
const makeProxy = (e) => {
7+
const proxy = {}
8+
for (const key in e) {
9+
if (typeof e[key] === 'function') {
10+
proxy[key] = e[key].bind(e)
11+
} else {
12+
proxy[key] = e[key]
1913
}
20-
return proxy
2114
}
15+
return proxy
16+
}
2217

23-
const bodyListener = function (callback, options, e) {
24-
const {respectEffect = false, customEvent = false} = options
25-
const {id} = this.props
18+
const bodyListener = function (callback, options, e) {
19+
const {respectEffect = false, customEvent = false} = options
20+
const {id} = this.props
2621

27-
const tip = e.target.dataset.tip
28-
const _for = e.target.dataset.for
22+
const tip = e.target.dataset.tip
23+
const _for = e.target.dataset.for
2924

30-
const target = e.target
31-
if (!!target.getAttribute('data-event') && !customEvent) {
32-
return
33-
}
25+
const target = e.target
26+
if (this.isCustomEvent(target) && !customEvent) {
27+
return
28+
}
3429

35-
if (tip != null &&
36-
(!respectEffect || this.getEffect(target) === 'float') &&
37-
((id == null && _for == null) || (id != null && _for === id))
38-
) {
39-
const proxy = makeProxy(e)
40-
proxy.currentTarget = target
41-
callback(proxy)
42-
}
30+
const isTargetBelongsToTooltip =
31+
(id == null && _for == null) || (id != null && _for === id)
32+
33+
if (tip != null &&
34+
(!respectEffect || this.getEffect(target) === 'float') &&
35+
isTargetBelongsToTooltip
36+
) {
37+
const proxy = makeProxy(e)
38+
proxy.currentTarget = target
39+
callback(proxy)
4340
}
41+
}
42+
43+
const findCustomEvents = (targetArray, dataAttribute) => {
44+
const events = {}
45+
targetArray.forEach(target => {
46+
const event = target.getAttribute(dataAttribute)
47+
if (event) event.split(' ').forEach(event => events[event] = true)
48+
})
49+
50+
return events
51+
}
4452

45-
const findCustomEvents = (targetArray, dataAttribute) => {
46-
const events = {}
47-
targetArray.forEach(target => {
48-
const event = target.getAttribute(dataAttribute)
49-
if (event) event.split(' ').forEach(event => events[event] = true)
50-
})
53+
const getBody = () => document.getElementsByTagName('body')[0]
5154

52-
return events
55+
export default function (target) {
56+
target.prototype.isBodyMode = function () {
57+
return this.props.bodyMode
5358
}
5459

5560
target.prototype.bindBodyListener = function () {
5661
const { id } = this.props
5762
const { event, eventOff } = this.state
58-
const body = document.getElementsByTagName('body')[0]
59-
6063
const targetArray = this.getTargetArray(id)
64+
const body = getBody()
6165

6266
const customEvents = findCustomEvents(targetArray, 'data-event')
6367
const customEventsOff = findCustomEvents(targetArray, 'data-event-off')
@@ -77,7 +81,8 @@ export default function (target) {
7781

7882
for (const event in customEvents) {
7983
listeners[event] = bodyListener.bind(this, (e) => {
80-
checkStatus.call(this, e.currentTarget.getAttribute('data-event-off') || eventOff, e)
84+
const targetEventOff = e.currentTarget.getAttribute('data-event-off') || eventOff
85+
checkStatus.call(this, targetEventOff, e)
8186
}, { customEvent: true })
8287
}
8388
for (const event in customEventsOff) {
@@ -89,7 +94,7 @@ export default function (target) {
8994
}
9095

9196
target.prototype.unbindBodyListener = function (body) {
92-
body = body || document.getElementsByTagName('body')[0]
97+
body = body || getBody()
9398

9499
const listeners = this.bodyModeListeners
95100
for (const event in listeners) {

0 commit comments

Comments
 (0)