MultiCmdsCommand.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. /**
  6. * @param cmdArray array containing command objects
  7. * @constructor
  8. */
  9. var MultiCmdsCommand = function ( cmdArray ) {
  10. Command.call( this );
  11. this.type = 'MultiCmdsCommand';
  12. this.name = 'Multiple Changes';
  13. this.cmdArray = ( cmdArray !== undefined ) ? cmdArray : [];
  14. };
  15. MultiCmdsCommand.prototype = {
  16. execute: function () {
  17. this.editor.signals.sceneGraphChanged.active = false;
  18. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  19. this.cmdArray[ i ].execute();
  20. }
  21. this.editor.signals.sceneGraphChanged.active = true;
  22. this.editor.signals.sceneGraphChanged.dispatch();
  23. },
  24. undo: function () {
  25. this.editor.signals.sceneGraphChanged.active = false;
  26. for ( var i = this.cmdArray.length - 1; i >= 0; i -- ) {
  27. this.cmdArray[ i ].undo();
  28. }
  29. this.editor.signals.sceneGraphChanged.active = true;
  30. this.editor.signals.sceneGraphChanged.dispatch();
  31. },
  32. toJSON: function () {
  33. var output = Command.prototype.toJSON.call( this );
  34. var cmds = [];
  35. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  36. cmds.push( this.cmdArray[ i ].toJSON() );
  37. }
  38. output.cmds = cmds;
  39. return output;
  40. },
  41. fromJSON: function ( json ) {
  42. Command.prototype.fromJSON.call( this, json );
  43. var cmds = json.cmds;
  44. for ( var i = 0; i < cmds.length; i ++ ) {
  45. var cmd = new window[ cmds[ i ].type ](); // creates a new object of type "json.type"
  46. cmd.fromJSON( cmds[ i ] );
  47. this.cmdArray.push( cmd );
  48. }
  49. }
  50. };