@@ -28,6 +28,11 @@ export default class HParsons extends RunestoneBase {
2828        this . divid  =  opts . orig . id ; 
2929        this . containerDiv  =  opts . orig ; 
3030        this . useRunestoneServices  =  opts . useRunestoneServices ; 
31+ 
32+         // Set the storageId (key for storing data) 
33+         var  storageId  =  super . localStorageKey ( ) ; 
34+         this . storageId  =  storageId ; 
35+         
3136        this . origElem  =  orig ; 
3237        this . origText  =  this . origElem . textContent ; 
3338        this . code  =  $ ( orig ) . text ( )  ||  "\n\n\n\n\n" ; 
@@ -71,6 +76,7 @@ export default class HParsons extends RunestoneBase {
7176
7277        // initializing functionalities for different feedback 
7378        this . feedbackController . init ( ) ; 
79+         this . checkServer ( 'hparsons' ,  true ) ; 
7480    } 
7581
7682    // copied from activecode, already modified to add parsons 
@@ -110,7 +116,6 @@ export default class HParsons extends RunestoneBase {
110116        this . feedbackController . createOutput ( ) ; 
111117    } 
112118
113-     // copied from activecode 
114119    createControls ( )  { 
115120        var  ctrlDiv  =  document . createElement ( "div" ) ; 
116121        $ ( ctrlDiv ) . addClass ( "hp_actions" ) ; 
@@ -122,8 +127,10 @@ export default class HParsons extends RunestoneBase {
122127        ctrlDiv . appendChild ( this . runButton ) ; 
123128        $ ( this . runButton ) . attr ( "type" ,  "button" ) ; 
124129        $ ( this . runButton ) . text ( "Run" ) ; 
130+         var  that  =  this ; 
125131        this . runButton . onclick  =  ( )  =>  { 
126-             this . feedbackController . runButtonHandler ( ) ; 
132+             that . feedbackController . runButtonHandler ( ) ; 
133+             that . setLocalStorage ( ) ; 
127134        } ; 
128135
129136        // Reset button 
@@ -134,15 +141,79 @@ export default class HParsons extends RunestoneBase {
134141        ctrlDiv . appendChild ( resetBtn ) ; 
135142        this . resetButton  =  resetBtn ; 
136143        this . resetButton . onclick  =  ( )  =>  { 
137-             this . hparsonsInput . resetInput ( ) ; 
138-             this . feedbackController . reset ( ) ; 
144+             that . hparsonsInput . resetInput ( ) ; 
145+             that . setLocalStorage ( ) ; 
146+             that . feedbackController . reset ( ) ; 
139147        } ; 
140148        $ ( resetBtn ) . attr ( "type" ,  "button" ) ; 
141149
142150        $ ( this . outerDiv ) . prepend ( ctrlDiv ) ; 
143151        this . controlDiv  =  ctrlDiv ; 
144152    } 
145153
154+     // Return previous answers in local storage 
155+     //  
156+     localData ( )  { 
157+         var  data  =  localStorage . getItem ( this . storageId ) ; 
158+         if  ( data  !==  null )  { 
159+             if  ( data . charAt ( 0 )  ==  "{" )  { 
160+                 data  =  JSON . parse ( data ) ; 
161+             }  else  { 
162+                 data  =  { } ; 
163+             } 
164+         }  else  { 
165+             data  =  { } ; 
166+         } 
167+         return  data ; 
168+     } 
169+     // RunestoneBase: Sent when the server has data 
170+     restoreAnswers ( serverData )  { 
171+         // TODO: not tested with server data yet.  
172+         // Server side data should be: 
173+         /* 
174+             { 
175+                 answer: Array<string>, // list of answer block content 
176+                 count: ?number // number of previous attempts if block-based feedback 
177+             } 
178+         */ 
179+         if  ( serverData . answer ) { 
180+             this . hparsonsInput . restoreAnswer ( serverData . answer ) ; 
181+         } 
182+         if  ( serverData . count )  { 
183+             this . feedbackController . checkCount  =  serverData . count ; 
184+         } 
185+     } 
186+     // RunestoneBase: Load what is in local storage 
187+     checkLocalStorage ( )  { 
188+         if  ( this . graderactive )  { 
189+             // Zihan: I think this means the component is still loading? 
190+             return ; 
191+         } 
192+         let  localData  =  this . localData ( ) ; 
193+         if  ( localData . answer )  { 
194+             this . hparsonsInput . restoreAnswer ( localData . answer ) ; 
195+         } 
196+         if  ( localData . count )  { 
197+             this . feedbackController . checkCount  =  localData . count ; 
198+         } 
199+     } 
200+     // RunestoneBase: Set the state of the problem in local storage 
201+     setLocalStorage ( data )  { 
202+         let  currentState  =  { } ; 
203+         if  ( data  ==  undefined )  { 
204+             currentState  =  { 
205+                 answer : this . hparsonsInput . getParsonsTextArray ( ) 
206+             } 
207+             if  ( this . isBlockGrading )  { 
208+                 // if this is block grading, add number of previous attempts too 
209+                 currentState . count  =  this . feedbackController . checkCount ; 
210+             } 
211+         }  else  { 
212+             currentState  =  data ; 
213+         } 
214+         localStorage . setItem ( this . storageId ,  JSON . stringify ( currentState ) ) ; 
215+     } 
216+ 
146217    logHorizontalParsonsEvent ( hparsonsEvent )  { 
147218        let  ev  =  { 
148219            event : "hparsons" , 
0 commit comments