/
select-concept-from-list.js
61 lines (55 loc) · 2.64 KB
/
select-concept-from-list.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
angular.module('uicommons.widget.select-concept-from-list', [ 'ui.bootstrap' ])
.directive('selectConceptFromList', [ '$timeout', function($timeout) {
return {
restrict: 'E',
scope: {
ngModel: '=',
id: '@',
locale: '@',
placeholder: '@',
requiredIf: '=',
concepts: '&'
},
link: function($scope, element, attrs) {
$scope.required = attrs.hasOwnProperty('required'); // required attribute has no value
$scope.inputId = emr.domId($scope.id, 'sel-concept', 'input');
$scope.size = attrs.size ? attrs.size : 40;
var options = [];
_.each($scope.concepts(), function(concept) {
_.each(concept.names, function(name) {
if (emr.isCompatibleWithSessionLocale(name.locale)) {
var display = name.name === concept.display ?
name.name :
name.name + " → " + concept.display;
options.push({
display: display,
searchOn: name.name,
concept: concept
});
}
});
});
$scope.options = _.sortBy(options, function(item) {
return item.display.length;
});
$scope.verify = function() {
if(!$scope.ngModel) {
angular.element('#'+$scope.inputId).val('');
}
}
$scope.startsWith = function(actual, expected) {
return emr.startsWithIgnoreCase(actual, expected);
}
$scope.onSelect = function($item, $model, $label) {
$timeout(function() {
emr.focusNextElement(element.closest('body'), element.find('#'+$scope.inputId));
}, 10);
}
},
template: '<input type="text" id="{{ inputId }}" ng-model="ngModel" ng-blur="verify()" ' +
'ng-required="{{ required || requiredIf }}" size="{{ size }}" ' +
'typeahead-on-select="onSelect($item, $model, $label)" ' +
'typeahead="opt.concept as opt.display for opt in options | filter:{searchOn:$viewValue}:startsWith" ' +
'typeahead-editable="false" autocomplete="off" placeholder="{{ placeholder }}" />'
};
}]);