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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-08-16 22:30:59 +00:00
/*
2021-02-04 22:04:35 +00:00
* Copyright (c) 2010-2021 Tim Düsterhus.
2018-08-16 22:30:59 +00:00
*
* Use of this software is governed by the Business Source License
* included in the LICENSE file.
*
2021-02-04 22:04:35 +00:00
* Change Date: 2025-02-04
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'
2020-11-01 16:41:19 +00:00
2018-08-16 22:30:59 +00:00
const DEPENDENCIES = ['UiInput']
class Autocompleter extends Suggestion {
constructor(input) {
const elementId = DomUtil.identify(input.input)
const options = {
callbackSelect: (_elementId, selection) =>
this.insertSelection(selection),
}
2018-08-16 22:30:59 +00:00
super(elementId, options)
this.input = input
this.completions = new Map()
this.completionId = 0
2018-08-16 22:30:59 +00:00
}
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(...args) {
return this._keyDown(...args)
}
2018-08-16 22:30:59 +00:00
_keyDown(event) {
const result = (super.keyDown || super._keyDown).call(this, event)
2018-08-16 22:30:59 +00:00
if (!result && EventKey.Enter(event)) {
this.cancelNextSubmit = true
}
}
keyUp(...args) {
return this._keyUp(...args)
}
2018-08-16 22:30:59 +00:00
_keyUp(event) {
const value = this.input.getText(true)
if (this._value !== value) {
this.sendCompletions([])
2018-08-16 22:30:59 +00:00
this._value = value
}
}
insertSelection(selection) {
let text
if ((text = this.completions.get(parseInt(selection.objectId, 10)))) {
this.input.insertText(text, { append: false })
}
2018-08-16 22:30:59 +00:00
}
sendCompletions(completions) {
this.completions = new Map()
const returnValues = completions.map((completion) => {
this.completions.set(++this.completionId, completion)
return { label: completion, objectID: this.completionId }
})
this._ajaxSuccess({ returnValues })
2018-08-16 22:30:59 +00:00
}
}
Autocompleter.DEPENDENCIES = DEPENDENCIES
return Autocompleter
})