CmdSetScriptValue.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.cursorPosition = cursorPosition; // Format {line: 2, ch: 3}
  15. };
  16. CmdSetScriptValue.prototype = {
  17. execute: function () {
  18. this.script[ this.attributeName ] = this.newValue;
  19. this.editor.signals.scriptChanged.dispatch();
  20. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  21. },
  22. undo: function () {
  23. this.script[ this.attributeName ] = this.oldValue;
  24. this.editor.signals.scriptChanged.dispatch();
  25. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  26. },
  27. update: function ( cmd ) {
  28. this.cursorPosition = cmd.cursorPosition;
  29. this.newValue = cmd.newValue;
  30. },
  31. toJSON: function () {
  32. var output = Cmd.prototype.toJSON.call( this );
  33. output.objectUuid = this.object.uuid;
  34. output.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  35. output.attributeName = this.attributeName;
  36. output.oldValue = this.oldValue;
  37. output.newValue = this.newValue;
  38. output.cursorPosition = this.cursorPosition;
  39. return output;
  40. },
  41. fromJSON: function ( json ) {
  42. Cmd.prototype.fromJSON.call( this, json );
  43. this.oldValue = json.oldValue;
  44. this.newValue = json.newValue;
  45. this.attributeName = json.attributeName;
  46. this.object = this.editor.objectByUuid( json.objectUuid );
  47. this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
  48. this.cursorPosition = json.cursorPosition;
  49. }
  50. };