diff --git a/lang/en/home.php b/lang/en/home.php index f528b01..ac533ed 100755 --- a/lang/en/home.php +++ b/lang/en/home.php @@ -140,4 +140,6 @@ $g_lang["traversal_order"] = "Traversal order: "; $g_lang["curve_edge"] = "Edge bend"; + $g_lang["undo"] = "Undo"; + ?> diff --git a/lang/ru/home.php b/lang/ru/home.php index 8525f3a..522a490 100755 --- a/lang/ru/home.php +++ b/lang/ru/home.php @@ -143,4 +143,5 @@ $g_lang["traversal_order"] = "Порядок обхода: "; $g_lang["curve_edge"] = "Изгиб дуги"; + $g_lang["undo"] = "Отменить"; ?> diff --git a/script/Appilcation.js b/script/Appilcation.js index 36b189f..123da39 100644 --- a/script/Appilcation.js +++ b/script/Appilcation.js @@ -24,6 +24,7 @@ function Application(document, window) this.SetDefaultTransformations(); this.algorithmsValues = {}; this.userAction = function(){}; + this.undoStack = []; }; // List of graph. @@ -39,7 +40,8 @@ Application.prototype.handler = null; Application.prototype.status = {}; // Graph name length Application.prototype.graphNameLength = 16; - +// Max undo stack size +Application.prototype.maxUndoStackSize = 8; Application.prototype.getMousePos = function(canvas, e) { @@ -449,6 +451,12 @@ Application.prototype.DeleteObject = function(object) } } +Application.prototype.IsCorrectObject = function(object) +{ + return (object instanceof BaseVertex) || + (object instanceof BaseEdge); +} + Application.prototype.FindVertex = function(id) { return this.graph.FindVertex(id); @@ -1261,4 +1269,37 @@ Application.prototype.IsGraphFitOnViewport = function() && Math.floor(canvasPositionInverse.y + canvasHeight) >= Math.floor(graphBBox.maxPoint.y)); } - +Application.prototype.PushToStack = function(actionName) +{ + var object = {}; + object.actionName = actionName; + object.graphSave = this.graph.SaveToXML(); + + this.undoStack.push(object); + + while (this.undoStack.length > this.maxUndoStackSize) + { + this.undoStack.shift(); + } +} + +Application.prototype.Undo = function() +{ + if (this.IsUndoStackEmpty()) + return; + + var state = this.undoStack.pop(); + this.graph = new Graph(); + this.graph.LoadFromXML(state.graphSave); + this.redrawGraph(); +} + +Application.prototype.ClearUndoStack = function() +{ + this.undoStack = []; +} + +Application.prototype.IsUndoStackEmpty = function() +{ + return (this.undoStack.length <= 0); +} diff --git a/script/EventHandlers.js b/script/EventHandlers.js index f29493f..3c5903a 100644 --- a/script/EventHandlers.js +++ b/script/EventHandlers.js @@ -14,6 +14,8 @@ function BaseHandler(app) { this.app = app; this.app.setRenderPath([]); + + this.app.ClearUndoStack(); } // Need redraw or nor. @@ -531,7 +533,7 @@ ConnectionGraphHandler.prototype.UpdateSecondVertexMenu = function(vertex2Text) function DeleteGraphHandler(app) { BaseHandler.apply(this, arguments); - this.message = g_selectObjectToDelete; + this.message = g_selectObjectToDelete; } // inheritance. @@ -540,10 +542,38 @@ DeleteGraphHandler.prototype = Object.create(BaseHandler.prototype); DeleteGraphHandler.prototype.MouseDown = function(pos) { var selectedObject = this.GetSelectedObject(pos); - - this.app.DeleteObject(selectedObject); - + + if (!this.app.IsCorrectObject(selectedObject)) + return; + + this.app.PushToStack("Delete"); + this.app.DeleteObject(selectedObject); this.needRedraw = true; + + this.UpdateUndoButton(); +} + +DeleteGraphHandler.prototype.UpdateUndoButton = function() +{ + if (!this.app.IsUndoStackEmpty()) + { + this.message = g_selectObjectToDelete + ""; + + var handler = this; + $('#message').unbind(); + $('#message').on('click', '#undoDelete', function(){ + handler.app.Undo(); + userAction("Undo.Delete"); + + handler.UpdateUndoButton(); + }); + } + else + { + this.message = g_selectObjectToDelete; + } + + this.app.updateMessage(); } /** diff --git a/script/texts.js b/script/texts.js index 04dd9d4..bf55517 100644 --- a/script/texts.js +++ b/script/texts.js @@ -97,6 +97,8 @@ var g_traversalOrder = "Traversal order: "; var g_curveEdge = "Curved edge"; +var g_Undo = "Undo"; + function loadTexts() { g_textsSelectAndMove = document.getElementById("SelectAndMoveObject").innerHTML; @@ -193,4 +195,6 @@ function loadTexts() g_traversalOrder = document.getElementById("traversalOrder").innerHTML; g_curveEdge = document.getElementById("curveEdge").innerHTML; + + g_Undo = document.getElementById("undoTranslate").innerHTML; } diff --git a/tpl/home.php b/tpl/home.php index 425bcf1..2868bd3 100755 --- a/tpl/home.php +++ b/tpl/home.php @@ -10,7 +10,7 @@ - +