function KeyInputHandler() {
			this.run = function(e) {
				if (readyForKeyInput === false) {
					return;
				}
				var key = e.keyCode ? e.keyCode : e.which;
				readyForKeyInput = false;

				if (key === 38) {
					up();
				}
				if (key === 40) {
					down();
				}
				if (key === 13) {
					enter(e);
				}
				if (key === 27) {
					escape(e);
				}

				$scope.$apply();
			};

			// Move to previous option on up key press.
			function up() {
				if ($ctrl.keyboardFocusIndex === 0 || $ctrl.keyboardFocusIndex === null) {
					return;
				}
				$ctrl.keyboardFocusIndex -= 1;
				adjustScroll(false);
			}

			// Move to next option on down key press.
			function down() {
				if ($ctrl.keyboardFocusIndex === null) {
					$ctrl.keyboardFocusIndex = 0;
					adjustScroll(true);
					$scope.$apply();
					return;
				}

				if ($ctrl.keyboardFocusIndex >= $ctrl.filteredOptions.length - 1) {
					return;
				}

				$ctrl.keyboardFocusIndex += 1;
				adjustScroll(true);
			}

			// Close out search and select option on enter key press.
			function enter(e) {
				if ($ctrl.keyboardFocusIndex === null) {
					return;
				}
				selectOption($ctrl.filteredOptions[$ctrl.keyboardFocusIndex]);
				e.target.blur();
				readyForKeyInput = true;
			}

			// Close out search on escape key press.
			function escape(e) {
				e.target.blur();
				readyForKeyInput = true;
			}

			// Adjusts the scroll value of the list based on which listItem is currently focused.
			function adjustScroll(isDownKey) {
				var listId = 'option-list';
				var listItemId = 'option-list-item-' + $ctrl.keyboardFocusIndex;

				// Gets the "next" list item based on whether the down key or up key was pressed.
				var nextListItemDirection = isDownKey ? 1 : -1;
				var nextListItemId = 'option-list-item-' + ($ctrl.keyboardFocusIndex + nextListItemDirection);

				var list = $document[0].getElementById(listId);
				var listItem = $document[0].getElementById(listItemId);
				var nextListItem = $document[0].getElementById(nextListItemId) || listItem;

				// adjusts scroll value when the nextListItem is ~below~ the viewable window.
				if (nextListItem.offsetTop >= list.offsetHeight + list.scrollTop) {
					list.scrollTop = nextListItem.offsetTop - list.offsetHeight + nextListItem.offsetHeight;
				}

				// adjusts scroll value when the nextListItem is ~above~ the viewable window.
				if (list.scrollTop > nextListItem.offsetTop) {
					list.scrollTop = nextListItem.offsetTop;
				}
			}
		}
// An object for handling key inputs while focused on search-and-select.
An object for handling key inputs while focused on search-and-select.
An object for handling key inputs while focused on search - and - select .
function KeyInputHandler() {
			this.run = function(e) {
				if (readyForKeyInput === false) {
					return;
				}
				var key = e.keyCode ? e.keyCode : e.which;
				readyForKeyInput = false;
				if (key === 38) {
					up();
				}
				if (key === 40) {
					down();
				}
				if (key === 13) {
					enter(e);
				}
				if (key === 27) {
					escape(e);
				}
				$scope.$apply();
			};
			function up() {
				if ($ctrl.keyboardFocusIndex === 0 || $ctrl.keyboardFocusIndex === null) {
					return;
				}
				$ctrl.keyboardFocusIndex -= 1;
				adjustScroll(false);
			}
			function down() {
				if ($ctrl.keyboardFocusIndex === null) {
					$ctrl.keyboardFocusIndex = 0;
					adjustScroll(true);
					$scope.$apply();
					return;
				}
				if ($ctrl.keyboardFocusIndex >= $ctrl.filteredOptions.length - 1) {
					return;
				}
				$ctrl.keyboardFocusIndex += 1;
				adjustScroll(true);
			}
			function enter(e) {
				if ($ctrl.keyboardFocusIndex === null) {
					return;
				}
				selectOption($ctrl.filteredOptions[$ctrl.keyboardFocusIndex]);
				e.target.blur();
				readyForKeyInput = true;
			}
			function escape(e) {
				e.target.blur();
				readyForKeyInput = true;
			}
			function adjustScroll(isDownKey) {
				var listId = 'option-list';
				var listItemId = 'option-list-item-' + $ctrl.keyboardFocusIndex;
				var nextListItemDirection = isDownKey ? 1 : -1;
				var nextListItemId = 'option-list-item-' + ($ctrl.keyboardFocusIndex + nextListItemDirection);
				var list = $document[0].getElementById(listId);
				var listItem = $document[0].getElementById(listItemId);
				var nextListItem = $document[0].getElementById(nextListItemId) || listItem;
				if (nextListItem.offsetTop >= list.offsetHeight + list.scrollTop) {
					list.scrollTop = nextListItem.offsetTop - list.offsetHeight + nextListItem.offsetHeight;
				}
				if (list.scrollTop > nextListItem.offsetTop) {
					list.scrollTop = nextListItem.offsetTop;
				}
			}
		}
