/* * jQuery Key Selection - v1.0.1 * Cycle throught arbitrary elements with your arrow keys and select them with enter or your mouse. * * * Made by Christian Voigt * Under MIT License */ !function(a,b,c){function d(b,c){this.element=b,this.options=a.extend({},f,c),this._defaults=f,this._name=e,this.init()}var e="keySelection",f={exclusiveKeyListener:!0,scrollToKeyHoverItem:!0,scrollContainer:"html,body",scrollMargin:10,selectionItemSelector:".selection-item",scrollAnimationDuration:150,keyActions:[{keyCode:13,action:"select"},{keyCode:38,action:"up"},{keyCode:40,action:"down"},{keyCode:37,action:"up"},{keyCode:39,action:"down"},{keyCode:9,action:"down"},{keyCode:32,action:"select"}]};d.prototype={keys:{enter:13,up:38,down:40,left:37,right:39,tab:9,space:32},init:function(){var b=this;this.keydownHandler=function(c){var d=!1;return a.each(b.options.keyActions,function(a,e){if(e.keyCode===c.which){switch(e.action){case"up":b.up(),d=!0;break;case"down":b.down(),d=!0;break;case"select":b.select(),d=!0}return!1}}),d&&b.options.exclusiveKeyListener?!1:void 0},a(c).on("keydown",this.keydownHandler),this.clickHandler=function(){b.select(a(this))},a(this.element).on("click",this.options.selectionItemSelector,this.clickHandler)},down:function(){if(!this.stopped){var b=a(this.element).find(this.options.selectionItemSelector),c=b.filter(".key-hover"),d=b.index(c);c.removeClass("key-hover"),c=b.length>d+1?a(b[d+1]).addClass("key-hover"):a(b[0]).addClass("key-hover"),this.options.scrollToKeyHoverItem&&this.scrollTo(c),a(this.element).trigger({type:"keySelection.keyHover",keyHoverElement:c.get(0)})}},up:function(){if(!this.stopped){var b=a(this.element).find(this.options.selectionItemSelector),c=b.filter(".key-hover"),d=b.index(c);c.removeClass("key-hover"),c=d>0?a(b[d-1]).addClass("key-hover"):a(b[b.length-1]).addClass("key-hover"),this.options.scrollToKeyHoverItem&&this.scrollTo(c),a(this.element).trigger({type:"keySelection.keyHover",keyHoverElement:c.get(0)})}},select:function(b){if(!this.stopped){var c=a(this.element).find(this.options.selectionItemSelector+".selected");c.removeClass("selected"),!b&&c.hasClass("key-hover")||b&&c.get(0)===b.get(0)||(b&&b.is(this.options.selectionItemSelector)?(a(this.element).find(this.options.selectionItemSelector+".key-hover").removeClass("key-hover"),c=b.addClass("selected"),c.addClass("key-hover")):c=a(this.element).find(this.options.selectionItemSelector+".key-hover").addClass("selected"),a(this.element).trigger({type:"keySelection.selection",selectedElement:c.get(0)}))}},scrollTo:function(b){a(this.options.scrollContainer).animate({scrollTop:b.offset().top-this.options.scrollMargin},this.options.scrollAnimationDuration)},start:function(){this.stopped&&(this.init(),this.stopped=!1)},stop:function(){this.stopped||(a(c).off("keydown",this.keydownHandler),a(this.element).off("click",this.options.selectionItemSelector,this.clickHandler),this.stopped=!0)},destroy:function(){this.stop(),a.data(this.element,"plugin_"+e,null)}},a.fn[e]=function(b){return this.each(function(){if(a.data(this,"plugin_"+e)||a.data(this,"plugin_"+e,new d(this,b)),("string"==typeof b||b instanceof String)&&/stop|up|down|select|stop|start|destroy/.test(b)){var c=a.data(this,"plugin_"+e);c[b].call(c)}})}}(jQuery,window,document);