1 /** 2 * An facade to add Commands, undo and redo them 3 * @this {History} 4 * @constructor 5 * @author Zack Newsham zack_newsham@yahoo.co.uk 6 * @author Alex <alex@scriptoid.com> 7 */ 8 function History(){ 9 } 10 11 /**Object is a figure*/ 12 History.OBJECT_FIGURE = 0; 13 14 /**Object is a connector*/ 15 History.OBJECT_CONNECTOR = 1; 16 17 /**Object is a connection point*/ 18 History.OBJECT_CONNECTION_POINT = 2; 19 20 /**Object is a generic object*/ 21 History.OBJECT_STATIC = 3; 22 23 /**Object is a group*/ 24 History.OBJECT_GROUP = 4; 25 26 /**Object is a glue*/ 27 History.OBJECT_GLUE = 5; 28 29 /**Where the {Array} or commands is stored*/ 30 History.COMMANDS = []; 31 32 /**The current command inde within the vector of undoable objects. At that position there will be a Command*/ 33 History.CURRENT_POINTER = -1; 34 35 36 37 /* Add an action to the stack of undoable actions. 38 * We position at current pointer, remove everything after it and then add the new 39 * action 40 * @param {Command} command - the command History must store 41 */ 42 History.addUndo = function(command){ 43 if(doUndo){ 44 /**As we are now positioned on CURRENT_POINTER(where current Command is stored) we will 45 *delete anything after it, add new Command and increase CURRENT_POINTER 46 **/ 47 48 //remove commands after current command 49 History.COMMANDS.splice(History.CURRENT_POINTER +1, History.COMMANDS.length); 50 51 //add new command 52 History.COMMANDS.push(command); 53 54 //increase the current pointer 55 History.CURRENT_POINTER++; 56 } 57 } 58 59 /**Undo current command*/ 60 History.undo = function(){ 61 if(History.CURRENT_POINTER >= 0){ 62 Log.info('undo()->Type of action: ' + History.COMMANDS[History.CURRENT_POINTER].oType); 63 History.COMMANDS[History.CURRENT_POINTER].undo(); 64 65 History.CURRENT_POINTER --; 66 } 67 } 68 69 /**Redo a command*/ 70 History.redo = function(){ 71 if(History.CURRENT_POINTER + 1 < History.COMMANDS.length){ 72 Log.info('redo()->Type of action: ' + History.COMMANDS[History.CURRENT_POINTER+1].oType); 73 History.COMMANDS[History.CURRENT_POINTER + 1].redo(); 74 75 History.CURRENT_POINTER++; 76 } 77 } 78 79 80