/
SelectInputDirective.js
61 lines (46 loc) · 1.76 KB
/
SelectInputDirective.js
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
'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: '=',
required: '=',
as: '@'
},
/* eslint-disable no-path-concat */
template: fs.readFileSync(__dirname + '/SelectInput.html', 'utf8'),
/* eslint-enable no-path-concat */
link: link
};
function link ($scope, $element, $attrs) {
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);
});
$scope.form = $scope.$parent[$attrs.form] || $scope.$parent.$ctrl[$attrs.form];
$scope.id = Math.floor(Math.random() * Date.now());
}
function getOptionsFromProperty ($scope) {
return $scope.model[$scope.property + 's'];
}
};
Core.directive('tractorSelect', SelectInputDirective);