Add content selection before removing content
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Language / Text.js
1 /**
2 * I18n interface for wysiwyg input fields.
3 *
4 * @author Alexander Ebert
5 * @copyright 2001-2018 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Language/Text
8 */
9 define(['Core', './Input'], function (Core, LanguageInput) {
10 "use strict";
11
12 /**
13 * @exports WoltLabSuite/Core/Language/Text
14 */
15 return {
16 /**
17 * Initializes an WYSIWYG input field.
18 *
19 * @param {string} elementId input element id
20 * @param {Object} values preset values per language id
21 * @param {Object} availableLanguages language names per language id
22 * @param {boolean} forceSelection require i18n input
23 */
24 init: function(elementId, values, availableLanguages, forceSelection) {
25 var element = elById(elementId);
26 if (!element || element.nodeName !== 'TEXTAREA' || !element.classList.contains('wysiwygTextarea')) {
27 throw new Error("Expected <textarea class=\"wysiwygTextarea\" /> for id '" + elementId + "'.");
28 }
29
30 LanguageInput.init(elementId, values, availableLanguages, forceSelection);
31
32 //noinspection JSUnresolvedFunction
33 LanguageInput.registerCallback(elementId, 'select', this._callbackSelect.bind(this));
34 //noinspection JSUnresolvedFunction
35 LanguageInput.registerCallback(elementId, 'submit', this._callbackSubmit.bind(this));
36 },
37
38 /**
39 * Refreshes the editor content on language switch.
40 *
41 * @param {Element} element input element
42 * @protected
43 */
44 _callbackSelect: function (element) {
45 if (window.jQuery !== undefined) {
46 window.jQuery(element).redactor('code.set', element.value);
47 }
48 },
49
50 /**
51 * Refreshes the input element value on submit.
52 *
53 * @param {Element} element input element
54 * @protected
55 */
56 _callbackSubmit: function (element) {
57 if (window.jQuery !== undefined) {
58 element.value = window.jQuery(element).redactor('code.get');
59 }
60 }
61 }
62 });