Convert `Language/Text` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Sun, 8 Nov 2020 18:04:00 +0000 (19:04 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 8 Nov 2020 18:04:00 +0000 (19:04 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Language/Text.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Language/Input.ts
wcfsetup/install/files/ts/WoltLabSuite/Core/Language/Text.js [deleted file]
wcfsetup/install/files/ts/WoltLabSuite/Core/Language/Text.ts [new file with mode: 0644]

index b3388226cb65696737d233d344e0d2c0eae50db1..7672312e539c5555bdfba7b532f7e3d6dd24fc52 100644 (file)
@@ -2,56 +2,42 @@
  * I18n interface for wysiwyg input fields.
  *
  * @author      Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
+ * @copyright  2001-2019 WoltLab GmbH
  * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @module      WoltLabSuite/Core/Language/Text
  */
-define(['Core', './Input'], function (Core, LanguageInput) {
+define(["require", "exports", "tslib", "./Input"], function (require, exports, tslib_1, LanguageInput) {
     "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.init = void 0;
+    LanguageInput = tslib_1.__importStar(LanguageInput);
     /**
-     * @exports     WoltLabSuite/Core/Language/Text
+     * Refreshes the editor content on language switch.
      */
-    return {
-        /**
-         * Initializes an WYSIWYG input field.
-         *
-         * @param       {string}        elementId               input element id
-         * @param       {Object}        values                  preset values per language id
-         * @param       {Object}        availableLanguages      language names per language id
-         * @param       {boolean}       forceSelection          require i18n input
-         */
-        init: function (elementId, values, availableLanguages, forceSelection) {
-            var element = elById(elementId);
-            if (!element || element.nodeName !== 'TEXTAREA' || !element.classList.contains('wysiwygTextarea')) {
-                throw new Error("Expected <textarea class=\"wysiwygTextarea\" /> for id '" + elementId + "'.");
-            }
-            LanguageInput.init(elementId, values, availableLanguages, forceSelection);
-            //noinspection JSUnresolvedFunction
-            LanguageInput.registerCallback(elementId, 'select', this._callbackSelect.bind(this));
-            //noinspection JSUnresolvedFunction
-            LanguageInput.registerCallback(elementId, 'submit', this._callbackSubmit.bind(this));
-        },
-        /**
-         * Refreshes the editor content on language switch.
-         *
-         * @param       {Element}       element         input element
-         * @protected
-         */
-        _callbackSelect: function (element) {
-            if (window.jQuery !== undefined) {
-                window.jQuery(element).redactor('code.set', element.value);
-            }
-        },
-        /**
-         * Refreshes the input element value on submit.
-         *
-         * @param       {Element}       element         input element
-         * @protected
-         */
-        _callbackSubmit: function (element) {
-            if (window.jQuery !== undefined) {
-                element.value = window.jQuery(element).redactor('code.get');
-            }
+    function callbackSelect(element) {
+        if (window.jQuery !== undefined) {
+            window.jQuery(element).redactor("code.set", element.value);
         }
-    };
+    }
+    /**
+     * Refreshes the input element value on submit.
+     */
+    function callbackSubmit(element) {
+        if (window.jQuery !== undefined) {
+            element.value = window.jQuery(element).redactor("code.get");
+        }
+    }
+    /**
+     * Initializes an WYSIWYG input field.
+     */
+    function init(elementId, values, availableLanguages, forceSelection) {
+        const element = document.getElementById(elementId);
+        if (!element || element.nodeName !== "TEXTAREA" || !element.classList.contains("wysiwygTextarea")) {
+            throw new Error(`Expected <textarea class="wysiwygTextarea" /> for id '${elementId}'.`);
+        }
+        LanguageInput.init(elementId, values, availableLanguages, forceSelection);
+        LanguageInput.registerCallback(elementId, "select", callbackSelect);
+        LanguageInput.registerCallback(elementId, "submit", callbackSubmit);
+    }
+    exports.init = init;
 });
index dd56ae193c81fd583b28c0e118939a8d09d0aa10..7c414312f725b4e3a9ae55257e3a42dbea28e9e4 100644 (file)
@@ -15,19 +15,19 @@ import * as StringUtil from "../StringUtil";
 
 type LanguageId = number;
 
-interface I18nValues {
+export interface I18nValues {
   // languageID => value
   [key: string]: string;
 }
 
-interface Languages {
+export interface Languages {
   // languageID => languageName
   [key: string]: string;
 }
 
 type Values = Map<LanguageId, string>;
 
-type InputOrTextarea = HTMLInputElement | HTMLTextAreaElement;
+export type InputOrTextarea = HTMLInputElement | HTMLTextAreaElement;
 
 type CallbackEvent = "select" | "submit";
 type Callback = (element: InputOrTextarea) => void;
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Language/Text.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Language/Text.js
deleted file mode 100644 (file)
index d263e68..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * I18n interface for wysiwyg input fields.
- * 
- * @author      Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module      WoltLabSuite/Core/Language/Text
- */
-define(['Core', './Input'], function (Core, LanguageInput) {
-       "use strict";
-       
-       /**
-        * @exports     WoltLabSuite/Core/Language/Text
-        */
-       return {
-               /**
-                * Initializes an WYSIWYG input field.
-                * 
-                * @param       {string}        elementId               input element id
-                * @param       {Object}        values                  preset values per language id
-                * @param       {Object}        availableLanguages      language names per language id
-                * @param       {boolean}       forceSelection          require i18n input
-                */
-               init: function(elementId, values, availableLanguages, forceSelection) {
-                       var element = elById(elementId);
-                       if (!element || element.nodeName !== 'TEXTAREA' || !element.classList.contains('wysiwygTextarea')) {
-                               throw new Error("Expected <textarea class=\"wysiwygTextarea\" /> for id '" + elementId + "'.");
-                       }
-                       
-                       LanguageInput.init(elementId, values, availableLanguages, forceSelection);
-                       
-                       //noinspection JSUnresolvedFunction
-                       LanguageInput.registerCallback(elementId, 'select', this._callbackSelect.bind(this));
-                       //noinspection JSUnresolvedFunction
-                       LanguageInput.registerCallback(elementId, 'submit', this._callbackSubmit.bind(this));
-               },
-               
-               /**
-                * Refreshes the editor content on language switch.
-                * 
-                * @param       {Element}       element         input element
-                * @protected
-                */
-               _callbackSelect: function (element) {
-                       if (window.jQuery !== undefined) {
-                               window.jQuery(element).redactor('code.set', element.value);
-                       }
-               },
-               
-               /**
-                * Refreshes the input element value on submit.
-                * 
-                * @param       {Element}       element         input element
-                * @protected
-                */
-               _callbackSubmit: function (element) {
-                       if (window.jQuery !== undefined) {
-                               element.value = window.jQuery(element).redactor('code.get');
-                       }
-               }
-       }
-});
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Language/Text.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Language/Text.ts
new file mode 100644 (file)
index 0000000..3070681
--- /dev/null
@@ -0,0 +1,49 @@
+/**
+ * I18n interface for wysiwyg input fields.
+ *
+ * @author      Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module      WoltLabSuite/Core/Language/Text
+ */
+
+import { I18nValues, InputOrTextarea, Languages } from "./Input";
+import * as LanguageInput from "./Input";
+
+/**
+ * Refreshes the editor content on language switch.
+ */
+function callbackSelect(element: InputOrTextarea): void {
+  if (window.jQuery !== undefined) {
+    window.jQuery(element).redactor("code.set", element.value);
+  }
+}
+
+/**
+ * Refreshes the input element value on submit.
+ */
+function callbackSubmit(element: InputOrTextarea): void {
+  if (window.jQuery !== undefined) {
+    element.value = window.jQuery(element).redactor("code.get") as string;
+  }
+}
+
+/**
+ * Initializes an WYSIWYG input field.
+ */
+export function init(
+  elementId: string,
+  values: I18nValues,
+  availableLanguages: Languages,
+  forceSelection: boolean,
+): void {
+  const element = document.getElementById(elementId);
+  if (!element || element.nodeName !== "TEXTAREA" || !element.classList.contains("wysiwygTextarea")) {
+    throw new Error(`Expected <textarea class="wysiwygTextarea" /> for id '${elementId}'.`);
+  }
+
+  LanguageInput.init(elementId, values, availableLanguages, forceSelection);
+
+  LanguageInput.registerCallback(elementId, "select", callbackSelect);
+  LanguageInput.registerCallback(elementId, "submit", callbackSubmit);
+}