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-11-02 20:29:59 +00:00
|
|
|
* Change Date: 2024-11-02
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define([
|
|
|
|
'../console',
|
|
|
|
'../Helper',
|
|
|
|
'WoltLabSuite/Core/Core',
|
|
|
|
'WoltLabSuite/Core/Event/Key',
|
|
|
|
'../DataStructure/EventEmitter',
|
|
|
|
'../DataStructure/Throttle',
|
|
|
|
], function (console, Helper, Core, EventKey, EventEmitter, Throttle) {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
class Input {
|
|
|
|
constructor() {
|
|
|
|
this.inputContainer = elById('chatInputContainer')
|
2020-11-01 16:41:19 +00:00
|
|
|
this.input = elBySel('textarea', this.inputContainer)
|
|
|
|
this.charCounter = elBySel('.charCounter', this.inputContainer)
|
|
|
|
this.errorElement = elBySel('.innerError', this.inputContainer)
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bootstrap() {
|
|
|
|
if (typeof window.elInnerError === 'function') {
|
|
|
|
elRemove(this.errorElement)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.input.addEventListener('keydown', this.handleInputKeyDown.bind(this))
|
2020-11-01 16:41:19 +00:00
|
|
|
this.input.addEventListener(
|
|
|
|
'input',
|
|
|
|
Throttle(this.handleInput.bind(this))
|
|
|
|
)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
Helper.makeFlexible(this.input)
|
|
|
|
this.handleInput()
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInput(event) {
|
2020-11-01 16:41:19 +00:00
|
|
|
this.charCounter.textContent = `${
|
|
|
|
this.input.value.length
|
|
|
|
} / ${this.input.getAttribute('maxlength')}`
|
2018-08-16 22:30:59 +00:00
|
|
|
this.emit('input')
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputKeyDown(event) {
|
|
|
|
if (EventKey.Enter(event) && !event.shiftKey) {
|
2018-08-22 17:10:10 +00:00
|
|
|
if (event.isComposing) {
|
2020-11-01 16:41:19 +00:00
|
|
|
console.debug(
|
|
|
|
'Ui/Input.handleInputKeyDown',
|
|
|
|
'Ignored Enter key while composing characters.'
|
|
|
|
)
|
2018-08-22 17:10:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 22:30:59 +00:00
|
|
|
// prevent generation of a new line
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
if (this.getText().length === 0) return
|
|
|
|
|
|
|
|
const parameters = { cancel: false, input: this }
|
|
|
|
this.emit('beforeSubmit', parameters)
|
|
|
|
if (!parameters.cancel) {
|
|
|
|
this.emit('submit')
|
|
|
|
}
|
2020-11-01 16:41:19 +00:00
|
|
|
} else if (EventKey.Tab(event)) {
|
2018-08-16 22:30:59 +00:00
|
|
|
// prevent leaving the input
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
this.emit('autocomplete')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getText(raw = false) {
|
|
|
|
if (raw) {
|
|
|
|
return this.input.value
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.input.value.trim()
|
|
|
|
}
|
|
|
|
|
|
|
|
select(start, end = undefined) {
|
|
|
|
if (end === undefined) end = this.getText(true).length
|
|
|
|
|
|
|
|
this.input.setSelectionRange(start, end)
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.input.focus()
|
|
|
|
}
|
|
|
|
|
|
|
|
insertText(text, options) {
|
|
|
|
this.focus()
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
options = Object.assign({ append: true, prepend: false }, options)
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (!(options.append || options.prepend)) {
|
|
|
|
// replace
|
|
|
|
this.input.value = text
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.append) {
|
2020-11-01 16:41:19 +00:00
|
|
|
this.input.value += text
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.prepend) {
|
2020-11-01 16:41:19 +00:00
|
|
|
this.input.value = text + this.input.value
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// always position caret at the end
|
|
|
|
const length = this.input.value.length
|
|
|
|
this.input.setSelectionRange(length, length)
|
|
|
|
|
|
|
|
Core.triggerEvent(this.input, 'input')
|
|
|
|
}
|
|
|
|
|
|
|
|
inputError(message) {
|
|
|
|
if (typeof window.elInnerError === 'function') {
|
|
|
|
elInnerError(this.inputContainer.firstElementChild, message)
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
this.inputContainer.classList.add('formError')
|
|
|
|
this.errorElement.textContent = message
|
|
|
|
elShow(this.errorElement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hideInputError() {
|
|
|
|
if (typeof window.elInnerError === 'function') {
|
|
|
|
elInnerError(this.inputContainer.firstElementChild, false)
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
this.inputContainer.classList.remove('formError')
|
|
|
|
this.errorElement.textContent = ''
|
|
|
|
elHide(this.errorElement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EventEmitter(Input.prototype)
|
|
|
|
|
|
|
|
return Input
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|