Created
October 10, 2014 15:36
-
-
Save djazayeri/7081ce9e56862ef953f7 to your computer and use it in GitHub Desktop.
coded-or-free-text-answer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('uicommons.widget.coded-or-free-text-answer', [ 'conceptService', 'ui.bootstrap' ]) | |
.directive('codedOrFreeTextAnswer', ['ConceptService', function(ConceptService) { | |
function isExactMatch(candidate, query) { | |
query = emr.stripAccents(query.toLowerCase()); | |
return candidate.conceptName && emr.stripAccents(candidate.conceptName.name.toLowerCase()) === query; | |
} | |
return { | |
restrict: 'E', | |
scope: { | |
ngModel: '=', // the field whose value you want to set to the selected person | |
id: '@', | |
conceptClass: '@' | |
}, | |
controller: function($scope) { | |
$scope.inputId = ($scope.id ? $scope.id : 'coded-or-free-text-answer-' + Math.floor(Math.random() * 10000)) + '-input'; | |
$scope.search = function(term) { | |
var query = { q: term }; | |
if ($scope.conceptClass) { | |
query.conceptClass = $scope.conceptClass; | |
} | |
var promise = ConceptService.searchConcepts(query).then(function(results) { | |
var list = []; | |
var exactMatch = false; | |
angular.forEach(results.data, function(item) { | |
list.push(item); | |
if (!exactMatch && isExactMatch(item, query)) { | |
exactMatch = true; | |
} | |
}); | |
if (!exactMatch) { | |
list.push({ | |
concept: null, | |
conceptName: null, | |
word: query, | |
transientWeight: Number.MAX_VALUE | |
}); | |
} | |
return list; | |
}); | |
return promise; | |
}; | |
$scope.format = function(result) { | |
if (result.conceptName) { | |
// TODO | |
} | |
else if (result.concept) { | |
// TODO | |
} | |
else { | |
return result.word; | |
} | |
} | |
}, | |
template: '<input type="text" id="{{ inputId }}" ng-model="ngModel" ' + | |
'typeahead="result as format(result) for result in search($viewValue) | filter:$viewValue" ' + | |
'typeahead-editable="false" autofocus />' | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment