formstrap 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +33 -0
  4. data/CHANGELOG.md +1 -0
  5. data/CODE_OF_CONDUCT.md +84 -0
  6. data/Gemfile +28 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +118 -0
  9. data/Rakefile +10 -0
  10. data/app/assets/config/headmin_manifest.js +2 -0
  11. data/app/assets/images/avatar.jpg +0 -0
  12. data/app/assets/images/document.docx +0 -0
  13. data/app/assets/images/document.pdf +0 -0
  14. data/app/assets/images/image.jpg +0 -0
  15. data/app/assets/images/spreadsheet.xls +0 -0
  16. data/app/assets/images/video.mp4 +0 -0
  17. data/app/assets/javascripts/formstrap/config/i18n.js +11 -0
  18. data/app/assets/javascripts/formstrap/controllers/autocomplete_controller.js +318 -0
  19. data/app/assets/javascripts/formstrap/controllers/date_range_controller.js +38 -0
  20. data/app/assets/javascripts/formstrap/controllers/dropzone_controller.js +31 -0
  21. data/app/assets/javascripts/formstrap/controllers/file_preview_controller.js +244 -0
  22. data/app/assets/javascripts/formstrap/controllers/flatpickr_controller.js +35 -0
  23. data/app/assets/javascripts/formstrap/controllers/infinite_scroller_controller.js +28 -0
  24. data/app/assets/javascripts/formstrap/controllers/media_controller.js +252 -0
  25. data/app/assets/javascripts/formstrap/controllers/media_modal_controller.js +147 -0
  26. data/app/assets/javascripts/formstrap/controllers/redactorx_controller.js +40 -0
  27. data/app/assets/javascripts/formstrap/controllers/repeater_controller.js +148 -0
  28. data/app/assets/javascripts/formstrap/controllers/select_controller.js +49 -0
  29. data/app/assets/javascripts/formstrap/controllers/textarea_controller.js +48 -0
  30. data/app/assets/javascripts/formstrap/index.js +32 -0
  31. data/app/assets/javascripts/formstrap.js +11515 -0
  32. data/app/assets/stylesheets/formstrap/forms/autocomplete.scss +27 -0
  33. data/app/assets/stylesheets/formstrap/forms/file.scss +83 -0
  34. data/app/assets/stylesheets/formstrap/forms/media.scss +10 -0
  35. data/app/assets/stylesheets/formstrap/forms/repeater.scss +62 -0
  36. data/app/assets/stylesheets/formstrap/forms/search.scss +12 -0
  37. data/app/assets/stylesheets/formstrap/forms.scss +12 -0
  38. data/app/assets/stylesheets/formstrap/general.scss +18 -0
  39. data/app/assets/stylesheets/formstrap/media/index.scss +9 -0
  40. data/app/assets/stylesheets/formstrap/media.scss +1 -0
  41. data/app/assets/stylesheets/formstrap/utilities/buttons.scss +27 -0
  42. data/app/assets/stylesheets/formstrap/utilities/dropzone.scss +72 -0
  43. data/app/assets/stylesheets/formstrap/utilities.scss +2 -0
  44. data/app/assets/stylesheets/formstrap/vendor/flatpickr.css +903 -0
  45. data/app/assets/stylesheets/formstrap/vendor/tom-select-bootstrap.scss +535 -0
  46. data/app/assets/stylesheets/formstrap.css +1559 -0
  47. data/app/assets/stylesheets/formstrap.scss +11 -0
  48. data/app/controllers/concerns/formstrap/pagination.rb +27 -0
  49. data/app/controllers/formstrap/media_controller.rb +68 -0
  50. data/app/controllers/formstrap_controller.rb +2 -0
  51. data/app/models/concerns/formstrap/autocompletable.rb +36 -0
  52. data/app/models/concerns/formstrap/hintable.rb +22 -0
  53. data/app/models/concerns/formstrap/input_groupable.rb +21 -0
  54. data/app/models/concerns/formstrap/labelable.rb +31 -0
  55. data/app/models/concerns/formstrap/listable.rb +26 -0
  56. data/app/models/concerns/formstrap/placeholderable.rb +11 -0
  57. data/app/models/concerns/formstrap/validatable.rb +38 -0
  58. data/app/models/concerns/formstrap/wrappable.rb +19 -0
  59. data/app/models/formstrap/.DS_Store +0 -0
  60. data/app/models/formstrap/association_view.rb +100 -0
  61. data/app/models/formstrap/blocks_view.rb +43 -0
  62. data/app/models/formstrap/checkbox_view.rb +50 -0
  63. data/app/models/formstrap/color_view.rb +45 -0
  64. data/app/models/formstrap/date_range_view.rb +23 -0
  65. data/app/models/formstrap/date_view.rb +43 -0
  66. data/app/models/formstrap/datetime_range_view.rb +23 -0
  67. data/app/models/formstrap/datetime_view.rb +43 -0
  68. data/app/models/formstrap/email_view.rb +46 -0
  69. data/app/models/formstrap/file_view.rb +106 -0
  70. data/app/models/formstrap/flatpickr_range_view.rb +89 -0
  71. data/app/models/formstrap/flatpickr_view.rb +27 -0
  72. data/app/models/formstrap/hidden_view.rb +8 -0
  73. data/app/models/formstrap/hint_view.rb +4 -0
  74. data/app/models/formstrap/input_group_view.rb +17 -0
  75. data/app/models/formstrap/label_view.rb +22 -0
  76. data/app/models/formstrap/media_item_view.rb +41 -0
  77. data/app/models/formstrap/media_view.rb +143 -0
  78. data/app/models/formstrap/number_view.rb +47 -0
  79. data/app/models/formstrap/password_view.rb +42 -0
  80. data/app/models/formstrap/redactorx_view.rb +57 -0
  81. data/app/models/formstrap/search_view.rb +46 -0
  82. data/app/models/formstrap/select_view.rb +61 -0
  83. data/app/models/formstrap/switch_view.rb +21 -0
  84. data/app/models/formstrap/text_view.rb +46 -0
  85. data/app/models/formstrap/textarea_view.rb +47 -0
  86. data/app/models/formstrap/url_view.rb +46 -0
  87. data/app/models/formstrap/wrapper_view.rb +17 -0
  88. data/app/models/formstrap/wysiwyg_view.rb +15 -0
  89. data/app/models/view_model.rb +62 -0
  90. data/app/views/formstrap/_association.html.erb +30 -0
  91. data/app/views/formstrap/_autocomplete.html.erb +11 -0
  92. data/app/views/formstrap/_blocks.html.erb +45 -0
  93. data/app/views/formstrap/_checkbox.html.erb +34 -0
  94. data/app/views/formstrap/_color.html.erb +32 -0
  95. data/app/views/formstrap/_datalist.html.erb +3 -0
  96. data/app/views/formstrap/_date.html.erb +41 -0
  97. data/app/views/formstrap/_date_range.html.erb +40 -0
  98. data/app/views/formstrap/_datetime.html.erb +41 -0
  99. data/app/views/formstrap/_datetime_range.html.erb +40 -0
  100. data/app/views/formstrap/_email.html.erb +43 -0
  101. data/app/views/formstrap/_errors.html.erb +19 -0
  102. data/app/views/formstrap/_file.html.erb +94 -0
  103. data/app/views/formstrap/_flatpickr.html.erb +33 -0
  104. data/app/views/formstrap/_flatpickr_range.html.erb +40 -0
  105. data/app/views/formstrap/_hidden.html.erb +23 -0
  106. data/app/views/formstrap/_hint.html.erb +21 -0
  107. data/app/views/formstrap/_input_group.html.erb +21 -0
  108. data/app/views/formstrap/_label.html.erb +22 -0
  109. data/app/views/formstrap/_media.html.erb +60 -0
  110. data/app/views/formstrap/_number.html.erb +41 -0
  111. data/app/views/formstrap/_password.html.erb +39 -0
  112. data/app/views/formstrap/_redactorx.html.erb +31 -0
  113. data/app/views/formstrap/_repeater.html.erb +128 -0
  114. data/app/views/formstrap/_search.html.erb +43 -0
  115. data/app/views/formstrap/_select.html.erb +43 -0
  116. data/app/views/formstrap/_switch.html.erb +29 -0
  117. data/app/views/formstrap/_text.html.erb +42 -0
  118. data/app/views/formstrap/_textarea.html.erb +39 -0
  119. data/app/views/formstrap/_to_ary.html.erb +0 -0
  120. data/app/views/formstrap/_url.html.erb +43 -0
  121. data/app/views/formstrap/_validation.html.erb +18 -0
  122. data/app/views/formstrap/_wrapper.html.erb +8 -0
  123. data/app/views/formstrap/_wysiwyg.html.erb +28 -0
  124. data/app/views/formstrap/autocomplete/_item.html.erb +3 -0
  125. data/app/views/formstrap/autocomplete/_list.html.erb +3 -0
  126. data/app/views/formstrap/blocks/_modal.html.erb +20 -0
  127. data/app/views/formstrap/fields/_base.html.erb +25 -0
  128. data/app/views/formstrap/fields/_file.html.erb +17 -0
  129. data/app/views/formstrap/fields/_files.html.erb +17 -0
  130. data/app/views/formstrap/fields/_group.html.erb +52 -0
  131. data/app/views/formstrap/fields/_list.html.erb +31 -0
  132. data/app/views/formstrap/fields/_text.html.erb +17 -0
  133. data/app/views/formstrap/media/_item.html.erb +38 -0
  134. data/app/views/formstrap/media/_media_item_modal.html.erb +77 -0
  135. data/app/views/formstrap/media/_modal.html.erb +40 -0
  136. data/app/views/formstrap/media/_thumbnail.html.erb +20 -0
  137. data/app/views/formstrap/media/_validation.html.erb +10 -0
  138. data/app/views/formstrap/media/create.turbo_stream.erb +5 -0
  139. data/app/views/formstrap/media/index.html.erb +3 -0
  140. data/app/views/formstrap/media/index.turbo_stream.erb +11 -0
  141. data/app/views/formstrap/media/show.html.erb +9 -0
  142. data/app/views/formstrap/media/thumbnail.html.erb +3 -0
  143. data/app/views/formstrap/media/update.turbo_stream.erb +3 -0
  144. data/app/views/formstrap/pagination/_infinite.html.erb +7 -0
  145. data/app/views/formstrap/repeater/_row.html.erb +53 -0
  146. data/app/views/formstrap/shared/_notifications.html.erb +20 -0
  147. data/app/views/formstrap/shared/_popup.html.erb +32 -0
  148. data/app/views/formstrap/shared/_thumbnail.html.erb +35 -0
  149. data/bin/console +14 -0
  150. data/bin/setup +8 -0
  151. data/config/importmap.rb +2 -0
  152. data/config/locales/activerecord/en.yml +12 -0
  153. data/config/locales/activerecord/nl.yml +13 -0
  154. data/config/locales/defaults/en.yml +215 -0
  155. data/config/locales/defaults/nl.yml +213 -0
  156. data/config/locales/devise/en.yml +65 -0
  157. data/config/locales/devise/nl.yml +85 -0
  158. data/config/locales/en.yml +6 -0
  159. data/config/locales/formstrap/forms/en.yml +39 -0
  160. data/config/locales/formstrap/forms/nl.yml +39 -0
  161. data/config/locales/formstrap/media/en.yml +24 -0
  162. data/config/locales/formstrap/media/nl.yml +24 -0
  163. data/config/locales/formstrap/thumbnail/en.yml +4 -0
  164. data/config/locales/formstrap/thumbnail/nl.yml +4 -0
  165. data/config/locales/nl.yml +6 -0
  166. data/config/routes.rb +11 -0
  167. data/esbuild-css.js +25 -0
  168. data/esbuild-js.js +11 -0
  169. data/formstrap.gemspec +37 -0
  170. data/formstrap.iml +34 -0
  171. data/lib/formstrap/engine.rb +27 -0
  172. data/lib/formstrap/form_builder.rb +177 -0
  173. data/lib/formstrap/form_helper.rb +19 -0
  174. data/lib/formstrap/version.rb +3 -0
  175. data/lib/formstrap.rb +6 -0
  176. data/package.json +54 -0
  177. data/src/js/formstrap.js +1 -0
  178. data/src/scss/formstrap.scss +1 -0
  179. data/yarn.lock +1998 -0
  180. metadata +224 -0
