UndoStack.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. define("dojox/sketch/UndoStack", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/lang",
  4. "dojo/_base/declare",
  5. "../xml/DomParser"
  6. ], function(dojo){
  7. dojo.getObject("sketch", true, dojox);
  8. var ta=dojox.sketch;
  9. ta.CommandTypes={ Create:"Create", Move:"Move", Modify:"Modify", Delete:"Delete", Convert:"Convert"};
  10. dojo.declare("dojox.sketch.UndoStack",null,{
  11. constructor: function(figure){
  12. this.figure=figure;
  13. this._steps=[];
  14. this._undoedSteps=[];
  15. },
  16. apply: function(state, from, to){
  17. // the key here is to neutrally move from one state to another.
  18. // we let the individual functions (i.e. undo and redo) actually
  19. // determine the from and to; all we do here is implement it.
  20. // check whether this is a fullText step
  21. if(!from && !to && state.fullText){
  22. this.figure.setValue(state.fullText);
  23. return;
  24. }
  25. var fromText=from.shapeText;
  26. var toText=to.shapeText;
  27. if(fromText.length==0&&toText.length==0){
  28. // nothing to reapply?
  29. return;
  30. }
  31. if(fromText.length==0){
  32. // We are creating.
  33. var o=dojox.xml.DomParser.parse(toText).documentElement;
  34. var a=this.figure._loadAnnotation(o);
  35. if(a) this.figure._add(a);
  36. return;
  37. }
  38. if(toText.length==0){
  39. // we are deleting.
  40. var ann=this.figure.getAnnotator(from.shapeId);
  41. this.figure._delete([ann],true);
  42. return;
  43. }
  44. // we can simply reinit and draw from the shape itself,
  45. // regardless of the actual command.
  46. var nann=this.figure.getAnnotator(to.shapeId);
  47. var no=dojox.xml.DomParser.parse(toText).documentElement;
  48. nann.draw(no);
  49. this.figure.select(nann);
  50. return;
  51. },
  52. // stack methods.
  53. add: function(/*String*/cmd, /*ta.Annotation?*/ann, /*String?*/before){
  54. var id=ann?ann.id:'';
  55. //var bbox=ann?ann.getBBox():{};
  56. var after=ann?ann.serialize():"";
  57. if(cmd==ta.CommandTypes.Delete){ after=""; }
  58. /*if(ann){
  59. // fix the bbox x/y coords
  60. var t=ann.transform;
  61. bbox.x+=t.dx;
  62. bbox.y+=t.dy;
  63. }*/
  64. var state={
  65. cmdname:cmd,
  66. //bbox:bbox,
  67. // fullText:fullText,
  68. before:{
  69. shapeId: id,
  70. shapeText:before||''
  71. },
  72. after:{
  73. shapeId: id,
  74. shapeText:after
  75. }
  76. };
  77. //console.log('dojox.sketch history add',state);
  78. this._steps.push(state);
  79. this._undoedSteps = [];
  80. },
  81. destroy: function(){},
  82. undo: function(){
  83. var state=this._steps.pop();
  84. if(state){
  85. this._undoedSteps.push(state);
  86. this.apply(state,state.after,state.before);
  87. }
  88. },
  89. redo: function(){
  90. var state=this._undoedSteps.pop();
  91. if(state){
  92. this._steps.push(state);
  93. this.apply(state,state.before,state.after);
  94. }
  95. }
  96. });
  97. return dojox.sketch.UndoStack;
  98. });