SmartDirectives.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. define(['angular'], function(angular) {
  2. 'use strict';
  3. angular.module('SmartDirectives',[]).directive("focusMe", function() {
  4. return {
  5. restrict: "A",
  6. link: function(scope, element, attrs) {
  7. attrs.$observe("focusWhen", function() {
  8. if (attrs.focusWhen == "true") {
  9. element[0].focus();
  10. }
  11. });
  12. }
  13. };
  14. }).directive("enterAsTab",function(){
  15. return {
  16. restrict:"A",
  17. link:function (scope, element, attrs) {
  18. element.bind("keyup", function (event) {
  19. if(event.which === 13) {
  20. var focusable = document.getElementsByTagName('input');
  21. // Get the index of the currently focused element
  22. var currentIndex = Array.prototype.indexOf.call(focusable, event.target);
  23. // Find the next items in the list
  24. var nextIndex = currentIndex == focusable.length - 1 ? 0 : currentIndex + 1;
  25. // Focus the next element
  26. if(nextIndex >= 0 && nextIndex < focusable.length){
  27. if(focusable[nextIndex].value == 0||focusable[nextIndex].value == '')
  28. focusable[nextIndex].focus();
  29. }
  30. }
  31. });
  32. }
  33. }});
  34. });