Skip to content

Commit

Permalink
Add keydown events for up/down arrows, just functionalities, no tests…
Browse files Browse the repository at this point in the history
… yet
  • Loading branch information
Hidenari Nozaki committed Jun 22, 2014
1 parent 3fa18e3 commit f844723
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions angucomplete-alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,26 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$parse',
scope.currentIndex = index;
};

scope.selectResult = function(result) {
if (scope.matchClass) {
result.title = result.title.toString().replace(/(<([^>]+)>)/ig, '');
}

if (scope.clearSelected) {
scope.searchStr = null;
}
else {
scope.searchStr = lastSearchTerm = result.title;
}
callOrAssign(result);
scope.showDropdown = false;
scope.results = [];
};

inputField = elem.find('input');

inputField.on('keyup', scope.keyPressed);

scope.keyPressed = function(event) {
if (!(event.which === KEY_UP || event.which === KEY_DW || event.which === KEY_EN)) {
if (!scope.searchStr || scope.searchStr === '') {
Expand All @@ -281,43 +301,26 @@ angular.module('angucomplete-alt', [] ).directive('angucompleteAlt', ['$parse',
}
};

scope.selectResult = function(result) {
if (scope.matchClass) {
result.title = result.title.toString().replace(/(<([^>]+)>)/ig, '');
}

if (scope.clearSelected) {
scope.searchStr = null;
}
else {
scope.searchStr = lastSearchTerm = result.title;
}
callOrAssign(result);
scope.showDropdown = false;
scope.results = [];
};

inputField = elem.find('input');

inputField.on('keyup', scope.keyPressed);

elem.on('keyup', function (event) {
elem.on('keydown', function (event) {
if(event.which === KEY_DW && scope.results) {
event.preventDefault();
if ((scope.currentIndex + 1) < scope.results.length) {
scope.$apply(function() {
scope.currentIndex ++;
});
event.preventDefault();
}

} else if(event.which === KEY_UP) {
} else if(event.which === KEY_UP && scope.results) {
event.preventDefault();
if (scope.currentIndex >= 1) {
scope.currentIndex --;
scope.$apply();
event.preventDefault();
scope.$apply(function() {
scope.currentIndex --;
});
}
}
});

} else if (event.which === KEY_EN && scope.results) {
elem.on('keyup', function (event) {
if (event.which === KEY_EN && scope.results) {
if (scope.currentIndex >= 0 && scope.currentIndex < scope.results.length) {
scope.selectResult(scope.results[scope.currentIndex]);
scope.$apply();
Expand Down

0 comments on commit f844723

Please sign in to comment.