MultiCmdsCommand.js 1.5 KB

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