1 /** 2 * Creates a new UndoController. The undo controller manages an instance of 3 * UndoManager and delegates all undo and redo commands to the undo manager. 4 * 5 * @constructor 6 * @param {mindmaps.EventBus} eventBus 7 * @param {mindmaps.CommandRegistry} commandRegistry 8 */ 9 mindmaps.UndoController = function(eventBus, commandRegistry) { 10 11 /** 12 * Initialise. 13 * 14 * @private 15 */ 16 this.init = function() { 17 this.undoManager = new UndoManager(128); 18 this.undoManager.stateChanged = this.undoStateChanged.bind(this); 19 20 this.undoCommand = commandRegistry.get(mindmaps.UndoCommand); 21 this.undoCommand.setHandler(this.doUndo.bind(this)); 22 23 this.redoCommand = commandRegistry.get(mindmaps.RedoCommand); 24 this.redoCommand.setHandler(this.doRedo.bind(this)); 25 26 eventBus.subscribe(mindmaps.Event.DOCUMENT_OPENED, this.documentOpened 27 .bind(this)); 28 29 eventBus.subscribe(mindmaps.Event.DOCUMENT_CLOSED, this.documentClosed 30 .bind(this)); 31 }; 32 33 /** 34 * Handler for state changed event from undo manager. 35 */ 36 this.undoStateChanged = function() { 37 this.undoCommand.setEnabled(this.undoManager.canUndo()); 38 this.redoCommand.setEnabled(this.undoManager.canRedo()); 39 }; 40 41 /** 42 * @see mindmaps.UndoManager#addUndo 43 */ 44 this.addUndo = function(undoFunc, redoFunc) { 45 this.undoManager.addUndo(undoFunc, redoFunc); 46 }; 47 48 /** 49 * Handler for undo command. 50 */ 51 this.doUndo = function() { 52 this.undoManager.undo(); 53 }; 54 55 /** 56 * Handler for redo command. 57 */ 58 this.doRedo = function() { 59 this.undoManager.redo(); 60 }; 61 62 /** 63 * Handler for document opened event. 64 */ 65 this.documentOpened = function() { 66 this.undoManager.reset(); 67 this.undoStateChanged(); 68 }; 69 70 /** 71 * Handler for document closed event. 72 */ 73 this.documentClosed = function() { 74 this.undoManager.reset(); 75 this.undoStateChanged(); 76 }; 77 78 this.init(); 79 };