Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML4SE-228] Agreements saves into the special json file. #77

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ data class MainTaskTrackerConfig(

const val PLUGIN_NAME = "tasktracker"
val pluginFolderPath = "${PathManager.getPluginsPath()}/$PLUGIN_NAME"
val agreementFilePath = "$pluginFolderPath/agreement/agreement.json"
val logFilesFolder = "$pluginFolderPath/logs"
const val PLUGIN_PROPERTIES_FILE = "$PLUGIN_NAME.properties"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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.serialization.json.Json
import org.jetbrains.concurrency.Promise
import org.jetbrains.research.tasktracker.TaskTrackerPlugin
import org.jetbrains.research.tasktracker.config.content.task.base.Task
Expand All @@ -24,6 +25,7 @@ import org.jetbrains.research.tasktracker.tracking.fileEditor.FileEditorTracker
import org.jetbrains.research.tasktracker.tracking.toolWindow.ToolWindowTracker
import org.jetbrains.research.tasktracker.tracking.webcam.WebCamTracker
import org.jetbrains.research.tasktracker.tracking.webcam.collectAllDevices
import org.jetbrains.research.tasktracker.ui.main.panel.models.AgreementChecker
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.HtmlTemplate
Expand Down Expand Up @@ -167,10 +169,21 @@ class MainPluginPanelFactory : ToolWindowFactory {
}

/**
* If all required fields are filled then
* [saveAgreements][org.jetbrains.research.tasktracker.ui.main.panel.saveAgreements]
* function will be called.
*
* @return **true** if any required field is not filled. **false** otherwise.
*/
fun checkInputs(): Promise<Boolean> =
mainWindow.executeJavaScriptAsync("allChecked()").then { it.toBoolean() }
mainWindow.executeJavaScriptAsync("checkAllInputs()").then {
val agreementChecker = Json.decodeFromString(AgreementChecker.serializer(), it.toString())
if (agreementChecker.allRequiredChecked()) {
saveAgreements(it.toString())
return@then false
}
true
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jetbrains.research.tasktracker.ui.main.panel

import com.intellij.openapi.util.io.FileUtil
import org.jetbrains.research.tasktracker.config.MainTaskTrackerConfig
import org.jetbrains.research.tasktracker.util.UIBundle
import java.io.File
import javax.swing.JButton

fun createJButton(
Expand All @@ -11,3 +14,14 @@ fun createJButton(
isBorderPainted = isBorderPaintedProp
isVisible = isVisibleProp
}

/**
* Saves selected agreements as a json file.
*
* @property agreementString is an object to save in String format
*/
fun saveAgreements(agreementString: String) {
val agreementFile = File(MainTaskTrackerConfig.agreementFilePath)
FileUtil.createIfDoesntExist(agreementFile)
agreementFile.writeText(agreementString)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jetbrains.research.tasktracker.ui.main.panel.models

import kotlinx.serialization.Serializable

@Serializable
data class AgreementChecker(val name: String, val email: String, val agreements: List<Agreement>) {

fun allRequiredChecked() = agreements.filter { it.required }.all { it.checked }
}

@Serializable
data class Agreement(val checked: Boolean, val text: String, val required: Boolean)
2 changes: 1 addition & 1 deletion ijPlugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<toolWindow factoryClass="org.jetbrains.research.tasktracker.ui.main.panel.MainPluginPanelFactory"
id="TaskTracker" anchor="right"/>
<postStartupActivity implementation="org.jetbrains.research.tasktracker.activities.InitActivity"/>
<notificationGroup id="Tasktracker" displayType="BALLOON"/>
<notificationGroup id="tasktracker" displayType="BALLOON"/>
</extensions>

<actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<script>
let requiredFields = document.querySelectorAll('input[required]')

// Loop through each required input element and add the 'required' class to its parent label
requiredFields.forEach(function (inputElement) {
mikrise2 marked this conversation as resolved.
Show resolved Hide resolved
const parentLabel = inputElement.parentElement.querySelector('label');
if (parentLabel) {
Expand All @@ -36,19 +35,44 @@
};
});

function allChecked() {
let emptyRequiredFields = [];
function checkAllInputs() {
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
return JSON.stringify(getData())
}

function getData() {
let email = document.getElementById('email')
let name = document.getElementById('name')
let inputs = {
email: email.value,
name: name.value,
agreements: []
}
let checkboxes = document.querySelectorAll('input[type=checkbox]')
checkboxes.forEach(function (field) {
inputs.agreements.push({
checked: field.checked,
text: findLabelForControl(field),
required: field.hasAttribute("required")
})
})
return inputs
}

function findLabelForControl(field) {
let labelId = field.id
let labels = document.getElementsByTagName('label')
for (let i = 0; i < labels.length; i++) {
if (labels[i].htmlFor === labelId)
return labels[i].innerHTML
}
}
</script>

This file was deleted.

Loading