RemoveScriptCommand.js 1.7 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. import { Command } from '../Command.js';
  6. /**
  7. * @param editor Editor
  8. * @param object THREE.Object3D
  9. * @param script javascript object
  10. * @constructor
  11. */
  12. function RemoveScriptCommand( editor, object, script ) {
  13. Command.call( this, editor );
  14. this.type = 'RemoveScriptCommand';
  15. this.name = 'Remove Script';
  16. this.object = object;
  17. this.script = script;
  18. if ( this.object && this.script ) {
  19. this.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  20. }
  21. }
  22. RemoveScriptCommand.prototype = {
  23. execute: function () {
  24. if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
  25. if ( this.index !== - 1 ) {
  26. this.editor.scripts[ this.object.uuid ].splice( this.index, 1 );
  27. }
  28. this.editor.signals.scriptRemoved.dispatch( this.script );
  29. },
  30. undo: function () {
  31. if ( this.editor.scripts[ this.object.uuid ] === undefined ) {
  32. this.editor.scripts[ this.object.uuid ] = [];
  33. }
  34. this.editor.scripts[ this.object.uuid ].splice( this.index, 0, this.script );
  35. this.editor.signals.scriptAdded.dispatch( this.script );
  36. },
  37. toJSON: function () {
  38. var output = Command.prototype.toJSON.call( this );
  39. output.objectUuid = this.object.uuid;
  40. output.script = this.script;
  41. output.index = this.index;
  42. return output;
  43. },
  44. fromJSON: function ( json ) {
  45. Command.prototype.fromJSON.call( this, json );
  46. this.script = json.script;
  47. this.index = json.index;
  48. this.object = this.editor.objectByUuid( json.objectUuid );
  49. }
  50. };
  51. export { RemoveScriptCommand };