Created
February 25, 2017 11:34
-
-
Save jiaming0708/acdc466842b6f7332455848145535b75 to your computer and use it in GitHub Desktop.
angularJS directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('directive', []) | |
.directive('enterToTab', function () { | |
return { | |
restrict: 'A', | |
scope: { | |
afterEvent: '&afterEnter', | |
beforeEvent: '&keydown' | |
}, | |
link: function (scope, element, attrs) { | |
element.bind('keydown', function (event) { | |
//call event before enter | |
scope.beforeEvent({ 'event': event }); | |
//we only process "enter" | |
if (event.which != 13) | |
return; | |
//first, the element will lose focus | |
element[0].blur(); | |
//find all element have attribute | |
var lstElement = document.querySelectorAll('[enter-to-tab]'); | |
//it will focus to next element, when we find more than one directive | |
if (lstElement.length > 1) { | |
//current element finds index | |
var index = -1; | |
for (var idx in lstElement) { | |
if (lstElement[idx] === element[0]) { | |
index = idx; | |
break; | |
} | |
} | |
//index is last element, move to first element | |
if (++index === lstElement.length) { | |
index = 0; | |
} | |
//select all text | |
lstElement[index].select(); | |
} | |
//call event after enter | |
scope.afterEvent({ 'event': event }); | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment