@@ -39,10 +39,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3939var originFrontendJsFile = 'opt-office-mix.js' ;
4040
4141// modeled after Kurt's simplelab.ts
42- var _labEditor = null ; // for Edit mode
43- var _labInstance = null ; // for View mode
42+ var _labEditor = null ; // for Edit mode
43+ var _labViewer = null ; // for View mode
4444
45- var alreadyInit = false ;
4645
4746function executeCode ( forceStartingInstr , forceRawInputLst ) {
4847 $ ( '#loadingPane' ) . html ( 'Please wait ... executing Python code.' ) . show ( ) ;
@@ -92,14 +91,13 @@ function officeMixFinishSuccessfulExecution() {
9291 $ ( '#pyOutputPane #editCodeLinkDiv' ) . hide ( ) ; // don't have explicit "Edit code" link
9392 $ ( '#loadingPane' ) . hide ( ) ;
9493
95- $ ( "#toggleModebtn" ) . html ( "Edit code " ) ;
94+ $ ( "#toggleModebtn" ) . html ( "Edit" ) ;
9695}
9796
9897
9998// Adapted from Kurt's simplelab.ts
10099//
101- // Helper method to return the lab configuration from the provided data
102- //
100+ // Returns the lab configuration from the provided data
103101function getConfigurationFromData ( dat ) {
104102 var appVersion = { major : 1 , minor : 0 } ;
105103 var activityComponent = {
@@ -131,23 +129,17 @@ function setFronendError(lines) {
131129}
132130
133131
134-
135- function officeMixEnterViewMode ( ) {
132+ function officeMixEnterViewMode ( firstTime ) {
136133 if ( _labEditor ) {
137- _labEditor . done ( function ( ) {
138- _labEditor = null ;
139- } ) ;
134+ _labEditor . done ( function ( ) { _labEditor = null ; } ) ;
140135 }
141136
142137 Labs . takeLab ( function ( err , labInstance ) {
143138 if ( labInstance ) {
144- _labInstance = labInstance ;
145-
146- // first-time initialization:
147- if ( ! alreadyInit ) {
148- alreadyInit = true ;
139+ _labViewer = labInstance ; // global
149140
150- var savedAppState = _labInstance . components [ 0 ] . component . data ;
141+ if ( firstTime ) {
142+ var savedAppState = _labViewer . components [ 0 ] . component . data ;
151143 setToggleOptions ( savedAppState ) ;
152144 if ( savedAppState . code ) {
153145 setCodeMirrorVal ( savedAppState . code ) ;
@@ -157,65 +149,60 @@ function officeMixEnterViewMode() {
157149 executeCodeFromScratch ( ) ;
158150 }
159151 } ) ;
152+
153+ $ ( "#footer" ) . hide ( ) ;
160154}
161155
162- function officeMixEnterEditMode ( ) {
163- if ( _labInstance ) {
164- _labInstance . done ( function ( ) {
165- _labInstance = null ;
166- } ) ;
156+ function officeMixEnterEditMode ( firstTime ) {
157+ if ( _labViewer ) {
158+ _labViewer . done ( function ( ) { _labViewer = null ; } ) ;
167159 }
168160
169- Labs . editLab ( editLabCallback ) ; // create _labEditor
161+ Labs . editLab ( function ( err , labEditor ) {
162+ if ( labEditor ) {
163+ _labEditor = labEditor ; // global
164+
165+ if ( firstTime ) {
166+ _labEditor . getConfiguration ( function ( err , configuration ) {
167+ if ( configuration ) {
168+ var savedAppState = configuration . components [ 0 ] . data ;
169+ setToggleOptions ( savedAppState ) ;
170+ if ( savedAppState . code ) {
171+ setCodeMirrorVal ( savedAppState . code ) ;
172+ }
173+ }
174+ } ) ;
175+
176+ // set configuration on every code edit and option toggle, to
177+ // set the 'dirty bit' on the enclosing PPT file
178+ pyInputCodeMirror . on ( "change" , saveCurrentConfiguration ) ;
179+ $ ( 'select' ) . change ( saveCurrentConfiguration ) ;
180+ }
181+ }
182+ } ) ;
170183
171- $ ( "#toggleModebtn" ) . html ( "Visualize execution " ) ;
184+ $ ( "#toggleModebtn" ) . html ( "Preview " ) ;
172185 enterEditMode ( ) ;
186+ $ ( "#footer" ) . show ( ) ;
173187}
174188
175-
176189function saveCurrentConfiguration ( ) {
177- _labEditor . setConfiguration ( getConfigurationFromData ( getAppState ( ) ) ,
178- function ( ) { } /* empty error handler */ ) ;
179- }
180-
181-
182- function editLabCallback ( err , labEditor ) {
183- if ( labEditor ) {
184- _labEditor = labEditor ; // global
185- initLabEditorCallbacks ( ) ; // should be called only once
190+ if ( _labEditor ) {
191+ _labEditor . setConfiguration ( getConfigurationFromData ( getAppState ( ) ) ,
192+ function ( ) { } /* empty error handler */ ) ;
186193 }
187194}
188195
189196
190- function initLabEditorCallbacks ( ) {
191- if ( alreadyInit ) {
192- return ;
193- }
194- alreadyInit = true ;
195-
196- _labEditor . getConfiguration ( function ( err , configuration ) {
197- if ( configuration ) {
198- var savedAppState = configuration . components [ 0 ] . data ;
199- setToggleOptions ( savedAppState ) ;
200- if ( savedAppState . code ) {
201- setCodeMirrorVal ( savedAppState . code ) ;
202- }
203- }
204- } ) ;
205-
206- // set configuration on every code edit and option toggle, to
207- // set the 'dirty bit' on the enclosing PPT file
208- pyInputCodeMirror . on ( "change" , saveCurrentConfiguration ) ;
209- $ ( 'select' ) . change ( saveCurrentConfiguration ) ;
210- }
211-
212197$ ( document ) . ready ( function ( ) {
198+ // DON'T switch into office mix view mode ... this is just a "Preview"
199+ // that's only relevant in Edit mode
213200 $ ( "#toggleModebtn" ) . click ( function ( ) {
214201 if ( appMode == 'edit' ) {
215- officeMixEnterViewMode ( ) ;
216- }
217- else {
218- officeMixEnterEditMode ( ) ;
202+ executeCodeFromScratch ( ) ;
203+ } else {
204+ $ ( "#toggleModebtn" ) . html ( "Preview" ) ;
205+ enterEditMode ( ) ;
219206 }
220207 } ) ;
221208
@@ -275,31 +262,26 @@ $(document).ready(function() {
275262
276263 Labs . connect ( function ( err , connectionResponse ) {
277264 var initialMode = Labs . Core . LabMode [ connectionResponse . mode ] ;
278- $ ( "#testDiv" ) . append ( '<p>Initial mode: ' + initialMode + '</p>' ) ;
279265
280266 if ( initialMode == 'Edit' ) {
281- officeMixEnterEditMode ( ) ;
282- }
283- else if ( initialMode == 'View' ) {
284- officeMixEnterViewMode ( ) ;
267+ officeMixEnterEditMode ( true ) ;
268+ } else if ( initialMode == 'View' ) {
269+ officeMixEnterViewMode ( true ) ;
285270 }
286271
287272 // initialize these callbacks only after Labs.connect is successful
288273 Labs . on ( Labs . Core . EventTypes . ModeChanged , function ( data ) {
289274 if ( data . mode == 'Edit' ) {
290- officeMixEnterEditMode ( ) ;
291- }
292- else if ( data . mode == 'View' ) {
293- officeMixEnterViewMode ( ) ;
275+ officeMixEnterEditMode ( false ) ;
276+ } else if ( data . mode == 'View' ) {
277+ officeMixEnterViewMode ( false ) ;
294278 }
295279 } ) ;
296280
297281 Labs . on ( Labs . Core . EventTypes . Activate , function ( ) {
298- $ ( "#testDiv" ) . append ( '<p>Activate</p>' ) ;
299282 } ) ;
300283
301284 Labs . on ( Labs . Core . EventTypes . Deactivate , function ( ) {
302- $ ( "#testDiv" ) . append ( '<p>Deactivate</p>' ) ;
303285 } ) ;
304286 } ) ;
305287} ) ;
0 commit comments