CmdAddScript.js 1.2 KB

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