2018-06-15 06:27:24 -04:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
2018-06-27 07:21:26 -04:00
|
|
|
inputSuggestionsController = ($scope, $element, $attrs, Keys) ->
|
2018-06-15 06:27:24 -04:00
|
|
|
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
|
2018-06-19 12:10:02 -04:00
|
|
|
ctrl.onBlur()
|
2018-06-15 06:27:24 -04:00
|
|
|
ctrl.handleKeyDown = ($event) ->
|
2018-06-27 07:21:26 -04:00
|
|
|
if ($event.which == Keys.TAB or $event.which == Keys.ENTER) and ctrl.suggestion? and ctrl.suggestion != ""
|
2018-06-15 06:27:24 -04:00
|
|
|
$event.preventDefault()
|
|
|
|
ctrl.localNgModel += ctrl.suggestion
|
2018-06-19 12:10:02 -04:00
|
|
|
ctrl.suggestion = null
|
|
|
|
ctrl.showHint = false
|
2018-06-15 06:27:24 -04:00
|
|
|
$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"
|
2018-06-19 12:10:02 -04:00
|
|
|
localNgModelOptions: "=?ngModelOptions"
|
2018-06-15 06:27:24 -04:00
|
|
|
getSuggestion: "&"
|
2018-06-19 12:10:02 -04:00
|
|
|
onBlur: "&?"
|
|
|
|
inputId: "@?"
|
|
|
|
inputName: "@?"
|
|
|
|
inputPlaceholder: "@?"
|
|
|
|
inputType: "@?"
|
|
|
|
inputRequired: "=?"
|
2018-06-15 06:27:24 -04:00
|
|
|
controller: inputSuggestionsController
|
2018-06-27 08:47:49 -04:00
|
|
|
template: [
|
|
|
|
'<div class="input-suggestions">',
|
|
|
|
'<div class="form-control input-suggestions-shadow">',
|
|
|
|
'<span ng-bind="$ctrl.localNgModel"',
|
|
|
|
' class="input-suggestions-shadow-existing"',
|
|
|
|
' ng-show="$ctrl.showHint">',
|
|
|
|
'</span>',
|
|
|
|
'<span ng-bind="$ctrl.suggestion"',
|
|
|
|
' class="input-suggestions-shadow-suggested"',
|
|
|
|
' ng-show="$ctrl.showHint">',
|
|
|
|
'</span>',
|
|
|
|
'</div>',
|
|
|
|
'<input type="text"',
|
|
|
|
' class="form-control input-suggestions-main"',
|
|
|
|
' ng-focus="$ctrl.handleFocus()"',
|
|
|
|
' ng-keyDown="$ctrl.handleKeyDown($event)"',
|
|
|
|
' ng-blur="$ctrl.handleBlur()"',
|
|
|
|
' ng-model="$ctrl.localNgModel"',
|
|
|
|
' ng-model-options="$ctrl.localNgModelOptions"',
|
|
|
|
' ng-model-options="{ debounce: 50 }"',
|
|
|
|
' ng-attr-id="{{ ::$ctrl.inputId }}"',
|
|
|
|
' ng-attr-placeholder="{{ ::$ctrl.inputPlaceholder }}"',
|
|
|
|
' ng-attr-type="{{ ::$ctrl.inputType }}"',
|
|
|
|
' ng-attr-name="{{ ::$ctrl.inputName }}"',
|
|
|
|
' ng-required="::$ctrl.inputRequired">',
|
|
|
|
'</div>'
|
|
|
|
].join ""
|
|
|
|
|
2018-06-15 06:27:24 -04:00
|
|
|
}
|