# all files / Core/Components/VariableInput/ VariableInputDirective.js

100% Statements 18/18
100% Branches 6/6
100% Functions 2/2
100% Lines 18/18

    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

    1

    1

    1
    1

    1
    8

    1
    10
    1

    9
    1

    8
    1

    7
    7

    7
    7

    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/VariableNameValidator');

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

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

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

            link: link
        };

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

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

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

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

            $scope.isClass = Object.prototype.hasOwnProperty.call($attrs, 'isClass');
            $scope.property = camelcase($scope.label);
        }
    };

    Core.directive('tractorVariableInput', VariableInputDirective);