function KeyInputHandler ( ) { this . run = function ( e ) { if ( readyForKeyInput === false ) { return ; } var key = e . keyCode ? e . keyCode : e . which ; readyForKeyInput = false ; if ( key === 38 ) { up ( ) ; } if ( key === 40 ) { down ( ) ; } if ( key === 13 ) { enter ( e ) ; } if ( key === 27 ) { escape ( e ) ; } $scope . $apply ( ) ; } ; function up ( ) { if ( $ctrl . keyboardFocusIndex === 0 || $ctrl . keyboardFocusIndex === null ) { return ; } $ctrl . keyboardFocusIndex -= 1 ; adjustScroll ( false ) ; } function down ( ) { if ( $ctrl . keyboardFocusIndex === null ) { $ctrl . keyboardFocusIndex = 0 ; adjustScroll ( true ) ; $scope . $apply ( ) ; return ; } if ( $ctrl . keyboardFocusIndex >= $ctrl . filteredOptions . length - 1 ) { return ; } $ctrl . keyboardFocusIndex += 1 ; adjustScroll ( true ) ; } function enter ( e ) { if ( $ctrl . keyboardFocusIndex === null ) { return ; } selectOption ( $ctrl . filteredOptions [ $ctrl . keyboardFocusIndex ] ) ; e . target . blur ( ) ; readyForKeyInput = true ; } function escape ( e ) { e . target . blur ( ) ; readyForKeyInput = true ; } function adjustScroll ( isDownKey ) { var listId = 'option-list' ; var listItemId = 'option-list-item-' + $ctrl . keyboardFocusIndex ; var nextListItemDirection = isDownKey ? 1 : - 1 ; var nextListItemId = 'option-list-item-' + ( $ctrl . keyboardFocusIndex + nextListItemDirection ) ; var list = $document [ 0 ] . getElementById ( listId ) ; var listItem = $document [ 0 ] . getElementById ( listItemId ) ; var nextListItem = $document [ 0 ] . getElementById ( nextListItemId ) || listItem ; if ( nextListItem . offsetTop >= list . offsetHeight + list . scrollTop ) { list . scrollTop = nextListItem . offsetTop - list . offsetHeight + nextListItem . offsetHeight ; } if ( list . scrollTop > nextListItem . offsetTop ) { list . scrollTop = nextListItem . offsetTop ; } } }
An object for handling key inputs while focused on search-and-select.
An object for handling key inputs while focused on search - and - select .
// Move to previous option on up key press. // Move to next option on down key press. // Close out search and select option on enter key press. // Close out search on escape key press. // Adjusts the scroll value of the list based on which listItem is currently focused. // Gets the "next" list item based on whether the down key or up key was pressed. // adjusts scroll value when the nextListItem is ~below~ the viewable window. // adjusts scroll value when the nextListItem is ~above~ the viewable window.