8000 call detach listeners · mikolalysenko/mouse-change@2aba950 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2aba950

Browse files
committed
call detach listeners
1 parent 9b05511 commit 2aba950

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

mouse-listen.js

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module.exports = mouseListen
44

55
var mouse = require('mouse-event')
66

7-
function mouseListen(element, callback) {
8-
if(!callback) {
7+
function mouseListen (element, callback) {
8+
if (!callback) {
99
callback = element
1010
element = window
1111
}
@@ -14,95 +14,94 @@ function mouseListen(element, callback) {
1414
var x = 0
1515
var y = 0
1616
var mods = {
17-
shift: false,
18-
alt: false,
17+
shift: false,
18+
alt: false,
1919
control: false,
20-
meta: false
20+
meta: false
2121
}
2222
var attached = false
2323

24-
function updateMods(ev) {
24+
function updateMods (ev) {
2525
var changed = false
26-
if('altKey' in ev) {
26+
if ('altKey' in ev) {
2727
changed = changed || ev.altKey !== mods.alt
2828
mods.alt = !!ev.altKey
2929
}
30-
if('shiftKey' in ev) {
30+
if ('shiftKey' in ev) {
3131
changed = changed || ev.shiftKey !== mods.shift
3232
mods.shift = !!ev.shiftKey
3333
}
34-
if('ctrlKey' in ev) {
34+
if ('ctrlKey' in ev) {
3535
changed = changed || ev.ctrlKey !== mods.control
3636
mods.control = !!ev.ctrlKey
3737
}
38-
if('metaKey' in ev) {
38+
if ('metaKey' in ev) {
3939
changed = changed || ev.metaKey !== mods.meta
4040
mods.meta = !!ev.metaKey
4141
}
4242
return changed
4343
}
4444

45-
function handleEvent(nextButtons, ev) {
45+
function handleEvent (nextButtons, ev) {
4646
var nextX = mouse.x(ev)
4747
var nextY = mouse.y(ev)
48-
if('buttons' in ev) {
49-
nextButtons = ev.buttons|0
48+
if ('buttons' in ev) {
49+
nextButtons = ev.buttons | 0
5050
}
51-
if(nextButtons !== buttonState ||
52-
nextX !== x ||
53-
nextY !== y ||
54-
updateMods(ev)) {
55-
buttonState = nextButtons|0
56-
x = nextX||0
57-
y = nextY||0
51+
if (nextButtons !== buttonState ||
52+
nextX !== x ||
53+
nextY !== y ||
54+
updateMods(ev)) {
55+
buttonState = nextButtons | 0
56+
x = nextX || 0
57+
y = nextY || 0
5858
callback && callback(buttonState, x, y, mods)
5959
}
6060
}
6161

62-
function clearState(ev) {
62+
function clearState (ev) {
6363
handleEvent(0, ev)
6464
}
6565

66-
function handleBlur() {
67-
if(buttonState ||
66+
function handleBlur () {
67+
if (buttonState ||
6868
x ||
6969
y ||
7070
mods.shift ||
7171
mods.alt ||
7272
mods.meta ||
7373
mods.control) {
74-
7574
x = y = 0
7675
buttonState = 0
7776
mods.shift = mods.alt = mods.control = mods.meta = false
7877
callback && callback(0, 0, 0, mods)
7978
}
8079
}
8180

82-
function handleMods(ev) {
83-
if(updateMods(ev)) {
81+
function handleMods (ev) {
82+
if (updateMods(ev)) {
8483
callback && callback(buttonState, x, y, mods)
8584
}
8685
}
8786

88-
function handleMouseMove(ev) {
89-
if(mouse.buttons(ev) === 0) {
87+
function handleMouseMove (ev) {
88+
if (mouse.buttons(ev) === 0) {
9089
handleEvent(0, ev)
9190
} else {
9291
handleEvent(buttonState, ev)
9392
}
9493
}
9594

96-
function handleMouseDown(ev) {
95+
function handleMouseDown (ev) {
9796
handleEvent(buttonState | mouse.buttons(ev), ev)
9897
}
9998

100-
function handleMouseUp(ev) {
99+
function handleMouseUp (ev) {
101100
handleEvent(buttonState & ~mouse.buttons(ev), ev)
102101
}
103102

104-
function attachListeners() {
105-
if(attached) {
103+
function attachListeners () {
104+
if (attached) {
106105
return
107106
}
108107
attached = true
@@ -124,7 +123,7 @@ function mouseListen(element, callback) {
124123
element.addEventListener('keydown', handleMods)
125124
element.addEventListener('keypress', handleMods)
126125

127-
if(element !== window) {
126+
if (element !== window) {
128127
window.addEventListener('blur', handleBlur)
129128

130129
window.addEventListener('keyup', handleMods)
@@ -133,8 +132,8 @@ function mouseListen(element, callback) {
133132
}
134133
}
135134

136-
function detachListeners() {
137-
if(!attached) {
135+
function detachListeners () {
136+
if (!attached) {
138137
return
139138
}
140139
attached = false
@@ -156,7 +155,7 @@ function mouseListen(element, callback) {
156155
element.removeEventListener('keydown', handleMods)
157156
element.removeEventListener('keypress', handleMods)
158157

159-
if(element !== window) {
158+
if (element !== window) {
160159
window.removeEventListener('blur', handleBlur)
161160

162161
window.removeEventListener('keyup', handleMods)
@@ -165,7 +164,7 @@ function mouseListen(element, callback) {
165164
}
166165
}
167166

168-
//Attach listeners
167+
// Attach listeners
169168
attachListeners()
170169

171170
var result = {
@@ -174,30 +173,30 @@ function mouseListen(element, callback) {
174173

175174
Object.defineProperties(result, {
176175
enabled: {
177-
get: function() { return attached },
178-
set: function(f) {
179-
if(f) {
176+
get: function () { return attached },
177+
set: function (f) {
178+
if (f) {
180179
attachListeners()
181180
} else {
182-
detachListeners
181+
detachListeners()
183182
}
184183
},
185184
enumerable: true
186185
},
187186
buttons: {
188-
get: function() { return buttonState },
187+
get: function () { return buttonState },
189188
enumerable: true
190189
},
191190
x: {
192-
get: function() { return x },
191+
get: function () { return x },
193192
enumerable: true
194193
},
195194
y: {
196-
get: function() { return y },
195+
get: function () { return y },
197196
enumerable: true
198197
},
199198
mods: {
200-
get: function() { return mods },
199+
get: function () { return mods },
201200
enumerable: true
202201
}
203202
})

0 commit comments

Comments
 (0)
0