Apply code suggestions for form builder TS code
authorMatthias Schmidt <gravatronics@live.com>
Tue, 8 Dec 2020 17:21:46 +0000 (18:21 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 15 Dec 2020 17:23:04 +0000 (18:23 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Dialog.js
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Manager.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Dialog.ts
wcfsetup/install/files/ts/WoltLabSuite/Core/Form/Builder/Manager.ts

index 27351c1d9b196a594cab6c62d2d20cae984dbbee..7e89def38442ca4edbf163ada9a0b62ebcac67e9 100644 (file)
@@ -7,12 +7,12 @@
  * @module  WoltLabSuite/Core/Form/Builder/Dialog
  * @since 5.2
  */
-define(["require", "exports", "tslib", "../../Core", "../../Ui/Dialog", "../../Ajax", "./Manager"], function (require, exports, tslib_1, Core, Dialog_1, Ajax, Manager_1) {
+define(["require", "exports", "tslib", "../../Core", "../../Ui/Dialog", "../../Ajax", "./Manager"], function (require, exports, tslib_1, Core, Dialog_1, Ajax, FormBuilderManager) {
     "use strict";
     Core = tslib_1.__importStar(Core);
     Dialog_1 = tslib_1.__importDefault(Dialog_1);
     Ajax = tslib_1.__importStar(Ajax);
-    Manager_1 = tslib_1.__importDefault(Manager_1);
+    FormBuilderManager = tslib_1.__importStar(FormBuilderManager);
     class FormBuilderDialog {
         constructor(dialogId, className, actionName, options) {
             this.init(dialogId, className, actionName, options);
@@ -24,10 +24,10 @@ define(["require", "exports", "tslib", "../../Core", "../../Ui/Dialog", "../../A
             this._options = Core.extend({
                 actionParameters: {},
                 destroyOnClose: false,
-                usesDboAction: new RegExp(/\w+\\data\\/).test(this._className),
+                usesDboAction: /\w+\\data\\/.test(this._className),
             }, options);
             this._options.dialog = Core.extend(this._options.dialog || {}, {
-                onClose: this._dialogOnClose.bind(this),
+                onClose: () => this._dialogOnClose(),
             });
             this._formId = "";
             this._dialogContent = "";
@@ -155,8 +155,8 @@ define(["require", "exports", "tslib", "../../Core", "../../Ui/Dialog", "../../A
          */
         destroy(ignoreDialog = false) {
             if (this._formId !== "") {
-                if (Manager_1.default.hasForm(this._formId)) {
-                    Manager_1.default.unregisterForm(this._formId);
+                if (FormBuilderManager.hasForm(this._formId)) {
+                    FormBuilderManager.unregisterForm(this._formId);
                 }
                 if (ignoreDialog !== true) {
                     Dialog_1.default.destroy(this);
@@ -170,7 +170,7 @@ define(["require", "exports", "tslib", "../../Core", "../../Ui/Dialog", "../../A
             if (this._formId === "") {
                 throw new Error("Form has not been requested yet.");
             }
-            return Manager_1.default.getData(this._formId);
+            return FormBuilderManager.getData(this._formId);
         }
         /**
          * Opens the dialog form.
index eef3fa8213bad76fb3e120a683554baf58537622..927c9ff8be934e64b486243ac71d2eeb47560c08 100644 (file)
  */
 define(["require", "exports", "tslib", "../../Core", "../../Event/Handler", "./Field/Field", "./Field/Dependency/Manager"], function (require, exports, tslib_1, Core, EventHandler, Field_1, Manager_1) {
     "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.unregisterForm = exports.registerForm = exports.registerField = exports.hasForm = exports.hasField = exports.getForm = exports.getField = exports.getData = void 0;
     Core = tslib_1.__importStar(Core);
     EventHandler = tslib_1.__importStar(EventHandler);
     Field_1 = tslib_1.__importDefault(Field_1);
     Manager_1 = tslib_1.__importDefault(Manager_1);
     const _fields = new Map();
     const _forms = new Map();
-    const FormBuilderManager = {
-        /**
-         * Returns a promise returning the data of the form with the given id.
-         */
-        getData(formId) {
-            if (!this.hasForm(formId)) {
-                throw new Error("Unknown form with id '" + formId + "'.");
+    /**
+     * Returns a promise returning the data of the form with the given id.
+     */
+    function getData(formId) {
+        if (!hasForm(formId)) {
+            throw new Error("Unknown form with id '" + formId + "'.");
+        }
+        const promises = [];
+        _fields.get(formId).forEach(function (field) {
+            const fieldData = field.getData();
+            if (!(fieldData instanceof Promise)) {
+                throw new TypeError("Data for field with id '" + field.getId() + "' is no promise.");
             }
-            const promises = [];
-            _fields.get(formId).forEach(function (field) {
-                const fieldData = field.getData();
-                if (!(fieldData instanceof Promise)) {
-                    throw new TypeError("Data for field with id '" + field.getId() + "' is no promise.");
-                }
-                promises.push(fieldData);
-            });
-            return Promise.all(promises).then(function (promiseData) {
-                let data = {};
-                promiseData.forEach((_data) => {
-                    data = Core.extend(data, _data);
-                });
-                return data;
-            });
-        },
-        /**
-         * Returns the registered form field with given.
-         *
-         * @since 5.2.3
-         */
-        getField(formId, fieldId) {
-            if (!this.hasField(formId, fieldId)) {
-                throw new Error("Unknown field with id '" + formId + "' for form with id '" + fieldId + "'.");
-            }
-            return _fields.get(formId).get(fieldId);
-        },
-        /**
-         * Returns the registered form with given id.
-         */
-        getForm(formId) {
-            if (!this.hasForm(formId)) {
-                throw new Error("Unknown form with id '" + formId + "'.");
-            }
-            return _forms.get(formId);
-        },
-        /**
-         * Returns `true` if a field with the given id has been registered for the form with the given id
-         * and `false` otherwise.
-         */
-        hasField(formId, fieldId) {
-            if (!this.hasForm(formId)) {
-                throw new Error("Unknown form with id '" + formId + "'.");
-            }
-            return _fields.get(formId).has(fieldId);
-        },
-        /**
-         * Returns `true` if a form with the given id has been registered and `false` otherwise.
-         */
-        hasForm(formId) {
-            return _forms.has(formId);
-        },
-        /**
-         * Registers the given field for the form with the given id.
-         */
-        registerField(formId, field) {
-            if (!this.hasForm(formId)) {
-                throw new Error("Unknown form with id '" + formId + "'.");
-            }
-            if (!(field instanceof Field_1.default)) {
-                throw new Error("Add field is no instance of 'WoltLabSuite/Core/Form/Builder/Field/Field'.");
-            }
-            const fieldId = field.getId();
-            if (this.hasField(formId, fieldId)) {
-                throw new Error("Form field with id '" + fieldId + "' has already been registered for form with id '" + formId + "'.");
-            }
-            _fields.get(formId).set(fieldId, field);
-            EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerField", {
-                field: field,
-                formId: formId,
-            });
-        },
-        /**
-         * Registers the form with the given id.
-         */
-        registerForm(formId) {
-            if (this.hasForm(formId)) {
-                throw new Error("Form with id '" + formId + "' has already been registered.");
-            }
-            const form = document.getElementById(formId);
-            if (form === null) {
-                throw new Error("Unknown form with id '" + formId + "'.");
-            }
-            _forms.set(formId, form);
-            _fields.set(formId, new Map());
-            EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerForm", {
-                formId: formId,
-            });
-        },
-        /**
-         * Unregisters the form with the given id.
-         */
-        unregisterForm: function (formId) {
-            if (!this.hasForm(formId)) {
-                throw new Error("Unknown form with id '" + formId + "'.");
-            }
-            EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "beforeUnregisterForm", {
-                formId: formId,
-            });
-            _forms.delete(formId);
-            _fields.get(formId).forEach(function (field) {
-                field.destroy();
-            });
-            _fields.delete(formId);
-            Manager_1.default.unregister(formId);
-            EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "afterUnregisterForm", {
-                formId: formId,
-            });
-        },
-    };
-    Core.enableLegacyInheritance(FormBuilderManager);
-    return FormBuilderManager;
+            promises.push(fieldData);
+        });
+        return Promise.all(promises).then(function (promiseData) {
+            return promiseData.reduce((carry, current) => Core.extend(carry, current), {});
+        });
+    }
+    exports.getData = getData;
+    /**
+     * Returns the registered form field with given.
+     *
+     * @since 5.2.3
+     */
+    function getField(formId, fieldId) {
+        if (!hasField(formId, fieldId)) {
+            throw new Error("Unknown field with id '" + formId + "' for form with id '" + fieldId + "'.");
+        }
+        return _fields.get(formId).get(fieldId);
+    }
+    exports.getField = getField;
+    /**
+     * Returns the registered form with given id.
+     */
+    function getForm(formId) {
+        if (!hasForm(formId)) {
+            throw new Error("Unknown form with id '" + formId + "'.");
+        }
+        return _forms.get(formId);
+    }
+    exports.getForm = getForm;
+    /**
+     * Returns `true` if a field with the given id has been registered for the form with the given id
+     * and `false` otherwise.
+     */
+    function hasField(formId, fieldId) {
+        if (!hasForm(formId)) {
+            throw new Error("Unknown form with id '" + formId + "'.");
+        }
+        return _fields.get(formId).has(fieldId);
+    }
+    exports.hasField = hasField;
+    /**
+     * Returns `true` if a form with the given id has been registered and `false` otherwise.
+     */
+    function hasForm(formId) {
+        return _forms.has(formId);
+    }
+    exports.hasForm = hasForm;
+    /**
+     * Registers the given field for the form with the given id.
+     */
+    function registerField(formId, field) {
+        if (!hasForm(formId)) {
+            throw new Error("Unknown form with id '" + formId + "'.");
+        }
+        if (!(field instanceof Field_1.default)) {
+            throw new Error("Add field is no instance of 'WoltLabSuite/Core/Form/Builder/Field/Field'.");
+        }
+        const fieldId = field.getId();
+        if (hasField(formId, fieldId)) {
+            throw new Error("Form field with id '" + fieldId + "' has already been registered for form with id '" + formId + "'.");
+        }
+        _fields.get(formId).set(fieldId, field);
+        EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerField", {
+            field: field,
+            formId: formId,
+        });
+    }
+    exports.registerField = registerField;
+    /**
+     * Registers the form with the given id.
+     */
+    function registerForm(formId) {
+        if (hasForm(formId)) {
+            throw new Error("Form with id '" + formId + "' has already been registered.");
+        }
+        const form = document.getElementById(formId);
+        if (form === null) {
+            throw new Error("Unknown form with id '" + formId + "'.");
+        }
+        _forms.set(formId, form);
+        _fields.set(formId, new Map());
+        EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerForm", {
+            formId: formId,
+        });
+    }
+    exports.registerForm = registerForm;
+    /**
+     * Unregisters the form with the given id.
+     */
+    function unregisterForm(formId) {
+        if (!hasForm(formId)) {
+            throw new Error("Unknown form with id '" + formId + "'.");
+        }
+        EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "beforeUnregisterForm", {
+            formId: formId,
+        });
+        _forms.delete(formId);
+        _fields.get(formId).forEach(function (field) {
+            field.destroy();
+        });
+        _fields.delete(formId);
+        Manager_1.default.unregister(formId);
+        EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "afterUnregisterForm", {
+            formId: formId,
+        });
+    }
+    exports.unregisterForm = unregisterForm;
 });
index 1fe3a786fe21129f0cad904638f56bec84967085..f7664674a2ce4a81b548dc3a89079649966be814 100644 (file)
@@ -13,7 +13,7 @@ import UiDialog from "../../Ui/Dialog";
 import { DialogCallbackObject, DialogCallbackSetup, DialogData } from "../../Ui/Dialog/Data";
 import * as Ajax from "../../Ajax";
 import { AjaxCallbackObject, AjaxCallbackSetup, DatabaseObjectActionResponse, RequestOptions } from "../../Ajax/Data";
-import FormBuilderManager from "./Manager";
+import * as FormBuilderManager from "./Manager";
 import { FormBuilderData, FormBuilderDialogOptions } from "./Data";
 
 interface AjaxResponseReturnValues {
@@ -46,12 +46,12 @@ class FormBuilderDialog implements AjaxCallbackObject, DialogCallbackObject {
       {
         actionParameters: {},
         destroyOnClose: false,
-        usesDboAction: new RegExp(/\w+\\data\\/).test(this._className),
+        usesDboAction: /\w+\\data\\/.test(this._className),
       },
       options,
     ) as FormBuilderDialogOptions;
     this._options.dialog = Core.extend(this._options.dialog || {}, {
-      onClose: this._dialogOnClose.bind(this),
+      onClose: () => this._dialogOnClose(),
     });
 
     this._formId = "";
index 47d20f39feba5ddd613cf771f233c213e64e77ab..e498cf9a87ceb595568e22c679fc4bf104982b27 100644 (file)
@@ -15,161 +15,152 @@ import Field from "./Field/Field";
 import DependencyManager from "./Field/Dependency/Manager";
 import { FormBuilderData } from "./Data";
 
-const _fields = new Map<string, Map<string, Field>>();
-const _forms = new Map<string, HTMLElement>();
-
-const FormBuilderManager = {
-  /**
-   * Returns a promise returning the data of the form with the given id.
-   */
-  getData(formId: string): Promise<FormBuilderData> {
-    if (!this.hasForm(formId)) {
-      throw new Error("Unknown form with id '" + formId + "'.");
-    }
+type FormId = string;
+type FieldId = string;
 
-    const promises: Promise<FormBuilderData>[] = [];
+const _fields = new Map<FormId, Map<FieldId, Field>>();
+const _forms = new Map<FormId, HTMLElement>();
 
-    _fields.get(formId)!.forEach(function (field) {
-      const fieldData = field.getData();
+/**
+ * Returns a promise returning the data of the form with the given id.
+ */
+export function getData(formId: FieldId): Promise<FormBuilderData> {
+  if (!hasForm(formId)) {
+    throw new Error("Unknown form with id '" + formId + "'.");
+  }
 
-      if (!(fieldData instanceof Promise)) {
-        throw new TypeError("Data for field with id '" + field.getId() + "' is no promise.");
-      }
+  const promises: Promise<FormBuilderData>[] = [];
 
-      promises.push(fieldData);
-    });
+  _fields.get(formId)!.forEach(function (field) {
+    const fieldData = field.getData();
 
-    return Promise.all(promises).then(function (promiseData: FormBuilderData[]) {
-      let data = {};
+    if (!(fieldData instanceof Promise)) {
+      throw new TypeError("Data for field with id '" + field.getId() + "' is no promise.");
+    }
 
-      promiseData.forEach((_data) => {
-        data = Core.extend(data, _data);
-      });
+    promises.push(fieldData);
+  });
 
-      return data;
-    });
-  },
+  return Promise.all(promises).then(function (promiseData: FormBuilderData[]) {
+    return promiseData.reduce((carry, current) => Core.extend(carry, current), {});
+  });
+}
 
-  /**
  * Returns the registered form field with given.
  *
  * @since 5.2.3
  */
-  getField(formId: string, fieldId: string): Field {
-    if (!this.hasField(formId, fieldId)) {
-      throw new Error("Unknown field with id '" + formId + "' for form with id '" + fieldId + "'.");
-    }
+/**
+ * Returns the registered form field with given.
+ *
+ * @since 5.2.3
+ */
+export function getField(formId: FieldId, fieldId: FieldId): Field {
+  if (!hasField(formId, fieldId)) {
+    throw new Error("Unknown field with id '" + formId + "' for form with id '" + fieldId + "'.");
+  }
 
-    return _fields.get(formId)!.get(fieldId)!;
-  },
+  return _fields.get(formId)!.get(fieldId)!;
+}
 
-  /**
  * Returns the registered form with given id.
  */
-  getForm(formId: string): HTMLElement {
-    if (!this.hasForm(formId)) {
-      throw new Error("Unknown form with id '" + formId + "'.");
-    }
+/**
+ * Returns the registered form with given id.
+ */
+export function getForm(formId: FieldId): HTMLElement {
+  if (!hasForm(formId)) {
+    throw new Error("Unknown form with id '" + formId + "'.");
+  }
 
-    return _forms.get(formId)!;
-  },
+  return _forms.get(formId)!;
+}
 
-  /**
  * Returns `true` if a field with the given id has been registered for the form with the given id
  * and `false` otherwise.
  */
-  hasField(formId: string, fieldId: string): boolean {
-    if (!this.hasForm(formId)) {
-      throw new Error("Unknown form with id '" + formId + "'.");
-    }
+/**
+ * Returns `true` if a field with the given id has been registered for the form with the given id
+ * and `false` otherwise.
+ */
+export function hasField(formId: FieldId, fieldId: FieldId): boolean {
+  if (!hasForm(formId)) {
+    throw new Error("Unknown form with id '" + formId + "'.");
+  }
 
-    return _fields.get(formId)!.has(fieldId);
-  },
-
-  /**
-   * Returns `true` if a form with the given id has been registered and `false` otherwise.
-   */
-  hasForm(formId): boolean {
-    return _forms.has(formId);
-  },
-
-  /**
-   * Registers the given field for the form with the given id.
-   */
-  registerField(formId: string, field: Field): void {
-    if (!this.hasForm(formId)) {
-      throw new Error("Unknown form with id '" + formId + "'.");
-    }
+  return _fields.get(formId)!.has(fieldId);
+}
 
-    if (!(field instanceof Field)) {
-      throw new Error("Add field is no instance of 'WoltLabSuite/Core/Form/Builder/Field/Field'.");
-    }
+/**
+ * Returns `true` if a form with the given id has been registered and `false` otherwise.
+ */
+export function hasForm(formId: FieldId): boolean {
+  return _forms.has(formId);
+}
 
-    const fieldId = field.getId();
+/**
+ * Registers the given field for the form with the given id.
+ */
+export function registerField(formId: FieldId, field: Field): void {
+  if (!hasForm(formId)) {
+    throw new Error("Unknown form with id '" + formId + "'.");
+  }
 
-    if (this.hasField(formId, fieldId)) {
-      throw new Error(
-        "Form field with id '" + fieldId + "' has already been registered for form with id '" + formId + "'.",
-      );
-    }
+  if (!(field instanceof Field)) {
+    throw new Error("Add field is no instance of 'WoltLabSuite/Core/Form/Builder/Field/Field'.");
+  }
 
-    _fields.get(formId)!.set(fieldId, field);
+  const fieldId = field.getId();
 
-    EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerField", {
-      field: field,
-      formId: formId,
-    });
-  },
+  if (hasField(formId, fieldId)) {
+    throw new Error(
+      "Form field with id '" + fieldId + "' has already been registered for form with id '" + formId + "'.",
+    );
+  }
 
-  /**
-   * Registers the form with the given id.
-   */
-  registerForm(formId: string): void {
-    if (this.hasForm(formId)) {
-      throw new Error("Form with id '" + formId + "' has already been registered.");
-    }
+  _fields.get(formId)!.set(fieldId, field);
 
-    const form = document.getElementById(formId);
-    if (form === null) {
-      throw new Error("Unknown form with id '" + formId + "'.");
-    }
+  EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerField", {
+    field: field,
+    formId: formId,
+  });
+}
 
-    _forms.set(formId, form);
-    _fields.set(formId, new Map<string, Field>());
+/**
+ * Registers the form with the given id.
+ */
+export function registerForm(formId: FieldId): void {
+  if (hasForm(formId)) {
+    throw new Error("Form with id '" + formId + "' has already been registered.");
+  }
 
-    EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerForm", {
-      formId: formId,
-    });
-  },
+  const form = document.getElementById(formId);
+  if (form === null) {
+    throw new Error("Unknown form with id '" + formId + "'.");
+  }
 
-  /**
-   * Unregisters the form with the given id.
-   */
-  unregisterForm: function (formId: string): void {
-    if (!this.hasForm(formId)) {
-      throw new Error("Unknown form with id '" + formId + "'.");
-    }
+  _forms.set(formId, form);
+  _fields.set(formId, new Map<FieldId, Field>());
 
-    EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "beforeUnregisterForm", {
-      formId: formId,
-    });
+  EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "registerForm", {
+    formId: formId,
+  });
+}
 
-    _forms.delete(formId);
+/**
+ * Unregisters the form with the given id.
+ */
+export function unregisterForm(formId: FieldId): void {
+  if (!hasForm(formId)) {
+    throw new Error("Unknown form with id '" + formId + "'.");
+  }
 
-    _fields.get(formId)!.forEach(function (field) {
-      field.destroy();
-    });
+  EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "beforeUnregisterForm", {
+    formId: formId,
+  });
 
-    _fields.delete(formId);
+  _forms.delete(formId);
 
-    DependencyManager.unregister(formId);
+  _fields.get(formId)!.forEach(function (field) {
+    field.destroy();
+  });
 
-    EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "afterUnregisterForm", {
-      formId: formId,
-    });
-  },
-};
+  _fields.delete(formId);
 
-Core.enableLegacyInheritance(FormBuilderManager);
+  DependencyManager.unregister(formId);
 
-export = FormBuilderManager;
+  EventHandler.fire("WoltLabSuite/Core/Form/Builder/Manager", "afterUnregisterForm", {
+    formId: formId,
+  });
+}