inputSuggestions.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import App from '../base'
  14. const inputSuggestionsController = function($scope, $element, $attrs, Keys) {
  15. const ctrl = this
  16. ctrl.showHint = false
  17. ctrl.hasFocus = false
  18. ctrl.handleFocus = function() {
  19. ctrl.hasFocus = true
  20. return (ctrl.suggestion = null)
  21. }
  22. ctrl.handleBlur = function() {
  23. ctrl.showHint = false
  24. ctrl.hasFocus = false
  25. ctrl.suggestion = null
  26. return ctrl.onBlur()
  27. }
  28. ctrl.handleKeyDown = function($event) {
  29. if (
  30. ($event.which === Keys.TAB || $event.which === Keys.ENTER) &&
  31. ctrl.suggestion != null &&
  32. ctrl.suggestion !== ''
  33. ) {
  34. $event.preventDefault()
  35. ctrl.localNgModel += ctrl.suggestion
  36. }
  37. ctrl.suggestion = null
  38. return (ctrl.showHint = false)
  39. }
  40. $scope.$watch('$ctrl.localNgModel', function(newVal, oldVal) {
  41. if (ctrl.hasFocus && newVal !== oldVal) {
  42. ctrl.suggestion = null
  43. ctrl.showHint = false
  44. return ctrl
  45. .getSuggestion({ userInput: newVal })
  46. .then(function(suggestion) {
  47. if (suggestion != null && newVal === ctrl.localNgModel) {
  48. ctrl.showHint = true
  49. return (ctrl.suggestion = suggestion.replace(newVal, ''))
  50. }
  51. })
  52. .catch(() => (ctrl.suggestion = null))
  53. }
  54. })
  55. }
  56. export default App.component('inputSuggestions', {
  57. bindings: {
  58. localNgModel: '=ngModel',
  59. localNgModelOptions: '=?ngModelOptions',
  60. getSuggestion: '&',
  61. onBlur: '&?',
  62. inputId: '@?',
  63. inputName: '@?',
  64. inputPlaceholder: '@?',
  65. inputType: '@?',
  66. inputRequired: '=?'
  67. },
  68. controller: inputSuggestionsController,
  69. template: [
  70. '<div class="input-suggestions">',
  71. '<div class="form-control input-suggestions-shadow">',
  72. '<span ng-bind="$ctrl.localNgModel"',
  73. ' class="input-suggestions-shadow-existing"',
  74. ' ng-show="$ctrl.showHint">',
  75. '</span>',
  76. '<span ng-bind="$ctrl.suggestion"',
  77. ' class="input-suggestions-shadow-suggested"',
  78. ' ng-show="$ctrl.showHint">',
  79. '</span>',
  80. '</div>',
  81. '<input type="text"',
  82. ' class="form-control input-suggestions-main"',
  83. ' ng-focus="$ctrl.handleFocus()"',
  84. ' ng-keyDown="$ctrl.handleKeyDown($event)"',
  85. ' ng-blur="$ctrl.handleBlur()"',
  86. ' ng-model="$ctrl.localNgModel"',
  87. ' ng-model-options="$ctrl.localNgModelOptions"',
  88. ' ng-model-options="{ debounce: 50 }"',
  89. ' ng-attr-id="{{ ::$ctrl.inputId }}"',
  90. ' ng-attr-placeholder="{{ ::$ctrl.inputPlaceholder }}"',
  91. ' ng-attr-type="{{ ::$ctrl.inputType }}"',
  92. ' ng-attr-name="{{ ::$ctrl.inputName }}"',
  93. ' ng-required="::$ctrl.inputRequired">',
  94. '</div>'
  95. ].join('')
  96. })