Skip to content
Snippets Groups Projects
Commit b4d915a7 authored by Maximilian Giller's avatar Maximilian Giller
Browse files

Merge branch 'ajax-file-upload-max'

parents cb2b57cc 1ed6e43d
No related branches found
No related tags found
No related merge requests found
...@@ -9,4 +9,5 @@ export const COLOR_PALETTE = [ ...@@ -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. // 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;
import { PLUGIN_PATH } from "../config"; import { PLUGIN_PATH } from "../config";
import jQuery from "jquery";
export const DATASETS_URL = PLUGIN_PATH + "datasets/datasets.php";
/** /**
* Returns the json object from the stored graph as promise. * Returns the json object from the stored graph as promise.
* *
* @param {String} spaceId Identification of graph to load. * @param {String} spaceId Identification of graph to load.
* *
* @returns Promise returning graph object * @returns Promise returning graph object
*/ */
export function loadGraphJson(spaceId) { export function loadGraphJson(spaceId) {
return fetch(DATASETS_URL + "?space_id=" + spaceId) let payload = {
.then((r) => r.json()); action: "get_space",
space: spaceId,
};
return jQuery
.ajax({
type: "POST",
url: ajax_object.ajax_url,
data: payload,
})
.then((data) => JSON.parse(data));
} }
/** /**
...@@ -21,23 +30,16 @@ export function loadGraphJson(spaceId) { ...@@ -21,23 +30,16 @@ export function loadGraphJson(spaceId) {
* @param {object} json Graph object * @param {object} json Graph object
*/ */
export function saveGraphJson(spaceId, json) { export function saveGraphJson(spaceId, json) {
var payload = { let payload = {
space_id: spaceId, action: "update_space",
graph: JSON.stringify(json), graph: JSON.stringify(json),
space: spaceId,
}; };
var auth = getAuthPayload(); return jQuery
if (auth === undefined) { .ajax({
return undefined; type: "POST",
} url: ajax_object.ajax_url,
data: payload,
return fetch(DATASETS_URL, { });
method: "POST",
body: JSON.stringify(Object.assign(payload, auth)),
});
}
function getAuthPayload() {
//! TODO: Implement auth
return {};
} }
<?php <?php
function handle_request() add_action("wp_ajax_get_space", "get_space"); // Fires only for logged-in-users
{ add_action("wp_ajax_nopriv_get_space", 'get_space' ); // Fires for everyone
if ($_SERVER["REQUEST_METHOD"] == "GET") { function get_space() {
handle_get($_GET); $file_path = get_space_file_path($_POST["space"]);
} else if ($_SERVER["REQUEST_METHOD"] == "POST") { $content = file_get_contents($file_path);
handle_post(get_post_data()); echo $content;
}
}
function get_post_data() wp_die();
{
return json_decode(file_get_contents('php://input'), true);
} }
function handle_get($request_data) { add_action("wp_ajax_update_space", "update_space"); // Fires only for logged-in-users
$file_path = get_space_file_path($request_data["space_id"]); //add_action("wp_ajax_nopriv_update_space", 'update_space' ); // Fires for everyone
function update_space() {
// Check user capabilities
if (current_user_can("edit_posts")) {
// Use json encoding.
$graph = stripslashes($_POST["graph"]);
$content = file_get_contents($file_path); store_new_graph($graph, $_POST["space"]);
echo $content;
wp_die();
} else {
echo "Insufficient permissions!";
}
} }
function handle_post($request_data) { function store_new_graph($graph, $space_id) {
$file_path = get_space_file_path($request_data["space_id"]); $file_path = get_space_file_path($space_id);
$result = file_put_contents($file_path, $graph);
file_put_contents($file_path, $request_data["graph"]); //echo print_r($_POST);
echo "Saved file at ";
echo $file_path;
echo " ";
echo $result;
} }
function get_space_file_path($space_id) { function get_space_file_path($space_id) {
return __DIR__."/".$space_id.".json"; return __DIR__."/".$space_id.".json";
} }
handle_request();
...@@ -525,7 +525,7 @@ let infoOverlay = null; ...@@ -525,7 +525,7 @@ let infoOverlay = null;
// Only execute, if corresponding dom is present // Only execute, if corresponding dom is present
if (document.getElementById("3d-graph") !== null) { 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"); filterOverlay = new FilterOverlay(G, "node");
infoOverlay = new NodeInfoOverlay(G); infoOverlay = new NodeInfoOverlay(G);
G.infoOverlay = infoOverlay; G.infoOverlay = infoOverlay;
......
...@@ -3,6 +3,7 @@ import * as Graph from "./graph"; ...@@ -3,6 +3,7 @@ import * as Graph from "./graph";
import { loadGraphJson } from "../../datasets/datasets"; import { loadGraphJson } from "../../datasets/datasets";
import ForceGraph from "force-graph"; import ForceGraph from "force-graph";
import * as Interactions from "./interactions"; import * as Interactions from "./interactions";
import { SPACE } from "../../config";
export var state; export var state;
...@@ -20,7 +21,7 @@ window.onload = function () { ...@@ -20,7 +21,7 @@ window.onload = function () {
Interactions.initInteractions(); 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) => { .then((graphConfig) => {
state = new State(); state = new State();
graph = new Graph.Graph(graphConfig); graph = new Graph.Graph(graphConfig);
......
...@@ -14,17 +14,21 @@ function ks_add_graph($atts = []): string ...@@ -14,17 +14,21 @@ function ks_add_graph($atts = []): string
$space_id = get_space_id_from_atts($atts); $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 $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__); $plugin_dir = plugin_dir_url(__FILE__);
//$dataset = $plugin_dir.'datasets/miserables.json';
$variables = "<script> $script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js';
var plugin_path = '$plugin_dir'; // $script = "<script src='$script_path'></script>";
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>";
//wp_enqueue_script('kg-script', $script_path); //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 = []) function ks_add_editor($atts = [])
...@@ -33,10 +37,8 @@ function ks_add_editor($atts = []) ...@@ -33,10 +37,8 @@ function ks_add_editor($atts = [])
$space_id = get_space_id_from_atts($atts); $space_id = get_space_id_from_atts($atts);
$plugin_dir = plugin_dir_url(__FILE__); $plugin_dir = plugin_dir_url(__FILE__);
echo "<script> wp_localize_script("ks-editor-js", "space", array('id' => $space_id));
var plugin_path = '$plugin_dir'; wp_localize_script("ks-editor-js", "plugin", array('path' => $plugin_dir));
var space_id = '$space_id';
</script>";
require_once(__DIR__ . '/editor/editor.php'); require_once(__DIR__ . '/editor/editor.php');
} }
...@@ -45,8 +47,16 @@ function ks_add_editor_dependencies() ...@@ -45,8 +47,16 @@ function ks_add_editor_dependencies()
{ {
$script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js'; $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(), false); 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_enqueue_script("ks-editor-js");
$style_file_version = date("ymd-Gis", filemtime(plugin_dir_path(__FILE__) . "editor/css/editor.css")); $style_file_version = date("ymd-Gis", filemtime(plugin_dir_path(__FILE__) . "editor/css/editor.css"));
wp_enqueue_style("ks-editor-css", plugins_url("editor/css/editor.css", __FILE__), array(), $style_file_version); wp_enqueue_style("ks-editor-css", plugins_url("editor/css/editor.css", __FILE__), array(), $style_file_version);
...@@ -71,7 +81,8 @@ function escape_space_id($id) ...@@ -71,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)) { if ($atts != "" && array_key_exists("space", $atts)) {
return escape_space_id($atts["space"]); return escape_space_id($atts["space"]);
} else { } else {
...@@ -79,6 +90,9 @@ function get_space_id_from_atts($atts) { ...@@ -79,6 +90,9 @@ function get_space_id_from_atts($atts) {
} }
} }
require_once(__DIR__ . '/datasets/datasets.php');
add_action('wp_enqueue_scripts', 'kg_load_css'); add_action('wp_enqueue_scripts', 'kg_load_css');
add_shortcode('knowledge-space', 'ks_add_graph'); add_shortcode('knowledge-space', 'ks_add_graph');
add_shortcode('knowledge-space-editor', 'ks_add_editor'); add_shortcode('knowledge-space-editor', 'ks_add_editor');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment