Refactor Undo and SaveLoad from disk.

This commit is contained in:
Oleg Sh 2023-12-03 13:40:50 +02:00
parent fdbff31a13
commit 00f35ebe71
5 changed files with 148 additions and 112 deletions

View File

@ -37,6 +37,8 @@
include ("features/enum_vertices/EnumVertices.js"),
include ("model/texts.js", modulDir),
include ("model/UndoStack.js", modulDir),
include ("model/DiskSaveLoad.js", modulDir),
include ("model/Application.js", modulDir),
include ("ui/ya_metrika.js", modulDir),
include ("ui/editor.js", modulDir),

File diff suppressed because one or more lines are too long

View File

@ -30,7 +30,7 @@ function Application(document, window, listener)
this.SetDefaultTransformations();
this.algorithmsValues = {};
this.undoStack = [];
this.undoStack = new UndoStack(this.maxUndoStackSize);
this.edgeCommonStyle = new CommonEdgeStyle();
this.isEdgeCommonStyleCustom = false;
@ -782,7 +782,7 @@ Application.prototype.SetHandlerMode = function(mode)
}
else if (mode == "graphUndo")
{
if (!this.IsUndoStackEmpty())
if (!this.undoStack.IsUndoStackEmpty())
this.Undo();
}
else if (g_AlgorithmIds.indexOf(mode) >= 0)
@ -892,7 +892,7 @@ Application.prototype.onPostLoadEvent = function()
}
}
if (this.IsUndoStackEmpty())
if (this.undoStack.IsUndoStackEmpty())
document.getElementById('GraphUndo').style.display = 'none';
this.updateMessage();
@ -1084,24 +1084,17 @@ Application.prototype.SaveGraphOnDisk = function ()
{
var graphAsString = this.graph.SaveToXML(this.SaveUserSettings());
var styleSave = this.SaveUserSettings();
if (this.savedGraphName.length <= 0)
{
this.savedGraphName = this.GetNewGraphName();
}
var app = this;
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveGraph.php?name=" + this.savedGraphName,
data: graphAsString,
dataType: "text"
})
.done(function( msg )
{
document.cookie = "graphName=" + app.savedGraphName;
});
DiskSaveLoad.SaveGraphOnDisk(this.savedGraphName, graphAsString, function( msg )
{
document.cookie = "graphName=" + app.savedGraphName;
});
}
Application.prototype.SaveGraphImageOnDisk = function (showDialogCallback)
@ -1124,23 +1117,11 @@ Application.prototype.SaveGraphImageOnDisk = function (showDialogCallback)
rectParams = "&x=" + Math.round(pos.x * this.canvasScale) + "&y=" + Math.round(pos.y * this.canvasScale)
+ "&width=" + Math.round(bbox.size().x * this.canvasScale) + "&height=" + Math.round(bbox.size().y * this.canvasScale);
//console.log(rectParams);
}
var imageBase64Data = this.canvas.toDataURL();
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveImage.php?name=" + imageName + rectParams,
data: {
base64data : imageBase64Data
},
dataType: "text",
success: function(data){
showDialogCallback();
}
});
DiskSaveLoad.SaveGraphImageOnDisk(imageName, rectParams, imageBase64Data, showDialogCallback);
return imageName;
}
@ -1159,17 +1140,7 @@ Application.prototype.SaveFullGraphImageOnDisk = function (showDialogCallback, f
var imageBase64Data = canvas.toDataURL();
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveImage.php?name=" + imageName + rectParams,
data: {
base64data : imageBase64Data
},
dataType: "text",
success: function(data){
showDialogCallback();
}
});
DiskSaveLoad.SaveGraphImageOnDisk(imageName, rectParams, imageBase64Data, showDialogCallback);
return imageName;
}
@ -1181,21 +1152,7 @@ Application.prototype.SaveSVGGraphOnDisk = function (showDialogCallback)
this.stopRenderTimer();
var svgText = this._printToSVG();
var bbox = this.graph.getGraphBBox();
var imageBase64Data = canvas.toDataURL();
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveSvg.php?name=" + imageName,
data: {
svgdata : svgText
},
dataType: "text",
success: function(data){
showDialogCallback();
}
});
DiskSaveLoad.SaveSVGGraphOnDisk(imageName, svgText, showDialogCallback);
return imageName;
}
@ -1223,19 +1180,13 @@ Application.prototype.LoadGraphFromString = function (str)
Application.prototype.LoadGraphFromDisk = function (graphName)
{
var app = this;
$.ajax({
type: "GET",
url: "/" + SiteDir + "cgi-bin/loadGraph.php?name=" + graphName
})
.done(function( msg )
var app = this;
DiskSaveLoad.LoadGraphFromDisk(graphName, function( msg )
{
app.LoadGraphFromString(msg);
});
}
Application.prototype.GetNewGraphName = function()
{
var name = this.GetNewName();
@ -1428,53 +1379,30 @@ Application.prototype.IsGraphFitOnViewport = function()
Application.prototype.PushToStack = function(actionName)
{
var object = {};
object.actionName = actionName;
object.graphSave = this.graph.SaveToXML(this.SaveUserSettings());
this.undoStack.push(object);
while (this.undoStack.length > this.maxUndoStackSize)
{
this.undoStack.shift();
}
//console.log("push undo:" + object.actionName + " size =" + this.undoStack.length);
this.undoStack.PushToStack(actionName, this.graph.SaveToXML(this.SaveUserSettings()));
document.getElementById('GraphUndo').style.display = 'inline-block';
}
Application.prototype.Undo = function()
{
if (this.IsUndoStackEmpty())
let data = this.undoStack.Undo();
if (data == null)
return;
var state = this.undoStack.pop();
this.graph = new Graph();
var userSettings = {};
this.graph.LoadFromXML(state.graphSave, userSettings);
this.graph.LoadFromXML(data, userSettings);
if (userSettings.hasOwnProperty("data") && userSettings["data"].length > 0)
this.LoadUserSettings(userSettings["data"]);
this.redrawGraph();
//console.log("undo:" + state.actionName + " size =" + this.undoStack.length);
if (this.IsUndoStackEmpty())
if (this.undoStack.IsUndoStackEmpty())
document.getElementById('GraphUndo').style.display = 'none';
}
Application.prototype.ClearUndoStack = function()
{
this.undoStack = [];
}
Application.prototype.IsUndoStackEmpty = function()
{
return (this.undoStack.length <= 0);
}
Application.prototype.SaveUserSettings = function()
{
var res = "{";

View File

@ -0,0 +1,52 @@
// Disk save load
var DiskSaveLoad = function () {};
DiskSaveLoad.LoadGraphFromDisk = function (graphName, callback)
{
$.ajax({
type: "GET",
url: "/" + SiteDir + "cgi-bin/loadGraph.php?name=" + graphName
})
.done(callback);
}
DiskSaveLoad.SaveSVGGraphOnDisk = function (imageName, svgText, callback)
{
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveSvg.php?name=" + imageName,
data: {
svgdata : svgText
},
dataType: "text",
success: callback
});
return imageName;
}
DiskSaveLoad.SaveGraphOnDisk = function (savedGraphName, graphAsString, callback)
{
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveGraph.php?name=" + savedGraphName,
data: graphAsString,
dataType: "text"
})
.done(callback);
}
DiskSaveLoad.SaveGraphImageOnDisk = function (imageName, rectParams, imageBase64Data, callback)
{
$.ajax({
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveImage.php?name=" + imageName + rectParams,
data: {
base64data : imageBase64Data
},
dataType: "text",
success: callback
});
}

View File

@ -0,0 +1,41 @@
// Undo Stack
function UndoStack(maxUndoStackSize) {
this.undoStack = [];
this.maxUndoStackSize = maxUndoStackSize;
}
UndoStack.prototype.PushToStack = function(actionName, dataToSave)
{
var object = {};
object.actionName = actionName;
object.data = dataToSave;
this.undoStack.push(object);
while (this.undoStack.length > this.maxUndoStackSize)
{
this.undoStack.shift();
}
}
UndoStack.prototype.Undo = function()
{
if (this.IsUndoStackEmpty())
return null;
var state = this.undoStack.pop();
return state.data;
}
UndoStack.prototype.ClearUndoStack = function()
{
this.undoStack = [];
}
UndoStack.prototype.IsUndoStackEmpty = function()
{
return (this.undoStack.length <= 0);
}