inputSuggestions.js 3.1 KB

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