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 ("features/enum_vertices/EnumVertices.js"),
include ("model/texts.js", modulDir), include ("model/texts.js", modulDir),
include ("model/UndoStack.js", modulDir),
include ("model/DiskSaveLoad.js", modulDir),
include ("model/Application.js", modulDir), include ("model/Application.js", modulDir),
include ("ui/ya_metrika.js", modulDir), include ("ui/ya_metrika.js", modulDir),
include ("ui/editor.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.SetDefaultTransformations();
this.algorithmsValues = {}; this.algorithmsValues = {};
this.undoStack = []; this.undoStack = new UndoStack(this.maxUndoStackSize);
this.edgeCommonStyle = new CommonEdgeStyle(); this.edgeCommonStyle = new CommonEdgeStyle();
this.isEdgeCommonStyleCustom = false; this.isEdgeCommonStyleCustom = false;
@ -782,7 +782,7 @@ Application.prototype.SetHandlerMode = function(mode)
} }
else if (mode == "graphUndo") else if (mode == "graphUndo")
{ {
if (!this.IsUndoStackEmpty()) if (!this.undoStack.IsUndoStackEmpty())
this.Undo(); this.Undo();
} }
else if (g_AlgorithmIds.indexOf(mode) >= 0) 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'; document.getElementById('GraphUndo').style.display = 'none';
this.updateMessage(); this.updateMessage();
@ -1083,8 +1083,6 @@ Application.prototype.SetPairSmart = function (pair)
Application.prototype.SaveGraphOnDisk = function () Application.prototype.SaveGraphOnDisk = function ()
{ {
var graphAsString = this.graph.SaveToXML(this.SaveUserSettings()); var graphAsString = this.graph.SaveToXML(this.SaveUserSettings());
var styleSave = this.SaveUserSettings();
if (this.savedGraphName.length <= 0) if (this.savedGraphName.length <= 0)
{ {
@ -1092,16 +1090,11 @@ Application.prototype.SaveGraphOnDisk = function ()
} }
var app = this; var app = this;
$.ajax({
type: "POST", DiskSaveLoad.SaveGraphOnDisk(this.savedGraphName, graphAsString, function( msg )
url: "/" + SiteDir + "cgi-bin/saveGraph.php?name=" + this.savedGraphName, {
data: graphAsString, document.cookie = "graphName=" + app.savedGraphName;
dataType: "text" });
})
.done(function( msg )
{
document.cookie = "graphName=" + app.savedGraphName;
});
} }
Application.prototype.SaveGraphImageOnDisk = function (showDialogCallback) 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) 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); + "&width=" + Math.round(bbox.size().x * this.canvasScale) + "&height=" + Math.round(bbox.size().y * this.canvasScale);
//console.log(rectParams);
} }
var imageBase64Data = this.canvas.toDataURL(); var imageBase64Data = this.canvas.toDataURL();
$.ajax({ DiskSaveLoad.SaveGraphImageOnDisk(imageName, rectParams, imageBase64Data, showDialogCallback);
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveImage.php?name=" + imageName + rectParams,
data: {
base64data : imageBase64Data
},
dataType: "text",
success: function(data){
showDialogCallback();
}
});
return imageName; return imageName;
} }
@ -1159,17 +1140,7 @@ Application.prototype.SaveFullGraphImageOnDisk = function (showDialogCallback, f
var imageBase64Data = canvas.toDataURL(); var imageBase64Data = canvas.toDataURL();
$.ajax({ DiskSaveLoad.SaveGraphImageOnDisk(imageName, rectParams, imageBase64Data, showDialogCallback);
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveImage.php?name=" + imageName + rectParams,
data: {
base64data : imageBase64Data
},
dataType: "text",
success: function(data){
showDialogCallback();
}
});
return imageName; return imageName;
} }
@ -1180,22 +1151,8 @@ Application.prototype.SaveSVGGraphOnDisk = function (showDialogCallback)
this.stopRenderTimer(); this.stopRenderTimer();
var svgText = this._printToSVG(); var svgText = this._printToSVG();
var bbox = this.graph.getGraphBBox();
var imageBase64Data = canvas.toDataURL();
$.ajax({ DiskSaveLoad.SaveSVGGraphOnDisk(imageName, svgText, showDialogCallback);
type: "POST",
url: "/" + SiteDir + "cgi-bin/saveSvg.php?name=" + imageName,
data: {
svgdata : svgText
},
dataType: "text",
success: function(data){
showDialogCallback();
}
});
return imageName; return imageName;
} }
@ -1223,19 +1180,13 @@ Application.prototype.LoadGraphFromString = function (str)
Application.prototype.LoadGraphFromDisk = function (graphName) Application.prototype.LoadGraphFromDisk = function (graphName)
{ {
var app = this; var app = this;
DiskSaveLoad.LoadGraphFromDisk(graphName, function( msg )
$.ajax({
type: "GET",
url: "/" + SiteDir + "cgi-bin/loadGraph.php?name=" + graphName
})
.done(function( msg )
{ {
app.LoadGraphFromString(msg); app.LoadGraphFromString(msg);
}); });
} }
Application.prototype.GetNewGraphName = function() Application.prototype.GetNewGraphName = function()
{ {
var name = this.GetNewName(); var name = this.GetNewName();
@ -1428,53 +1379,30 @@ Application.prototype.IsGraphFitOnViewport = function()
Application.prototype.PushToStack = function(actionName) Application.prototype.PushToStack = function(actionName)
{ {
var object = {}; this.undoStack.PushToStack(actionName, this.graph.SaveToXML(this.SaveUserSettings()));
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);
document.getElementById('GraphUndo').style.display = 'inline-block'; document.getElementById('GraphUndo').style.display = 'inline-block';
} }
Application.prototype.Undo = function() Application.prototype.Undo = function()
{ {
if (this.IsUndoStackEmpty()) let data = this.undoStack.Undo();
if (data == null)
return; return;
var state = this.undoStack.pop();
this.graph = new Graph(); this.graph = new Graph();
var userSettings = {}; var userSettings = {};
this.graph.LoadFromXML(state.graphSave, userSettings); this.graph.LoadFromXML(data, userSettings);
if (userSettings.hasOwnProperty("data") && userSettings["data"].length > 0) if (userSettings.hasOwnProperty("data") && userSettings["data"].length > 0)
this.LoadUserSettings(userSettings["data"]); this.LoadUserSettings(userSettings["data"]);
this.redrawGraph(); this.redrawGraph();
//console.log("undo:" + state.actionName + " size =" + this.undoStack.length); if (this.undoStack.IsUndoStackEmpty())
if (this.IsUndoStackEmpty())
document.getElementById('GraphUndo').style.display = 'none'; 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() Application.prototype.SaveUserSettings = function()
{ {
var res = "{"; 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);
}