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