CmdRemoveScript.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Created by Daniel on 20.07.15.
  3. */
  4. CmdRemoveScript = function ( object, script ) {
  5. Cmd.call( this );
  6. this.type = 'CmdRemoveScript';
  7. this.name = 'Remove Script';
  8. this.object = object;
  9. this.objectUuid = ( object !== undefined ) ? object.uuid : undefined;
  10. this.script = script;
  11. };
  12. CmdRemoveScript.prototype = {
  13. init: function () {
  14. if ( this.object === undefined ) {
  15. this.object = this.editor.objectByUuid( this.objectUuid );
  16. }
  17. },
  18. execute: function () {
  19. this.init();
  20. if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
  21. this.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  22. if ( this.index !== - 1 ) {
  23. this.editor.scripts[ this.object.uuid ].splice( this.index, 1 );
  24. }
  25. this.editor.signals.scriptRemoved.dispatch( this.script );
  26. },
  27. undo: function () {
  28. this.init();
  29. if ( this.editor.scripts[ this.object.uuid ] === undefined ) {
  30. this.editor.scripts[ this.object.uuid ] = [];
  31. }
  32. this.editor.scripts[ this.object.uuid ].splice( this.index, 0, this.script );
  33. this.editor.signals.scriptAdded.dispatch( this.script );
  34. },
  35. toJSON: function () {
  36. var output = Cmd.prototype.toJSON.call( this );
  37. output.objectUuid = this.objectUuid;
  38. output.script = this.script;
  39. output.index = this.index;
  40. return output;
  41. },
  42. fromJSON: function ( json ) {
  43. Cmd.prototype.fromJSON.call( this, json );
  44. this.objectUuid = json.objectUuid;
  45. this.script = json.script;
  46. this.index = json.index;
  47. }
  48. };