diff --git a/services/web/public/coffee/components/inputSuggestions.coffee b/services/web/public/coffee/components/inputSuggestions.coffee new file mode 100644 index 0000000000..c5bfbbf933 --- /dev/null +++ b/services/web/public/coffee/components/inputSuggestions.coffee @@ -0,0 +1,49 @@ +define [ + "base" +], (App) -> + inputSuggestionsController = ($scope, $element, $attrs) -> + ctrl = @ + ctrl.showHint = false + ctrl.hasFocus = false + ctrl.handleFocus = () -> + ctrl.hasFocus = true + ctrl.suggestion = null + ctrl.handleBlur = () -> + ctrl.showHint = false + ctrl.hasFocus = false + ctrl.suggestion = null + ctrl.handleKeyDown = ($event) -> + if ($event.which == 9 or $event.which == 13) and ctrl.suggestion? and ctrl.suggestion != "" + $event.preventDefault() + ctrl.localNgModel += ctrl.suggestion + ctrl.suggestion = null + ctrl.showHint = false + + $scope.$watch "$ctrl.localNgModel", (newVal, oldVal) -> + if ctrl.hasFocus and newVal != oldVal + ctrl.suggestion = null + ctrl.showHint = false + ctrl.getSuggestion({ userInput: newVal }) + .then (suggestion) -> + if suggestion? and newVal == ctrl.localNgModel + ctrl.showHint = true + ctrl.suggestion = suggestion.replace newVal, "" + .catch () -> ctrl.suggestion = null + return + + App.component "inputSuggestions", { + bindings: + localNgModel: "=ngModel" + getSuggestion: "&" + inputId: "@" + inputPlaceholder: "@" + controller: inputSuggestionsController + template: """ +