sonarr-repo-only/UI/RouteBinder.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

"use strict";
2013-06-21 01:43:58 +00:00
define(function () {
//This module will automatically route all links through backbone router rather than
//causing links to reload pages.
var routeBinder = {
2013-06-21 01:43:58 +00:00
bind: function (router) {
this._router = router;
$(document).on('click', 'a[href]', this._handleClick);
},
_isInTab: function (element) {
return;
},
_handleClick: function (event) {
var $target = $(event.target);
//check if tab nav
if ($target.parents('.nav-tabs').length) {
return;
}
if ($target.hasClass('no-router')) {
return;
}
console.log('click');
event.preventDefault();
var href = event.target.getAttribute('href');
if (!href && $target.parent('a') && $target.parent('a')[0]) {
var linkElement = $target.parent('a')[0];
href = linkElement.getAttribute('href');
}
if (!href) {
throw 'couldnt find route target';
}
if (!href.startsWith('http')) {
2013-06-21 01:43:58 +00:00
this._router.navigate(href, { trigger: true });
}
else {
//Open in new tab
window.open(href, '_blank');
}
}
};
return routeBinder;
});