Scoop API

scoop:scoop.ui.component:0.1.0

src/questionnaire/data.scoop

Selected production source · src/questionnaire/data.scoop

1package scoop.ui.component
3import scoop.collections.HashSet
5public struct QuestionKey(val value: String): Equals<QuestionKey>, Hash {
6 public override operator fun equals(other: QuestionKey): Boolean = value == other.value
7 public override fun hash(): Long = value.hash()
8}
10public struct QuestionChoiceKey(val value: String): Equals<QuestionChoiceKey>, Hash {
11 public override operator fun equals(other: QuestionChoiceKey): Boolean = value == other.value
12 public override fun hash(): Long = value.hash()
15public enum QuestionKind {
16 Single, Multiple, Freeform
19public class QuestionChoice public constructor(
20 public val key: QuestionChoiceKey,
21 public val title: String,
22 public val description: String = "",
23 public val enabled: Boolean = true
26public class QuestionFreeform public constructor(
27 public val title: String = "Other",
28 public val placeholder: String = "Type your answer",
29 public val allowBlank: Boolean = false
32public enum QuestionAnswer {
33 Choice(QuestionChoiceKey), Text(String)
36public enum QuestionnaireAnswer {
37 Unanswered, Skipped, Single(QuestionAnswer), Multiple(Array<QuestionAnswer>)
40public 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 else
59 choices.size > 0L || panelPresent(freeform),
60 "question kind requires matching choices or freeform"
61 )
62 }
65/** Text snapshots are made only for persistence; live editing has one TextEditorState. */
66public 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 = false
75public class QuestionnaireSnapshot public constructor(
76 public val drafts: Array<QuestionDraft>,
77 public val active: QuestionKey? = None
80public class QuestionResponse public constructor(
81 public val key: QuestionKey,
82 public val answer: QuestionnaireAnswer
85public class QuestionnaireResult public constructor(public val answers: Array<QuestionResponse>)
87public enum QuestionnaireIssueKind {
88 AnswerRequired, TextRequired, Message(String)
91public class QuestionnaireIssue public constructor(
92 public val key: QuestionKey,
93 public val kind: QuestionnaireIssueKind
96public class QuestionnaireError public constructor(
97 public val key: QuestionKey,
98 public val text: String
101public struct QuestionnaireProgress(val position: Long, val total: Long)
103internal fun questionHasChoice(question: Question, key: QuestionChoiceKey): Boolean {
104 for (choice in question.choices)
105 if (choice.key == key && choice.enabled)
106 return true
107 return false
110internal fun questionContains(keys: Array<QuestionChoiceKey>, key: QuestionChoiceKey): Boolean {
111 for (value in keys)
112 if (value == key)
113 return true
114 return false
117internal fun questionFirst(records: Array<QuestionRecord>): QuestionKey? {
118 for (record in records)
119 if (record.question.enabled)
120 return Some(record.question.key)
121 return None
124internal 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 }