-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.directional.min.js
13 lines (13 loc) · 2.88 KB
/
jquery.directional.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*!
* jQUery.Directional v1.0.9
* by Jean Kássio
*
* More info:
* https://github.com/jeankassio
*
* Copyright Jean Kássio
* Released under the MIT license
* https://github.com/jeankassio/jQuery.Directional/blob/main/LICENSE
*
* @preserve
*/ !function(t){t.fn.Directional=function(e){let n=t.extend({rewriteindexes:!1,selectlement:!1,clickonenter:!1,checkistv:!1},e);if(n.checkistv&&is?.SmartTV?.()===!1)return!1;function i(){let t=document.activeElement;return l(t)}function o(t){return l(t[0])}function f(e){$active=i(),$tbIndex=t("[tabindex]").filter(function(){let n=o(t(this)),i=t(this).attr("tabindex")>=0,f=e?n.top<$active.top:$active.bottom-n.top<0,r=t(document.activeElement).attr("tabindex")!==t(this).attr("tabindex");return n.left-$active.left<$active.width&&($active.right,n.right,$active.width),i&&f&&r});let n,f=0;t.each($tbIndex,function(e,i){let r=o(t(i)),l=c($active.left,$active.top,r.left,r.top+r.height);void 0!==n?l<f&&(n=t(i),f=l):(n=t(i),f=l)}),void 0!==n&&n.length>0&&(n.focus(),window.scrollTo({top:n.offset().top+n.height()/2-t(window).height()/2,behavior:"smooth"}))}function r(e){$active=i(),$tbIndex=t("[tabindex]").filter(function(){let n=o(t(this)),i=t(this).attr("tabindex")>=0,f=e?n.left<$active.left:$active.left-n.right<0,r=t(document.activeElement).attr("tabindex")!==t(this).attr("tabindex"),c=n.top-$active.top<$active.height&&$active.bottom-n.bottom<$active.height;return i&&f&&c&&r});let n,f=0;t.each($tbIndex,function(e,i){let r=o(t(i)),l=c($active.left,$active.top,r.left,r.top);void 0!==n?l<f&&(n=t(i),f=l):(n=t(i),f=l)}),void 0!==n&&n.length>0&&(n.focus(),window.scrollTo({top:n.offset().top+n.height()/2-t(window).height()/2,behavior:"smooth"}))}function c(t,e,n,i){return Math.hypot(n-t,i-e)}function l(t){let e=t.getBoundingClientRect();return e.top=function t(e){return e.offsetParent?e.offsetTop+t(e.offsetParent):e.offsetTop||0}(t)-window.scrollY,e.left=function t(e){return e.offsetParent?e.offsetLeft+t(e.offsetParent):e.offsetleft||0}(t)-window.scrollX,e.bottom=e.top+t.offsetHeight,e.right=e.left+t.offsetWidth,e}return n.rewriteindexes&&($tbIndex=t("[tabindex]").filter(function(){return t(this).attr("tabindex")>=0}),$i=0,t.each($tbIndex,function(e,n){if(0==t(`[tabindex="${$i}"]`).length)t(n).attr("tabindex",$i);else{for(;t(`[tabindex="${$i}"]`).length>0;)$i++;t(n).attr("tabindex",$i)}$i++})),n.selectelement&&(n.selectelement instanceof jQuery?n.selectelement.focus():t(n.selectelement).focus()),t(document).on("keyup",function(e){switch(e.which){case 37:e.preventDefault(),r(!0);break;case 38:e.preventDefault(),f(!0);break;case 39:e.preventDefault(),r(!1);break;case 40:e.preventDefault(),f(!1);break;case 13:n.clickonenter&&t(document.activeElement).click()}}),t(document).on("focusout","[tabindex]",function(){$tbIndex=t(this).attr("tabindex"),setTimeout(function(){var e=t(document.activeElement).attr("tabindex");(void 0===e||!1==e)&&(r(!1),r(!0))},10)}),this}}(jQuery);