scoop:scoop.ui.component:0.1.0
src/questionnaire/data.scoop
Selected production source · src/questionnaire/data.scoop
1
package scoop.ui.component3
import scoop.collections.HashSet5
public struct QuestionKey(val value: String): Equals<QuestionKey>, Hash {6
public override operator fun equals(other: QuestionKey): Boolean = value == other.value7
public override fun hash(): Long = value.hash()8
}10
public struct QuestionChoiceKey(val value: String): Equals<QuestionChoiceKey>, Hash {11
public override operator fun equals(other: QuestionChoiceKey): Boolean = value == other.value12
public override fun hash(): Long = value.hash()13
}15
public enum QuestionKind {16
Single, Multiple, Freeform17
}19
public class QuestionChoice public constructor(20
public val key: QuestionChoiceKey,21
public val title: String,22
public val description: String = "",23
public val enabled: Boolean = true24
)26
public class QuestionFreeform public constructor(27
public val title: String = "Other",28
public val placeholder: String = "Type your answer",29
public val allowBlank: Boolean = false30
)32
public enum QuestionAnswer {33
Choice(QuestionChoiceKey), Text(String)34
}36
public enum QuestionnaireAnswer {37
Unanswered, Skipped, Single(QuestionAnswer), Multiple(Array<QuestionAnswer>)38
}40
public class Question public constructor(41
public val key: QuestionKey,42
public val title: String,43
public val kind: QuestionKind,44
public val description: String = "",45
public val required: Boolean = true,46
public val enabled: Boolean = true,47
public val choices: Array<QuestionChoice> = [],48
public val freeform: QuestionFreeform? = None,49
public val validate: (QuestionnaireAnswer) -> String? = { _ -> None }50
) {51
init {52
val seen = HashSet<QuestionChoiceKey>()53
for (choice in choices)54
requireComponent(seen.add(choice.key), "question choice keys must be unique")55
requireComponent(56
if (kind == QuestionKind.Freeform)57
choices.size == 0L && panelPresent(freeform)58
else59
choices.size > 0L || panelPresent(freeform),60
"question kind requires matching choices or freeform"61
)62
}63
}65
/** Text snapshots are made only for persistence; live editing has one TextEditorState. */66
public class QuestionDraft public constructor(67
public val key: QuestionKey,68
public val kind: QuestionKind,69
public val choices: Array<QuestionChoiceKey> = [],70
public val useText: Boolean = false,71
public val text: String? = None,72
public val skipped: Boolean = false73
)75
public class QuestionnaireSnapshot public constructor(76
public val drafts: Array<QuestionDraft>,77
public val active: QuestionKey? = None78
)80
public class QuestionResponse public constructor(81
public val key: QuestionKey,82
public val answer: QuestionnaireAnswer83
)85
public class QuestionnaireResult public constructor(public val answers: Array<QuestionResponse>)87
public enum QuestionnaireIssueKind {88
AnswerRequired, TextRequired, Message(String)89
}91
public class QuestionnaireIssue public constructor(92
public val key: QuestionKey,93
public val kind: QuestionnaireIssueKind94
)96
public class QuestionnaireError public constructor(97
public val key: QuestionKey,98
public val text: String99
)101
public struct QuestionnaireProgress(val position: Long, val total: Long)103
internal fun questionHasChoice(question: Question, key: QuestionChoiceKey): Boolean {104
for (choice in question.choices)105
if (choice.key == key && choice.enabled)106
return true107
return false108
}110
internal fun questionContains(keys: Array<QuestionChoiceKey>, key: QuestionChoiceKey): Boolean {111
for (value in keys)112
if (value == key)113
return true114
return false115
}117
internal fun questionFirst(records: Array<QuestionRecord>): QuestionKey? {118
for (record in records)119
if (record.question.enabled)120
return Some(record.question.key)121
return None122
}124
internal fun questionRecords(questions: Array<Question>): Array<QuestionRecord> {125
val seen = HashSet<QuestionKey>()126
return Array<QuestionRecord>(questions.size) {127
index ->128
val question = questions[index]129
requireComponent(seen.add(question.key), "question keys must be unique")130
QuestionRecord(question)131
}132
}