nitro_kit 0.9.0 → 2.0.0.alpha.2
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.
- checksums.yaml +4 -4
- data/.agents/plugins/marketplace.json +20 -0
- data/CHANGELOG.md +172 -0
- data/LICENSE +28 -0
- data/README.md +22 -15
- data/STYLE_GUIDE.md +425 -0
- data/app/assets/stylesheets/nitro_kit-tailwind-v4.css +38 -0
- data/app/assets/stylesheets/nitro_kit.css +7239 -0
- data/app/components/nitro_kit/accordion.rb +125 -65
- data/app/components/nitro_kit/alert.rb +80 -43
- data/app/components/nitro_kit/app_navigation.rb +307 -0
- data/app/components/nitro_kit/app_shell.rb +227 -0
- data/app/components/nitro_kit/appearance_bootstrap.rb +146 -0
- data/app/components/nitro_kit/appearance_picker.rb +156 -0
- data/app/components/nitro_kit/auth_shell.rb +34 -0
- data/app/components/nitro_kit/avatar.rb +85 -29
- data/app/components/nitro_kit/avatar_stack.rb +155 -9
- data/app/components/nitro_kit/badge.rb +43 -81
- data/app/components/nitro_kit/button.rb +180 -103
- data/app/components/nitro_kit/button_group.rb +63 -11
- data/app/components/nitro_kit/button_to.rb +69 -0
- data/app/components/nitro_kit/card.rb +42 -45
- data/app/components/nitro_kit/checkbox.rb +115 -62
- data/app/components/nitro_kit/checkbox_group.rb +131 -22
- data/app/components/nitro_kit/choice.rb +55 -0
- data/app/components/nitro_kit/combobox.rb +292 -105
- data/app/components/nitro_kit/command_palette.rb +412 -0
- data/app/components/nitro_kit/component.rb +322 -46
- data/app/components/nitro_kit/container.rb +32 -0
- data/app/components/nitro_kit/control_group.rb +45 -0
- data/app/components/nitro_kit/danger_zone.rb +78 -0
- data/app/components/nitro_kit/data_section.rb +106 -0
- data/app/components/nitro_kit/details_table.rb +229 -0
- data/app/components/nitro_kit/dialog.rb +257 -83
- data/app/components/nitro_kit/dropdown.rb +335 -122
- data/app/components/nitro_kit/dropzone.rb +326 -0
- data/app/components/nitro_kit/empty_state.rb +99 -0
- data/app/components/nitro_kit/field.rb +461 -236
- data/app/components/nitro_kit/field_group.rb +15 -8
- data/app/components/nitro_kit/fieldset.rb +37 -39
- data/app/components/nitro_kit/flex.rb +49 -0
- data/app/components/nitro_kit/form_builder.rb +313 -72
- data/app/components/nitro_kit/form_section.rb +79 -0
- data/app/components/nitro_kit/grid.rb +34 -0
- data/app/components/nitro_kit/icon.rb +54 -25
- data/app/components/nitro_kit/input.rb +97 -10
- data/app/components/nitro_kit/label.rb +22 -6
- data/app/components/nitro_kit/layout_options.rb +7 -0
- data/app/components/nitro_kit/page_header.rb +76 -0
- data/app/components/nitro_kit/pagination.rb +355 -69
- data/app/components/nitro_kit/pagination_bar.rb +90 -0
- data/app/components/nitro_kit/progressive_image.rb +181 -0
- data/app/components/nitro_kit/radio_button.rb +77 -55
- data/app/components/nitro_kit/radio_button_group.rb +107 -36
- data/app/components/nitro_kit/responsive_value.rb +94 -0
- data/app/components/nitro_kit/rich_text_area.rb +31 -0
- data/app/components/nitro_kit/select.rb +129 -64
- data/app/components/nitro_kit/settings_layout.rb +152 -0
- data/app/components/nitro_kit/sheet.rb +202 -0
- data/app/components/nitro_kit/stat_grid.rb +75 -0
- data/app/components/nitro_kit/switch.rb +108 -51
- data/app/components/nitro_kit/table.rb +237 -45
- data/app/components/nitro_kit/tabs.rb +168 -63
- data/app/components/nitro_kit/textarea.rb +81 -10
- data/app/components/nitro_kit/toast.rb +211 -80
- data/app/components/nitro_kit/toolbar.rb +75 -0
- data/app/components/nitro_kit/tooltip.rb +235 -34
- data/app/components/nitro_kit/typeset.rb +28 -0
- data/app/javascript/controllers/nk/app_shell_controller.js +224 -0
- data/app/javascript/controllers/nk/appearance_controller.js +73 -0
- data/app/javascript/controllers/nk/avatar_controller.js +15 -0
- data/app/javascript/controllers/nk/button_controller.js +56 -0
- data/app/javascript/controllers/nk/checkable_controller.js +41 -0
- data/app/javascript/controllers/nk/combobox_controller.js +314 -82
- data/app/javascript/controllers/nk/command_palette_controller.js +249 -0
- data/app/javascript/controllers/nk/dialog_controller.js +14 -10
- data/app/javascript/controllers/nk/dropdown_controller.js +106 -58
- data/app/javascript/controllers/nk/dropzone/direct_upload.js +61 -0
- data/app/javascript/controllers/nk/dropzone/file_rules.js +77 -0
- data/app/javascript/controllers/nk/dropzone/form_submit_lock.js +30 -0
- data/app/javascript/controllers/nk/dropzone_controller.js +482 -0
- data/app/javascript/controllers/nk/overlay_position.js +37 -0
- data/app/javascript/controllers/nk/progressive_image_controller.js +116 -0
- data/app/javascript/controllers/nk/tabs_controller.js +135 -23
- data/app/javascript/controllers/nk/toast_controller.js +112 -45
- data/app/javascript/controllers/nk/tooltip_controller.js +12 -47
- data/config/importmap.rb +2 -0
- data/config/locales/en.yml +98 -0
- data/docs/agent_guide.md +139 -0
- data/docs/agent_native_spec.md +422 -0
- data/docs/component_contracts.md +263 -0
- data/docs/customization.md +595 -0
- data/docs/hotwire.md +94 -0
- data/docs/initialization_prompt.md +36 -0
- data/docs/migration_1_to_2.md +318 -0
- data/docs/new_app_strategy.md +22 -0
- data/docs/patterns/application_foundation.md +136 -0
- data/docs/patterns/crud_resource.md +144 -0
- data/docs/patterns/destructive_action.md +105 -0
- data/docs/patterns/flash_and_toast.md +57 -0
- data/docs/patterns/inline_edit.md +59 -0
- data/docs/patterns/queryable_collection.md +187 -0
- data/docs/patterns/resource_form.md +126 -0
- data/docs/rails_conventions.md +95 -0
- data/docs/rails_integration.md +514 -0
- data/lib/generators/nitro_kit/install_generator.rb +48 -0
- data/lib/generators/nitro_kit/upgrade_smoke_tests_generator.rb +67 -0
- data/lib/nitro_kit/engine.rb +20 -0
- data/lib/nitro_kit/installation.rb +671 -0
- data/lib/nitro_kit/migration_inventory.rb +345 -0
- data/lib/nitro_kit/upgrade_smoke_test.rb +403 -0
- data/lib/nitro_kit/version.rb +1 -1
- data/lib/nitro_kit.rb +5 -42
- data/lib/rails/commands/nitro_kit/nitro_kit_command.rb +42 -0
- data/lib/tasks/nitro_kit_tasks.rake +79 -4
- data/plugins/nitro-kit/.codex-plugin/plugin.json +28 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/SKILL.md +67 -0
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/SKILL.md +60 -0
- data/plugins/nitro-kit/skills/nitro-kit-rails/agents/openai.yaml +4 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/SKILL.md +62 -0
- data/plugins/nitro-kit/skills/nitro-kit-ui/agents/openai.yaml +4 -0
- data/src/stylesheets/nitro_kit/components/accordion.css +119 -0
- data/src/stylesheets/nitro_kit/components/alert.css +82 -0
- data/src/stylesheets/nitro_kit/components/app_navigation.css +156 -0
- data/src/stylesheets/nitro_kit/components/app_shell.css +605 -0
- data/src/stylesheets/nitro_kit/components/appearance_picker.css +117 -0
- data/src/stylesheets/nitro_kit/components/auth_shell.css +8 -0
- data/src/stylesheets/nitro_kit/components/avatar.css +81 -0
- data/src/stylesheets/nitro_kit/components/avatar_stack.css +80 -0
- data/src/stylesheets/nitro_kit/components/badge.css +76 -0
- data/src/stylesheets/nitro_kit/components/button.css +283 -0
- data/src/stylesheets/nitro_kit/components/button_group.css +53 -0
- data/src/stylesheets/nitro_kit/components/button_to.css +6 -0
- data/src/stylesheets/nitro_kit/components/card.css +70 -0
- data/src/stylesheets/nitro_kit/components/checkbox.css +160 -0
- data/src/stylesheets/nitro_kit/components/checkbox_group.css +61 -0
- data/src/stylesheets/nitro_kit/components/combobox.css +148 -0
- data/src/stylesheets/nitro_kit/components/command_palette.css +255 -0
- data/src/stylesheets/nitro_kit/components/container.css +25 -0
- data/src/stylesheets/nitro_kit/components/control_group.css +149 -0
- data/src/stylesheets/nitro_kit/components/danger_zone.css +54 -0
- data/src/stylesheets/nitro_kit/components/data_section.css +40 -0
- data/src/stylesheets/nitro_kit/components/details_table.css +53 -0
- data/src/stylesheets/nitro_kit/components/dialog.css +80 -0
- data/src/stylesheets/nitro_kit/components/dropdown.css +152 -0
- data/src/stylesheets/nitro_kit/components/dropzone.css +198 -0
- data/src/stylesheets/nitro_kit/components/empty_state.css +53 -0
- data/src/stylesheets/nitro_kit/components/field.css +79 -0
- data/src/stylesheets/nitro_kit/components/field_group.css +6 -0
- data/src/stylesheets/nitro_kit/components/fieldset.css +28 -0
- data/src/stylesheets/nitro_kit/components/flex.css +455 -0
- data/src/stylesheets/nitro_kit/components/form_section.css +71 -0
- data/src/stylesheets/nitro_kit/components/grid.css +308 -0
- data/src/stylesheets/nitro_kit/components/icon.css +26 -0
- data/src/stylesheets/nitro_kit/components/input.css +80 -0
- data/src/stylesheets/nitro_kit/components/label.css +9 -0
- data/src/stylesheets/nitro_kit/components/layout.css +440 -0
- data/src/stylesheets/nitro_kit/components/page_header.css +61 -0
- data/src/stylesheets/nitro_kit/components/pagination.css +119 -0
- data/src/stylesheets/nitro_kit/components/pagination_bar.css +41 -0
- data/src/stylesheets/nitro_kit/components/palette.css +113 -0
- data/src/stylesheets/nitro_kit/components/progressive_image.css +139 -0
- data/src/stylesheets/nitro_kit/components/radio_button.css +127 -0
- data/src/stylesheets/nitro_kit/components/radio_button_group.css +143 -0
- data/src/stylesheets/nitro_kit/components/rich_text_area.css +21 -0
- data/src/stylesheets/nitro_kit/components/select.css +70 -0
- data/src/stylesheets/nitro_kit/components/settings_layout.css +100 -0
- data/src/stylesheets/nitro_kit/components/sheet.css +100 -0
- data/src/stylesheets/nitro_kit/components/stat_grid.css +51 -0
- data/src/stylesheets/nitro_kit/components/switch.css +152 -0
- data/src/stylesheets/nitro_kit/components/table.css +116 -0
- data/src/stylesheets/nitro_kit/components/tabs.css +138 -0
- data/src/stylesheets/nitro_kit/components/textarea.css +39 -0
- data/src/stylesheets/nitro_kit/components/toast.css +131 -0
- data/src/stylesheets/nitro_kit/components/toolbar.css +78 -0
- data/src/stylesheets/nitro_kit/components/tooltip.css +124 -0
- data/src/stylesheets/nitro_kit/components/typeset.css +191 -0
- data/src/stylesheets/nitro_kit/layers.css +7 -0
- data/src/stylesheets/nitro_kit/reset.css +192 -0
- data/src/stylesheets/nitro_kit/tokens.css +198 -0
- metadata +182 -51
- data/Rakefile +0 -8
- data/app/assets/tailwind/application.css +0 -49
- data/app/components/nitro_kit/datepicker.rb +0 -9
- data/app/helpers/nitro_kit/accordion_helper.rb +0 -9
- data/app/helpers/nitro_kit/alert_helper.rb +0 -11
- data/app/helpers/nitro_kit/avatar_helper.rb +0 -13
- data/app/helpers/nitro_kit/badge_helper.rb +0 -13
- data/app/helpers/nitro_kit/button_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/button_helper.rb +0 -49
- data/app/helpers/nitro_kit/card_helper.rb +0 -9
- data/app/helpers/nitro_kit/checkbox_helper.rb +0 -50
- data/app/helpers/nitro_kit/combobox_helper.rb +0 -9
- data/app/helpers/nitro_kit/datepicker_helper.rb +0 -9
- data/app/helpers/nitro_kit/dialog_helper.rb +0 -9
- data/app/helpers/nitro_kit/dropdown_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_group_helper.rb +0 -9
- data/app/helpers/nitro_kit/field_helper.rb +0 -9
- data/app/helpers/nitro_kit/fieldset_helper.rb +0 -9
- data/app/helpers/nitro_kit/form_helper.rb +0 -13
- data/app/helpers/nitro_kit/icon_helper.rb +0 -9
- data/app/helpers/nitro_kit/input_helper.rb +0 -37
- data/app/helpers/nitro_kit/label_helper.rb +0 -19
- data/app/helpers/nitro_kit/pagination_helper.rb +0 -46
- data/app/helpers/nitro_kit/radio_button_helper.rb +0 -23
- data/app/helpers/nitro_kit/select_helper.rb +0 -24
- data/app/helpers/nitro_kit/switch_helper.rb +0 -9
- data/app/helpers/nitro_kit/table_helper.rb +0 -9
- data/app/helpers/nitro_kit/tabs_helper.rb +0 -9
- data/app/helpers/nitro_kit/textarea_helper.rb +0 -9
- data/app/helpers/nitro_kit/toast_helper.rb +0 -25
- data/app/helpers/nitro_kit/tooltip_helper.rb +0 -9
- data/app/javascript/controllers/nk/accordion_controller.js +0 -18
- data/app/javascript/controllers/nk/datepicker_controller.js +0 -5
- data/app/javascript/controllers/nk/switch_controller.js +0 -32
- data/lib/generators/nitro_kit/component_generator.rb +0 -84
- data/lib/nitro_kit/schema_builder.rb +0 -129
- data/lib/nitro_kit/variants.rb +0 -21
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus";
|
|
2
|
+
import DirectUploadSession from "controllers/nk/dropzone/direct_upload";
|
|
3
|
+
import {
|
|
4
|
+
formatBytes,
|
|
5
|
+
interpolate,
|
|
6
|
+
rejectionMessage,
|
|
7
|
+
validateFiles,
|
|
8
|
+
} from "controllers/nk/dropzone/file_rules";
|
|
9
|
+
import {
|
|
10
|
+
acquireFormSubmitLock,
|
|
11
|
+
releaseFormSubmitLock,
|
|
12
|
+
} from "controllers/nk/dropzone/form_submit_lock";
|
|
13
|
+
|
|
14
|
+
// The Dropzone component translates every user-facing string and passes it in
|
|
15
|
+
// as a Stimulus value. These are the shipped English fallbacks for markup that
|
|
16
|
+
// predates a value or was assembled without the component.
|
|
17
|
+
const MESSAGES = {
|
|
18
|
+
progressFor: "Upload progress for %{name}",
|
|
19
|
+
removeFile: "Remove %{name}",
|
|
20
|
+
uploading: "Uploading",
|
|
21
|
+
uploadingPercent: "Uploading %{percent}%",
|
|
22
|
+
uploaded: "Uploaded",
|
|
23
|
+
uploadFailed: "Upload failed",
|
|
24
|
+
ready: "Ready to submit",
|
|
25
|
+
statusEmpty: "No files selected.",
|
|
26
|
+
statusSelectedOne: "%{count} file selected.",
|
|
27
|
+
statusSelectedOther: "%{count} files selected.",
|
|
28
|
+
statusUploadingOne: "Uploading %{count} file.",
|
|
29
|
+
statusUploadingOther: "Uploading %{count} files.",
|
|
30
|
+
statusAttentionOne: "%{count} file needs attention.",
|
|
31
|
+
statusAttentionOther: "%{count} files need attention.",
|
|
32
|
+
statusUploadedOne: "%{count} file uploaded.",
|
|
33
|
+
statusUploadedOther: "%{count} files uploaded.",
|
|
34
|
+
statusReadyOne: "%{count} file ready to submit.",
|
|
35
|
+
statusReadyOther: "%{count} files ready to submit.",
|
|
36
|
+
errorsUploadFailed: "%{name} could not be uploaded.",
|
|
37
|
+
errorsUploadFailedDetail: "%{name} could not be uploaded: %{error}",
|
|
38
|
+
errorsUploadsInProgress:
|
|
39
|
+
"Wait for uploads to finish before submitting the form.",
|
|
40
|
+
errorsFailedFiles:
|
|
41
|
+
"Remove failed files or choose them again before submitting the form.",
|
|
42
|
+
errorsTooLarge: "%{name} is larger than %{size}.",
|
|
43
|
+
errorsNotAccepted: "%{name} is not an accepted file type.",
|
|
44
|
+
errorsMaxFilesOne: "Choose no more than %{count} file.",
|
|
45
|
+
errorsMaxFilesOther: "Choose no more than %{count} files.",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default class extends Controller {
|
|
49
|
+
static targets = [
|
|
50
|
+
"input",
|
|
51
|
+
"status",
|
|
52
|
+
"error",
|
|
53
|
+
"previewList",
|
|
54
|
+
"previewTemplate",
|
|
55
|
+
];
|
|
56
|
+
static values = {
|
|
57
|
+
directUpload: Boolean,
|
|
58
|
+
maxFiles: Number,
|
|
59
|
+
maxBytes: Number,
|
|
60
|
+
accept: String,
|
|
61
|
+
...Object.fromEntries(Object.keys(MESSAGES).map((key) => [key, String])),
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
message(key, replacements) {
|
|
65
|
+
return interpolate(this[`${key}Value`] || MESSAGES[key], replacements);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
pluralMessage(key, count, replacements = {}) {
|
|
69
|
+
return this.message(`${key}${count === 1 ? "One" : "Other"}`, {
|
|
70
|
+
count,
|
|
71
|
+
...replacements,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
get fileRuleMessages() {
|
|
76
|
+
return {
|
|
77
|
+
tooLarge: this.message("errorsTooLarge"),
|
|
78
|
+
notAccepted: this.message("errorsNotAccepted"),
|
|
79
|
+
maxFilesOne: this.message("errorsMaxFilesOne"),
|
|
80
|
+
maxFilesOther: this.message("errorsMaxFilesOther"),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
connect() {
|
|
85
|
+
this.entries = [];
|
|
86
|
+
this.nextIdentifier = 1;
|
|
87
|
+
this.dragDepth = 0;
|
|
88
|
+
this.initialInputDisabled = this.inputTarget.disabled;
|
|
89
|
+
this.submitLockOwner = {};
|
|
90
|
+
|
|
91
|
+
const files = Array.from(this.inputTarget.files);
|
|
92
|
+
if (files.length > 0) this.replaceFiles(files);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
disconnect() {
|
|
96
|
+
this.release({ clearInput: false });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
select() {
|
|
100
|
+
this.replaceFiles(Array.from(this.inputTarget.files));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
dragEnter(event) {
|
|
104
|
+
if (!this.hasFiles(event)) return;
|
|
105
|
+
|
|
106
|
+
event.preventDefault();
|
|
107
|
+
this.dragDepth += 1;
|
|
108
|
+
this.element.dataset.state = "drag";
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
dragOver(event) {
|
|
112
|
+
if (!this.hasFiles(event)) return;
|
|
113
|
+
|
|
114
|
+
event.preventDefault();
|
|
115
|
+
event.dataTransfer.dropEffect = "copy";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
dragLeave(event) {
|
|
119
|
+
if (!this.hasFiles(event)) return;
|
|
120
|
+
|
|
121
|
+
event.preventDefault();
|
|
122
|
+
this.dragDepth = Math.max(0, this.dragDepth - 1);
|
|
123
|
+
if (this.dragDepth === 0) this.reflectState();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
drop(event) {
|
|
127
|
+
if (!this.hasFiles(event)) return;
|
|
128
|
+
|
|
129
|
+
event.preventDefault();
|
|
130
|
+
this.dragDepth = 0;
|
|
131
|
+
this.replaceFiles(Array.from(event.dataTransfer.files));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
remove(event) {
|
|
135
|
+
const item = event.currentTarget.closest("[data-slot='dropzone-preview']");
|
|
136
|
+
const entry = this.entries.find(
|
|
137
|
+
(candidate) => candidate.id === item?.dataset.fileId,
|
|
138
|
+
);
|
|
139
|
+
if (!entry) return;
|
|
140
|
+
|
|
141
|
+
this.releaseEntry(entry, { removeElement: true });
|
|
142
|
+
this.entries = this.entries.filter((candidate) => candidate !== entry);
|
|
143
|
+
this.syncNativeFiles();
|
|
144
|
+
this.clearError();
|
|
145
|
+
if (this.uploadingEntries.length === 0) this.restoreSubmitControls();
|
|
146
|
+
|
|
147
|
+
const failed = this.entries.find(
|
|
148
|
+
(candidate) => candidate.state === "error",
|
|
149
|
+
);
|
|
150
|
+
if (failed) {
|
|
151
|
+
this.showError(
|
|
152
|
+
this.message("errorsUploadFailed", { name: failed.file.name }),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
this.reflectState();
|
|
157
|
+
this.announceSelection();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
submit(event) {
|
|
161
|
+
const form = this.element.closest("form");
|
|
162
|
+
if (!form || event.target !== form) return;
|
|
163
|
+
|
|
164
|
+
if (this.uploadingEntries.length > 0) {
|
|
165
|
+
event.preventDefault();
|
|
166
|
+
this.showError(this.message("errorsUploadsInProgress"));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (
|
|
171
|
+
this.directUploadValue &&
|
|
172
|
+
this.entries.some((entry) => entry.state !== "success")
|
|
173
|
+
) {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
this.showError(this.message("errorsFailedFiles"));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (this.directUploadValue && this.entries.length > 0) {
|
|
180
|
+
this.inputTarget.disabled = true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
teardown() {
|
|
185
|
+
this.release({ clearInput: true });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
release({ clearInput }) {
|
|
189
|
+
const input = this.element.querySelector(
|
|
190
|
+
':scope > [data-slot="dropzone-input"]',
|
|
191
|
+
);
|
|
192
|
+
const previewList = this.element.querySelector(
|
|
193
|
+
':scope > [data-slot="dropzone-preview-list"]',
|
|
194
|
+
);
|
|
195
|
+
const status = this.element.querySelector(
|
|
196
|
+
':scope > [data-slot="dropzone-status"]',
|
|
197
|
+
);
|
|
198
|
+
const error = this.element.querySelector(
|
|
199
|
+
':scope > [data-slot="dropzone-error"]',
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
this.entries?.forEach((entry) =>
|
|
203
|
+
this.releaseEntry(entry, { removeElement: true }),
|
|
204
|
+
);
|
|
205
|
+
this.entries = [];
|
|
206
|
+
this.restoreSubmitControls();
|
|
207
|
+
this.dragDepth = 0;
|
|
208
|
+
|
|
209
|
+
if (input) {
|
|
210
|
+
input.disabled = this.initialInputDisabled;
|
|
211
|
+
input.setCustomValidity("");
|
|
212
|
+
input.removeAttribute("aria-invalid");
|
|
213
|
+
if (clearInput) input.value = "";
|
|
214
|
+
}
|
|
215
|
+
if (previewList) {
|
|
216
|
+
previewList.replaceChildren();
|
|
217
|
+
previewList.hidden = true;
|
|
218
|
+
}
|
|
219
|
+
const selectedCount = input ? input.files.length : 0;
|
|
220
|
+
if (status) {
|
|
221
|
+
status.textContent =
|
|
222
|
+
selectedCount === 0
|
|
223
|
+
? this.message("statusEmpty")
|
|
224
|
+
: this.pluralMessage("statusSelected", selectedCount);
|
|
225
|
+
}
|
|
226
|
+
if (error) {
|
|
227
|
+
error.textContent = "";
|
|
228
|
+
error.hidden = true;
|
|
229
|
+
}
|
|
230
|
+
this.element.dataset.state = this.initialInputDisabled
|
|
231
|
+
? "disabled"
|
|
232
|
+
: selectedCount > 0
|
|
233
|
+
? "success"
|
|
234
|
+
: "idle";
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
replaceFiles(files) {
|
|
238
|
+
this.entries.forEach((entry) =>
|
|
239
|
+
this.releaseEntry(entry, { removeElement: true }),
|
|
240
|
+
);
|
|
241
|
+
this.entries = [];
|
|
242
|
+
this.restoreSubmitControls();
|
|
243
|
+
this.clearError();
|
|
244
|
+
|
|
245
|
+
const validation = validateFiles(files, {
|
|
246
|
+
maxFiles: this.maxFilesValue,
|
|
247
|
+
maxBytes: this.hasMaxBytesValue ? this.maxBytesValue : null,
|
|
248
|
+
accept: this.hasAcceptValue ? this.acceptValue : null,
|
|
249
|
+
messages: this.fileRuleMessages,
|
|
250
|
+
});
|
|
251
|
+
validation.accepted.forEach((file) => this.addFile(file));
|
|
252
|
+
this.syncNativeFiles();
|
|
253
|
+
|
|
254
|
+
if (this.directUploadValue) {
|
|
255
|
+
this.entries.forEach((entry) => this.startUpload(entry));
|
|
256
|
+
} else {
|
|
257
|
+
this.entries.forEach((entry) =>
|
|
258
|
+
this.updateEntry(entry, "success", 0, this.message("ready")),
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this.reflectState();
|
|
263
|
+
this.announceSelection();
|
|
264
|
+
if (validation.rejected.length > 0) {
|
|
265
|
+
this.showError(rejectionMessage(validation.rejected), {
|
|
266
|
+
blocking: this.entries.length === 0,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
addFile(file) {
|
|
272
|
+
const fragment = this.previewTemplateTarget.content.cloneNode(true);
|
|
273
|
+
const element = fragment.querySelector("[data-slot='dropzone-preview']");
|
|
274
|
+
const id = String(this.nextIdentifier++);
|
|
275
|
+
element.dataset.fileId = id;
|
|
276
|
+
element.querySelector("[data-slot='dropzone-file-name']").textContent =
|
|
277
|
+
file.name;
|
|
278
|
+
element.querySelector("[data-slot='dropzone-file-size']").textContent =
|
|
279
|
+
formatBytes(file.size);
|
|
280
|
+
|
|
281
|
+
const remove = element.querySelector(
|
|
282
|
+
"[data-slot='dropzone-remove-control']",
|
|
283
|
+
);
|
|
284
|
+
remove.setAttribute(
|
|
285
|
+
"aria-label",
|
|
286
|
+
this.message("removeFile", { name: file.name }),
|
|
287
|
+
);
|
|
288
|
+
const progress = element.querySelector("[data-slot='dropzone-progress']");
|
|
289
|
+
progress.setAttribute(
|
|
290
|
+
"aria-label",
|
|
291
|
+
this.message("progressFor", { name: file.name }),
|
|
292
|
+
);
|
|
293
|
+
progress.hidden = !this.directUploadValue;
|
|
294
|
+
|
|
295
|
+
const preview = element.querySelector(
|
|
296
|
+
"[data-slot='dropzone-preview-image']",
|
|
297
|
+
);
|
|
298
|
+
let objectUrl = null;
|
|
299
|
+
if (file.type.startsWith("image/")) {
|
|
300
|
+
objectUrl = URL.createObjectURL(file);
|
|
301
|
+
preview.src = objectUrl;
|
|
302
|
+
preview.hidden = false;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
this.previewListTarget.append(fragment);
|
|
306
|
+
this.previewListTarget.hidden = false;
|
|
307
|
+
this.entries.push({ id, file, element, objectUrl, state: "queued" });
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
startUpload(entry) {
|
|
311
|
+
entry.state = "uploading";
|
|
312
|
+
this.updateEntry(entry, "uploading", 0, this.message("uploading"));
|
|
313
|
+
this.reflectState();
|
|
314
|
+
this.disableSubmitControls();
|
|
315
|
+
|
|
316
|
+
entry.upload = new DirectUploadSession(
|
|
317
|
+
entry.file,
|
|
318
|
+
this.inputTarget.dataset.directUploadUrl,
|
|
319
|
+
{
|
|
320
|
+
onProgress: (event) => this.progress(entry, event),
|
|
321
|
+
onComplete: (error, attributes) =>
|
|
322
|
+
this.completeUpload(entry, error, attributes),
|
|
323
|
+
},
|
|
324
|
+
);
|
|
325
|
+
entry.upload.start();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
completeUpload(entry, error, attributes) {
|
|
329
|
+
if (entry.cancelled) return;
|
|
330
|
+
|
|
331
|
+
if (error) {
|
|
332
|
+
this.updateEntry(entry, "error", 0, this.message("uploadFailed"));
|
|
333
|
+
this.showError(
|
|
334
|
+
this.message("errorsUploadFailedDetail", {
|
|
335
|
+
name: entry.file.name,
|
|
336
|
+
error,
|
|
337
|
+
}),
|
|
338
|
+
);
|
|
339
|
+
} else {
|
|
340
|
+
entry.hiddenInput = this.createSignedIdInput(attributes.signed_id);
|
|
341
|
+
this.updateEntry(entry, "success", 100, this.message("uploaded"));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
this.reflectState();
|
|
345
|
+
if (this.uploadingEntries.length === 0) this.restoreSubmitControls();
|
|
346
|
+
this.announceSelection();
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
progress(entry, event) {
|
|
350
|
+
if (!event.lengthComputable || entry.cancelled) return;
|
|
351
|
+
|
|
352
|
+
const percentage = Math.round((event.loaded / event.total) * 100);
|
|
353
|
+
this.updateEntry(
|
|
354
|
+
entry,
|
|
355
|
+
"uploading",
|
|
356
|
+
percentage,
|
|
357
|
+
this.message("uploadingPercent", { percent: percentage }),
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
updateEntry(entry, state, progress, message) {
|
|
362
|
+
entry.state = state;
|
|
363
|
+
entry.element.dataset.state = state;
|
|
364
|
+
entry.element.querySelector("[data-slot='dropzone-progress']").value =
|
|
365
|
+
progress;
|
|
366
|
+
entry.element.querySelector(
|
|
367
|
+
"[data-slot='dropzone-file-status']",
|
|
368
|
+
).textContent = message;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
createSignedIdInput(value) {
|
|
372
|
+
const input = document.createElement("input");
|
|
373
|
+
input.type = "hidden";
|
|
374
|
+
input.name = this.inputTarget.name;
|
|
375
|
+
input.value = value;
|
|
376
|
+
input.dataset.slot = "dropzone-signed-id";
|
|
377
|
+
this.inputTarget.insertAdjacentElement("afterend", input);
|
|
378
|
+
return input;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
releaseEntry(entry, { removeElement }) {
|
|
382
|
+
entry.cancelled = true;
|
|
383
|
+
entry.upload?.cancel();
|
|
384
|
+
entry.upload = null;
|
|
385
|
+
entry.hiddenInput?.remove();
|
|
386
|
+
if (entry.objectUrl) URL.revokeObjectURL(entry.objectUrl);
|
|
387
|
+
if (removeElement) entry.element.remove();
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
syncNativeFiles() {
|
|
391
|
+
const transfer = new DataTransfer();
|
|
392
|
+
this.entries.forEach((entry) => transfer.items.add(entry.file));
|
|
393
|
+
this.inputTarget.files = transfer.files;
|
|
394
|
+
this.previewListTarget.hidden = this.entries.length === 0;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
reflectState() {
|
|
398
|
+
if (this.dragDepth > 0) return;
|
|
399
|
+
|
|
400
|
+
if (
|
|
401
|
+
!this.errorTarget.hidden ||
|
|
402
|
+
this.entries.some((entry) => entry.state === "error")
|
|
403
|
+
) {
|
|
404
|
+
this.element.dataset.state = "error";
|
|
405
|
+
} else if (this.uploadingEntries.length > 0) {
|
|
406
|
+
this.element.dataset.state = "uploading";
|
|
407
|
+
} else if (this.entries.length > 0) {
|
|
408
|
+
this.element.dataset.state = "success";
|
|
409
|
+
} else {
|
|
410
|
+
this.element.dataset.state = "idle";
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
announceSelection() {
|
|
415
|
+
const count = this.entries.length;
|
|
416
|
+
if (count === 0) {
|
|
417
|
+
this.statusTarget.textContent = this.message("statusEmpty");
|
|
418
|
+
} else if (this.uploadingEntries.length > 0) {
|
|
419
|
+
this.statusTarget.textContent = this.pluralMessage(
|
|
420
|
+
"statusUploading",
|
|
421
|
+
count,
|
|
422
|
+
);
|
|
423
|
+
} else if (this.entries.some((entry) => entry.state === "error")) {
|
|
424
|
+
this.statusTarget.textContent = this.pluralMessage(
|
|
425
|
+
"statusAttention",
|
|
426
|
+
count,
|
|
427
|
+
);
|
|
428
|
+
} else if (
|
|
429
|
+
this.directUploadValue &&
|
|
430
|
+
this.entries.every((entry) => entry.state === "success")
|
|
431
|
+
) {
|
|
432
|
+
this.statusTarget.textContent = this.pluralMessage(
|
|
433
|
+
"statusUploaded",
|
|
434
|
+
count,
|
|
435
|
+
);
|
|
436
|
+
} else {
|
|
437
|
+
this.statusTarget.textContent = this.pluralMessage("statusReady", count);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
showError(message, { blocking = true } = {}) {
|
|
442
|
+
this.errorTarget.textContent = message;
|
|
443
|
+
this.errorTarget.hidden = false;
|
|
444
|
+
this.inputTarget.setCustomValidity(blocking ? message : "");
|
|
445
|
+
if (blocking) {
|
|
446
|
+
this.inputTarget.setAttribute("aria-invalid", "true");
|
|
447
|
+
} else {
|
|
448
|
+
this.inputTarget.removeAttribute("aria-invalid");
|
|
449
|
+
}
|
|
450
|
+
this.element.dataset.state = "error";
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
clearError() {
|
|
454
|
+
this.errorTarget.textContent = "";
|
|
455
|
+
this.errorTarget.hidden = true;
|
|
456
|
+
this.inputTarget.setCustomValidity("");
|
|
457
|
+
this.inputTarget.removeAttribute("aria-invalid");
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
disableSubmitControls() {
|
|
461
|
+
const form = this.element.closest("form");
|
|
462
|
+
if (this.lockedForm && this.lockedForm !== form) {
|
|
463
|
+
releaseFormSubmitLock(this.lockedForm, this.submitLockOwner);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
this.lockedForm = form;
|
|
467
|
+
acquireFormSubmitLock(form, this.submitLockOwner);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
restoreSubmitControls() {
|
|
471
|
+
releaseFormSubmitLock(this.lockedForm, this.submitLockOwner);
|
|
472
|
+
this.lockedForm = null;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
hasFiles(event) {
|
|
476
|
+
return Array.from(event.dataTransfer?.types || []).includes("Files");
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
get uploadingEntries() {
|
|
480
|
+
return this.entries.filter((entry) => entry.state === "uploading");
|
|
481
|
+
}
|
|
482
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function positionOverlay(trigger, overlay, placement) {
|
|
2
|
+
const triggerRect = trigger.getBoundingClientRect();
|
|
3
|
+
overlay.style.setProperty("--_nk-combobox-width", `${triggerRect.width}px`);
|
|
4
|
+
const overlayRect = overlay.getBoundingClientRect();
|
|
5
|
+
const gap = 4;
|
|
6
|
+
const inset = 16;
|
|
7
|
+
const [side, alignment] = placement.split("-");
|
|
8
|
+
const preferredTop =
|
|
9
|
+
side === "top"
|
|
10
|
+
? triggerRect.top - overlayRect.height - gap
|
|
11
|
+
: triggerRect.bottom + gap;
|
|
12
|
+
const preferredLeft =
|
|
13
|
+
alignment === "end"
|
|
14
|
+
? triggerRect.right - overlayRect.width
|
|
15
|
+
: triggerRect.left;
|
|
16
|
+
const top = Math.min(
|
|
17
|
+
Math.max(preferredTop, inset),
|
|
18
|
+
window.innerHeight - overlayRect.height - inset,
|
|
19
|
+
);
|
|
20
|
+
const left = Math.min(
|
|
21
|
+
Math.max(preferredLeft, inset),
|
|
22
|
+
window.innerWidth - overlayRect.width - inset,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
overlay.style.setProperty("--_nk-overlay-top", `${top}px`);
|
|
26
|
+
overlay.style.setProperty("--_nk-overlay-left", `${left}px`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function observeOverlayPosition(callback) {
|
|
30
|
+
document.addEventListener("scroll", callback, true);
|
|
31
|
+
window.addEventListener("resize", callback);
|
|
32
|
+
|
|
33
|
+
return () => {
|
|
34
|
+
document.removeEventListener("scroll", callback, true);
|
|
35
|
+
window.removeEventListener("resize", callback);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus";
|
|
2
|
+
|
|
3
|
+
export default class extends Controller {
|
|
4
|
+
static targets = ["image", "fallback"];
|
|
5
|
+
|
|
6
|
+
connect() {
|
|
7
|
+
this.connected = true;
|
|
8
|
+
this.onLoad ||= this.loaded.bind(this);
|
|
9
|
+
this.onError ||= this.failed.bind(this);
|
|
10
|
+
this.element.dataset.enhanced = "true";
|
|
11
|
+
|
|
12
|
+
if (this.hasImageTarget) {
|
|
13
|
+
this.bindImage(this.imageTarget);
|
|
14
|
+
} else {
|
|
15
|
+
this.setState("empty");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
disconnect() {
|
|
20
|
+
this.connected = false;
|
|
21
|
+
this.releaseImage();
|
|
22
|
+
delete this.element.dataset.enhanced;
|
|
23
|
+
|
|
24
|
+
if (this.hasFallbackTarget) this.fallbackTarget.hidden = true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
imageTargetConnected(image) {
|
|
28
|
+
if (this.connected) this.bindImage(image);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
imageTargetDisconnected(image) {
|
|
32
|
+
if (this.boundImage !== image) return;
|
|
33
|
+
|
|
34
|
+
this.releaseImage();
|
|
35
|
+
if (this.connected && !this.hasImageTarget) this.setState("empty");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fallbackTargetConnected(fallback) {
|
|
39
|
+
if (!this.connected) return;
|
|
40
|
+
|
|
41
|
+
fallback.hidden = !["empty", "error"].includes(this.element.dataset.state);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
bindImage(image) {
|
|
45
|
+
if (this.boundImage === image) return;
|
|
46
|
+
|
|
47
|
+
this.releaseImage();
|
|
48
|
+
this.boundImage = image;
|
|
49
|
+
this.revision = (this.revision || 0) + 1;
|
|
50
|
+
this.setState("loading");
|
|
51
|
+
image.addEventListener("load", this.onLoad);
|
|
52
|
+
image.addEventListener("error", this.onError);
|
|
53
|
+
|
|
54
|
+
if (image.complete) this.reflectComplete(image);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
releaseImage() {
|
|
58
|
+
if (this.boundImage) {
|
|
59
|
+
this.boundImage.removeEventListener("load", this.onLoad);
|
|
60
|
+
this.boundImage.removeEventListener("error", this.onError);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.boundImage = null;
|
|
64
|
+
this.revision = (this.revision || 0) + 1;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async loaded(event) {
|
|
68
|
+
const image = event?.currentTarget || this.boundImage;
|
|
69
|
+
if (!image || image !== this.boundImage) return;
|
|
70
|
+
|
|
71
|
+
const revision = ++this.revision;
|
|
72
|
+
|
|
73
|
+
if (typeof image.decode === "function") {
|
|
74
|
+
try {
|
|
75
|
+
await image.decode();
|
|
76
|
+
} catch {
|
|
77
|
+
// naturalWidth below remains the source of truth for cached and SVG images.
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (
|
|
82
|
+
!this.connected ||
|
|
83
|
+
image !== this.boundImage ||
|
|
84
|
+
revision !== this.revision
|
|
85
|
+
) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.setState(image.naturalWidth > 0 ? "loaded" : "error");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
failed(event) {
|
|
93
|
+
const image = event?.currentTarget || this.boundImage;
|
|
94
|
+
if (!image || image !== this.boundImage) return;
|
|
95
|
+
|
|
96
|
+
this.revision = (this.revision || 0) + 1;
|
|
97
|
+
this.setState("error");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
reflectComplete(image) {
|
|
101
|
+
if (image.naturalWidth > 0) {
|
|
102
|
+
this.loaded();
|
|
103
|
+
} else {
|
|
104
|
+
this.failed();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setState(state) {
|
|
109
|
+
this.element.dataset.state = state;
|
|
110
|
+
this.element.setAttribute("aria-busy", String(state === "loading"));
|
|
111
|
+
|
|
112
|
+
if (this.hasFallbackTarget) {
|
|
113
|
+
this.fallbackTarget.hidden = !["empty", "error"].includes(state);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|