formatic 0.2.3 → 0.2.4

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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascript/declarations/date.d.ts +9 -0
  3. data/app/assets/javascript/declarations/file.d.ts +14 -0
  4. data/app/assets/javascript/declarations/select.d.ts +15 -0
  5. data/app/assets/javascript/declarations/setup.d.ts +3 -0
  6. data/app/assets/javascript/declarations/stepper.d.ts +12 -0
  7. data/app/assets/javascript/declarations/string.d.ts +14 -0
  8. data/app/assets/javascript/declarations/textarea.d.ts +15 -0
  9. data/app/assets/javascript/declarations/toggle.d.ts +8 -0
  10. data/app/assets/javascript/formatic/date.js +30 -0
  11. data/app/assets/javascript/formatic/date.js.map +1 -0
  12. data/app/assets/javascript/formatic/file.js +83 -0
  13. data/app/assets/javascript/formatic/file.js.map +1 -0
  14. data/app/assets/javascript/formatic/select.js +81 -0
  15. data/app/assets/javascript/formatic/select.js.map +1 -0
  16. data/app/assets/javascript/formatic/setup.js +42 -0
  17. data/app/assets/javascript/formatic/setup.js.map +1 -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 +101 -0
  28. data/app/assets/javascript/src/select.ts +101 -0
  29. data/app/assets/javascript/src/setup.js +44 -0
  30. data/app/assets/javascript/src/stepper.ts +80 -0
  31. data/app/assets/javascript/src/string.ts +89 -0
  32. data/app/assets/javascript/src/textarea.ts +101 -0
  33. data/app/assets/javascript/src/toggle.ts +76 -0
  34. data/app/assets/stylesheets/formatic/components/checklist.css +1 -0
  35. data/app/assets/stylesheets/formatic/components/checklist.css.map +1 -0
  36. data/app/assets/stylesheets/formatic/components/date.css +17 -0
  37. data/app/assets/stylesheets/formatic/components/date.sass +2 -2
  38. data/app/assets/stylesheets/formatic/components/select.css +17 -0
  39. data/app/assets/stylesheets/formatic/components/stepper.css +17 -0
  40. data/app/assets/stylesheets/formatic/components/string.css +17 -0
  41. data/app/assets/stylesheets/formatic/components/textarea.css +17 -0
  42. data/app/assets/stylesheets/formatic/components/time.css +17 -0
  43. data/app/assets/stylesheets/formatic/components/toggle.css +17 -0
  44. data/app/assets/stylesheets/formatic/components/wrapper.css +17 -0
  45. data/app/assets/stylesheets/formatic/formatic.css +450 -0
  46. data/app/assets/stylesheets/formatic/formatic.css.map +1 -0
  47. data/app/assets/stylesheets/formatic/generics/flip.css +1 -0
  48. data/app/assets/stylesheets/formatic/generics/flip.css.map +1 -0
  49. data/app/assets/stylesheets/formatic/scopes/form.css +17 -0
  50. data/app/assets/stylesheets/formatic/utilities/container.css +1 -0
  51. data/app/assets/stylesheets/formatic/utilities/container.css.map +1 -0
  52. data/app/assets/stylesheets/formatic/vendor.css +1107 -0
  53. data/app/assets/stylesheets/formatic/vendor.css.map +1 -0
  54. data/app/assets/stylesheets/formatic.css +18 -0
  55. data/app/assets/stylesheets/formatic.sass +17 -0
  56. data/app/assets/stylesheets/vendor.css +5 -0
  57. data/app/assets/stylesheets/vendor.css.map +1 -0
  58. data/app/assets/stylesheets/vendor.sass +1 -0
  59. data/app/components/formatic/base.rb +4 -1
  60. data/app/components/formatic/date.rb +1 -1
  61. data/app/components/formatic/file.rb +21 -0
  62. data/app/components/formatic/files.rb +8 -0
  63. data/app/components/formatic/select.rb +1 -1
  64. data/app/components/formatic/time.rb +3 -3
  65. data/config/importmap.rb +11 -0
  66. data/lib/formatic/choices/options.rb +1 -1
  67. data/lib/formatic/choices/records.rb +1 -1
  68. data/lib/formatic/engine.rb +12 -0
  69. data/lib/formatic/version.rb +1 -1
  70. data/lib/formatic/wrappers/required.rb +1 -1
  71. data/lib/formatic.rb +2 -1
  72. metadata +77 -17
  73. data/app/assets/javascript/formatic/components/date.ts +0 -54
  74. data/app/assets/javascript/formatic/components/select.ts +0 -113
  75. data/app/assets/javascript/formatic/components/stepper.ts +0 -89
  76. data/app/assets/javascript/formatic/components/string.ts +0 -103
  77. data/app/assets/javascript/formatic/components/textarea.ts +0 -112
  78. data/app/assets/javascript/formatic/components/toggle.ts +0 -89
  79. data/app/assets/javascript/formatic.js +0 -446
  80. data/app/assets/javascript/formatic.js.map +0 -1
  81. data/app/assets/stylesheets/formatic/index.sass +0 -17
  82. 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,101 @@
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
+
14
+ constructor(el: HTMLElement) {
15
+ this.el = el
16
+ this.url = this.input.dataset.directUploadUrl
17
+ this.setupBindings()
18
+ }
19
+
20
+ private setupBindings() {
21
+ this.pond = FilePond.create(this.input, {
22
+ credits: false,
23
+ onwarning: () => this.updateSubmit(),
24
+ onerror: () => this.updateSubmit(),
25
+ onaddfilestart: () => this.updateSubmit(),
26
+ onaddfileprogress: () => this.updateSubmit(),
27
+ onaddfile: () => this.updateSubmit(),
28
+ onprocessfilestart: () => this.updateSubmit(),
29
+ onprocessfileprogress: () => this.updateSubmit(),
30
+ onprocessfileabort: () => this.updateSubmit(),
31
+ onprocessfilerevert: () => this.updateSubmit(),
32
+ onprocessfile: () => this.updateSubmit(),
33
+ onprocessfiles: () => this.updateSubmit(),
34
+ onremovefile: () => this.updateSubmit(),
35
+ onpreparefile: () => this.updateSubmit(),
36
+ onupdatefiles: () => this.updateSubmit(),
37
+ onactivatefile: () => this.updateSubmit(),
38
+ onreorderfiles: () => this.updateSubmit(),
39
+ server: {
40
+ process: (fieldName, file, _metadata, load, error, progress, abort, transfer, options) => {
41
+
42
+ const uploader = new DirectUpload(file as File, this.url, {
43
+ directUploadWillStoreFileWithXHR: (request) => {
44
+ request.upload.addEventListener(
45
+ 'progress',
46
+ event => progress(event.lengthComputable, event.loaded, event.total)
47
+ )
48
+ }
49
+ })
50
+
51
+ uploader.create((errorResponse, blob) => {
52
+ if (errorResponse) {
53
+ error(`Something went wrong: ${errorResponse}`)
54
+ } else {
55
+ // See https://edgeguides.rubyonrails.org/active_storage_overview.html#direct-upload-javascript-events
56
+ // See https://stackoverflow.com/questions/75586818
57
+ load(blob.signed_id)
58
+ }
59
+ })
60
+
61
+ return {
62
+ abort: () => abort()
63
+ }
64
+ },
65
+ headers: {
66
+ 'X-CSRF-Token': document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content
67
+ }
68
+ },
69
+ })
70
+ }
71
+
72
+ private updateSubmit() {
73
+ const files = this.pond.getFiles()
74
+ const busyFiles = files.some(f => this.busyStatuses.has(f.status))
75
+ busyFiles ? this.disableSubmit() : this.enableSubmit()
76
+ }
77
+
78
+ private enableSubmit() {
79
+ this.submitButtons.forEach((button) => {
80
+ button.disabled = false
81
+ })
82
+ }
83
+
84
+ private disableSubmit() {
85
+ this.submitButtons.forEach((button) => {
86
+ button.disabled = true
87
+ })
88
+ }
89
+
90
+ private get input() {
91
+ return this.el.querySelector<HTMLLinkElement>('.js-formatic-file__input')
92
+ }
93
+
94
+ private get form() {
95
+ return this.el.closest<HTMLFormElement>('form')
96
+ }
97
+
98
+ private get submitButtons() {
99
+ return this.form.querySelectorAll<HTMLButtonElement>('[type="submit"]')
100
+ }
101
+ }
@@ -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,44 @@
1
+ import { Date } from 'formatic/date'
2
+ import { Select } from 'formatic/select'
3
+ import { Stepper } from 'formatic/stepper'
4
+ import { String } from 'formatic/string'
5
+ import { Textarea } from 'formatic/textarea'
6
+ import { Toggle } from 'formatic/toggle'
7
+ import { FormaticFile } from 'formatic/file'
8
+
9
+ document.addEventListener('DOMContentLoaded', () => {
10
+ document.querySelectorAll('.js-formatic-file').forEach((el) => {
11
+ console.debug('[Formatic] Instantiating File...')
12
+ new FormaticFile(el)
13
+ })
14
+
15
+ document.querySelectorAll('.js-formatic-date').forEach((el) => {
16
+ console.debug('[Formatic] Instantiating Date...')
17
+ new Date(el)
18
+ })
19
+
20
+ document.querySelectorAll('.js-formatic-select').forEach((el) => {
21
+ console.debug('[Formatic] Instantiating Select...')
22
+ new Select(el)
23
+ })
24
+
25
+ document.querySelectorAll('.js-formatic-stepper').forEach((el) => {
26
+ console.debug('[Formatic] Instantiating Stepper...')
27
+ new Stepper(el)
28
+ })
29
+
30
+ document.querySelectorAll('.js-formatic-string').forEach((el) => {
31
+ console.debug('[Formatic] Instantiating String...')
32
+ new String(el)
33
+ })
34
+
35
+ document.querySelectorAll('.js-formatic-textarea').forEach((el) => {
36
+ console.debug('[Formatic] Instantiating Textarea...')
37
+ new Textarea(el)
38
+ })
39
+
40
+ document.querySelectorAll('.js-formatic-toggle').forEach((el) => {
41
+ console.debug('[Formatic] Instantiating Toggle...')
42
+ new Toggle(el)
43
+ })
44
+ })
@@ -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
+ }