-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
64 lines (50 loc) · 1.02 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
/**
* Module dependencies.
*/
var delegate = require('delegate');
var url = require('url');
/**
* Handle link delegation on `el` or the document,
* and invoke `fn(e)` when clickable.
*
* @param {Element|Function} el or fn
* @param {Function} [fn]
* @api public
*/
module.exports = function(el, fn){
// default to document
if ('function' == typeof el) {
fn = el;
el = document;
}
var token = delegate.bind(el, 'a', 'click', function(e){
if (clickable(e)) fn(e);
});
return function(){
delegate.unbind(el, 'click', token);
};
};
/**
* Check if `e` is clickable.
*/
function clickable(e) {
if (1 != which(e)) return;
if (e.metaKey || e.ctrlKey || e.shiftKey) return;
if (e.defaultPrevented) return;
// target
var el = e.target;
// check target
if (el.target) return;
// x-origin
if (url.isCrossDomain(el.getAttribute('href'))) return;
return true;
}
/**
* Event button.
*/
function which(e) {
e = e || window.event;
return null == e.which
? e.button
: e.which;
}