From ac87e58d26186a78e14f5a90e7d1f7f1d68bc7db Mon Sep 17 00:00:00 2001
From: Maximilian Giller <m.giller@tu-bs.de>
Date: Tue, 8 Feb 2022 17:56:47 +0100
Subject: [PATCH] Fixing issues with type selection

---
 datasets/datasets.php               | 51 ++++++++---------------------
 editor/editor.php                   |  6 ----
 editor/js/editor.js                 | 24 +++++++++++---
 editor/js/tools/menus/selectmenu.js |  7 ++++
 4 files changed, 41 insertions(+), 47 deletions(-)

diff --git a/datasets/datasets.php b/datasets/datasets.php
index ef660cd..b32e096 100644
--- a/datasets/datasets.php
+++ b/datasets/datasets.php
@@ -10,9 +10,9 @@ require_once(__DIR__ . "/ks-datasets-database.php");
 $EMPTY_SPACE = '{"links":[],"nodes":[]}';
 $SPACES_DIR = __DIR__ . "/spaces/";
 
-add_action("wp_ajax_get_space", "kg_get_space"); // Fires only for logged-in-users
-add_action("wp_ajax_nopriv_get_space", 'kg_get_space'); // Fires for everyone
-function kg_get_space()
+add_action("wp_ajax_get_space", "ks_get_space"); // Fires only for logged-in-users
+add_action("wp_ajax_nopriv_get_space", 'ks_get_space'); // Fires for everyone
+function ks_get_space()
 {
     $space_id = $_POST["space_id"];
 
@@ -132,8 +132,8 @@ function ks_get_nodetype_to_node($nodetype_id)
     );
 }
 
-add_action("wp_ajax_list_spaces", "kg_list_spaces"); // Fires only for logged-in-users
-function kg_list_spaces()
+add_action("wp_ajax_list_spaces", "ks_list_spaces"); // Fires only for logged-in-users
+function ks_list_spaces()
 {
     $spaces = ks_select_all_spaces();
     $spaces = ks_spaces_to_array($spaces);
@@ -144,8 +144,8 @@ function kg_list_spaces()
     wp_die();
 }
 
