-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Handle JSInput extra files when copying/pasting (#32847)
This takes into account the extra files that are usually required when copying problems containing JSInputs. Static files such as additional CSS and JS files needed to interact and style the problem.
- Loading branch information
1 parent
6598abb
commit b97007e
Showing
7 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* Original Source: https://files.edx.org/custom-js-example/jsinput_example.css */ | ||
|
||
.directions { | ||
font-size: large | ||
} | ||
|
||
.feedback { | ||
font-size: medium; | ||
border: 2px solid cornflowerblue; | ||
padding: 5px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!-- Original Source: https://files.edx.org/custom-js-example/jsinput_example.html --> | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Simple Question</title> | ||
<link rel="stylesheet" type="text/css" href="simple-question.css"> | ||
</head> | ||
<body> | ||
<script src="jschannel.js"></script> | ||
<script src="simple-question.js" defer></script> | ||
<img src="image.jpg" /> | ||
<label class="directions">Here is a list below, please select: | ||
<select class="choices"></select> | ||
</label> | ||
<p aria-live="polite" class="feedback"></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* Original Source: https://files.edx.org/custom-js-example/jsinput_example.js */ | ||
|
||
/* globals Channel */ | ||
|
||
(function() { | ||
'use strict'; | ||
|
||
// state will be populated via initial_state via the `setState` method. Defining dummy values here | ||
// to make the expected structure clear. | ||
var state = { | ||
availableChoices: [], | ||
selectedChoice: '' | ||
}, | ||
channel, | ||
select = document.getElementsByClassName('choices')[0], | ||
feedback = document.getElementsByClassName('feedback')[0]; | ||
|
||
function populateSelect() { | ||
// Populate the select from `state.availableChoices`. | ||
var i, option; | ||
|
||
// Clear out any pre-existing options. | ||
while (select.firstChild) { | ||
select.removeChild(select.firstChild); | ||
} | ||
|
||
// Populate the select with the available choices. | ||
for (i = 0; i < state.availableChoices.length; i++) { | ||
option = document.createElement('option'); | ||
option.value = i; | ||
option.innerHTML = state.availableChoices[i]; | ||
if (state.availableChoices[i] === state.selectedChoice) { | ||
option.selected = true; | ||
} | ||
select.appendChild(option); | ||
} | ||
feedback.innerText = "The currently selected answer is '" + state.selectedChoice + "'."; | ||
} | ||
|
||
function getGrade() { | ||
// The following return value may or may not be used to grade server-side. | ||
// If getState and setState are used, then the Python grader also gets access | ||
// to the return value of getState and can choose it instead to grade. | ||
return JSON.stringify(state.selectedChoice); | ||
} | ||
|
||
function getState() { | ||
// Returns the current state (which can be used for grading). | ||
return JSON.stringify(state); | ||
} | ||
|
||
// This function will be called with 1 argument when JSChannel is not used, | ||
// 2 otherwise. In the latter case, the first argument is a transaction | ||
// object that will not be used here | ||
// (see http://mozilla.github.io/jschannel/docs/) | ||
function setState() { | ||
var stateString = arguments.length === 1 ? arguments[0] : arguments[1]; | ||
state = JSON.parse(stateString); | ||
populateSelect(); | ||
} | ||
|
||
// Establish a channel only if this application is embedded in an iframe. | ||
// This will let the parent window communicate with this application using | ||
// RPC and bypass SOP restrictions. | ||
if (window.parent !== window) { | ||
channel = Channel.build({ | ||
window: window.parent, | ||
origin: '*', | ||
scope: 'JSInput' | ||
}); | ||
|
||
channel.bind('getGrade', getGrade); | ||
channel.bind('getState', getState); | ||
channel.bind('setState', setState); | ||
} | ||
|
||
select.addEventListener('change', function() { | ||
state.selectedChoice = select.options[select.selectedIndex].text; | ||
feedback.innerText = "You have selected '" + state.selectedChoice + | ||
"'. Click Submit to grade your answer."; | ||
}); | ||
|
||
return { | ||
getState: getState, | ||
setState: setState, | ||
getGrade: getGrade | ||
}; | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters