@lango-project/lango-client 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. package/LICENSE +1 -0
  2. package/NOTICE +5 -1
  3. package/README.md +570 -906
  4. package/cli/.lango/localization.mjs +108 -0
  5. package/{dist/node → cli}/.lango/runtime.mjs +112 -19
  6. package/cli/.lango/vendor/i18next.LICENSE +21 -0
  7. package/cli/.lango/vendor/i18next.mjs +2434 -0
  8. package/cli/create.mjs +62 -0
  9. package/cli/create.mjs.map +1 -0
  10. package/dist/.lango/localization.mjs +108 -0
  11. package/dist/.lango/runtime.mjs +112 -19
  12. package/dist/.lango/vendor/i18next.LICENSE +21 -0
  13. package/dist/.lango/vendor/i18next.mjs +2434 -0
  14. package/dist/accordion.mjs +277 -0
  15. package/dist/accordion.mjs.map +1 -0
  16. package/dist/activities.mjs +185 -0
  17. package/dist/activities.mjs.map +1 -0
  18. package/dist/app.mjs +636 -0
  19. package/dist/app.mjs.map +1 -0
  20. package/dist/boot.mjs +24 -0
  21. package/dist/boot.mjs.map +1 -0
  22. package/dist/bootstrap-LICENSE +21 -0
  23. package/dist/bootstrap.min.css +6 -0
  24. package/dist/bootstrap.min.css.map +1 -0
  25. package/dist/button.mjs +77 -0
  26. package/dist/button.mjs.map +1 -0
  27. package/dist/chip.mjs +134 -0
  28. package/dist/chip.mjs.map +1 -0
  29. package/dist/client.css +95 -0
  30. package/dist/collections.mjs +77 -0
  31. package/dist/collections.mjs.map +1 -0
  32. package/dist/combo_box.mjs +408 -0
  33. package/dist/combo_box.mjs.map +1 -0
  34. package/dist/content.mjs +123 -0
  35. package/dist/content.mjs.map +1 -0
  36. package/dist/control.mjs +139 -0
  37. package/dist/control.mjs.map +1 -0
  38. package/dist/date_input.mjs +242 -0
  39. package/dist/date_input.mjs.map +1 -0
  40. package/dist/error_reporter.mjs +136 -0
  41. package/dist/error_reporter.mjs.map +1 -0
  42. package/dist/fields.mjs +454 -0
  43. package/dist/fields.mjs.map +1 -0
  44. package/dist/file_input.mjs +227 -0
  45. package/dist/file_input.mjs.map +1 -0
  46. package/dist/form.mjs +272 -0
  47. package/dist/form.mjs.map +1 -0
  48. package/dist/heartbeat.mjs +167 -254
  49. package/dist/heartbeat.mjs.map +1 -1
  50. package/dist/index.html +18 -0
  51. package/dist/indicators.mjs +127 -0
  52. package/dist/indicators.mjs.map +1 -0
  53. package/dist/input_changes.mjs +58 -0
  54. package/dist/input_changes.mjs.map +1 -0
  55. package/dist/input_group.mjs +38 -0
  56. package/dist/input_group.mjs.map +1 -0
  57. package/dist/layouts.mjs +195 -0
  58. package/dist/layouts.mjs.map +1 -0
  59. package/dist/library.mjs +130 -807
  60. package/dist/library.mjs.map +1 -1
  61. package/dist/longpoll.mjs +254 -0
  62. package/dist/longpoll.mjs.map +1 -0
  63. package/dist/main.mjs +138 -0
  64. package/dist/main.mjs.map +1 -0
  65. package/dist/markdown.mjs +61 -0
  66. package/dist/markdown.mjs.map +1 -0
  67. package/dist/menus.mjs +468 -0
  68. package/dist/menus.mjs.map +1 -0
  69. package/dist/navigation.mjs +260 -410
  70. package/dist/navigation.mjs.map +1 -1
  71. package/dist/numeric_input.mjs +181 -0
  72. package/dist/numeric_input.mjs.map +1 -0
  73. package/dist/otp_input.mjs +113 -0
  74. package/dist/otp_input.mjs.map +1 -0
  75. package/dist/presentations.mjs +852 -0
  76. package/dist/presentations.mjs.map +1 -0
  77. package/dist/renderer.mjs +293 -0
  78. package/dist/renderer.mjs.map +1 -0
  79. package/dist/rich_content.mjs +99 -0
  80. package/dist/rich_content.mjs.map +1 -0
  81. package/dist/tables.mjs +369 -0
  82. package/dist/tables.mjs.map +1 -0
  83. package/dist/tabs.mjs +205 -0
  84. package/dist/tabs.mjs.map +1 -0
  85. package/dist/templates.mjs +46 -0
  86. package/dist/templates.mjs.map +1 -0
  87. package/dist/vendor/DOMPurify-LICENSE +202 -0
  88. package/dist/vendor/dompurify.mjs +2540 -0
  89. package/dist/vendor/flatpickr-LICENSE.md +21 -0
  90. package/dist/vendor/flatpickr.min.css +13 -0
  91. package/dist/vendor/flatpickr.mjs +2340 -0
  92. package/dist/vendor/flatpickr.mjs.map +7 -0
  93. package/dist/vendor/marked-LICENSE.md +44 -0
  94. package/dist/vendor/marked.esm.js.map +7 -0
  95. package/dist/vendor/marked.mjs +85 -0
  96. package/dist/vendor/purify.es.mjs.map +1 -0
  97. package/dist/widgets.mjs +197 -0
  98. package/dist/widgets.mjs.map +1 -0
  99. package/docs/implementation.md +42 -0
  100. package/package.json +25 -43
  101. package/src/accordion.lango +198 -0
  102. package/src/activities.lango +132 -0
  103. package/src/app.lango +508 -0
  104. package/src/boot.lango +12 -0
  105. package/src/button.lango +49 -0
  106. package/src/chip.lango +94 -0
  107. package/src/collections.lango +46 -0
  108. package/src/combo_box.lango +298 -0
  109. package/src/content.lango +75 -0
  110. package/src/control.lango +100 -0
  111. package/src/create.lango +42 -0
  112. package/src/date_input.lango +215 -0
  113. package/src/error_reporter.lango +95 -0
  114. package/src/fields.lango +327 -0
  115. package/src/file_input.lango +175 -0
  116. package/src/form.lango +195 -0
  117. package/src/heartbeat.lango +136 -199
  118. package/src/indicators.lango +94 -0
  119. package/src/input_changes.lango +33 -0
  120. package/src/input_group.lango +22 -0
  121. package/src/layouts.lango +137 -0
  122. package/src/library.lango +194 -599
  123. package/src/longpoll.lango +187 -0
  124. package/src/main.lango +109 -0
  125. package/src/markdown.lango +48 -0
  126. package/src/menus.lango +336 -0
  127. package/src/navigation.lango +209 -299
  128. package/src/numeric_input.lango +131 -0
  129. package/src/otp_input.lango +83 -0
  130. package/src/presentations.lango +617 -0
  131. package/src/renderer.lango +288 -0
  132. package/src/rich_content.lango +66 -0
  133. package/src/tables.lango +271 -0
  134. package/src/tabs.lango +144 -0
  135. package/src/templates.lango +29 -0
  136. package/src/widgets.lango +141 -0
  137. package/web/client.css +95 -0
  138. package/web/index.html +18 -0
  139. package/dist/application.mjs +0 -429
  140. package/dist/application.mjs.map +0 -1
  141. package/dist/coordinator.mjs +0 -91
  142. package/dist/coordinator.mjs.map +0 -1
  143. package/dist/demo.mjs +0 -110
  144. package/dist/demo.mjs.map +0 -1
  145. package/dist/demo_controls.mjs +0 -59
  146. package/dist/demo_controls.mjs.map +0 -1
  147. package/dist/errors.mjs +0 -201
  148. package/dist/errors.mjs.map +0 -1
  149. package/dist/form_state.mjs +0 -266
  150. package/dist/form_state.mjs.map +0 -1
  151. package/dist/inheritance.mjs +0 -240
  152. package/dist/inheritance.mjs.map +0 -1
  153. package/dist/input_values.mjs +0 -65
  154. package/dist/input_values.mjs.map +0 -1
  155. package/dist/node/.lango/xmldom/conventions.js +0 -429
  156. package/dist/node/.lango/xmldom/dom-parser.js +0 -591
  157. package/dist/node/.lango/xmldom/dom.js +0 -3759
  158. package/dist/node/.lango/xmldom/entities.js +0 -2171
  159. package/dist/node/.lango/xmldom/errors.js +0 -205
  160. package/dist/node/.lango/xmldom/grammar.js +0 -559
  161. package/dist/node/.lango/xmldom/index.js +0 -41
  162. package/dist/node/.lango/xmldom/package.json +0 -1
  163. package/dist/node/.lango/xmldom/sax.js +0 -983
  164. package/dist/node/.lango/xmldom.LICENSE.md +0 -8
  165. package/dist/node/browser_host.mjs +0 -351
  166. package/dist/node/browser_host.mjs.map +0 -1
  167. package/dist/node/create_app.mjs +0 -118
  168. package/dist/node/create_app.mjs.map +0 -1
  169. package/dist/presentation.mjs +0 -89
  170. package/dist/presentation.mjs.map +0 -1
  171. package/dist/protocol.mjs +0 -706
  172. package/dist/protocol.mjs.map +0 -1
  173. package/dist/session.mjs +0 -471
  174. package/dist/session.mjs.map +0 -1
  175. package/dist/tokens.mjs +0 -298
  176. package/dist/tokens.mjs.map +0 -1
  177. package/examples/contacts.lango +0 -293
  178. package/examples/contacts_flow.lango +0 -174
  179. package/examples/contacts_store.lango +0 -109
  180. package/examples/contacts_store_test.lango +0 -65
  181. package/examples/demo.css +0 -87
  182. package/examples/demo_test.lango +0 -356
  183. package/examples/index.html +0 -32
  184. package/examples/pages.lango +0 -174
  185. package/examples/server.lango +0 -189
  186. package/examples/worker.lango +0 -142
  187. package/src/application.lango +0 -327
  188. package/src/browser_host.lango +0 -276
  189. package/src/coordinator.lango +0 -57
  190. package/src/demo.lango +0 -110
  191. package/src/demo_controls.lango +0 -53
  192. package/src/errors.lango +0 -149
  193. package/src/errors_test.lango +0 -157
  194. package/src/form_state.lango +0 -168
  195. package/src/heartbeat_test.lango +0 -479
  196. package/src/inheritance.lango +0 -167
  197. package/src/input_values.lango +0 -37
  198. package/src/logout_test.lango +0 -564
  199. package/src/navigation_test.lango +0 -556
  200. package/src/presentation.lango +0 -55
  201. package/src/protocol.lango +0 -787
  202. package/src/renderer_test.lango +0 -2047
  203. package/src/session.lango +0 -343
  204. package/src/session_test.lango +0 -455
  205. package/src/tabs_test.lango +0 -216
  206. package/src/tokens.lango +0 -215
  207. package/template/.vscode/extensions.json +0 -3
  208. package/template/.vscode/settings.json +0 -9
  209. package/template/README.md +0 -44
  210. package/template/gitignore +0 -4
  211. package/template/package.json +0 -22
  212. package/template/public/app.css +0 -10
  213. package/template/public/index.html +0 -24
  214. package/template/src/client.lango +0 -78
  215. package/template/src/pages.lango +0 -55
  216. package/template/src/server.lango +0 -24
  217. package/template/src/worker.lango +0 -77
  218. package/template/test/app.lango +0 -9
  219. package/tools/browser_runner.lango +0 -32
  220. package/tools/browser_test.lango +0 -343
  221. package/tools/create_app.lango +0 -88
  222. package/tools/create_app_test.lango +0 -43
  223. package/tools/starter_test.lango +0 -227
  224. /package/{dist/node → cli}/.lango/bignumber.mjs +0 -0
  225. /package/{dist → cli}/.lango/xmldom/conventions.js +0 -0
  226. /package/{dist → cli}/.lango/xmldom/dom-parser.js +0 -0
  227. /package/{dist → cli}/.lango/xmldom/dom.js +0 -0
  228. /package/{dist → cli}/.lango/xmldom/entities.js +0 -0
  229. /package/{dist → cli}/.lango/xmldom/errors.js +0 -0
  230. /package/{dist → cli}/.lango/xmldom/grammar.js +0 -0
  231. /package/{dist → cli}/.lango/xmldom/index.js +0 -0
  232. /package/{dist → cli}/.lango/xmldom/package.json +0 -0
  233. /package/{dist → cli}/.lango/xmldom/sax.js +0 -0
  234. /package/{dist → cli}/.lango/xmldom.LICENSE.md +0 -0
