formatic 0.2.3 → 0.2.5

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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/app/assets/javascript/declarations/date.d.ts +9 -0
  4. data/app/assets/javascript/declarations/file.d.ts +16 -0
  5. data/app/assets/javascript/declarations/select.d.ts +15 -0
  6. data/app/assets/javascript/declarations/setup.d.ts +1 -0
  7. data/app/assets/javascript/declarations/stepper.d.ts +12 -0
  8. data/app/assets/javascript/declarations/string.d.ts +14 -0
  9. data/app/assets/javascript/declarations/textarea.d.ts +15 -0
  10. data/app/assets/javascript/declarations/toggle.d.ts +8 -0
  11. data/app/assets/javascript/formatic/date.js +30 -0
  12. data/app/assets/javascript/formatic/date.js.map +1 -0
  13. data/app/assets/javascript/formatic/file.js +100 -0
  14. data/app/assets/javascript/formatic/file.js.map +1 -0
  15. data/app/assets/javascript/formatic/select.js +81 -0
  16. data/app/assets/javascript/formatic/select.js.map +1 -0
  17. data/app/assets/javascript/formatic/setup.js +44 -0
  18. data/app/assets/javascript/formatic/stepper.js +67 -0
  19. data/app/assets/javascript/formatic/stepper.js.map +1 -0
  20. data/app/assets/javascript/formatic/string.js +71 -0
  21. data/app/assets/javascript/formatic/string.js.map +1 -0
  22. data/app/assets/javascript/formatic/textarea.js +81 -0
  23. data/app/assets/javascript/formatic/textarea.js.map +1 -0
  24. data/app/assets/javascript/formatic/toggle.js +63 -0
  25. data/app/assets/javascript/formatic/toggle.js.map +1 -0
  26. data/app/assets/javascript/src/date.ts +41 -0
  27. data/app/assets/javascript/src/file.ts +122 -0
  28. data/app/assets/javascript/src/select.ts +101 -0
  29. data/app/assets/javascript/src/stepper.ts +80 -0
  30. data/app/assets/javascript/src/string.ts +89 -0
  31. data/app/assets/javascript/src/textarea.ts +101 -0
  32. data/app/assets/javascript/src/toggle.ts +76 -0
  33. data/app/assets/stylesheets/formatic/components/date.sass +2 -2
  34. data/app/assets/stylesheets/formatic/formatic.css +450 -0
  35. data/app/assets/stylesheets/formatic/vendor.css +1107 -0
  36. data/app/assets/stylesheets/formatic.sass +17 -0
  37. data/app/assets/stylesheets/vendor.sass +1 -0
  38. data/app/components/formatic/base.rb +10 -1
  39. data/app/components/formatic/checklist.rb +6 -2
  40. data/app/components/formatic/date.rb +1 -1
  41. data/app/components/formatic/file.rb +27 -0
  42. data/app/components/formatic/files.rb +8 -0
  43. data/app/components/formatic/select.rb +1 -1
  44. data/app/components/formatic/textarea.rb +7 -3
  45. data/app/components/formatic/time.rb +3 -3
  46. data/config/importmap.rb +11 -0
  47. data/lib/formatic/choices/options.rb +1 -1
  48. data/lib/formatic/choices/records.rb +1 -1
  49. data/lib/formatic/engine.rb +24 -0
  50. data/lib/formatic/version.rb +1 -1
  51. data/lib/formatic/wrappers/required.rb +1 -1
  52. data/lib/formatic.rb +2 -1
  53. data/vscode/formatic.code-snippets +58 -0
  54. metadata +56 -17
  55. data/app/assets/javascript/formatic/components/date.ts +0 -54
  56. data/app/assets/javascript/formatic/components/select.ts +0 -113
  57. data/app/assets/javascript/formatic/components/stepper.ts +0 -89
  58. data/app/assets/javascript/formatic/components/string.ts +0 -103
  59. data/app/assets/javascript/formatic/components/textarea.ts +0 -112
  60. data/app/assets/javascript/formatic/components/toggle.ts +0 -89
  61. data/app/assets/javascript/formatic.js +0 -446
  62. data/app/assets/javascript/formatic.js.map +0 -1
  63. data/app/assets/stylesheets/formatic/index.sass +0 -17
  64. data/app/assets/stylesheets/formatic/package.json +0 -5
