Skip to content

Commit 5ec86ac

Browse files
committed
bah
1 parent a6d203d commit 5ec86ac

1 file changed

Lines changed: 137 additions & 7 deletions

File tree

‎v3/js/opt-office-mix.js‎

Lines changed: 137 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3636
// should all be imported BEFORE this file
3737

3838

39+
var originFrontendJsFile = 'opt-office-mix.js';
40+
41+
// modeled after Kurt's simplelab.ts
42+
var _labEditor = null; // for Edit mode
43+
var _labInstance = null; // for View mode
44+
45+
var alreadyInit = false;
46+
3947
function executeCode(forceStartingInstr, forceRawInputLst) {
4048
$('#loadingPane').html('Please wait ... executing Python code.').show();
4149

@@ -55,7 +63,7 @@ function executeCode(forceStartingInstr, forceRawInputLst) {
5563
heap_primitives: ($('#heapPrimitivesSelector').val() == 'true'),
5664
show_only_outputs: false,
5765
py_crazy_mode: false,
58-
origin: 'opt-office-mix.js'};
66+
origin: originFrontendJsFile};
5967

6068
var startingInstruction = forceStartingInstr ? forceStartingInstr : 0;
6169

@@ -75,16 +83,44 @@ function executeCode(forceStartingInstr, forceRawInputLst) {
7583
backend_script, backendOptionsObj,
7684
frontendOptionsObj,
7785
'pyOutputPane',
78-
optFinishSuccessfulExecution, handleUncaughtExceptionFunc);
86+
officeMixFinishSuccessfulExecution, handleUncaughtExceptionFunc);
7987
}
8088

8189

82-
function optFinishSuccessfulExecution() {
90+
function officeMixFinishSuccessfulExecution() {
8391
enterDisplayMode(); // do this first!
8492
$('#pyOutputPane #editCodeLinkDiv').hide(); // don't have explicit "Edit code" link
8593
$('#loadingPane').hide();
94+
95+
$("#toggleModebtn").html("Edit code");
96+
}
97+
98+
99+
// Adapted from Kurt's simplelab.ts
100+
//
101+
// Helper method to return the lab configuration from the provided data
102+
//
103+
function getConfigurationFromData(dat) {
104+
var appVersion = { major: 1, minor: 0 };
105+
var activityComponent = {
106+
type: Labs.Components.ActivityComponentType,
107+
name: "", // XXX: can this be blank?
108+
values: {},
109+
data: dat,
110+
secure: false
111+
};
112+
var configuration = {
113+
appVersion: appVersion,
114+
components: [activityComponent],
115+
name: "", // XXX: can this be blank?
116+
timeline: null,
117+
analytics: null
118+
};
119+
120+
return configuration;
86121
}
87122

123+
88124
// override setFronendError in opt-frontend-common.js
89125
// NB: this file must be included AFTER opt-frontend-common.js
90126
function setFronendError(lines) {
@@ -95,7 +131,94 @@ function setFronendError(lines) {
95131
}
96132

97133

134+
135+
function officeMixEnterViewMode() {
136+
if (_labEditor) {
137+
_labEditor.done(function() {
138+
_labEditor = null;
139+
});
140+
}
141+
142+
Labs.takeLab(function(err, labInstance) {
143+
if (labInstance) {
144+
_labInstance = labInstance;
145+
146+
// first-time initialization:
147+
if (!alreadyInit) {
148+
alreadyInit = true;
149+
150+
var savedAppState = _labInstance.components[0].component.data;
151+
setToggleOptions(savedAppState);
152+
if (savedAppState.code) {
153+
setCodeMirrorVal(savedAppState.code);
154+
}
155+
}
156+
157+
executeCodeFromScratch();
158+
}
159+
});
160+
}
161+
162+
function officeMixEnterEditMode() {
163+
if (_labInstance) {
164+
_labInstance.done(function() {
165+
_labInstance = null;
166+
});
167+
}
168+
169+
Labs.editLab(editLabCallback); // create _labEditor
170+
171+
$("#toggleModebtn").html("Visualize execution");
172+
enterEditMode();
173+
}
174+
175+
176+
function 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
186+
}
187+
}
188+
189+
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+
98212
$(document).ready(function() {
213+
$("#toggleModebtn").click(function() {
214+
if (appMode == 'edit') {
215+
officeMixEnterViewMode();
216+
}
217+
else {
218+
officeMixEnterEditMode();
219+
}
220+
});
221+
99222
// To run in https://labsjs.blob.core.windows.net/sdk/LabsJS-1.0.4/labshost.html
100223
// append "?PostMessageLabHost" to the query string.
101224
Labs.DefaultHostBuilder = function() {
@@ -133,7 +256,7 @@ $(document).ready(function() {
133256
extraKeys: {Tab: function(cm) {cm.replaceSelection(" ", "end");}}
134257
});
135258

136-
pyInputCodeMirror.setSize(null, '420px');
259+
pyInputCodeMirror.setSize(null, '350px');
137260

138261
$(window).resize(redrawConnectors);
139262

@@ -152,15 +275,22 @@ $(document).ready(function() {
152275

153276
Labs.connect(function (err, connectionResponse) {
154277
var initialMode = Labs.Core.LabMode[connectionResponse.mode];
155-
$("#testDiv").append('<p>' + initialMode + '</p>');
278+
$("#testDiv").append('<p>Initial mode: ' + initialMode + '</p>');
279+
280+
if (initialMode == 'Edit') {
281+
officeMixEnterEditMode();
282+
}
283+
else if (initialMode == 'View') {
284+
officeMixEnterViewMode();
285+
}
156286

157287
// initialize these callbacks only after Labs.connect is successful
158288
Labs.on(Labs.Core.EventTypes.ModeChanged, function(data) {
159289
if (data.mode == 'Edit') {
160-
enterEditMode();
290+
officeMixEnterEditMode();
161291
}
162292
else if (data.mode == 'View') {
163-
executeCodeFromScratch();
293+
officeMixEnterViewMode();
164294
}
165295
});
166296

0 commit comments

Comments
 (0)