@@ -0,0 +1,38 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+
3
+ export default class extends Controller {
4
+ update (event) {
5
+ const flatpickr = event.target._flatpickr
6
+ const startDate = flatpickr.selectedDates[0]
7
+ const endDate = flatpickr.selectedDates[1]
8
+
9
+ this.setStartDateInputValue(this.formatDate(startDate))
10
+ this.setEndDateInputValue(this.formatDate(endDate))
11
+ }
12
+
13
+ setStartDateInputValue (value) {
14
+ const startDateInput = this.startDateInput()
15
+ startDateInput.value = value
16
+ }
17
+
18
+ setEndDateInputValue (value) {
19
+ const endDateInput = this.endDateInput()
20
+ endDateInput.value = value
21
+ }
22
+
23
+ startDateInput () {
24
+ return this.element.nextElementSibling
25
+ }
26
+
27
+ endDateInput () {
28
+ return this.startDateInput().nextElementSibling
29
+ }
30
+
31
+ formatDate (date) {
32
+ if (date instanceof Date) {
33
+ return date.toLocaleDateString('nl-BE', { day: '2-digit', month: '2-digit', year: 'numeric' })
34
+ } else {
35
+ return null
36
+ }
37
+ }
38
+ }
@@ -0,0 +1,31 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+
3
+ // References:
4
+ // https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
5
+
6
+ export default class extends Controller {
7
+ static get targets () {
8
+ return ['input']
9
+ }
10
+
11
+ connect () {
12
+ // Drag
13
+ this.inputTarget.addEventListener('dragover', (event) => {
14
+ this.element.classList.add('focus')
15
+ })
16
+ this.inputTarget.addEventListener('dragleave', (event) => {
17
+ this.element.classList.remove('focus')
18
+ })
19
+ this.inputTarget.addEventListener('drop', (event) => {
20
+ this.element.classList.remove('focus')
21
+ })
22
+
23
+ // Focus
24
+ this.inputTarget.addEventListener('focusin', (event) => {
25
+ this.element.classList.add('focus')
26
+ })
27
+ this.inputTarget.addEventListener('focusout', (event) => {
28
+ this.element.classList.remove('focus')
29
+ })
30
+ }
31
+ }
@@ -0,0 +1,244 @@
1
+ /* global FileReader, DataTransfer */
2
+ import { Controller } from '@hotwired/stimulus'
3
+
4
+ // References:
5
+ // https://developer.mozilla.org/en-US/docs/Web/API/FileReader
6
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file
7
+
8
+ export default class extends Controller {
9
+ static get targets () {
10
+ return ['thumbnails', 'template', 'input', 'placeholder', 'thumbnail', 'thumbnailDestroy']
11
+ }
12
+
13
+ connect () {
14
+ this.thumbnailWidth = this.firstThumbnailWidth()
15
+ this.thumbnailHeight = this.firstThumbnailHeight()
16
+ }
17
+
18
+ preview () {
19
+ this.removeThumbnails()
20
+ this.addThumbnails()
21
+ this.togglePlaceholder()
22
+ }
23
+
24
+ togglePlaceholder () {
25
+ if (this.hasFilesSelected()) {
26
+ this.hidePlaceholder()
27
+ } else {
28
+ this.showPlaceholder()
29
+ }
30
+ }
31
+
32
+ hasFilesSelected () {
33
+ return this.hasInputFiles() || this.hasVisibleAttachments()
34
+ }
35
+
36
+ hasVisibleAttachments () {
37
+ return this.visibleAttachments().length > 0
38
+ }
39
+
40
+ hasAttachments () {
41
+ return this.attachments().length > 0
42
+ }
43
+
44
+ hasInputFiles () {
45
+ return this.inputFiles().length > 0
46
+ }
47
+
48
+ attachments () {
49
+ return this.thumbnailTargets.filter((thumbnail) => {
50
+ return this.isAttachment(thumbnail)
51
+ })
52
+ }
53
+
54
+ visibleAttachments () {
55
+ return this.attachments().filter((thumbnail) => {
56
+ return !thumbnail.classList.contains('d-none')
57
+ })
58
+ }
59
+
60
+ inputFiles () {
61
+ return Array.from(this.inputTarget.files)
62
+ }
63
+
64
+ writeInputFiles (files) {
65
+ const dataTransfer = new DataTransfer()
66
+ files.forEach((file) => {
67
+ dataTransfer.items.add(file)
68
+ })
69
+ this.inputTarget.files = dataTransfer.files
70
+ }
71
+
72
+ remove (event) {
73
+ const thumbnail = event.target.closest('[data-file-preview-target="thumbnail"]')
74
+ this.removeThumbnail(thumbnail, event.params.name)
75
+ this.togglePlaceholder()
76
+ }
77
+
78
+ isAttachment (thumbnail) {
79
+ return thumbnail.querySelector('[data-file-preview-target="thumbnailDestroy"]')
80
+ }
81
+
82
+ removeInputFile (thumbnail, fileName) {
83
+ let files = this.inputFiles()
84
+ files = files.filter((file) => {
85
+ return file.name !== fileName
86
+ })
87
+ thumbnail.remove()
88
+ this.writeInputFiles(files)
89
+ }
90
+
91
+ removeAttachment (thumbnail) {
92
+ const destroyInput = thumbnail.querySelector('[data-file-preview-target="thumbnailDestroy"]')
93
+ destroyInput.value = '1'
94
+ thumbnail.classList.add('d-none')
95
+ }
96
+
97
+ showPlaceholder () {
98
+ this.placeholderTarget.classList.remove('d-none')
99
+ }
100
+
101
+ hidePlaceholder () {
102
+ this.placeholderTarget.classList.add('d-none')
103
+ }
104
+
105
+ removeThumbnails () {
106
+ this.thumbnailTargets.forEach((thumbnail) => {
107
+ this.removeThumbnail(thumbnail)
108
+ })
109
+ }
110
+
111
+ removeThumbnail (thumbnail, filename) {
112
+ if (this.isAttachment(thumbnail)) {
113
+ this.removeAttachment(thumbnail)
114
+ } else {
115
+ this.removeInputFile(thumbnail, filename)
116
+ }
117
+ }
118
+
119
+ addThumbnails () {
120
+ const files = this.inputFiles()
121
+ files.forEach((file) => {
122
+ const thumbnail = this.generateDummyThumbnail()
123
+ this.appendThumbnail(thumbnail)
124
+ this.updateThumbnail(this.lastThumbnail(), file)
125
+ })
126
+ }
127
+
128
+ generateDummyThumbnail () {
129
+ return this.templateTarget.content.cloneNode(true)
130
+ }
131
+
132
+ appendThumbnail (thumbnail) {
133
+ return this.thumbnailsTarget.appendChild(thumbnail)
134
+ }
135
+
136
+ updateThumbnail (thumbnail, file) {
137
+ this.updateThumbnailRemoveButton(thumbnail, file.name)
138
+
139
+ const title = this.titleForFile(file)
140
+ this.updateThumbnailTitle(thumbnail, title)
141
+
142
+ const icon = this.iconForMimeType(file.type)
143
+ this.updateThumbnailIcon(thumbnail, icon)
144
+
145
+ if (this.isImage(file)) {
146
+ this.updateThumbnailImage(thumbnail, file)
147
+ }
148
+ }
149
+
150
+ updateThumbnailImage (thumbnail, file) {
151
+ this.fileToBase64(file).then(base64 => {
152
+ this.updateThumbnailBackground(thumbnail, base64)
153
+ this.removeThumbnailIcon(thumbnail)
154
+ })
155
+ }
156
+
157
+ titleForFile (file) {
158
+ const byteSizeString = this.bytesToString(file.size)
159
+ return `${file.name} (${byteSizeString})`
160
+ }
161
+
162
+ bytesToString (bytes) {
163
+ const i = Math.floor(Math.log(bytes) / Math.log(1024))
164
+ return (bytes / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]
165
+ }
166
+
167
+ updateThumbnailRemoveButton (thumbnail, fileName) {
168
+ const removeButton = thumbnail.querySelector('.h-form-file-thumbnail-remove')
169
+ if (removeButton) {
170
+ removeButton.dataset.filePreviewNameParam = fileName
171
+ }
172
+ }
173
+
174
+ updateThumbnailTitle (thumbnail, title) {
175
+ thumbnail.title = title
176
+ }
177
+
178
+ updateThumbnailBackground (thumbnail, url) {
179
+ const thumbnailBackground = thumbnail.querySelector('.h-thumbnail-bg')
180
+ thumbnailBackground.style.backgroundImage = `url('${url}')`
181
+ }
182
+
183
+ removeThumbnailIcon (thumbnail) {
184
+ thumbnail.querySelector('.h-thumbnail-bg').innerHTML = ''
185
+ }
186
+
187
+ updateThumbnailIcon (thumbnail, icon) {
188
+ thumbnail.querySelector('.h-thumbnail-bg').innerHTML = icon
189
+ }
190
+
191
+ iconForMimeType (mimeType) {
192
+ const typeMap = {
193
+ image: ['image/bmp', 'image/gif', 'image/vnd.microsoft.icon', 'image/jpeg', 'image/png', 'image/svg+xml', 'image/tiff', 'image/webp'],
194
+ play: ['video/mp4', 'video/mpeg', 'video/ogg', 'video/mp2t', 'video/webm', 'video/3gpp', 'video/3gpp2'],
195
+ music: ['audio/aac', 'audio/midi', 'audio/x-midi', 'audio/mpeg', 'audio/ogg', 'audio/opus', 'audio/wav', 'audio/webm', 'audio/3gpp', 'audio/3gpp2'],
196
+ word: ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
197
+ ppt: ['application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
198
+ excel: ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
199
+ slides: ['application/vnd.oasis.opendocument.presentation'],
200
+ spreadsheet: ['application/vnd.oasis.opendocument.spreadsheet'],
201
+ richtext: ['application/vnd.oasis.opendocument.text'],
202
+ zip: ['application/zip application/x-7z-compressed', 'application/x-bzip application/x-bzip2 application/gzip application/vnd.rar'],
203
+ pdf: ['application/pdf']
204
+ }
205
+
206
+ const iconName = Object.keys(typeMap).find(key => typeMap[key].includes(mimeType))
207
+ const fullIconName = ['bi', 'file', 'earmark', iconName].filter(e => typeof e === 'string' && e !== '').join('-')
208
+
209
+ return `<i class="bi ${fullIconName} h-thumbnail-icon"></i>`
210
+ }
211
+
212
+ isImage (file) {
213
+ return file.type.match(/^image/) !== null
214
+ }
215
+
216
+ fileToBase64 (file) {
217
+ return new Promise((resolve, reject) => {
218
+ const reader = new FileReader()
219
+ reader.readAsDataURL(file)
220
+ reader.onload = () => resolve(reader.result)
221
+ reader.onerror = error => reject(error)
222
+ })
223
+ }
224
+
225
+ thumbnails () {
226
+ return this.thumbnailsTarget.querySelectorAll('.img-thumbnail')
227
+ }
228
+
229
+ firstThumbnail () {
230
+ return this.thumbnailsTarget.firstElementChild
231
+ }
232
+
233
+ lastThumbnail () {
234
+ return this.thumbnailsTarget.lastElementChild
235
+ }
236
+
237
+ firstThumbnailWidth () {
238
+ return this.firstThumbnail().style.width
239
+ }
240
+
241
+ firstThumbnailHeight () {
242
+ return this.firstThumbnail().style.height
243
+ }
244
+ }
@@ -0,0 +1,35 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+ import flatpickr from 'flatpickr'
3
+ import { Dutch } from 'flatpickr/dist/esm/l10n/nl.js'
4
+ import I18n from '../config/i18n'
5
+
6
+ export default class extends Controller {
7
+ connect () {
8
+ const options = { ...this.defaultOptions(), ...this.options() }
9
+ flatpickr(this.element, options)
10
+ }
11
+
12
+ options () {
13
+ return JSON.parse(this.element.getAttribute('data-flatpickr'))
14
+ }
15
+
16
+ defaultOptions () {
17
+ return {
18
+ allowInput: true,
19
+ dateFormat: 'd/m/Y',
20
+ locale: this.getLocale(I18n.locale)
21
+ }
22
+ }
23
+
24
+ getLocale (locale) {
25
+ const locales = this.locales()
26
+ return locales[locale]
27
+ }
28
+
29
+ locales () {
30
+ return {
31
+ en: null,
32
+ nl: Dutch
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,28 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+
3
+ export default class extends Controller {
4
+ connect () {
5
+ this.clickWhenInViewport()
6
+
7
+ document.querySelector('.modal-body').addEventListener('scroll', () => {
8
+ this.clickWhenInViewport()
9
+ })
10
+ }
11
+
12
+ clickWhenInViewport () {
13
+ if (!this.isLoading() && this.isInViewport()) {
14
+ this.element.setAttribute('clicked', 1)
15
+ this.element.click()
16
+ }
17
+ }
18
+
19
+ isLoading () {
20
+ return this.element.hasAttribute('clicked')
21
+ }
22
+
23
+ isInViewport () {
24
+ const rect = this.element.getBoundingClientRect()
25
+
26
+ return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth)
27
+ }
28
+ }
@@ -0,0 +1,252 @@
1
+ import { Controller } from '@hotwired/stimulus'
2
+ import Sortable from 'sortablejs'
3
+
4
+ export default class extends Controller {
5
+ static get targets () {
6
+ return ['item', 'template', 'thumbnails', 'modalButton', 'placeholder', 'count', 'editButton', 'validationInput']
7
+ }
8
+
9
+ connect () {
10
+ document.addEventListener('mediaSelectionSubmitted', (event) => {
11
+ if (event.detail.name === this.element.dataset.name) {
12
+ this.selectItems(event.detail.items)
13
+ }
14
+ })
15
+
16
+ // Init sorting
17
+ if (this.hasSorting()) {
18
+ this.initSortable()
19
+ }
20
+
21
+ this.validate()
22
+ }
23
+
24
+ // Actions
25
+ destroy (event) {
26
+ const item = event.currentTarget.closest('[data-media-target=\'item\']')
27
+ this.destroyItem(item)
28
+ }
29
+
30
+ syncIds () {
31
+ const ids = this.activeIds()
32
+
33
+ this.modalButtonTargets.forEach((button) => {
34
+ const url = new URL(button.getAttribute('href'))
35
+
36
+ // Remove all ids[]
37
+ url.searchParams.delete('ids[]')
38
+
39
+ // Add new ids
40
+ ids.forEach((id) => {
41
+ url.searchParams.append('ids[]', id)
42
+ })
43
+
44
+ button.setAttribute('href', url.toString())
45
+ })
46
+ }
47
+
48
+ // Methods
49
+ initSortable () {
50
+ Sortable.create(this.thumbnailsTarget, {
51
+ handle: '.media-drag-sort-handle',
52
+ onEnd: (event) => {
53
+ this.resetPositions()
54
+ }
55
+ })
56
+ }
57
+
58
+ hasSorting () {
59
+ return this.element.dataset.sort === 'true'
60
+ }
61
+
62
+ destroyItem (item) {
63
+ this.removeItem(item)
64
+ this.postProcess()
65
+ }
66
+
67
+ selectItems (items) {
68
+ // Destroy all deselected items
69
+ this.removeAllDeselectedItems(items)
70
+
71
+ // Add all new selected items
72
+ this.addNewItems(items)
73
+
74
+ this.postProcess()
75
+ }
76
+
77
+ postProcess () {
78
+ // Reset positions
79
+ this.resetPositions()
80
+
81
+ // Sync Ids
82
+ this.syncIds()
83
+
84
+ // Toggle placeholder
85
+ this.togglePlaceholder()
86
+
87
+ // Validate
88
+ this.validate()
89
+ }
90
+
91
+ validate () {
92
+ this.clearValidation()
93
+ this.validateMinimum()
94
+ this.validateMaximum()
95
+ }
96
+
97
+ clearValidation () {
98
+ this.validationInputTarget.setCustomValidity('')
99
+ }
100
+
101
+ validateMinimum () {
102
+ const count = this.activeItems().length
103
+ if (count < this.minActiveItems()) {
104
+ this.validationInputTarget.setCustomValidity(this.validationInputTarget.dataset.minMessage)
105
+ }
106
+ }
107
+
108
+ validateMaximum () {
109
+ const count = this.activeItems().length
110
+ if (count > this.maxActiveItems()) {
111
+ this.validationInputTarget.setCustomValidity(this.validationInputTarget.dataset.maxMessage)
112
+ }
113
+ }
114
+
115
+ minActiveItems () {
116
+ return parseInt(this.element.dataset.min, 10) || 0
117
+ }
118
+
119
+ maxActiveItems () {
120
+ return parseInt(this.element.dataset.max, 10) || Infinity
121
+ }
122
+
123
+ resetPositions () {
124
+ this.activeItems().forEach((item, index) => {
125
+ const positionInput = item.querySelector('input[name*=\'position\']')
126
+ if (positionInput) {
127
+ positionInput.value = index
128
+ }
129
+ })
130
+ }
131
+
132
+ addNewItems (items) {
133
+ const itemTargetIds = this.itemTargets.map((i) => { return parseInt(i.querySelectorAll('input')[1].value) })
134
+ items.forEach((item) => {
135
+ if (itemTargetIds.includes(item.blobId)) {
136
+ // Do not add this item (as it is already present)
137
+ return
138
+ }
139
+
140
+ this.addItem(item)
141
+ })
142
+ }
143
+
144
+ addItem (item) {
145
+ const currentItem = this.itemByBlobId(item.blobId)
146
+ if (currentItem) {
147
+ // (re-)enable if already exists
148
+ this.enableItem(currentItem)
149
+ } else {
150
+ // Create if item doesn't exist yet
151
+ this.createItem(item)
152
+ }
153
+ }
154
+
155
+ togglePlaceholder () {
156
+ if (this.activeItems().length > 0) {
157
+ this.hidePlaceholder()
158
+ } else {
159
+ this.showPlaceholder()
160
+ }
161
+ }
162
+
163
+ showPlaceholder () {
164
+ this.placeholderTarget.classList.remove('d-none')
165
+ }
166
+
167
+ hidePlaceholder () {
168
+ this.placeholderTarget.classList.add('d-none')
169
+ }
170
+
171
+ enableItem (item) {
172
+ item.querySelector('input[name*=\'_destroy\']').value = false
173
+ item.classList.remove('d-none')
174
+ }
175
+
176
+ createItem (item) {
177
+ // Copy template
178
+ const template = this.templateTarget
179
+ const html = this.randomizeIds(template)
180
+ this.thumbnailsTarget.insertAdjacentHTML('beforeend', html)
181
+
182
+ // Set new values
183
+ const newItem = this.itemTargets.pop()
184
+ newItem.querySelector('input[name*="[blob_id]"]').value = item.blobId
185
+ newItem.querySelector('input[name*="[_destroy]"]').value = false
186
+
187
+ // Update edit button url
188
+ const editButton = newItem.querySelector('[data-media-target="editButton"]')
189
+ editButton.setAttribute('href', editButton.getAttribute('href').replace('$1', item.blobId))
190
+
191
+ // Copy thumbnail
192
+ const oldThumbnail = newItem.querySelector('.h-thumbnail')
193
+ const newThumbnail = item.thumbnail.cloneNode(true)
194
+ oldThumbnail.parentNode.replaceChild(newThumbnail, oldThumbnail)
195
+ }
196
+
197
+ randomizeIds (template) {
198
+ const regex = new RegExp(template.dataset.templateIdRegex, 'g')
199
+ const randomNumber = Math.floor(100000000 + Math.random() * 900000000)
200
+ return template.innerHTML.replace(regex, randomNumber)
201
+ }
202
+
203
+ removeAllDeselectedItems (items) {
204
+ this.removeDeselectedItems(items, this.itemTargets)
205
+ }
206
+
207
+ removeDeselectedItems (elements, items) {
208
+ const returnedBlobIds = elements.map((e) => { return e.blobId })
209
+
210
+ items.forEach((item) => {
211
+ const blobId = parseInt(item.querySelectorAll('input')[1].value)
212
+ if (returnedBlobIds.includes(blobId)) {
213
+ // Do not delete this one
214
+ return
215
+ }
216
+
217
+ this.removeItem(item)
218
+ })
219
+ }
220
+
221
+ removeItem (item) {
222
+ item.querySelector('input[name*=\'_destroy\']').value = 1
223
+ item.classList.add('d-none')
224
+
225
+ // Reset positions
226
+ this.resetPositions()
227
+
228
+ // Sync ids
229
+ this.syncIds()
230
+
231
+ // Toggle placeholder
232
+ this.togglePlaceholder()
233
+ }
234
+
235
+ itemByBlobId (blobId) {
236
+ return this.itemTargets.find((item) => {
237
+ return item.querySelector('input[name*=\'blob_id\']').value === blobId
238
+ })
239
+ }
240
+
241
+ activeItems () {
242
+ return this.itemTargets.filter((item) => {
243
+ return item.querySelector('input[name$=\'[_destroy]\']').value === 'false'
244
+ })
245
+ }
246
+
247
+ activeIds () {
248
+ return this.activeItems().map((item) => {
249
+ return item.querySelector('input[name$=\'[blob_id]\']').value
250
+ })
251
+ }
252
+ }