forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [AXM-361] move JS bridge to separate file
- Loading branch information
1 parent
169b301
commit e697524
Showing
2 changed files
with
58 additions
and
60 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,55 @@ | ||
function sendMessageToIOS(message) { | ||
window?.webkit?.messageHandlers?.IOSBridge?.postMessage(message); | ||
} | ||
|
||
function sendMessageToAndroid(message) { | ||
window?.AndroidBridge?.postMessage(message); | ||
} | ||
|
||
function markProblemCompletedIOS(message) { | ||
markProblemCompleted(message); | ||
} | ||
|
||
function markProblemCompletedAndroid(message) { | ||
markProblemCompleted(message); | ||
} | ||
|
||
function markProblemCompleted(message) { | ||
const data = JSON.parse(message).data; | ||
|
||
const problem = new Problem($(".xblock-student_view")); | ||
problem.submitButton.attr({disabled: "disabled"}); | ||
problem.gentle_alert("Answer submitted."); | ||
|
||
data.split("&").forEach(function (item) { | ||
const [, answer] = item.split('=', 2); | ||
problem.el.find('input[id$="' + answer + '"]').each(function () { | ||
if (this.type === "checkbox" || this.type === "radio") { | ||
this.checked = true; | ||
} else { | ||
this.value = answer; | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.IOSBridge) { | ||
window.addEventListener("messageFromIOS", function (event) { | ||
markProblemCompletedIOS(event.data); | ||
}); | ||
} | ||
|
||
if (window.AndroidBridge) { | ||
window.addEventListener("messageFromAndroid", function (event) { | ||
markProblemCompletedAndroid(event.data); | ||
}); | ||
} | ||
|
||
const originalAjax = $.ajax; | ||
$.ajax = function (options) { | ||
if (options.url && options.url.endsWith("problem_check")) { | ||
sendMessageToIOS(JSON.stringify(options)); | ||
sendMessageToAndroid(JSON.stringify(options)); | ||
} | ||
return originalAjax.call(this, options); | ||
} |
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