diff --git a/config.js b/config.js index e1c17d8234348b6cd6f6144ab94e9fc188f7271c..2e077a94ba26edfc6753676e5b88fad0930b9df8 100644 --- a/config.js +++ b/config.js @@ -9,4 +9,5 @@ export const COLOR_PALETTE = [ ]; // Just renaming a variable which is given by the PHP script. This avoids errors in all other files. -export const PLUGIN_PATH = plugin_path; +export const PLUGIN_PATH = plugin.path; +export const SPACE = space.id; diff --git a/display/graph.js b/display/graph.js index 32ba3da9fe8421c5566eecf72e268905999f3cee..351feff88d5ac40c454b7a774dba707bb71fbd3f 100644 --- a/display/graph.js +++ b/display/graph.js @@ -520,7 +520,7 @@ let infoOverlay = null; // Only execute, if corresponding dom is present if (document.getElementById("3d-graph") !== null) { - G = new Graph(space_id); // space_id defined globaly through knowledge-space.php + G = new Graph(Config.SPACE); // space_id defined globaly through knowledge-space.php filterOverlay = new FilterOverlay(G, "node"); infoOverlay = new NodeInfoOverlay(G); G.infoOverlay = infoOverlay; diff --git a/editor/js/editor.js b/editor/js/editor.js index 8473bf1e54744105bda4bd90c035242a024c16b5..a361e4ed4a230150b49b5d871dd7d01b91cf5b6d 100644 --- a/editor/js/editor.js +++ b/editor/js/editor.js @@ -3,6 +3,7 @@ import * as Graph from "./graph"; import { loadGraphJson } from "../../datasets/datasets"; import ForceGraph from "force-graph"; import * as Interactions from "./interactions"; +import { SPACE } from "../../config"; export var state; @@ -20,7 +21,7 @@ window.onload = function () { Interactions.initInteractions(); - loadGraphJson(space_id) // space_id defined globaly through knowledge-space.php + loadGraphJson(SPACE) // space_id defined globaly through knowledge-space.php .then((graphConfig) => { state = new State(); graph = new Graph.Graph(graphConfig); diff --git a/knowledge-space.php b/knowledge-space.php index 8b85da0679a867680b997d19f9355b1d1d713063..53dc5191b94463c035e5f64f9096673368776271 100644 --- a/knowledge-space.php +++ b/knowledge-space.php @@ -14,17 +14,21 @@ function ks_add_graph($atts = []): string $space_id = get_space_id_from_atts($atts); $div = '<div id="3d-graph"></div>'; // The id "3d-graph" indicates, that the javascript associated with this should automatically be executed $plugin_dir = plugin_dir_url(__FILE__); - //$dataset = $plugin_dir.'datasets/miserables.json'; - $variables = "<script> - var plugin_path = '$plugin_dir'; - var space_id = '$space_id'; - </script>"; - - $script_path = $plugin_dir . 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js'; - $script = "<script src='$script_path'></script>"; + + $script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js'; + // $script = "<script src='$script_path'></script>"; //wp_enqueue_script('kg-script', $script_path); + wp_enqueue_script("ks-display-js", plugins_url($script_path, __FILE__), array('jquery'), false); + wp_localize_script( + 'ks-display-js', + 'ajax_object', + array('ajax_url' => admin_url('admin-ajax.php')) + ); + wp_localize_script("ks-display-js", "space", array('id' => $space_id)); + wp_localize_script("ks-display-js", "plugin", array('path' => $plugin_dir)); - return $div . $variables . $script; + return $div . $variables; + // return $div . $variables . $script; } function ks_add_editor($atts = []) @@ -33,10 +37,8 @@ function ks_add_editor($atts = []) $space_id = get_space_id_from_atts($atts); $plugin_dir = plugin_dir_url(__FILE__); - echo "<script> - var plugin_path = '$plugin_dir'; - var space_id = '$space_id'; - </script>"; + wp_localize_script("ks-editor-js", "space", array('id' => $space_id)); + wp_localize_script("ks-editor-js", "plugin", array('path' => $plugin_dir)); require_once(__DIR__ . '/editor/editor.php'); } @@ -45,12 +47,15 @@ function ks_add_editor_dependencies() { $script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js'; -// wp_enqueue_script('jquery'); + // wp_enqueue_script('jquery'); wp_enqueue_script("ks-editor-js", plugins_url($script_path, __FILE__), array('jquery'), false); //wp_register_script("ks-editor-js", plugins_url($script_path, __FILE__), array('jquery'), false); - wp_localize_script( 'ks-editor-js', 'ajax_object', - array( 'ajax_url' => admin_url( 'admin-ajax.php' ))); + wp_localize_script( + 'ks-editor-js', + 'ajax_object', + array('ajax_url' => admin_url('admin-ajax.php')) + ); //wp_enqueue_script("ks-editor-js"); $style_file_version = date("ymd-Gis", filemtime(plugin_dir_path(__FILE__) . "editor/css/editor.css")); @@ -76,7 +81,8 @@ function escape_space_id($id) ); } -function get_space_id_from_atts($atts) { +function get_space_id_from_atts($atts) +{ if ($atts != "" && array_key_exists("space", $atts)) { return escape_space_id($atts["space"]); } else {