CmdAddScript.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.name = 'Add Script';
  8. this.object = object;
  9. this.objectUuid = ( object !== undefined ) ? object.uuid : undefined;
  10. this.script = script;
  11. };
  12. CmdAddScript.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 ) {
  21. this.editor.scripts[ this.object.uuid ] = [];
  22. }
  23. this.editor.scripts[ this.object.uuid ].push( this.script );
  24. this.editor.signals.scriptAdded.dispatch( this.script );
  25. },
  26. undo: function () {
  27. this.init();
  28. if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
  29. var index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  30. if ( index !== - 1 ) {
  31. this.editor.scripts[ this.object.uuid ].splice( index, 1 );
  32. }
  33. this.editor.signals.scriptRemoved.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. return output;
  40. },
  41. fromJSON: function ( json ) {
  42. Cmd.prototype.fromJSON.call( this, json );
  43. this.objectUuid = json.objectUuid;
  44. this.script = json.script;
  45. }
  46. };