@@ -0,0 +1,63 @@
1
+ export class Toggle {
2
+ constructor(el) {
3
+ this.el = el;
4
+ this.setupBindings();
5
+ }
6
+ setupBindings() {
7
+ this.checkbox.addEventListener('click', (event) => {
8
+ event.preventDefault();
9
+ const box = event.target;
10
+ box.checked ? this.activate(box) : this.deactivate(box);
11
+ });
12
+ }
13
+ activate(box) {
14
+ const form = box.closest('form');
15
+ const data = new FormData(form);
16
+ data.set('_method', 'patch');
17
+ fetch(form.action, {
18
+ method: 'POST',
19
+ headers: { 'Accept': 'text/javascript' },
20
+ body: data
21
+ })
22
+ .then(response => {
23
+ if (response.status == 201) {
24
+ console.debug('Activation confirmed');
25
+ box.checked = true;
26
+ }
27
+ else {
28
+ console.debug('Activation not confirmed');
29
+ console.log("Activation denied");
30
+ }
31
+ }).catch(err => {
32
+ console.warn(err);
33
+ console.debug('Failed badly to activate');
34
+ });
35
+ }
36
+ deactivate(box) {
37
+ const form = box.closest('form');
38
+ const data = new FormData(form);
39
+ data.set('_method', 'delete');
40
+ fetch(form.action, {
41
+ method: 'POST',
42
+ headers: { 'Accept': 'text/javascript' },
43
+ body: data
44
+ })
45
+ .then(response => {
46
+ if (response.status == 204) {
47
+ console.debug('Deactivation confirmed');
48
+ box.checked = false;
49
+ }
50
+ else {
51
+ console.debug('Deactivation not confirmed');
52
+ console.log("Deactivation denied");
53
+ }
54
+ }).catch(err => {
55
+ console.warn(err);
56
+ console.debug('Failed badly to deactivate');
57
+ });
58
+ }
59
+ get checkbox() {
60
+ return this.el.querySelector('input[type="checkbox"]');
61
+ }
62
+ }
63
+ //# sourceMappingURL=toggle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toggle.js","sourceRoot":"","sources":["../src/toggle.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,MAAM;IAIjB,YAAY,EAAe;QACzB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAA;QACZ,IAAI,CAAC,aAAa,EAAE,CAAA;IACtB,CAAC;IAEO,aAAa;QACnB,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YAChD,KAAK,CAAC,cAAc,EAAE,CAAA;YACtB,MAAM,GAAG,GAAqB,KAAK,CAAC,MAAM,CAAA;YAC1C,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;QACzD,CAAC,CAAC,CAAA;IACJ,CAAC;IAEO,QAAQ,CAAC,GAAqB;QACpC,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAkB,MAAM,CAAC,CAAA;QACjD,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAA;QAC/B,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;QAE5B,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;YACjB,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,QAAQ,EAAE,iBAAiB,EAAE;YACxC,IAAI,EAAE,IAAI;SACX,CAAC;aACC,IAAI,CAAC,QAAQ,CAAC,EAAE;YAGf,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG,EAAE,CAAC;gBAC3B,OAAO,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAA;gBACrC,GAAG,CAAC,OAAO,GAAG,IAAI,CAAA;YACpB,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAA;gBACzC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;YAClC,CAAC;QACH,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YACb,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YACjB,OAAO,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAA;QAC3C,CAAC,CAAC,CAAA;IACN,CAAC;IAEO,UAAU,CAAC,GAAqB;QACtC,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAkB,MAAM,CAAC,CAAA;QACjD,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAA;QAC/B,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;QAE7B,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;YACjB,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,QAAQ,EAAE,iBAAiB,EAAE;YACxC,IAAI,EAAE,IAAI;SACX,CAAC;aACC,IAAI,CAAC,QAAQ,CAAC,EAAE;YAEf,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG,EAAE,CAAC;gBAC3B,OAAO,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAA;gBACvC,GAAG,CAAC,OAAO,GAAG,KAAK,CAAA;YACrB,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAA;gBAC3C,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAA;YACpC,CAAC;QACH,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YACb,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YACjB,OAAO,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAA;QAC7C,CAAC,CAAC,CAAA;IACN,CAAC;IAMD,IAAY,QAAQ;QAClB,OAAO,IAAI,CAAC,EAAE,CAAC,aAAa,CAAmB,wBAAwB,CAAC,CAAA;IAC1E,CAAC;CACF"}
@@ -0,0 +1,41 @@
1
+ export class Date {
2
+ private el: HTMLElement
3
+
4
+ constructor(el: HTMLElement) {
5
+ this.el = el
6
+ this.setupBindings()
7
+ }
8
+
9
+ private setupBindings() {
10
+ this.shortcutButtons.forEach((el) => {
11
+ el.addEventListener('click', (event) => {
12
+ event.preventDefault()
13
+ const shortcut = <HTMLInputElement>event.currentTarget
14
+
15
+ this.dayInput.value = shortcut.dataset.day
16
+ this.monthInput.value = shortcut.dataset.month
17
+ this.yearInput.value = shortcut.dataset.year
18
+ })
19
+ })
20
+ }
21
+
22
+ // ------------
23
+ // DOM Elements
24
+ // ------------
25
+
26
+ private get dayInput() {
27
+ return this.el.querySelector<HTMLSelectElement>('.js-formatic-date__day')
28
+ }
29
+
30
+ private get monthInput() {
31
+ return this.el.querySelector<HTMLSelectElement>('.js-formatic-date__month')
32
+ }
33
+
34
+ private get yearInput() {
35
+ return this.el.querySelector<HTMLSelectElement>('.js-formatic-date__year')
36
+ }
37
+
38
+ private get shortcutButtons() {
39
+ return this.el.querySelectorAll<HTMLElement>('.js-formatic-date__shortcut')
40
+ }
41
+ }
@@ -0,0 +1,122 @@
1
+ import * as FilePond from "filepond"
2
+ import { DirectUpload } from "@rails/activestorage"
3
+
4
+ export class FormaticFile {
5
+ private el: HTMLElement
6
+ private url: string
7
+ private pond: FilePond.FilePond
8
+ private busyStatuses = new Set([
9
+ FilePond.FileStatus.LOADING,
10
+ FilePond.FileStatus.PROCESSING_QUEUED,
11
+ FilePond.FileStatus.PROCESSING,
12
+ ]);
13
+ private inputID: string | null = null;
14
+
15
+ constructor(el: HTMLElement) {
16
+ this.el = el
17
+ this.url = this.input.dataset.directUploadUrl
18
+ this.inputID = this.input.id
19
+ this.setupBindings()
20
+ }
21
+
22
+ private setupBindings() {
23
+ this.pond = FilePond.create(this.input, {
24
+ credits: false,
25
+ onwarning: () => this.updateSubmit(),
26
+ onerror: () => this.updateSubmit(),
27
+ onaddfilestart: () => this.updateSubmit(),
28
+ onaddfileprogress: () => this.updateSubmit(),
29
+ onaddfile: () => this.updateSubmit(),
30
+ onprocessfilestart: () => this.updateSubmit(),
31
+ onprocessfileprogress: () => this.updateSubmit(),
32
+ onprocessfileabort: () => this.updateSubmit(),
33
+ onprocessfilerevert: () => this.updateSubmit(),
34
+ onprocessfile: () => this.updateSubmit(),
35
+ onprocessfiles: () => this.updateSubmit(),
36
+ onremovefile: () => this.updateSubmit(),
37
+ onpreparefile: () => this.updateSubmit(),
38
+ onupdatefiles: () => this.updateSubmit(),
39
+ onactivatefile: () => this.updateSubmit(),
40
+ onreorderfiles: () => this.updateSubmit(),
41
+ server: {
42
+ process: (fieldName, file, _metadata, load, error, progress, abort, transfer, options) => {
43
+
44
+ const uploader = new DirectUpload(file as File, this.url, {
45
+ directUploadWillStoreFileWithXHR: (request) => {
46
+ request.upload.addEventListener(
47
+ 'progress',
48
+ event => progress(event.lengthComputable, event.loaded, event.total)
49
+ )
50
+ }
51
+ })
52
+
53
+ uploader.create((errorResponse, blob) => {
54
+ if (errorResponse) {
55
+ error(`Something went wrong: ${errorResponse}`)
56
+ } else {
57
+ // See https://edgeguides.rubyonrails.org/active_storage_overview.html#direct-upload-javascript-events
58
+ // See https://stackoverflow.com/questions/75586818
59
+ load(blob.signed_id)
60
+ }
61
+ })
62
+
63
+ return {
64
+ abort: () => abort()
65
+ }
66
+ },
67
+ headers: {
68
+ 'X-CSRF-Token': document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content
69
+ }
70
+ },
71
+ oninit: () => this.updateLabelId(),
72
+ })
73
+ }
74
+
75
+ // FilePond replaces the <input id="X"> with <div id="X"><input id="something-new">
76
+ // But the <form> may have <label for="X"> so we need to update them.
77
+ // to become <label for="something-new">
78
+ private updateLabelId() {
79
+ if (!this.inputID) return
80
+
81
+ const labels = this.form.querySelectorAll(`label[for="${this.inputID}"]`)
82
+ if (labels.length === 0) return
83
+
84
+ const pondInput = this.input.querySelector('input[type="file"]')
85
+ if (!pondInput) return
86
+
87
+ const pondInputId = pondInput.id;
88
+ if (!pondInputId) return
89
+
90
+ labels.forEach(label => label.setAttribute('for', pondInputId))
91
+ }
92
+
93
+ private updateSubmit() {
94
+ const files = this.pond.getFiles()
95
+ const busyFiles = files.some(f => this.busyStatuses.has(f.status))
96
+ busyFiles ? this.disableSubmit() : this.enableSubmit()
97
+ }
98
+
99
+ private enableSubmit() {
100
+ this.submitButtons.forEach((button) => {
101
+ button.disabled = false
102
+ })
103
+ }
104
+
105
+ private disableSubmit() {
106
+ this.submitButtons.forEach((button) => {
107
+ button.disabled = true
108
+ })
109
+ }
110
+
111
+ private get input() {
112
+ return this.el.querySelector<HTMLInputElement>('.js-formatic-file__input')
113
+ }
114
+
115
+ private get form() {
116
+ return this.el.closest<HTMLFormElement>('form')
117
+ }
118
+
119
+ private get submitButtons() {
120
+ return this.form.querySelectorAll<HTMLButtonElement>('[type="submit"]')
121
+ }
122
+ }
@@ -0,0 +1,101 @@
1
+ import autosize from "autosize"
2
+
3
+ export class Select {
4
+ private el: HTMLElement
5
+ private timeoutId: ReturnType<typeof setTimeout>
6
+
7
+ constructor(el: HTMLElement) {
8
+ this.el = el
9
+ this.setupBindings()
10
+ }
11
+
12
+ private setupBindings() {
13
+ this.autosize()
14
+
15
+ this.el.addEventListener('input', (event) => {
16
+ event.preventDefault()
17
+ if (this.autoSubmit) this.actionSave()
18
+ })
19
+ }
20
+
21
+ // ACTIONS
22
+
23
+ private actionSave() {
24
+ this.guiWaitingForSave()
25
+ this.debounce(this.actionSaveNow.bind(this))()
26
+ }
27
+
28
+ private actionSaveNow() {
29
+ console.debug("Saving select...")
30
+ this.el.classList.remove('is-loading')
31
+ this.el.classList.add('is-saved')
32
+ this.save()
33
+ }
34
+
35
+ // GUI
36
+
37
+ private guiWaitingForSave() {
38
+ this.el.classList.add('is-loading')
39
+ this.el.classList.remove('is-saved')
40
+ this.el.classList.remove('is-failed')
41
+ }
42
+
43
+ private guiSaved() {
44
+ this.el.classList.remove('is-loading')
45
+ this.el.classList.add('is-saved')
46
+ this.el.classList.remove('is-failed')
47
+ }
48
+
49
+ private guiFailed() {
50
+ this.el.classList.remove('is-loading')
51
+ this.el.classList.remove('is-saved')
52
+ this.el.classList.add('is-failed')
53
+ }
54
+
55
+ // Attributes
56
+
57
+ private get autoSubmit() {
58
+ return this.el.classList.contains('is-autosubmit')
59
+ }
60
+
61
+ // Helpers
62
+
63
+ private save() {
64
+ const form = this.el.closest<HTMLFormElement>('form')
65
+ const data = new FormData(form)
66
+ data.set('_method', 'patch')
67
+
68
+ fetch(form.action, {
69
+ method: 'POST',
70
+ headers: { 'Accept': 'text/javascript' },
71
+ body: data
72
+ })
73
+ .then(response => {
74
+ if (response.status == 201) {
75
+ console.debug('Select content saved')
76
+ this.guiSaved()
77
+ } else {
78
+ console.debug('Select content not saved')
79
+ this.guiFailed()
80
+ }
81
+ }).catch(err => {
82
+ console.warn(err)
83
+ console.debug('Failed badly to save')
84
+ })
85
+ }
86
+
87
+ private debounce(fn: Function) {
88
+ return () => {
89
+ clearTimeout(this.timeoutId)
90
+ this.timeoutId = setTimeout(() => fn(), 1000)
91
+ }
92
+ }
93
+
94
+ private autosize() {
95
+ try {
96
+ autosize(this.el) // External library.
97
+ } catch (error) {
98
+ console.warn(`Formatic is missing the autosize library`)
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,80 @@
1
+ export class Stepper {
2
+ private el: HTMLElement
3
+
4
+ constructor(el: HTMLElement) {
5
+ this.el = el
6
+ this.setupBindings()
7
+ }
8
+
9
+ private setupBindings() {
10
+ this.incrementButtons.forEach((el) => {
11
+ el.addEventListener('click', (event) => {
12
+ event.preventDefault()
13
+ this.increment()
14
+ })
15
+ })
16
+
17
+ this.decrementButtons.forEach((el) => {
18
+ el.addEventListener('click', (event) => {
19
+ event.preventDefault()
20
+ this.decrement()
21
+ })
22
+ })
23
+
24
+ this.numberInput.addEventListener('click', (event) => {
25
+ this.numberInput.select()
26
+ })
27
+ }
28
+
29
+ private increment() {
30
+ if (!this.number && this.number != 0) {
31
+ // Incrementing leads to `0` (it's common to quickly want to achieve the number zero).
32
+ // But if the minimum is e.g. `1` then it should be that instead.
33
+ this.numberInput.value = Math.max(...[0, this.minimum]).toString()
34
+ } else if (isNaN(this.number)) {
35
+ // Do nothing on weird stuff.
36
+ return
37
+ } else if (this.number < this.minimum) {
38
+ this.numberInput.value = this.minimum.toString()
39
+ } else {
40
+ this.numberInput.value = (this.number + 1).toString()
41
+ }
42
+ }
43
+
44
+ private decrement() {
45
+ if (!this.number && this.number != 0) {
46
+ // Decementing empty leads to `-1`.
47
+ // But if the minimum is e.g. `0` then it should be that instead.
48
+ this.numberInput.value = Math.max(...[-1, this.minimum]).toString()
49
+ } else if (isNaN(this.number)) {
50
+ // Do nothing on weird stuff.
51
+ return
52
+ } else if (this.number <= this.minimum) {
53
+ // Do nothing if at the minimum already
54
+ return
55
+ } else {
56
+ // Normal decrement.
57
+ this.numberInput.value = (this.number - 1).toString()
58
+ }
59
+ }
60
+
61
+ private get minimum() {
62
+ return parseInt(this.numberInput.min)
63
+ }
64
+
65
+ private get number() {
66
+ return parseInt(this.numberInput.value)
67
+ }
68
+
69
+ private get incrementButtons() {
70
+ return this.el.querySelectorAll<HTMLLinkElement>('.js-formatic-stepper__increment')
71
+ }
72
+
73
+ private get decrementButtons() {
74
+ return this.el.querySelectorAll<HTMLLinkElement>('.js-formatic-stepper__decrement')
75
+ }
76
+
77
+ private get numberInput() {
78
+ return this.el.querySelector<HTMLInputElement>('.js-formatic-stepper__number')
79
+ }
80
+ }
@@ -0,0 +1,89 @@
1
+ export class String {
2
+ private el: HTMLElement
3
+ private timeoutId: ReturnType<typeof setTimeout>
4
+
5
+ constructor(el: HTMLElement) {
6
+ this.el = el
7
+ this.setupBindings()
8
+ }
9
+
10
+ private setupBindings() {
11
+ this.el.addEventListener('input', (event) => {
12
+ event.preventDefault()
13
+ if (this.autoSubmit) this.actionSave()
14
+ })
15
+ }
16
+
17
+ // ACTIONS
18
+
19
+ private actionSave() {
20
+ this.guiWaitingForSave()
21
+ this.debounce(this.actionSaveNow.bind(this))()
22
+ }
23
+
24
+ private actionSaveNow() {
25
+ console.debug("Saving text input...")
26
+ this.el.classList.remove('is-loading')
27
+ this.el.classList.add('is-saved')
28
+ this.save()
29
+ }
30
+
31
+ // GUI
32
+
33
+ private guiWaitingForSave() {
34
+ this.el.classList.add('is-loading')
35
+ this.el.classList.remove('is-saved')
36
+ this.el.classList.remove('is-failed')
37
+ }
38
+
39
+ private guiSaved() {
40
+ this.el.classList.remove('is-loading')
41
+ this.el.classList.add('is-saved')
42
+ this.el.classList.remove('is-failed')
43
+ }
44
+
45
+ private guiFailed() {
46
+ this.el.classList.remove('is-loading')
47
+ this.el.classList.remove('is-saved')
48
+ this.el.classList.add('is-failed')
49
+ }
50
+
51
+ // Attributes
52
+
53
+ private get autoSubmit() {
54
+ return this.el.classList.contains('is-autosubmit')
55
+ }
56
+
57
+ // Helpers
58
+
59
+ private save() {
60
+ const form = this.el.closest<HTMLFormElement>('form')
61
+ const data = new FormData(form)
62
+ data.set('_method', 'patch')
63
+
64
+ fetch(form.action, {
65
+ method: 'POST',
66
+ headers: { 'Accept': 'text/javascript' },
67
+ body: data
68
+ })
69
+ .then(response => {
70
+ if (response.status == 201) {
71
+ console.debug('String content saved')
72
+ this.guiSaved()
73
+ } else {
74
+ console.debug('String content not saved')
75
+ this.guiFailed()
76
+ }
77
+ }).catch(err => {
78
+ console.warn(err)
79
+ console.debug('Failed badly to save')
80
+ })
81
+ }
82
+
83
+ private debounce(fn: Function) {
84
+ return () => {
85
+ clearTimeout(this.timeoutId)
86
+ this.timeoutId = setTimeout(() => fn(), 1000)
87
+ }
88
+ }
89
+ }
@@ -0,0 +1,101 @@
1
+ import autosize from "autosize"
2
+
3
+ export class Textarea {
4
+ private el: HTMLElement
5
+ private timeoutId: ReturnType<typeof setTimeout>
6
+
7
+ constructor(el: HTMLElement) {
8
+ this.el = el
9
+ this.setupBindings()
10
+ }
11
+
12
+ private setupBindings() {
13
+ this.autosize()
14
+
15
+ this.el.addEventListener('input', (event) => {
16
+ event.preventDefault()
17
+ if (this.autoSubmit) this.actionSave()
18
+ })
19
+ }
20
+
21
+ // ACTIONS
22
+
23
+ private actionSave() {
24
+ this.guiWaitingForSave()
25
+ this.debounce(this.actionSaveNow.bind(this))()
26
+ }
27
+
28
+ private actionSaveNow() {
29
+ console.debug("Saving textarea...")
30
+ this.el.classList.remove('is-loading')
31
+ this.el.classList.add('is-saved')
32
+ this.save()
33
+ }
34
+
35
+ // GUI
36
+
37
+ private guiWaitingForSave() {
38
+ this.el.classList.add('is-loading')
39
+ this.el.classList.remove('is-saved')
40
+ this.el.classList.remove('is-failed')
41
+ }
42
+
43
+ private guiSaved() {
44
+ this.el.classList.remove('is-loading')
45
+ this.el.classList.add('is-saved')
46
+ this.el.classList.remove('is-failed')
47
+ }
48
+
49
+ private guiFailed() {
50
+ this.el.classList.remove('is-loading')
51
+ this.el.classList.remove('is-saved')
52
+ this.el.classList.add('is-failed')
53
+ }
54
+
55
+ // Attributes
56
+
57
+ private get autoSubmit() {
58
+ return this.el.classList.contains('is-autosubmit')
59
+ }
60
+
61
+ // Helpers
62
+
63
+ private save() {
64
+ const form = this.el.closest<HTMLFormElement>('form')
65
+ const data = new FormData(form)
66
+ data.set('_method', 'patch')
67
+
68
+ fetch(form.action, {
69
+ method: 'POST',
70
+ headers: { 'Accept': 'text/javascript' },
71
+ body: data
72
+ })
73
+ .then(response => {
74
+ if (response.status == 201) {
75
+ console.debug('Textarea content saved')
76
+ this.guiSaved()
77
+ } else {
78
+ console.debug('Textarea content not saved')
79
+ this.guiFailed()
80
+ }
81
+ }).catch(err => {
82
+ console.warn(err)
83
+ console.debug('Failed badly to save')
84
+ })
85
+ }
86
+
87
+ private debounce(fn: Function) {
88
+ return () => {
89
+ clearTimeout(this.timeoutId)
90
+ this.timeoutId = setTimeout(() => fn(), 1000)
91
+ }
92
+ }
93
+
94
+ private autosize() {
95
+ try {
96
+ autosize(this.el) // External library.
97
+ } catch (error) {
98
+ console.warn(`Formatic is missing the autosize library`)
99
+ }
100
+ }
101
+ }
@@ -0,0 +1,76 @@
1
+ export class Toggle {
2
+
3
+ private el: HTMLElement
4
+
5
+ constructor(el: HTMLElement) {
6
+ this.el = el
7
+ this.setupBindings()
8
+ }
9
+
10
+ private setupBindings() {
11
+ this.checkbox.addEventListener('click', (event) => {
12
+ event.preventDefault()
13
+ const box = <HTMLInputElement>event.target
14
+ box.checked ? this.activate(box) : this.deactivate(box)
15
+ })
16
+ }
17
+
18
+ private activate(box: HTMLInputElement) {
19
+ const form = box.closest<HTMLFormElement>('form')
20
+ const data = new FormData(form)
21
+ data.set('_method', 'patch')
22
+
23
+ fetch(form.action, {
24
+ method: 'POST',
25
+ headers: { 'Accept': 'text/javascript' },
26
+ body: data
27
+ })
28
+ .then(response => {
29
+ // For accurate user feedback, we make sure this is not an accidental 200.
30
+ // Your controller needs to respond with 201 on create/update.
31
+ if (response.status == 201) {
32
+ console.debug('Activation confirmed')
33
+ box.checked = true
34
+ } else {
35
+ console.debug('Activation not confirmed')
36
+ console.log("Activation denied")
37
+ }
38
+ }).catch(err => {
39
+ console.warn(err)
40
+ console.debug('Failed badly to activate')
41
+ })
42
+ }
43
+
44
+ private deactivate(box: HTMLInputElement) {
45
+ const form = box.closest<HTMLFormElement>('form')
46
+ const data = new FormData(form)
47
+ data.set('_method', 'delete')
48
+
49
+ fetch(form.action, {
50
+ method: 'POST',
51
+ headers: { 'Accept': 'text/javascript' },
52
+ body: data
53
+ })
54
+ .then(response => {
55
+ // Your controller needs to respond with 204 on destroy.
56
+ if (response.status == 204) {
57
+ console.debug('Deactivation confirmed')
58
+ box.checked = false
59
+ } else {
60
+ console.debug('Deactivation not confirmed')
61
+ console.log("Deactivation denied")
62
+ }
63
+ }).catch(err => {
64
+ console.warn(err)
65
+ console.debug('Failed badly to deactivate')
66
+ })
67
+ }
68
+
69
+ // ------------
70
+ // DOM Elements
71
+ // ------------
72
+
73
+ private get checkbox() {
74
+ return this.el.querySelector<HTMLInputElement>('input[type="checkbox"]')
75
+ }
76
+ }
@@ -36,11 +36,11 @@
36
36
  justify-content: center
37
37
  color: colors.$black
38
38
  +spacing.margin-right--smaller
39
- +spacing.padding-tiny
39
+ +spacing.padding--tiny
40
40
 
41
41
  &__clear
42
42
  +font-size.large
43
- +spacing.padding-small
43
+ +spacing.padding--small
44
44
 
45
45
  &__calendar-day-number
46
46
  +font-size.default