-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathember-href-to.js
44 lines (37 loc) · 1.27 KB
/
ember-href-to.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
import Em from 'ember';
function _getNormalisedRootUrl(router) {
let rootURL = router.rootURL;
if(rootURL.charAt(rootURL.length - 1) !== '/') {
rootURL = rootURL + '/';
}
return rootURL;
}
function _lookupRouter(applicationInstance) {
const container = 'lookup' in applicationInstance ? applicationInstance : applicationInstance.container;
return container.lookup('router:main');
}
export default {
name: 'ember-href-to',
initialize: function(applicationInstance) {
let router = _lookupRouter(applicationInstance);
let rootURL = _getNormalisedRootUrl(router);
let $body = Em.$(document.body);
$body.off('click.href-to', 'a');
$body.on('click.href-to', 'a', function(e) {
let $target = Em.$(e.currentTarget);
let handleClick = (e.which === 1 && !e.ctrlKey && !e.metaKey);
if(handleClick && !$target.hasClass('ember-view') && Em.isNone($target.attr('data-ember-action'))) {
let url = $target.attr('href');
if(url && url.indexOf(rootURL) === 0) {
url = url.substr(rootURL.length - 1);
if(router.router.recognizer.recognize(url)) {
router.handleURL(url);
router.router.updateURL(url);
return false;
}
}
}
return true;
});
}
};