Commit 58ca939b by jmclaus

Merge pull request #4292 from terman/jsinput

Supply initial_state attribute for jsinput tag
parents ac5f8d94 d3149f66
...@@ -580,6 +580,7 @@ class JSInput(InputTypeBase): ...@@ -580,6 +580,7 @@ class JSInput(InputTypeBase):
Attribute('gradefn', "gradefn"), Attribute('gradefn', "gradefn"),
Attribute('get_statefn', None), # Function to call in iframe Attribute('get_statefn', None), # Function to call in iframe
# to get current state. # to get current state.
Attribute('initial_state', None), # JSON string to be used as initial state
Attribute('set_statefn', None), # Function to call iframe to Attribute('set_statefn', None), # Function to call iframe to
# set state # set state
Attribute('width', "400"), # iframe width Attribute('width', "400"), # iframe width
......
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
% if saved_state: % if saved_state:
data-stored="${saved_state|x}" data-stored="${saved_state|x}"
% endif % endif
% if initial_state:
data-initial-state="${initial_state|x}"
% endif
% if get_statefn: % if get_statefn:
data-getstate="${get_statefn}" data-getstate="${get_statefn}"
% endif % endif
......
...@@ -38,6 +38,7 @@ data: | ...@@ -38,6 +38,7 @@ data: |
<jsinput gradefn="WebGLDemo.getGrade" <jsinput gradefn="WebGLDemo.getGrade"
get_statefn="WebGLDemo.getState" get_statefn="WebGLDemo.getState"
set_statefn="WebGLDemo.setState" set_statefn="WebGLDemo.setState"
initial_state='{"selectedObjects":{"cube":true,"cylinder":false}}'
width="400" width="400"
height="400" height="400"
html_file="https://studio.edx.org/c4x/edX/DemoX/asset/webGLDemo.html" html_file="https://studio.edx.org/c4x/edX/DemoX/asset/webGLDemo.html"
......
...@@ -54,6 +54,8 @@ var JSInput = (function ($, undefined) { ...@@ -54,6 +54,8 @@ var JSInput = (function ($, undefined) {
stateSetter = sectionAttr("data-setstate"), stateSetter = sectionAttr("data-setstate"),
// Get stored state // Get stored state
storedState = sectionAttr("data-stored"), storedState = sectionAttr("data-stored"),
// Get initial state
initialState = sectionAttr("data-initial-state"),
// Bypass single-origin policy only if this attribute is "false" // Bypass single-origin policy only if this attribute is "false"
// In that case, use JSChannel to do so. // In that case, use JSChannel to do so.
sop = sectionAttr("data-sop"), sop = sectionAttr("data-sop"),
...@@ -132,19 +134,25 @@ var JSInput = (function ($, undefined) { ...@@ -132,19 +134,25 @@ var JSInput = (function ($, undefined) {
// state to give it. If stateSetter is specified but calling it // state to give it. If stateSetter is specified but calling it
// fails, wait and try again, since the iframe might still be // fails, wait and try again, since the iframe might still be
// loading. // loading.
if (stateSetter && storedState) { if (stateSetter && (storedState || initialState)) {
var stateValue, jsonValue; var stateValue, jsonValue;
try { if (storedState) {
jsonValue = JSON.parse(storedState); try {
} catch (err) { jsonValue = JSON.parse(storedState);
jsonValue = storedState; } catch (err) {
} jsonValue = storedState;
}
if (typeof(jsonValue) === "object") { if (typeof(jsonValue) === "object") {
stateValue = jsonValue["state"]; stateValue = jsonValue["state"];
} else { } else {
stateValue = jsonValue; stateValue = jsonValue;
}
}
else {
// use initial_state string as the JSON string for stateValue.
stateValue = initialState;
} }
// Try calling setstate every 200ms while it throws an exception, // Try calling setstate every 200ms while it throws an exception,
......
...@@ -294,6 +294,9 @@ The following table describes the attributes of the ``jsinput`` element. ...@@ -294,6 +294,9 @@ The following table describes the attributes of the ``jsinput`` element.
- The function in your JavaScript application that saves the state of the - The function in your JavaScript application that saves the state of the
objects. objects.
- ``JSObject.setState`` - ``JSObject.setState``
* - initial_state
- A JSON string representing the initial state, if any, of the objects.
- '{"selectedObjects":{"cube":true,"cylinder":false}}'
* - width * - width
- The width of the IFrame in which your JavaScript application will be - The width of the IFrame in which your JavaScript application will be
displayed, in pixels. displayed, in pixels.
...@@ -309,4 +312,4 @@ The following table describes the attributes of the ``jsinput`` element. ...@@ -309,4 +312,4 @@ The following table describes the attributes of the ``jsinput`` element.
* - sop * - sop
- The same-origin policy (SOP), meaning that all elements have the same - The same-origin policy (SOP), meaning that all elements have the same
protocol, host, and port. To bypass the SOP, set to ``true``. protocol, host, and port. To bypass the SOP, set to ``true``.
- false - false
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment