UndoStack.js 2.9 KB

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