Tims-Chat/files_wcf/js/Bastelstu.be/Chat/Ui/Input/Autocompleter.js

73 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
* Copyright (c) 2010-2018 Tim Düsterhus.
*
* Use of this software is governed by the Business Source License
* included in the LICENSE file.
*
2020-10-20 16:01:58 +00:00
* Change Date: 2024-10-20
2018-08-16 22:30:59 +00:00
*
* On the date above, in accordance with the Business Source
* License, use of this software will be governed by version 2
* or later of the General Public License.
*/
define([ 'WoltLabSuite/Core/Dom/Util'
, 'WoltLabSuite/Core/Event/Key'
, 'WoltLabSuite/Core/Ui/Suggestion'
], function (DomUtil, EventKey, Suggestion) {
"use strict";
const DEPENDENCIES = [ 'UiInput' ]
class Autocompleter extends Suggestion {
constructor(input) {
const elementId = DomUtil.identify(input.input)
const options = { callbackSelect: (() => null) }
super(elementId, options)
this.input = input
this._options.callbackSelect = this.callbackSelect.bind(this)
}
bootstrap() {
this.input.on('beforeSubmit', (event) => {
if (event.target !== this.input) return
if (this.isActive() || this.cancelNextSubmit) {
event.detail.cancel = true
}
this.cancelNextSubmit = false
})
}
_keyDown(event) {
const result = super._keyDown(event)
if (!result && EventKey.Enter(event)) {
this.cancelNextSubmit = true
}
}
_keyUp(event) {
const value = this.input.getText(true)
if (this._value !== value) {
this._ajaxSuccess({ returnValues: [] })
this._value = value
}
}
callbackSelect(_, selected) {
this.input.insertText(selected.objectId, { append: false })
}
_ajaxSuccess(...args) {
this._value = this.input.getText(true)
return super._ajaxSuccess(...args)
}
}
Autocompleter.DEPENDENCIES = DEPENDENCIES
return Autocompleter
});