@@ -0,0 +1,215 @@
1
+ import js "flatpickr" as picker
2
+ import js global Intl
3
+ import js global Date
4
+ import js global Number
5
+ import js global RegExp
6
+ import js global Object
7
+ import "./fields"
8
+ class Field
9
+ import "./templates"
10
+ cloneMarkup(name, xml)
11
+ import "./input_changes"
12
+ class InputChanges
13
+
14
+ export dateText(value)
15
+ export dateRange(value, min = null, max = null)
16
+
17
+ dateText(value)
18
+ if value == null or value == ""
19
+ return null
20
+ pattern = new RegExp("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")
21
+ if !is_string(value) or !pattern.test(value)
22
+ throw "Date requires YYYY-MM-DD calendar text"
23
+ year = Number/parseInt(value.slice(0, 4), 10)
24
+ month = Number/parseInt(value.slice(5, 7), 10)
25
+ day = Number/parseInt(value.slice(8, 10), 10)
26
+ days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
27
+ if year < 1 or month < 1 or month > 12
28
+ throw "Invalid calendar date"
29
+ if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
30
+ days[1] = 29
31
+ index = month - 1
32
+ if day < 1 or day > days[index]
33
+ throw "Invalid calendar date"
34
+ return value
35
+
36
+ dateRange(value, min = null, max = null)
37
+ if value == null
38
+ value = {}
39
+ if !is_object(value) or is_array(value)
40
+ throw "Date value requires a start/end map"
41
+ start = dateText(value.start)
42
+ end = dateText(value.end)
43
+ if end != null and (start == null or end.localeCompare(start) < 0)
44
+ throw "Date range end must not precede its start"
45
+ for [start, end] as date
46
+ if date != null and ((min != null and date.localeCompare(min) < 0) or (max != null and date.localeCompare(max) > 0))
47
+ throw "Date is outside its min/max range"
48
+ return {
49
+ start: start
50
+ end: end
51
+ }
52
+
53
+ calendarLocale(options)
54
+ locale = options.locale
55
+ if locale == null
56
+ locale = []
57
+ months = []
58
+ shortMonths = []
59
+ weekdays = []
60
+ shortWeekdays = []
61
+ month = new Intl/DateTimeFormat(locale, {
62
+ month: "long"
63
+ timeZone: "UTC"
64
+ })
65
+ shortMonth = new Intl/DateTimeFormat(locale, {
66
+ month: "short"
67
+ timeZone: "UTC"
68
+ })
69
+ day = new Intl/DateTimeFormat(locale, {
70
+ weekday: "long"
71
+ timeZone: "UTC"
72
+ })
73
+ shortDay = new Intl/DateTimeFormat(locale, {
74
+ weekday: "short"
75
+ timeZone: "UTC"
76
+ })
77
+ index = 0
78
+ stamp = null
79
+ loop while index < 12
80
+ stamp = Date/UTC(2020, index, 1)
81
+ months.add(month.format(stamp))
82
+ shortMonths.add(shortMonth.format(stamp))
83
+ index = index + 1
84
+ index = 1
85
+ loop while index <= 7
86
+ stamp = Date/UTC(2020, 10, index)
87
+ weekdays.add(day.format(stamp))
88
+ shortWeekdays.add(shortDay.format(stamp))
89
+ index = index + 1
90
+ return {
91
+ weekdays: {
92
+ longhand: weekdays
93
+ shorthand: shortWeekdays
94
+ }
95
+ months: {
96
+ longhand: months
97
+ shorthand: shortMonths
98
+ }
99
+ rangeSeparator: " – "
100
+ monthAriaLabel: ""
101
+ yearAriaLabel: ""
102
+ }
103
+
104
+ export class DateInput(form, props) : Field
105
+ methods
106
+ getValue()
107
+ setValue(value)
108
+ setDisabled(value)
109
+ dispose()
110
+
111
+ DateInput(form, props)
112
+ super(form, props, cloneMarkup("date-input", <input type="text" class="form-control" />))
113
+ @changes = new InputChanges(form, props.url)
114
+ @locked = props.readonly == true
115
+ @min = dateText(props.min)
116
+ @max = dateText(props.max)
117
+ if @min != null and @max != null and @min.localeCompare(@max) > 0
118
+ throw "Date min must not exceed max"
119
+ @range = dateRange(props.value, @min, @max)
120
+ months = props.multiMonth
121
+ if months == null
122
+ months = 1
123
+ if !is_number(months) or !Number/isInteger(months) or months < 1 or months > 12
124
+ throw "Date multiMonth must be an integer between 1 and 12"
125
+ first = props.firstWeekDay
126
+ if first == null
127
+ first = 1
128
+ if !is_number(first) or !Number/isInteger(first) or first < 0 or first > 6
129
+ throw "Date firstWeekDay must be an integer between 0 and 6"
130
+ format = {}
131
+ if is_string(props.format)
132
+ format.display = props.format
133
+ else
134
+ if props.format != null
135
+ if !is_object(props.format) or is_array(props.format)
136
+ throw "Date format must be a display format or options map"
137
+ format = Object/assign({}, props.format)
138
+ display = format.display
139
+ if display == null
140
+ display = "Y-m-d"
141
+ locale = calendarLocale(format)
142
+ locale.firstDayOfWeek = first
143
+ if props.label != null
144
+ locale.monthAriaLabel = props.label
145
+ locale.yearAriaLabel = props.label
146
+ if format.calendar != null
147
+ locale = Object/assign(locale, format.calendar)
148
+ createPicker = picker.default
149
+ change = (dates)
150
+ if @input.disabled
151
+ setValue(@range)
152
+ return null
153
+ start = null
154
+ end = null
155
+ if dates.length > 0
156
+ start = @picker.formatDate(dates[0], "Y-m-d")
157
+ if dates.length > 1
158
+ end = @picker.formatDate(dates[1], "Y-m-d")
159
+ @range = dateRange({
160
+ start: start
161
+ end: end
162
+ }, @min, @max)
163
+ @changes.changed()
164
+ options = {
165
+ mode: "range"
166
+ dateFormat: display
167
+ showMonths: months
168
+ locale: locale
169
+ inline: props.inline == true
170
+ appendTo: @element
171
+ disableMobile: true
172
+ allowInput: false
173
+ onChange: change
174
+ }
175
+ if @min != null
176
+ options.minDate = createPicker.parseDate(@min, "Y-m-d")
177
+ if @max != null
178
+ options.maxDate = createPicker.parseDate(@max, "Y-m-d")
179
+ @picker = createPicker(@input, options)
180
+ if props.button == true
181
+ @input.className = "btn btn-outline-secondary"
182
+ setValue(@range)
183
+ setDisabled(props.disabled == true)
184
+
185
+ DateInput:getValue()
186
+ return {
187
+ start: @range.start
188
+ end: @range.end
189
+ }
190
+
191
+ DateInput:setValue(value)
192
+ range = dateRange(value, @min, @max)
193
+ dates = []
194
+ if range.start != null
195
+ dates.add(range.start)
196
+ if range.end != null
197
+ dates.add(range.end)
198
+ @picker.setDate(dates, false, "Y-m-d")
199
+ @range = range
200
+ return @this
201
+
202
+ DateInput:setDisabled(value)
203
+ @input.disabled = value or @locked == true
204
+ if @picker != null
205
+ calendar = @picker.calendarContainer
206
+ calendar.inert = @input.disabled
207
+ if @input.disabled
208
+ @picker.close()
209
+ return @this
210
+
211
+ DateInput:dispose()
212
+ @changes.dispose()
213
+ @picker.destroy()
214
+ super.dispose()
215
+ return null
@@ -0,0 +1,95 @@
1
+ import js global Promise
2
+ import js global Math
3
+ import js global RegExp
4
+
5
+ export class ErrorReporter(app, url)
6
+ properties
7
+ active
8
+ methods
9
+ capture(source, code, name, form)
10
+ schedule()
11
+ flush()
12
+ dispose()
13
+
14
+ ErrorReporter(app, url)
15
+ app.checkUrl(url)
16
+ @app = app
17
+ @url = url
18
+ @active = true
19
+ @pending = []
20
+ @timer = null
21
+ @flight = false
22
+ @window = app.now()
23
+ @sent = 0
24
+
25
+ ErrorReporter:capture(source, code, name, form)
26
+ if !@active or form == null or form.disposed
27
+ return null
28
+ if @app.now() - @window >= 60000
29
+ @window = @app.now()
30
+ @sent = 0
31
+ if @sent >= 10
32
+ return null
33
+ sources = ["ui", "window", "promise"]
34
+ valid = new RegExp("^[A-Za-z][A-Za-z0-9_]{0,63}$")
35
+ if !sources.includes(source) or !is_string(code) or !valid.test(code)
36
+ return null
37
+ if !is_string(name) or name.trim() == "" or name.length > 256
38
+ name = null
39
+ for @pending as entry
40
+ previous = entry.report
41
+ if entry.form == form and previous.source == source and previous.code == code and previous.name == name
42
+ previous.count = Math/min(100, previous.count + 1)
43
+ return null
44
+ if @pending.length >= 20
45
+ return null
46
+ report = {
47
+ source: source
48
+ code: code
49
+ count: 1
50
+ }
51
+ if name != null
52
+ report.name = name
53
+ @pending.add({
54
+ form: form
55
+ report: report
56
+ })
57
+ schedule()
58
+ return null
59
+
60
+ ErrorReporter:schedule()
61
+ if @active and !@flight and @timer == null and @pending.length > 0
62
+ action = ()
63
+ @timer = null
64
+ flush()
65
+ @timer = @app.schedule(action, 0)
66
+ return null
67
+
68
+ ErrorReporter:flush()
69
+ if !@active or @flight
70
+ return Promise/resolve(null)
71
+ if @sent >= 10
72
+ @pending = []
73
+ return Promise/resolve(null)
74
+ entry = @pending.shift()
75
+ if entry == null
76
+ return Promise/resolve(null)
77
+ if entry.form.disposed
78
+ schedule()
79
+ return Promise/resolve(null)
80
+ @sent = @sent + 1
81
+ @flight = true
82
+ return chain @app.report(entry.form, @url, entry.report, @this)
83
+ catch(error)
84
+ return null
85
+ finally
86
+ @flight = false
87
+ schedule()
88
+
89
+ ErrorReporter:dispose()
90
+ @active = false
91
+ @pending = []
92
+ if @timer != null
93
+ @app.cancelTimer(@timer)
94
+ @timer = null
95
+ return null
@@ -0,0 +1,327 @@
1
+ import js global document
2
+ import js global Object
3
+ import js global setTimeout
4
+ import js global clearTimeout
5
+ import "./control"
6
+ class Control
7
+ import "./templates"
8
+ cloneMarkup(name, xml)
9
+ controlId()
10
+
11
+ export sameValue(first, second)
12
+ export class Field(form, props, input) : Control
13
+ properties
14
+ input
15
+ labelElement
16
+ methods
17
+ setDisabled(value)
18
+
19
+ Field(form, props, input)
20
+ super(form, props, cloneMarkup("field", <div class="mb-3"></div>))
21
+ element = @element
22
+ @input = input
23
+ @readonly = props.readonly == true
24
+ @labelElement = null
25
+ input.id = controlId()
26
+ if props.label != null
27
+ @labelElement = document/createElement("label")
28
+ @labelElement.htmlFor = input.id
29
+ @labelElement.className = "form-label"
30
+ @labelElement.textContent = props.label
31
+ element.appendChild(@labelElement)
32
+ element.appendChild(input)
33
+ if props.description != null
34
+ description = document/createElement("div")
35
+ description.id = controlId()
36
+ description.className = "form-text"
37
+ description.textContent = props.description
38
+ element.appendChild(description)
39
+ input.setAttribute("aria-describedby", description.id)
40
+ if props.aria != null
41
+ for props.aria as name: value
42
+ input.setAttribute("aria-${name}", value)
43
+ input.required = props.required == true
44
+ input.readOnly = @readonly
45
+ if props.disableAutoComplete == true
46
+ input.autocomplete = "off"
47
+ # Derived fields are not initialized until this constructor returns.
48
+ input.disabled = props.disabled == true
49
+ if @readonly and (input.tagName == "SELECT" or input.tagName == "FIELDSET" or input.type == "checkbox" or input.type == "range")
50
+ input.disabled = true
51
+ validationTarget(input)
52
+
53
+ Field:setDisabled(value)
54
+ @input.disabled = value
55
+ if @readonly and (@input.tagName == "SELECT" or @input.tagName == "FIELDSET" or @input.type == "checkbox" or @input.type == "range")
56
+ @input.disabled = true
57
+ return @this
58
+
59
+ export class Input(form, props) : Field
60
+ methods
61
+ getValue()
62
+ setValue(value)
63
+ changed()
64
+ dispose()
65
+
66
+ Input(form, props)
67
+ super(form, props, inputElement(props))
68
+ input = @input
69
+ @numeric = input.type == "number"
70
+ @timer = null
71
+ @form = form
72
+ @url = props.url
73
+ if props.placeholder != null
74
+ input.placeholder = props.placeholder
75
+ if props.maxLength != null
76
+ input.maxLength = props.maxLength
77
+ if props.min != null
78
+ input.min = props.min
79
+ if props.max != null
80
+ input.max = props.max
81
+ setValue(props.value)
82
+ if @url != null
83
+ listener = ()
84
+ changed()
85
+ input.addEventListener("input", listener)
86
+ release = ()
87
+ capture input
88
+ capture listener
89
+ input.removeEventListener("input", listener)
90
+ @release.add(release)
91
+
92
+ Input:getValue()
93
+ if @numeric
94
+ if @input.value == "" or @input.validity.badInput
95
+ return null
96
+ try
97
+ return @input.valueAsNumber
98
+ catch error
99
+ if error.message == "NonFiniteNumber"
100
+ return null
101
+ throw error
102
+ return @input.value
103
+
104
+ Input:setValue(value)
105
+ if value == null
106
+ @input.value = ""
107
+ else
108
+ @input.value = "${value}"
109
+ return @this
110
+
111
+ Input:changed()
112
+ if @timer != null
113
+ clearTimeout(@timer)
114
+ action = ()
115
+ @timer = null
116
+ if !@form.disposed
117
+ chain @form.send(@url)
118
+ catch(error)
119
+ @form.app.error(@form, error)
120
+ @timer = setTimeout(action, 500)
121
+ return null
122
+
123
+ Input:dispose()
124
+ if @timer != null
125
+ clearTimeout(@timer)
126
+ @timer = null
127
+ return super.dispose()
128
+
129
+ export class Checkbox(form, props) : Field
130
+ methods
131
+ getValue()
132
+ setValue(value)
133
+
134
+ Checkbox(form, props)
135
+ super(form, props, cloneMarkup("checkbox", <input type="checkbox" class="form-check-input" />))
136
+ input = @input
137
+ @element.classList.add("form-check")
138
+ if @labelElement != null
139
+ @labelElement.className = "form-check-label"
140
+ @element.insertBefore(input, @labelElement)
141
+ initial = props.value
142
+ if props.indeterminate == true
143
+ initial = null
144
+ setValue(initial)
145
+
146
+ Checkbox:getValue()
147
+ if @input.indeterminate
148
+ return null
149
+ return @input.checked
150
+
151
+ Checkbox:setValue(value)
152
+ if value != null and !is_bool(value)
153
+ throw "Checkbox value must be boolean or null"
154
+ @input.indeterminate = value == null
155
+ @input.checked = value == true
156
+ return @this
157
+
158
+ export class Switch(form, props) : Checkbox
159
+ methods
160
+ setValue(value)
161
+
162
+ Switch(form, props)
163
+ super(form, props)
164
+ @element.classList.add("form-switch")
165
+ @input.setAttribute("role", "switch")
166
+ setValue(props.value)
167
+
168
+ Switch:setValue(value)
169
+ if value == null
170
+ value = false
171
+ if !is_bool(value)
172
+ throw "Switch value must be boolean"
173
+ return super.setValue(value)
174
+
175
+ export class Range(form, props) : Field
176
+ methods
177
+ getValue()
178
+ setValue(value)
179
+
180
+ Range(form, props)
181
+ super(form, props, cloneMarkup("range", <input type="range" class="form-range" />))
182
+ input = @input
183
+ if props.min != null
184
+ input.min = props.min
185
+ if props.max != null
186
+ input.max = props.max
187
+ if props.steps != null
188
+ input.step = props.steps
189
+ if props.value != null
190
+ setValue(props.value)
191
+
192
+ Range:getValue()
193
+ return @input.valueAsNumber
194
+
195
+ Range:setValue(value)
196
+ if !is_number(value)
197
+ throw "Range value must be a number"
198
+ @input.value = "${value}"
199
+ return @this
200
+
201
+ export class Select(form, props) : Field
202
+ methods
203
+ getValue()
204
+ setValue(value)
205
+
206
+ Select(form, props)
207
+ super(form, props, cloneMarkup("select", <select class="form-select"></select>))
208
+ input = @input
209
+ @options = props.options
210
+ if @options == null
211
+ @options = []
212
+ for @options as option
213
+ node = document/createElement("option")
214
+ node.textContent = option.label
215
+ input.appendChild(node)
216
+ setValue(props.value)
217
+
218
+ Select:getValue()
219
+ if @input.selectedIndex < 0
220
+ return null
221
+ return @options[@input.selectedIndex].value
222
+
223
+ Select:setValue(value)
224
+ @input.selectedIndex = -1
225
+ index = 0
226
+ for @options as option
227
+ if sameValue(option.value, value)
228
+ @input.selectedIndex = index
229
+ break
230
+ index = index + 1
231
+ return @this
232
+
233
+ export class Radio(form, props) : Field
234
+ methods
235
+ getValue()
236
+ setValue(value)
237
+
238
+ Radio(form, props)
239
+ super(form, props, cloneMarkup("radio", <fieldset role="radiogroup"></fieldset>))
240
+ input = @input
241
+ if props.label != null
242
+ input.setAttribute("aria-label", props.label)
243
+ @options = props.options
244
+ if @options == null
245
+ @options = []
246
+ @choices = []
247
+ for @options as option
248
+ choice = cloneMarkup("radio-choice", <input type="radio" class="form-check-input" />)
249
+ choice.id = controlId()
250
+ choice.name = input.id
251
+ choice.disabled = option.enabled == false
252
+ choice.required = props.required == true
253
+ label = document/createElement("label")
254
+ label.htmlFor = choice.id
255
+ label.className = "form-check-label"
256
+ label.textContent = option.label
257
+ row = cloneMarkup("radio-row", <div class="form-check"></div>)
258
+ row.appendChild(choice)
259
+ row.appendChild(label)
260
+ input.appendChild(row)
261
+ @choices.add(choice)
262
+ if Object/hasOwn(props, "value")
263
+ setValue(props.value)
264
+ else
265
+ if props.selectedIndex != null and props.selectedIndex >= 0 and props.selectedIndex < @choices.length
266
+ selected = @choices[props.selectedIndex]
267
+ selected.checked = true
268
+
269
+ Radio:getValue()
270
+ index = 0
271
+ for @choices as choice
272
+ if choice.checked
273
+ return @options[index].value
274
+ index = index + 1
275
+ return null
276
+
277
+ Radio:setValue(value)
278
+ selected = false
279
+ index = 0
280
+ for @choices as choice
281
+ choice.checked = !selected and sameValue(@options[index].value, value)
282
+ if choice.checked
283
+ selected = true
284
+ index = index + 1
285
+ return @this
286
+
287
+ sameValue(first, second)
288
+ if is_array(first)
289
+ if !is_array(second) or first.length != second.length
290
+ return false
291
+ index = 0
292
+ for first as item
293
+ if !sameValue(item, second[index])
294
+ return false
295
+ index = index + 1
296
+ return true
297
+ if is_object(first)
298
+ if !is_object(second) or is_array(second) or Object/keys(first).length != Object/keys(second).length
299
+ return false
300
+ for first as name: value
301
+ if !Object/hasOwn(second, name) or !sameValue(value, second[name])
302
+ return false
303
+ return true
304
+ return first == second
305
+
306
+ inputElement(props)
307
+ input = cloneMarkup("input", <input class="form-control" />)
308
+ variant = props.variant
309
+ if variant == null
310
+ variant = "text"
311
+ variant = variant.toLowerCase()
312
+ if variant == "string"
313
+ variant = "text"
314
+ allowed = ["text", "email", "password", "search", "tel", "url", "number", "decimal", "money"]
315
+ if !allowed.includes(variant)
316
+ throw "Unsupported input variant: ${variant}"
317
+ if props.lines != null and props.lines > 1
318
+ if variant != "text"
319
+ throw "Multiline input requires text or String"
320
+ input = cloneMarkup("textarea", <textarea class="form-control"></textarea>)
321
+ input.rows = props.lines
322
+ else
323
+ input.type = variant
324
+ if variant == "decimal" or variant == "money"
325
+ input.type = "text"
326
+ input.inputMode = "decimal"
327
+ return input