whitef0x0/tellform

View on GitHub
public/form_modules/forms/base/directives/on-enter-key.client.directive.js

Summary

Maintainability
A
1 hr
Test Coverage
'use strict';

//TODO: DAVID: Need to refactor this
angular.module('view-form').directive('onEnterKey', ['$rootScope', function($rootScope){
    return {
        restrict: 'A',
        link: function($scope, $element, $attrs) {
            $element.bind('keydown keypress', function(event) {

                var keyCode = event.which || event.keyCode;

                var onEnterKeyDisabled = false;
                if($attrs.onEnterKeyDisabled !== null) onEnterKeyDisabled = $attrs.onEnterKeyDisabled;

                if(keyCode === 13 && !event.shiftKey && !onEnterKeyDisabled) {
                    event.preventDefault();
                    $rootScope.$apply(function() {
                        $rootScope.$eval($attrs.onEnterKey);
                    });
                }
            });
        }
    };
}]).directive('onTabKey', ['$rootScope', function($rootScope){
    return {
        restrict: 'A',
        link: function($scope, $element, $attrs) {
            $element.bind('keyup keypress', function(event) {

                var keyCode = event.which || event.keyCode;

                if(keyCode === 9 && !event.shiftKey) {

                    event.preventDefault();
                    $rootScope.$apply(function() {
                        $rootScope.$eval($attrs.onTabKey);
                    });
                }
            });
        }
    };
}]).directive('onEnterOrTabKey', ['$rootScope', function($rootScope){
    return {
        restrict: 'A',
        link: function($scope, $element, $attrs) {
            $element.bind('keydown keypress', function(event) {

                var keyCode = event.which || event.keyCode;

                if((keyCode === 13 || keyCode === 9) && !event.shiftKey) {
                    event.preventDefault();
                    $rootScope.$apply(function() {
                        $rootScope.$eval($attrs.onEnterOrTabKey);
                    });
                }
            });
        }
    };
}]).directive('onTabAndShiftKey', ['$rootScope', function($rootScope){
    return {
        restrict: 'A',
        link: function($scope, $element, $attrs) {
            $element.bind('keydown keypress', function(event) {

                var keyCode = event.which || event.keyCode;

                if(keyCode === 9 && event.shiftKey) {

                    console.log('onTabAndShiftKey');
                    event.preventDefault();
                    $rootScope.$apply(function() {
                        $rootScope.$eval($attrs.onTabAndShiftKey);
                    });
                }
            });
        }
    };
}]);