Inject poll data into the editor’s meta data
authorAlexander Ebert <ebert@woltlab.com>
Fri, 28 Apr 2023 14:03:27 +0000 (16:03 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 28 Apr 2023 14:03:27 +0000 (16:03 +0200)
ts/WoltLabSuite/Core/Ui/Poll/Editor.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Editor.js

index bd0f3328c2982208e07046afb4aa1406a878f5a0..700227bb99fb511cc3fc2109fdadcf8d38b89879 100644 (file)
@@ -13,6 +13,7 @@ import UiSortableList from "../Sortable/List";
 import * as EventHandler from "../../Event/Handler";
 import * as DatePicker from "../../Date/Picker";
 import { DatabaseObjectActionResponse } from "../../Ajax/Data";
+import { listenToCkeditor } from "../../Component/Ckeditor/Event";
 
 interface UiPollEditorOptions {
   isAjax: boolean;
@@ -42,6 +43,17 @@ interface ValidationData {
   valid: boolean;
 }
 
+type PollData = {
+  pollEndTime: string;
+  pollIsChangeable?: boolean;
+  pollIsPublic?: boolean;
+  pollMaxVotes: number;
+  pollOptions: string[];
+  pollQuestion: string;
+  pollResultsRequireVote?: true;
+  pollSortByVotes?: boolean;
+};
+
 class UiPollEditor {
   private readonly container: HTMLElement;
   private readonly endTimeField: HTMLInputElement;
@@ -120,6 +132,10 @@ class UiPollEditor {
         this.reset();
       });
 
+      listenToCkeditor(element).collectMetaData((payload) => {
+        payload.metaData.poll = this.#getPollData();
+      });
+
       ["handleError", "submit", "validate"].forEach((event) => {
         EventHandler.add("com.woltlab.wcf.ckeditor5", event + "_" + this.wysiwygId, (...args: unknown[]) =>
           this[event](...args),
@@ -322,6 +338,35 @@ class UiPollEditor {
     }
   }
 
+  #getPollData(): PollData {
+    const data: PollData = {
+      pollEndTime: DatePicker.getValue(this.endTimeField),
+      pollMaxVotes: parseInt(this.maxVotesField.value) || 0,
+      pollQuestion: this.questionField.value,
+      pollOptions: [],
+    };
+
+    if (this.isChangeableYesField.checked) {
+      data.pollIsChangeable = true;
+    }
+
+    if (this.resultsRequireVoteYesField.checked) {
+      data.pollResultsRequireVote = true;
+    }
+
+    if (this.sortByVotesYesField.checked) {
+      data.pollSortByVotes = true;
+    }
+
+    if (this.isPublicYesField?.checked) {
+      data.pollIsPublic = true;
+    }
+
+    data.pollOptions = this.getOptions();
+
+    return data;
+  }
+
   /**
    * Validates the poll data.
    */
index 36d559dc4609a5473dfb1fcc8239d332bdfd2ddb..1fd8e86757dfb7012f2d89c77d01c1a96c8caafb 100644 (file)
@@ -6,7 +6,7 @@
  * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @woltlabExcludeBundle all
  */
-define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sortable/List", "../../Event/Handler", "../../Date/Picker"], function (require, exports, tslib_1, Core, Language, List_1, EventHandler, DatePicker) {
+define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sortable/List", "../../Event/Handler", "../../Date/Picker", "../../Component/Ckeditor/Event"], function (require, exports, tslib_1, Core, Language, List_1, EventHandler, DatePicker, Event_1) {
     "use strict";
     Core = tslib_1.__importStar(Core);
     Language = tslib_1.__importStar(Language);
@@ -73,6 +73,9 @@ define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sorta
                 element.addEventListener("reset", () => {
                     this.reset();
                 });
+                (0, Event_1.listenToCkeditor)(element).collectMetaData((payload) => {
+                    payload.metaData.poll = this.#getPollData();
+                });
                 ["handleError", "submit", "validate"].forEach((event) => {
                     EventHandler.add("com.woltlab.wcf.ckeditor5", event + "_" + this.wysiwygId, (...args) => this[event](...args));
                 });
@@ -246,6 +249,28 @@ define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sorta
                 });
             }
         }
+        #getPollData() {
+            const data = {
+                pollEndTime: DatePicker.getValue(this.endTimeField),
+                pollMaxVotes: parseInt(this.maxVotesField.value) || 0,
+                pollQuestion: this.questionField.value,
+                pollOptions: [],
+            };
+            if (this.isChangeableYesField.checked) {
+                data.pollIsChangeable = true;
+            }
+            if (this.resultsRequireVoteYesField.checked) {
+                data.pollResultsRequireVote = true;
+            }
+            if (this.sortByVotesYesField.checked) {
+                data.pollSortByVotes = true;
+            }
+            if (this.isPublicYesField?.checked) {
+                data.pollIsPublic = true;
+            }
+            data.pollOptions = this.getOptions();
+            return data;
+        }
         /**
          * Validates the poll data.
          */