MultiCmdsCommand.js 1.7 KB

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