Skip to content

Instantly share code, notes, and snippets.

@PEM-FR
Created April 20, 2011 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PEM-FR/c210c0344ca4d4bdfacb to your computer and use it in GitHub Desktop.
Save PEM-FR/c210c0344ca4d4bdfacb to your computer and use it in GitHub Desktop.
History Command Interface
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);
}
}
);
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];
}
}
);
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
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment