1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/UI/jQuery/RouteBinder.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2013-06-22 09:24:24 +03:00
'use strict';
2013-09-12 00:38:35 +03:00
define(function () {
//This module will automatically route all relative links through backbone router rather than
//causing links to reload pages.
var routeBinder = {
2013-06-21 04:43:58 +03:00
bind: function (router) {
2013-06-21 08:19:41 +03:00
var self = this;
$(document).on('click', 'a[href]', function (event) {
self._handleClick(event, router);
});
},
2013-06-21 08:19:41 +03:00
_handleClick: function (event, router) {
var $target = $(event.target);
//check if tab nav
if ($target.parents('.nav-tabs').length) {
return;
}
if ($target.hasClass('no-router')) {
return;
}
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 'couldn\'t find route target';
}
2013-09-12 00:38:35 +03:00
if (!href.startsWith('http')) {
2013-06-21 08:19:41 +03:00
router.navigate(href, { trigger: true });
}
else {
//Open in new tab
window.open(href, '_blank');
}
}
};
return routeBinder;
});