# all files / Core/Components/SelectInput/ SelectInputDirective.js

100% Statements 19/19
100% Branches 10/10
100% Functions 4/4
100% Lines 19/19

    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

    1

    1

    1

    1
    7

    1
    7
    1

    6
    1

    5
    5

    5
    1

    4
    4

    1
    6

    1
     'use strict';

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

    // Dependencies:
    var camelcase = require('change-case').camel;

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

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

            scope: {
                model: '=',
                label: '@',
                options: '=',
                as: '@'
            },

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

            link: link
        };

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

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

            $scope.property = camelcase($scope.label);
            $scope.selectOptions = getOptionsFromProperty($scope);

            if (_.isUndefined($scope.selectOptions) && _.isUndefined($scope.options)) {
                throw new Error('The "tractor-select" directive requires an "options" attribute, or a "label" attribute that matches a set of options on the "model".');
            }

            $scope.$watchCollection('options', function () {
                $scope.selectOptions = $scope.options || getOptionsFromProperty($scope);
            });
        }

        function getOptionsFromProperty ($scope) {
            return $scope.model[$scope.property + 's'];
        }
    };

    Core.directive('tractorSelect', SelectInputDirective);