MultiCmdsCommand.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 editor Editor
  7. * @param cmdArray array containing command objects
  8. * @constructor
  9. */
  10. var MultiCmdsCommand = function ( editor, cmdArray ) {
  11. Command.call( this, editor );
  12. this.type = 'MultiCmdsCommand';
  13. this.name = 'Multiple Changes';
  14. this.cmdArray = ( cmdArray !== undefined ) ? cmdArray : [];
  15. };
  16. MultiCmdsCommand.prototype = {
  17. execute: function () {
  18. this.editor.signals.sceneGraphChanged.active = false;
  19. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  20. this.cmdArray[ i ].execute();
  21. }
  22. this.editor.signals.sceneGraphChanged.active = true;
  23. this.editor.signals.sceneGraphChanged.dispatch();
  24. },
  25. undo: function () {
  26. this.editor.signals.sceneGraphChanged.active = false;
  27. for ( var i = this.cmdArray.length - 1; i >= 0; i -- ) {
  28. this.cmdArray[ i ].undo();
  29. }
  30. this.editor.signals.sceneGraphChanged.active = true;
  31. this.editor.signals.sceneGraphChanged.dispatch();
  32. },
  33. toJSON: function () {
  34. var output = Command.prototype.toJSON.call( this );
  35. var cmds = [];
  36. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  37. cmds.push( this.cmdArray[ i ].toJSON() );
  38. }
  39. output.cmds = cmds;
  40. return output;
  41. },
  42. fromJSON: function ( json ) {
  43. Command.prototype.fromJSON.call( this, json );
  44. var cmds = json.cmds;
  45. for ( var i = 0; i < cmds.length; i ++ ) {
  46. var cmd = new window[ cmds[ i ].type ](); // creates a new object of type "json.type"
  47. cmd.fromJSON( cmds[ i ] );
  48. this.cmdArray.push( cmd );
  49. }
  50. }
  51. };