-add_action("wp_ajax_import_space", "kg_import_space"); // Fires only for logged-in-users
-function kg_import_space()
+add_action("wp_ajax_import_space", "ks_import_space"); // Fires only for logged-in-users
+function ks_import_space()
 {
     // Check user capabilities
     if (!current_user_can("edit_posts")) {
@@ -169,7 +169,7 @@ function kg_import_space()
             $type_id = ks_insert_nodetype(array(
                 "space_id" => $space_id,
                 "name" => $node->type,
-                "color" => get_random_color(),
+                "color" => ks_get_random_color(),
             ));
         } else {
             $type_id = $type->nodetype_id;
@@ -208,8 +208,8 @@ function kg_import_space()
     wp_die();
 }
 
-add_action("wp_ajax_update_space", "kg_update_space"); // Fires only for logged-in-users
-function kg_update_space()
+add_action("wp_ajax_update_space", "ks_update_space"); // Fires only for logged-in-users
+function ks_update_space()
 {
     // Check user capabilities
     if (!current_user_can("edit_posts")) {
@@ -217,37 +217,14 @@ function kg_update_space()
         wp_die();
     }
 
-    // Use json encoding.
-    $graph = stripslashes($_POST["graph"]);
+    // // Use json encoding.
+    // $graph = stripslashes($_POST["graph"]);
 
-    kg_store_new_graph($graph, $_POST["space"]);
+    // ks_store_new_graph($graph, $_POST["space"]);
 
     wp_die();
 }
 
-function kg_store_new_graph($graph, $space_id)
-{
-    $file_path = kg_get_space_file_path($space_id);
-    $result = file_put_contents($file_path, $graph);
-
-    //echo print_r($_POST);
-    echo "Saved file at ";
-    echo $file_path;
-    echo " ";
-    echo $result;
-}
-
-function kg_get_space_file_path($space_id)
-{
-    // Cleaning up the space id
-    $space_id = str_replace("/", "-", $space_id);
-    $space_id = str_replace("\\", "-", $space_id);
-    $space_id = str_replace(".", "-", $space_id);
-
-    global $SPACES_DIR;
-    return $SPACES_DIR . $space_id . ".json";
-}
-
 function ks_spaces_to_array($spaces)
 {
     $array = array();
@@ -267,7 +244,7 @@ function ks_spaces_to_array($spaces)
     return $array;
 }
 
-function get_random_color()
+function ks_get_random_color()
 {
     return str_pad(strval(decbin(rand(0, pow(2, 24)))), 24, '0', STR_PAD_LEFT);
 }
diff --git a/editor/editor.php b/editor/editor.php
index 2cbd0ad..aefdc58 100644
--- a/editor/editor.php
+++ b/editor/editor.php
@@ -45,12 +45,6 @@
                 <label for="node-type">Type</label>
                 </br>
                 <select id="node-type" name="node-type" class="bottom-space">
-                    <option value="Vorlesung">Vorlesung</option>
-                    <option value="Algorithmus">Algorithmus</option>
-                    <option value="Definition">Definition</option>
-                    <option value="Beispiel">Beispiel</option>
-                    <option value="Übung">Übung</option>
-                    <option value="Kapitel">Kapitel</option>
                 </select>
 
                 </br>
diff --git a/editor/js/editor.js b/editor/js/editor.js
index 27b51c4..d55aaee 100644
--- a/editor/js/editor.js
+++ b/editor/js/editor.js
@@ -4,7 +4,7 @@ import { loadSpaceJson } from "../../datasets/datasets";
 import ForceGraph from "force-graph";
 import * as Interactions from "./interactions";
 import { setSpace, SPACE } from "../../config";
-import jquery from "jquery";
+import jQuery from "jquery";
 
 export var state = undefined;
 export var graph = undefined;
@@ -36,18 +36,34 @@ export function loadSpace(spaceId) {
         }
         const space = Object.values(response.spaces)[0];
 
-        jquery("#ks-editor #header-space-title").text(
+        jQuery("#ks-editor #header-space-title").text(
             " - " + space.name + " [" + space.space_id + "]"
         );
 
         state = new State();
         graph = new Graph.Graph(space);
-        load();
+
+        fillTypeSelector(graph.nodeTypes);
+        loadGraph();
 
         graph.restartSimulation();
     });
 }
 
+function fillTypeSelector(types) {
+    var selectContainer = jQuery("#ks-editor #node-type");
+    selectContainer.empty();
+    types.forEach((type) => {
+        var child =
+            '<option value="' +
+            type.name +
+            '">' +
+            type.name +
+            "</option>";
+        selectContainer.append(child);
+    });
+}
+
 function extractPositions(event) {
     return {
         graph: renderer.screen2GraphCoords(event.layerX, event.layerY),
@@ -55,7 +71,7 @@ function extractPositions(event) {
     };
 }
 
-function load() {
+function loadGraph() {
     const graphContainer = document.getElementById("2d-graph");
     const width = graphContainer.offsetWidth;
 
diff --git a/editor/js/tools/menus/selectmenu.js b/editor/js/tools/menus/selectmenu.js
index f0628b1..adc1448 100644
--- a/editor/js/tools/menus/selectmenu.js
+++ b/editor/js/tools/menus/selectmenu.js
@@ -1,4 +1,5 @@
 import { PLUGIN_PATH } from "../../../../config";
+import { graph } from "../../editor";
 import * as Graph from "../../graph";
 import { CONTEXT } from "../../state";
 import ToolMenu from "./toolmenu";
@@ -176,6 +177,10 @@ export class SelectMenu extends ToolMenu {
                 reference[Graph.TAG_CREATED] = true;
             });
         }
+        // Format types
+        else if (propertyKey === Graph.NODE_TYPE) {
+            formattedValue = graph.nodeTypes.filter((type) => type == rawValue);
+        }
 
         return formattedValue;
     }
@@ -252,6 +257,8 @@ export class SelectMenu extends ToolMenu {
             var formattedValue = undefined;
             if (propertyKey === Graph.NODE_REFERENCES && Array.isArray(value)) {
                 formattedValue = value.map((ref) => ref.url).join("\n");
+            } else if (propertyKey === Graph.NODE_TYPE && value != undefined) {
+                formattedValue = value.name;
             } else {
                 formattedValue = value;
             }
-- 
GitLab