CmdSetScriptValue.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Created by Daniel on 21.07.15.
  3. */
  4. CmdSetScriptValue = function ( object, script, attributeName, newValue, cursorPosition ) {
  5. Cmd.call( this );
  6. this.type = 'CmdSetScriptValue';
  7. this.name = 'Set Script.' + attributeName;
  8. this.updatable = true;
  9. this.object = object;
  10. this.script = script;
  11. this.attributeName = attributeName;
  12. this.oldValue = ( script !== undefined ) ? script[ this.attributeName ] : undefined;
  13. this.newValue = newValue;
  14. this.objectUuid = ( object !== undefined ) ? object.uuid : undefined;
  15. this.cursorPosition = cursorPosition; // Format {line: 2, ch: 3}
  16. };
  17. CmdSetScriptValue.prototype = {
  18. init: function () {
  19. if ( this.object === undefined ) {
  20. this.object = this.editor.objectByUuid( this.objectUuid );
  21. }
  22. if ( this.script === undefined ) {
  23. this.script = this.editor.scripts[ this.objectUuid ][ this.index ];
  24. }
  25. },
  26. execute: function () {
  27. this.init();
  28. this.index = this.editor.scripts[ this.objectUuid ].indexOf( this.script );
  29. this.script[ this.attributeName ] = this.newValue;
  30. this.editor.signals.scriptChanged.dispatch();
  31. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  32. },
  33. undo: function () {
  34. this.init();
  35. this.script[ this.attributeName ] = this.oldValue;
  36. this.editor.signals.scriptChanged.dispatch();
  37. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  38. },
  39. update: function ( cmd ) {
  40. this.cursorPosition = cmd.cursorPosition;
  41. this.newValue = cmd.newValue;
  42. },
  43. toJSON: function () {
  44. var output = Cmd.prototype.toJSON.call( this );
  45. output.objectUuid = this.objectUuid;
  46. output.index = this.index;
  47. output.attributeName = this.attributeName;
  48. output.oldValue = this.oldValue;
  49. output.newValue = this.newValue;
  50. output.cursorPosition = this.cursorPosition;
  51. return output;
  52. },
  53. fromJSON: function ( json ) {
  54. Cmd.prototype.fromJSON.call( this, json );
  55. this.objectUuid = json.objectUuid;
  56. this.index = json.index;
  57. this.attributeName = json.attributeName;
  58. this.oldValue = json.oldValue;
  59. this.newValue = json.newValue;
  60. this.cursorPosition = json.cursorPosition;
  61. }
  62. };