secret
Last active

History Command Interface

  • Download Gist
DeleteSelectedElement.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
dojo.provide("dojox.architect.commands.DeleteSelectedElement");
 
dojo.declare(
"dojox.architect.commands.DeleteSelectedElement",
dojox.architect.undo.ICommand,
{
// summary:
// Managing the deletion of an element
 
// _elements: [private] array
// Elements to delete
_elements: [],
// _widget: [private] dijit._Widget
// Widget creating the dojox.architect.commands.DeleteSelectedElement
// Used to namespace the publish
_widget: null,
 
constructor : function (/*dojo.NodeList*/ elements, /*dijit._Widget*/ widget){
elements.forEach(function(element){
this._elements.push({
item: element,
parent: element.parentNode,
suivant: element.nextSibling
});
}, this);
this._widget = widget;
},
 
getElements: function(){
return this._elements;
},
 
execute : function (){
dojo.forEach(this._elements, function(element){
var idElement = element.item.id;
element.parent.removeChild(element.item);
dojo.publish(this._widget.id + "/onDeleteElement", [idElement]);
}, this);
},
 
retablir : function (){
this.execute();
},
 
annule : function (){
dojo.forEach(this._elements, function(element){
element.parent.insertBefore(
element.item,
element.suivant
);
}, this);
}
}
);
History.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
dojo.provide("dojox.architect.undo.History");
 
dojo.declare(
"dojox.architect.undo.History",
null,
{
// summary:
// Factory of the Design Pattern Command :
// http://en.wikipedia.org/wiki/Command_pattern
// It's used to manage the historification of commands
// and undo/redo
 
// _commands: [private] dojox.architect.undo.ICommand[]
// Array containing all the issued commands
_commands : new Array(),
 
// _position: [private] int
// Array iterator
_position : -1,
 
clear : function(){
// summary:
// Clear History
this._commands = [];
this._position = -1;
},
 
addCommand : function(command){
// summary:
// Add a new command at the end of the history
// addCommand() is generally followed by an execute()
this._commands = this._commands.slice(0, this._position + 1);
this._commands.push(command);
},
 
undo : function(){
// summary:
// undo previous command
// return:
// dojox.architect.undo.ICommand
var commandToUndo = this._commands[this._position];
if(commandToUndo){
commandToUndo.undo();
--this._position;
return commandToUndo;
}
},
 
execute : function(){
// summary:
// Execute the next commande
// return:
// dojox.architect.undo.ICommand
if(this._commands[this._position + 1]){
++this._position;
this._commands[this._position].execute();
return this._commands[this._position];
}
},
 
redo : function(){
// summary:
// Redo the next commmand
// return:
// dojox.architect.undo.ICommand
if(this._commands[this._position + 1]){
++this._position;
this._commands[this._position].redo();
return this._commands[this._position];
}
},
 
getLastCommand : function(){
// summary:
// Return the last command of the history
return this._commands[this._commands.length - 1];
},
 
removeLastCommand : function(){
// summary:
// Delete the last command of the history
// return: dojox.architect.undo.ICommand
// The deleted command
--this._position;
return this._commands.pop();
},
 
count : function(){
// summary:
// Return the number of commands into the history
return this._commands.length;
},
 
getCurrentCommand : function(){
// summary:
// Return the current command
// Which is the last executed command
return this._commands[this._position];
}
}
);
ICommand.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
dojo.provide("dojox.architect.undo.ICommand");
 
dojo.declare(
"dojox.architect.undo.ICommand",
null,
{
// summary:
// Commands Interface used to manage an history and undo/redo
// All historized commands should implement this interface
//
// description:
// Command Interface from Design Pattern :
// http://en.wikipedia.org/wiki/Command_pattern
 
execute : function (){
// summary:
// Code executed by the command
},
 
redo : function (){
// summary:
// Code which redo the command
// It's often the same code as in execute,
// in this case, we could call this.execute();
// In some case where the redo is different from the
// execute, we can add the behaviours here
},
 
undo : function (){
// summary:
// Code which undo the command
}
}
);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.