-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (55 loc) · 1.3 KB
/
index.js
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
/**
* Module dependencies
*/
var events = require('event');
/**
* Command and control modifier keys
*/
var keys = {
91 : 'command',
17 : 'control'
};
/**
* Export `end-of`
*/
module.exports = endOf;
/**
* Initialize `end-of`
*
* @param {DOM Node} el
* @param {Function} fn
*/
function endOf(el, fn) {
var modifier = false;
events.bind(el, 'keyup', function(e) {
// More complicated because we need to use keyup
var mod = keys[e.which];
if(mod) { modifier = true; return; }
else if(modifier) { modifier = false; return; }
return fn(check(e));
});
}
/**
* Check
*
* Refactor of Tim Down's SO answer:
* http://stackoverflow.com/a/3904819/145435
*/
function check(e) {
var target = e.target,
val = target.value,
end = false;
if (typeof target.selectionStart == "number") {
// Non-IE browsers
end = (target.selectionEnd == val.length);
} else if (document.selection && document.selection.createRange) {
// IE branch
target.focus();
var selRange = document.selection.createRange();
var inputRange = target.createTextRange();
var inputSelRange = inputRange.duplicate();
inputSelRange.moveToBookmark(selRange.getBookmark());
end = inputSelRange.compareEndPoints("EndToEnd", inputRange) === 0;
}
return end;
}