# all files / Core/Components/StepInput/ StepInputDirective.js

32.14% Statements 9/28
0% Branches 0/14
0% Functions 0/3
32.14% Lines 9/28

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76

    1

    1

    1
    1
    1
    1

    1

    1

    1
     'use strict';

    // Utilities:
    var _ = require('lodash');
    var fs = require('fs');

    // Module:
    var Core = require('../../Core');

    // Dependencies:
    var camelcase = require('change-case').camel;
    require('../../Validators/ExampleNameValidator');
    require('../../Services/FileStructureService');
    require('./StepInputController');

    var StepInputDirective = function (fileStructureService) {
        return {
            restrict: 'E',

            scope: {
                model: '=',
                selected :'=',
                label: '@',
                example: '@',
            },

             /* eslint-disable no-path-concat */
            template: fs.readFileSync(__dirname + '/StepInput.html', 'utf8'),
            /* eslint-enable no-path-concat */

            link: link,

            controller: "StepInputController",
            controllerAs: 'stepInput',
            bindToController: true,

        };

        function link ($scope, $element, $attrs) {
            if (_.isUndefined($scope.stepInput.model)) {
                throw new Error('The "tractor-step-input" directive requires a "model" attribute.');
            }

            if (_.isUndefined($scope.stepInput.label)) {
                throw new Error('The "tractor-step-input" directive requires a "label" attribute.');
            }

            if (_.isUndefined($attrs.form)) {
                throw new Error('The "tractor-step-input" directive requires a "form" attribute.');
            }

            $scope.stepInput.form = $scope.$parent[$attrs.form];
            $scope.stepInput.id = Math.floor(Math.random() * Date.now());
            $scope.stepInput.property = camelcase($scope.stepInput.label);

            $scope.handleKeyDown = function (event) {
                if (event.keyCode === 40) {
                    event.preventDefault();
                    if ($scope.selectedIndex + 1 !== $scope.stepInput.items.length) {
                        $scope.selectedIndex++;
                    }
                }
                else if (event.keyCode === 38) {
                    event.preventDefault();
                    if ($scope.selectedIndex - 1 !== -1) {
                        $scope.selectedIndex--;
                    }
                }
            }
        }

    };

    Core.directive('tractorStepInput', StepInputDirective);