Skip to content

Commit

Permalink
[ML4SE-185] check agreement conditions on agreement page.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrise2 committed Oct 25, 2023
1 parent f87fb74 commit fc35cc0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ ktor-client-json = { module = "io.ktor:ktor-client-json", version.ref = "ktor" }
ktor-client-serialization = { module = "io.ktor:ktor-client-serialization", version.ref = "ktor" }
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-server-core = {module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor"}
ktor-server-netty = {module = "io.ktor:ktor-server-netty-jvm", version.ref = "ktor"}
ktor-server-tests = {module = "io.ktor:ktor-server-tests-jvm", version.ref = "ktor"}
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty-jvm", version.ref = "ktor" }
ktor-server-tests = { module = "io.ktor:ktor-server-tests-jvm", version.ref = "ktor" }

slf4j = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" }
postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ package org.jetbrains.research.tasktracker.config.agreement

import kotlinx.serialization.Serializable

/**
* text can contain urls, which should be covered in <a> tag.
*/

@Serializable
data class Agreement(val text: String, val required: Boolean = true)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.components.JBPanel
import com.intellij.ui.jcef.JBCefApp
import com.intellij.util.ui.JBUI
import kotlinx.coroutines.runBlocking
import org.jetbrains.concurrency.await
import org.jetbrains.research.tasktracker.TaskTrackerPlugin
import org.jetbrains.research.tasktracker.config.content.task.base.Task
import org.jetbrains.research.tasktracker.modelInference.model.EmoModel
Expand All @@ -25,7 +27,8 @@ import org.jetbrains.research.tasktracker.tracking.webcam.WebCamTracker
import org.jetbrains.research.tasktracker.tracking.webcam.collectAllDevices
import org.jetbrains.research.tasktracker.ui.main.panel.panelStates.agreementAcceptance
import org.jetbrains.research.tasktracker.ui.main.panel.storage.GlobalPluginStorage
import org.jetbrains.research.tasktracker.ui.main.panel.template.*
import org.jetbrains.research.tasktracker.ui.main.panel.template.HtmlTemplate
import org.jetbrains.research.tasktracker.ui.main.panel.template.WebcamChoicePageTemplate
import org.jetbrains.research.tasktracker.util.UIBundle
import java.awt.BorderLayout
import java.awt.FlowLayout
Expand Down Expand Up @@ -164,6 +167,15 @@ class MainPluginPanelFactory : ToolWindowFactory {
}
}

/**
* @return **true** if any required field is not filled. **false** otherwise.
*/
fun checkInputs(): Boolean {
return runBlocking {
return@runBlocking mainWindow.executeJavaScriptAsync("allChecked()").await().toBoolean()
}
}

fun setNextAction(listener: ActionListener) = nextButton.addListener(listener)

fun setBackAction(listener: ActionListener) = backButton.addListener(listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MainPluginWindow(service: MainWindowService) {
fun checkIfRadioButtonChecked(elementId: String): Promise<JsExpressionResult> =
windowBrowser.executeJavaScriptAsync("${getJsElementByIdCommand(elementId)}.checked")

fun executeJavaScriptAsync(code: String) = windowBrowser.executeJavaScriptAsync(code)
fun executeJavaScriptAsync(@Language("JavaScript") code: String) = windowBrowser.executeJavaScriptAsync(code)

fun executeJavascript(
@Language("JavaScript") codeBeforeInject: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@ import org.jetbrains.research.tasktracker.util.survey.SurveyParser

typealias Panel = MainPluginPanelFactory

/**
* A page for collecting user data, and checkboxes for user agreement acceptance.
*/
fun Panel.agreementAcceptance() {
loadBasePage(AgreementTemplate.loadCurrentTemplate(), "ui.button.next", false)
setNextAction {
if (!checkInputs()) {
welcomePage()
}
}
}

/**
* Switches the panel to the plugin description window.
*/
fun Panel.welcomePage() {
loadBasePage(
MainPageTemplate.loadCurrentTemplate(), "ui.button.next", false
)
loadBasePage(MainPageTemplate.loadCurrentTemplate(), "ui.button.next", true)
setNextAction {
selectTask()
}
setBackAction {
agreementAcceptance()
}
}

// TODO refactor it for many configs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class AgreementTemplate(private val agreements: List<Agreement>) : HtmlBaseFileT
get() = arrayOf(agreementsToHtml())

private fun agreementsToHtml() = agreements.mapIndexed { index, element ->
"""<p><input id="$index" type="checkbox" name="$index" /> ${element.text}</p>"""
buildString {
append("""<p><input id="$index" type="checkbox" name="$index" ${'$'}{if (element.required)""")
append(""" "required" else ""}> ${element.text}</p>""")
}
}.joinToString("")

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
<div class="container">
<label for="name">Please write your name: </label>
<input type="text" name="name" id="name" placeholder="Name">
<input type="text" name="name" id="name" placeholder="Name" required>
<br>
<label for="email">Please write your email: </label>
<input type="email" name="email" id="email" placeholder="[email protected]">
<input type="email" name="email" id="email" placeholder="[email protected]" required>
<br>
%s
</div>
</div>
<script>
let requiredFields = document.querySelectorAll('input[required]')

requiredFields.forEach(function (field) {
field.onclick = function () {
if (field.type === "checkbox") {
field.style.outline = '1px solid black'
} else {
field.style.border = '1px solid black';
}
};
});

function allChecked() {
let emptyRequiredFields = [];
requiredFields.forEach(function (field) {
if (field.type === "checkbox") {
if (!field.checked) {
emptyRequiredFields.push(field);
field.style.outline = '1px solid red'
}
} else if (field.value.trim() === "") {
emptyRequiredFields.push(field);
field.style.border = "1px solid red"
}
});
return emptyRequiredFields.length > 0
}
</script>

0 comments on commit fc35cc0

Please sign in to comment.