native-document 1.0.139 → 1.0.141
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.
- package/components.js +1 -1
- package/devtools/ComponentRegistry.js +3 -2
- package/dist/native-document.components.min.css +1 -0
- package/dist/native-document.components.min.js +11639 -8757
- package/dist/native-document.dev.js +2645 -2166
- package/dist/native-document.dev.js.map +1 -1
- package/dist/native-document.min.js +1 -1
- package/index.js +1 -0
- package/package.json +5 -2
- package/rollup.config.js +5 -2
- package/src/components/$traits/has-draggable/HasDraggable.js +69 -0
- package/src/components/$traits/has-draggable/has-draggable.css +8 -0
- package/src/components/$traits/{HasItems.js → has-items/HasItems.js} +2 -11
- package/src/components/$traits/has-position/HasFullPosition.js +51 -0
- package/src/components/$traits/has-position/HasPosition.js +23 -0
- package/src/components/$traits/has-resizable/HasResizable.js +113 -0
- package/src/components/$traits/has-resizable/has-resizable.css +121 -0
- package/src/components/$traits/has-validation/HasValidation.js +86 -0
- package/src/components/BaseComponent.js +82 -7
- package/src/components/accordion/Accordion.js +37 -31
- package/src/components/accordion/AccordionItem.js +14 -21
- package/src/components/alert/Alert.js +48 -40
- package/src/components/avatar/Avatar.js +4 -3
- package/src/components/avatar/AvatarGroup.js +8 -2
- package/src/components/badge/Badge.js +7 -7
- package/src/components/base-component.css +0 -0
- package/src/components/breadcrumb/BreadCrumb.js +30 -21
- package/src/components/button/Button.js +3 -14
- package/src/components/context-menu/ContextMenu.js +49 -26
- package/src/components/dropdown/Dropdown.js +172 -34
- package/src/components/dropdown/DropdownDivider.js +4 -3
- package/src/components/dropdown/DropdownGroup.js +19 -19
- package/src/components/dropdown/DropdownItem.js +24 -18
- package/src/components/dropdown/helpers.js +52 -0
- package/src/components/form/FormControl.js +207 -108
- package/src/components/form/field/Field.js +106 -174
- package/src/components/form/field/FieldCollection.js +110 -203
- package/src/components/form/field/types/AutocompleteField.js +26 -5
- package/src/components/form/field/types/CheckboxField.js +4 -4
- package/src/components/form/field/types/CheckboxGroupField.js +11 -5
- package/src/components/form/field/types/ColorField.js +4 -4
- package/src/components/form/field/types/DateField.js +110 -18
- package/src/components/form/field/types/EmailField.js +2 -2
- package/src/components/form/field/types/FileField.js +87 -21
- package/src/components/form/field/types/HiddenField.js +4 -4
- package/src/components/form/field/types/ImageField.js +4 -4
- package/src/components/form/field/types/NumberField.js +5 -5
- package/src/components/form/field/types/PasswordField.js +28 -6
- package/src/components/form/field/types/RadioField.js +12 -8
- package/src/components/form/field/types/RangeField.js +29 -5
- package/src/components/form/field/types/SearchField.js +4 -4
- package/src/components/form/field/types/SelectField.js +75 -8
- package/src/components/form/field/types/StringField.js +4 -4
- package/src/components/form/field/types/TelField.js +4 -4
- package/src/components/form/field/types/TextAreaField.js +7 -5
- package/src/components/form/field/types/TimeField.js +52 -6
- package/src/components/form/field/types/UrlField.js +4 -5
- package/src/components/form/field/types/file-field-mode/FileAvatarMode.js +104 -0
- package/src/components/form/field/types/file-field-mode/FileDropzoneMode.js +59 -0
- package/src/components/form/field/types/file-field-mode/FileItemPreview.js +74 -0
- package/src/components/form/field/types/file-field-mode/FileNativeMode.js +21 -0
- package/src/components/form/field/types/file-field-mode/FileUploadButtonMode.js +59 -0
- package/src/components/form/field/types/file-field-mode/FileWallMode.js +53 -0
- package/src/components/form/index.js +14 -2
- package/src/components/form/validation/Validation.js +9 -0
- package/src/components/list/List.js +1 -1
- package/src/components/list/ListGroup.js +1 -1
- package/src/components/menu/HasMenuItem.js +133 -0
- package/src/components/menu/Menu.js +47 -56
- package/src/components/menu/MenuGroup.js +36 -8
- package/src/components/menu/MenuItem.js +24 -10
- package/src/components/menu/MenuLink.js +2 -0
- package/src/components/modal/Modal.js +153 -23
- package/src/components/pagination/Pagination.js +55 -21
- package/src/components/popover/Popover.js +127 -40
- package/src/components/progress/Progress.js +14 -24
- package/src/components/skeleton/Skeleton.js +36 -13
- package/src/components/slider/Slider.js +96 -70
- package/src/components/spinner/Spinner.js +4 -2
- package/src/components/splitter/Splitter.js +15 -8
- package/src/components/splitter/SplitterGutter.js +28 -7
- package/src/components/splitter/SplitterPanel.js +9 -15
- package/src/components/splitter/index.js +3 -1
- package/src/components/stacks/AbsoluteStack.js +33 -0
- package/src/components/stacks/FixedStack.js +33 -0
- package/src/components/stacks/PositionStack.js +146 -0
- package/src/components/stacks/RelativeStack.js +33 -0
- package/src/components/{layouts → stacks}/Stack.js +22 -3
- package/src/components/{layouts → stacks}/index.js +6 -4
- package/src/components/stepper/Stepper.js +159 -67
- package/src/components/stepper/StepperStep.js +49 -12
- package/src/components/switch/Switch.js +59 -29
- package/src/components/switch/index.js +6 -0
- package/src/components/table/Column.js +26 -24
- package/src/components/table/ColumnGroup.js +4 -13
- package/src/components/table/DataTable.js +388 -103
- package/src/components/table/SimpleTable.js +31 -140
- package/src/components/tabs/Tabs.js +142 -44
- package/src/components/toast/Toast.js +40 -35
- package/src/components/tooltip/Tooltip.js +152 -35
- package/src/core/data/ObservableArray.js +94 -30
- package/src/core/data/ObservableChecker.js +20 -75
- package/src/core/data/ObservableItem.js +35 -3
- package/src/core/data/observable-helpers/computed.js +2 -1
- package/src/core/data/observable-helpers/object.js +13 -8
- package/src/core/data/observable-helpers/observable.is-to.js +196 -0
- package/src/core/elements/anchor/anchor.js +3 -2
- package/src/core/elements/control/for-each-array.js +44 -30
- package/src/core/elements/control/for-each.js +6 -3
- package/src/core/elements/control/show-if.js +11 -6
- package/src/core/elements/control/switch.js +2 -1
- package/src/core/elements/index.js +1 -1
- package/src/core/elements/svg.js +61 -0
- package/src/core/utils/HasEventEmitter.js +6 -0
- package/src/core/utils/debug-manager.js +2 -5
- package/src/core/utils/filters/standard.js +2 -1
- package/src/core/utils/property-accumulator.js +35 -6
- package/src/core/utils/shortcut-manager.js +242 -0
- package/src/core/utils/validator.js +1 -1
- package/src/core/wrappers/AttributesWrapper.js +41 -4
- package/src/core/wrappers/DocumentObserver.js +0 -1
- package/src/core/wrappers/HtmlElementWrapper.js +1 -1
- package/src/core/wrappers/NDElement.js +15 -2
- package/src/core/wrappers/SvgElementWrapper.js +15 -0
- package/src/core/wrappers/prototypes/attributes-extensions.js +4 -1
- package/src/core/wrappers/prototypes/nd-element-extensions.js +8 -1
- package/src/router/Router.js +0 -1
- package/src/ui/components/accordion/AccordionItemRender.js +63 -0
- package/src/ui/components/accordion/AccordionRender.js +34 -0
- package/src/ui/components/accordion/accordion.css +121 -0
- package/src/ui/components/alert/AlertRender.js +80 -0
- package/src/ui/components/alert/alert.css +163 -0
- package/src/ui/components/avatar/avata-group/AvatarGroupRender.js +49 -0
- package/src/ui/components/avatar/avata-group/avatar-group.css +38 -0
- package/src/ui/components/avatar/avatar/AvatarRender.js +87 -0
- package/src/ui/components/avatar/avatar/avatar.css +189 -0
- package/src/ui/components/badge/BadgeRender.js +24 -0
- package/src/ui/components/badge/badge.css +168 -0
- package/src/ui/components/breadcrumb/BreadcrumbRender.js +43 -0
- package/src/ui/components/breadcrumb/breadcrumb.css +55 -0
- package/src/ui/components/button/ButtonRender.js +65 -0
- package/src/ui/components/button/button.css +288 -354
- package/src/ui/components/contextmenu/ContextmenuRender.js +70 -0
- package/src/ui/components/contextmenu/contextmenu.css +36 -0
- package/src/ui/components/divider/DividerRender.js +70 -0
- package/src/ui/components/divider/divider.css +70 -0
- package/src/ui/components/dropdown/DropdownRender.js +92 -0
- package/src/ui/components/dropdown/divider/DropdownDividerRender.js +9 -0
- package/src/ui/components/dropdown/divider/dropdown-divider.css +0 -0
- package/src/ui/components/dropdown/dropdown.css +179 -0
- package/src/ui/components/dropdown/group/DropdownGroupRender.js +23 -0
- package/src/ui/components/dropdown/group/dropdown-group.css +0 -0
- package/src/ui/components/dropdown/item/DropdownItemRender.js +29 -0
- package/src/ui/components/dropdown/item/dropdown-item.css +0 -0
- package/src/ui/components/form/FieldCollectionRender.js +110 -0
- package/src/ui/components/form/FormControlRender.js +84 -0
- package/src/ui/components/form/field-collection.css +55 -0
- package/src/ui/components/form/fields/AutocompleteFieldRender.js +143 -0
- package/src/ui/components/form/fields/CheckboxFieldRender.js +59 -0
- package/src/ui/components/form/fields/CheckboxGroupFieldRender.js +92 -0
- package/src/ui/components/form/fields/ColorFieldRender.js +30 -0
- package/src/ui/components/form/fields/DateFieldRender.js +154 -0
- package/src/ui/components/form/fields/EmailFieldRender.js +5 -0
- package/src/ui/components/form/fields/FieldRender.js +117 -0
- package/src/ui/components/form/fields/FileFieldRender.js +41 -0
- package/src/ui/components/form/fields/HiddenFieldRender.js +14 -0
- package/src/ui/components/form/fields/ImageFieldRender.js +0 -0
- package/src/ui/components/form/fields/NumberFieldRender.js +52 -0
- package/src/ui/components/form/fields/PasswordFieldRender.js +65 -0
- package/src/ui/components/form/fields/RadioFieldRender.js +77 -0
- package/src/ui/components/form/fields/RangeFieldRender.js +121 -0
- package/src/ui/components/form/fields/SelectFieldRender.js +248 -0
- package/src/ui/components/form/fields/SliderFieldRender.js +359 -0
- package/src/ui/components/form/fields/StringFieldRender.js +6 -0
- package/src/ui/components/form/fields/TelFieldRender.js +6 -0
- package/src/ui/components/form/fields/TextAreaFieldRender.js +96 -0
- package/src/ui/components/form/fields/TimeFieldRender.js +141 -0
- package/src/ui/components/form/fields/UrlFieldRender.js +6 -0
- package/src/ui/components/form/fields/date-field.css +32 -0
- package/src/ui/components/form/fields/field.css +402 -0
- package/src/ui/components/form/fields/file-field.css +79 -0
- package/src/ui/components/form/fields/password-field.css +50 -0
- package/src/ui/components/form/fields/range-field.css +120 -0
- package/src/ui/components/form/fields/slider.css +195 -0
- package/src/ui/components/form/file-upload-mode/FileAvatarModeRender.js +143 -0
- package/src/ui/components/form/file-upload-mode/FileDropzoneModeRender.js +108 -0
- package/src/ui/components/form/file-upload-mode/FileNativeModeRender.js +22 -0
- package/src/ui/components/form/file-upload-mode/FileUploadButtonModeRender.js +89 -0
- package/src/ui/components/form/file-upload-mode/FileWallModeRender.js +91 -0
- package/src/ui/components/form/file-upload-mode/file-avatar-mode.css +139 -0
- package/src/ui/components/form/file-upload-mode/file-dropzone-mode.css +88 -0
- package/src/ui/components/form/file-upload-mode/file-upload-button-mode.css +44 -0
- package/src/ui/components/form/file-upload-mode/file-wall-mode.css +88 -0
- package/src/ui/components/form/form-control.css +40 -0
- package/src/ui/components/form/helpers.js +112 -0
- package/src/ui/components/form/index.js +61 -0
- package/src/ui/components/menu/MenuDividerRender.js +12 -0
- package/src/ui/components/menu/MenuGroupRender.js +60 -0
- package/src/ui/components/menu/MenuItemRender.js +57 -0
- package/src/ui/components/menu/MenuLinkRender.js +55 -0
- package/src/ui/components/menu/MenuRender.js +21 -0
- package/src/ui/components/menu/helpers.js +121 -0
- package/src/ui/components/menu/menu.css +308 -0
- package/src/ui/components/modal/ModalRender.js +119 -0
- package/src/ui/components/modal/modal.css +156 -0
- package/src/ui/components/pagination/PaginationRender.js +112 -0
- package/src/ui/components/pagination/pagination.css +63 -0
- package/src/ui/components/popover/PopoverRender.js +234 -0
- package/src/ui/components/popover/popover.css +139 -0
- package/src/ui/components/progress/ProgressRender.js +168 -0
- package/src/ui/components/progress/progress.css +197 -0
- package/src/ui/components/skeleton/SkeletonRender.js +79 -0
- package/src/ui/components/skeleton/skeleton.css +154 -0
- package/src/ui/components/spinner/SpinnerRender.js +46 -0
- package/src/ui/components/spinner/spinner.css +152 -0
- package/src/ui/components/splitter/SplitterGutterRender.js +94 -0
- package/src/ui/components/splitter/SplitterPanelRender.js +38 -0
- package/src/ui/components/splitter/SplitterRender.js +74 -0
- package/src/ui/components/splitter/splitter.css +128 -0
- package/src/ui/components/stacks/PositionStackRender.js +38 -0
- package/src/ui/components/stacks/StackRender.js +42 -0
- package/src/ui/components/stacks/absolute-stack/AbsoluteStackRender.js +5 -0
- package/src/ui/components/stacks/fixed-stack/FixedStackRender.js +5 -0
- package/src/ui/components/stacks/h-stack/HStackRender.js +7 -0
- package/src/ui/components/stacks/h-stack/h-stack.css +4 -0
- package/src/ui/components/stacks/index.js +15 -0
- package/src/ui/components/stacks/position-stack.css +62 -0
- package/src/ui/components/stacks/relative-stack/RelativeStackRender.js +7 -0
- package/src/ui/components/stacks/relative-stack/relative-stack.css +3 -0
- package/src/ui/components/stacks/stack.css +78 -0
- package/src/ui/components/stacks/v-stack/VStackRender.js +5 -0
- package/src/ui/components/stacks/v-stack/v-stack.css +4 -0
- package/src/ui/components/stepper/StepperRender.js +70 -0
- package/src/ui/components/stepper/StepperStepRender.js +67 -0
- package/src/ui/components/stepper/stepper.css +359 -0
- package/src/ui/components/switch/SwitchRender.js +82 -0
- package/src/ui/components/switch/switch.css +143 -0
- package/src/ui/components/table/data-table/DataTableRender.js +49 -0
- package/src/ui/components/table/data-table/bulk-actions.js +35 -0
- package/src/ui/components/table/data-table/data-table.css +246 -0
- package/src/ui/components/table/data-table/pagination.js +56 -0
- package/src/ui/components/table/data-table/tables.js +367 -0
- package/src/ui/components/table/data-table/toolbar.js +67 -0
- package/src/ui/components/table/simple-table/SimpleTableRender.js +191 -0
- package/src/ui/components/table/simple-table/simple-table.css +50 -0
- package/src/ui/components/tabs/TabsRender.js +226 -0
- package/src/ui/components/tabs/tabs.css +253 -0
- package/src/ui/components/toast/ToastRender.js +98 -0
- package/src/ui/components/toast/toast.css +201 -0
- package/src/ui/components/tooltip/TooltipRender.js +8 -0
- package/src/ui/components/tooltip/tooltip.css +113 -0
- package/src/ui/index.js +82 -0
- package/src/ui/tokens/colors-dark.scss +2 -1
- package/src/ui/tokens/reset.scss +3 -0
- package/types/control-flow.d.ts +2 -2
- package/src/components/layouts/ZStack.js +0 -41
- package/src/ui/components/button/button.render.js +0 -63
- /package/src/components/{layouts → stacks}/HStack.js +0 -0
- /package/src/components/{layouts → stacks}/VStack.js +0 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import {Div, Label, Select, Option, Span, ForEachArray, ShowIf} from '../../../../../elements';
|
|
2
|
+
import { $ } from '../../../../../index';
|
|
3
|
+
import {Dropdown} from '../../../../../components';
|
|
4
|
+
import {buildErrors} from '../helpers';
|
|
5
|
+
import DebugManager from "../../../../core/utils/debug-manager";
|
|
6
|
+
|
|
7
|
+
export default function SelectFieldRender($desc, instance) {
|
|
8
|
+
const props = instance.getEditableProps();
|
|
9
|
+
|
|
10
|
+
props.class.add('field');
|
|
11
|
+
props.class.add('is-select-field');
|
|
12
|
+
props.class.add({'is-disabled': $desc.disabled, 'has-error': $desc.hasErrors});
|
|
13
|
+
|
|
14
|
+
const content = [];
|
|
15
|
+
|
|
16
|
+
if($desc.label) {
|
|
17
|
+
content.push(buildLabel($desc));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const {trigger, nativeSelect} = buildSelectWrapper($desc, instance);
|
|
21
|
+
content.push(trigger);
|
|
22
|
+
|
|
23
|
+
if($desc.help) {
|
|
24
|
+
content.push(Span({class: 'field-hint', ...($desc.elementsProps.hint || {})}, $desc.help));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
content.push(buildErrors($desc));
|
|
28
|
+
|
|
29
|
+
instance.$input = nativeSelect;
|
|
30
|
+
|
|
31
|
+
return Div(instance.resolveProps(), content).nd.with({input: nativeSelect});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const buildLabel = ($desc) => {
|
|
35
|
+
return Label({
|
|
36
|
+
class: 'field-label',
|
|
37
|
+
for: $desc.id || $desc.name,
|
|
38
|
+
...($desc.elementsProps.label || {}),
|
|
39
|
+
}, $desc.label);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const buildSelectWrapper = ($desc, instance) => {
|
|
43
|
+
const nativeSelect = buildNativeSelect($desc);
|
|
44
|
+
const isTags = $desc.multiple && $desc.multipleDisplay === 'tags';
|
|
45
|
+
|
|
46
|
+
const valueDisplay = isTags
|
|
47
|
+
? buildTagsDisplay($desc)
|
|
48
|
+
: Span({class: 'select-field-value'}, buildSelectedLabel($desc));
|
|
49
|
+
|
|
50
|
+
const triggerOptions = [];
|
|
51
|
+
|
|
52
|
+
if($desc.clearable) {
|
|
53
|
+
const clearBtn = Span({class: 'select-field-clear'}, '×');
|
|
54
|
+
clearBtn.nd.onClick((e) => {
|
|
55
|
+
e.stopPropagation();
|
|
56
|
+
$desc.value?.__$isObservable && $desc.value.set($desc.multiple ? [] : null);
|
|
57
|
+
syncNativeSelect(nativeSelect, $desc);
|
|
58
|
+
instance.validate();
|
|
59
|
+
});
|
|
60
|
+
triggerOptions.push(ShowIf($desc.value.check(val => !!val.length), clearBtn));
|
|
61
|
+
}
|
|
62
|
+
triggerOptions.push(Span({class: 'select-field-chevron'}, '▾'));
|
|
63
|
+
|
|
64
|
+
const trigger = Div({
|
|
65
|
+
class: {'field-input select-field-trigger': true, 'has-tags': isTags},
|
|
66
|
+
...($desc.elementsProps.input || {}),
|
|
67
|
+
}, [
|
|
68
|
+
valueDisplay,
|
|
69
|
+
Div({ class: 'select-field-controls' }, triggerOptions),
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
const dropdown = buildDropdown($desc, instance, trigger, nativeSelect);
|
|
73
|
+
const triggerNd = trigger.nd.ghostDom(dropdown).ghostDom(nativeSelect);
|
|
74
|
+
|
|
75
|
+
return {trigger: triggerNd, nativeSelect};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const buildTagsDisplay = ($desc) => {
|
|
79
|
+
const options = Array.isArray($desc.options) ? $desc.options : [];
|
|
80
|
+
if(!$desc.value?.__$isObservableArray) {
|
|
81
|
+
DebugManager.error('SelectFieldRender', 'buildTagsDisplay', 'Value is not an observable array', $desc.value);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return Div({class: 'select-field-tags'},
|
|
86
|
+
ForEachArray($desc.value, (val) => {
|
|
87
|
+
const opt = options.find(o => (o.value ?? o) === val);
|
|
88
|
+
const label = opt?.label ?? opt ?? val;
|
|
89
|
+
|
|
90
|
+
const tag = Span({class: 'select-field-tag'}, [
|
|
91
|
+
Span({class: 'select-field-tag-label'}, label),
|
|
92
|
+
Span({class: 'select-field-tag-remove'}, '×'),
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
tag.querySelector('.select-field-tag-remove').addEventListener('click', (e) => {
|
|
96
|
+
e.stopPropagation();
|
|
97
|
+
if($desc.value?.__$isObservable) {
|
|
98
|
+
$desc.value.set($desc.value.val().filter(v => v !== val));
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return tag;
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const buildSelectedLabel = ($desc) => {
|
|
108
|
+
if(!$desc.value?.__$isObservable) {
|
|
109
|
+
return $desc.placeholder || 'Select...';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if($desc.selectedLabelRender) {
|
|
113
|
+
return $desc.selectedLabelRender($desc);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return $desc.value.transform((val) => {
|
|
117
|
+
if(!val || (Array.isArray(val) && val.length === 0)) {
|
|
118
|
+
return $desc.placeholder || 'Select...';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const options = Array.isArray($desc.options) ? $desc.options : [];
|
|
122
|
+
|
|
123
|
+
if(!$desc.multiple || !Array.isArray(val)) {
|
|
124
|
+
const opt = options.find(o => (o.value ?? o) === val);
|
|
125
|
+
return opt?.label ?? opt ?? val;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const mode = $desc.multipleDisplay || 'truncate';
|
|
129
|
+
|
|
130
|
+
if(mode === 'count') {
|
|
131
|
+
return $desc.countRender ? $desc.countRender(val) : (val.length + ' selected');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const labels = val.map(v => {
|
|
135
|
+
const opt = options.find(o => (o.value ?? o) === v);
|
|
136
|
+
return opt?.label ?? opt ?? v;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
if(mode === 'truncate') {
|
|
140
|
+
const max = $desc.truncateMax || 2;
|
|
141
|
+
const rest = labels.length - max;
|
|
142
|
+
if(rest > 0) {
|
|
143
|
+
const visibleLabels = labels.slice(0, max);
|
|
144
|
+
return $desc.truncateRender
|
|
145
|
+
? $desc.truncateRender(visibleLabels, rest)
|
|
146
|
+
: (visibleLabels + ' +' + rest + ' more');
|
|
147
|
+
}
|
|
148
|
+
return labels.join(', ');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return labels.join(', ');
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const buildNativeSelect = ($desc) => {
|
|
156
|
+
const $options = Array.isArray($desc.options)
|
|
157
|
+
? $.array($desc.options)
|
|
158
|
+
: $desc.options;
|
|
159
|
+
|
|
160
|
+
return Select({
|
|
161
|
+
name: $desc.name,
|
|
162
|
+
id: $desc.id || $desc.name,
|
|
163
|
+
multiple: $desc.multiple,
|
|
164
|
+
style: {display: 'none'},
|
|
165
|
+
}, ForEachArray($options, (option) => {
|
|
166
|
+
const optValue = option.value ?? option;
|
|
167
|
+
const optLabel = option.label ?? option;
|
|
168
|
+
const optProps = option.props ?? {};
|
|
169
|
+
return Option({value: optValue, ...optProps}, optLabel);
|
|
170
|
+
}));
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const syncNativeSelect = (nativeSelect, $desc) => {
|
|
174
|
+
const val = $desc.value?.__$isObservable ? $desc.value.val() : null;
|
|
175
|
+
if(!val) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
Array.from(nativeSelect.options).forEach(opt => {
|
|
180
|
+
opt.selected = $desc.multiple
|
|
181
|
+
? Array.isArray(val) && val.includes(opt.value)
|
|
182
|
+
: opt.value === String(val);
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const buildDropdown = ($desc, instance, trigger, nativeSelect) => {
|
|
187
|
+
const $options = Array.isArray($desc.options)
|
|
188
|
+
? $.array($desc.options)
|
|
189
|
+
: $desc.options;
|
|
190
|
+
|
|
191
|
+
let $sourceItem = $options;
|
|
192
|
+
if($desc.removeSelected && $options?.__$Observable) {
|
|
193
|
+
$sourceItem = $.array();
|
|
194
|
+
const updateSourceItem = () => {
|
|
195
|
+
const options = $options.val() || [];
|
|
196
|
+
const values = $desc.value?.val() || [];
|
|
197
|
+
$sourceItem.set(options.filter(v => !values.includes(v?.value || v)));
|
|
198
|
+
};
|
|
199
|
+
$.computed(updateSourceItem, [$options, $desc.value]);
|
|
200
|
+
updateSourceItem();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const dropdown = Dropdown()
|
|
204
|
+
.trigger(trigger)
|
|
205
|
+
.atBottomLeading()
|
|
206
|
+
.renderItem($desc.renderItem)
|
|
207
|
+
.matchTriggerWidth()
|
|
208
|
+
.closeOnSelect(!$desc.multiple);
|
|
209
|
+
|
|
210
|
+
if($desc.searchable) {
|
|
211
|
+
dropdown.searchable(true, $desc.searchPlaceholder);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return dropdown.bind($sourceItem, (option) => {
|
|
215
|
+
const optValue = option.value ?? option;
|
|
216
|
+
const optLabel = option.label ?? option;
|
|
217
|
+
|
|
218
|
+
const $isSelected = $desc.value?.__$isObservable
|
|
219
|
+
? $desc.value.is(v => $desc.multiple
|
|
220
|
+
? Array.isArray(v) && v.includes(optValue)
|
|
221
|
+
: v === optValue)
|
|
222
|
+
: false;
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
label: optLabel,
|
|
226
|
+
data: option,
|
|
227
|
+
value: optValue,
|
|
228
|
+
selected: $isSelected,
|
|
229
|
+
action: () => {
|
|
230
|
+
if($desc.value?.__$isObservable) {
|
|
231
|
+
if($desc.multiple) {
|
|
232
|
+
const current = $desc.value.val() || [];
|
|
233
|
+
const next = current.includes(optValue)
|
|
234
|
+
? current.filter(v => v !== optValue)
|
|
235
|
+
: [...current, optValue];
|
|
236
|
+
$desc.value.set(next);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
$desc.value.set(optValue);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
instance.emit('change', optValue ?? option ?? $desc.value);
|
|
243
|
+
syncNativeSelect(nativeSelect, $desc);
|
|
244
|
+
instance.validate();
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
};
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import {Div, Span, Switch, ForEachArray} from '../../../../../elements';
|
|
2
|
+
import { $, Observable } from '../../../../../index';
|
|
3
|
+
import {Tooltip} from '../../../../../components';
|
|
4
|
+
import './slider.css';
|
|
5
|
+
|
|
6
|
+
export default function SliderRender($desc, instance) {
|
|
7
|
+
const props = instance.getEditableProps();
|
|
8
|
+
|
|
9
|
+
props.class.add('slider');
|
|
10
|
+
props.class.add({
|
|
11
|
+
'is-vertical': $desc.vertical,
|
|
12
|
+
'is-reverse': $desc.reverse,
|
|
13
|
+
'is-disabled': $desc.disabled,
|
|
14
|
+
'is-readonly': $desc.readonly,
|
|
15
|
+
[`is-${$desc.variant}`]: !!$desc.variant,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
if($desc.height) {
|
|
19
|
+
props.style.add({ '--slider-vertical-height': $desc.height + 'px'});
|
|
20
|
+
}
|
|
21
|
+
if($desc.width) {
|
|
22
|
+
props.style.add({ '--slider-horizontal-width': $desc.width + 'px'});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if($desc.trackColor) {
|
|
26
|
+
props.style.add({ '--slider-track-color': $desc.trackColor });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return Div(instance.resolveProps(), [
|
|
30
|
+
buildSlider($desc, instance),
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const buildSlider = ($desc, instance) => {
|
|
35
|
+
if($desc.range?.__$Observable) {
|
|
36
|
+
return Switch($desc.range,
|
|
37
|
+
() => buildRangeSlider($desc, instance),
|
|
38
|
+
() => buildSingleSlider($desc, instance),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
if($desc.range) {
|
|
42
|
+
return buildRangeSlider($desc, instance);
|
|
43
|
+
}
|
|
44
|
+
return buildSingleSlider($desc, instance);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const buildSingleSlider = ($desc, instance) => {
|
|
48
|
+
if(!$desc.value?.__$isObservable) {
|
|
49
|
+
$desc.value = $($desc.defaultValue ?? $desc.min ?? 0);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const $percentValue = $desc.value.transform(v => toPercent(v, $desc));
|
|
53
|
+
|
|
54
|
+
const track = buildTrack($desc, $percentValue, null);
|
|
55
|
+
const thumb = buildThumb($desc, instance, $percentValue, $desc.value, false);
|
|
56
|
+
const marks = $desc.showMarks ? buildMarks($desc) : null;
|
|
57
|
+
const valueEl = $desc.showValue ? buildValueDisplay($desc.value, $desc) : null;
|
|
58
|
+
|
|
59
|
+
const sliderEl = Div({class: 'slider-inner'}, [track, thumb, marks]);
|
|
60
|
+
|
|
61
|
+
setupDrag($desc, instance, sliderEl, $desc.value, false, null);
|
|
62
|
+
|
|
63
|
+
return Div({class: 'slider-wrapper'}, [sliderEl, valueEl]);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const buildRangeSlider = ($desc, instance) => {
|
|
67
|
+
if(!$desc.valueStart?.__$isObservable) {
|
|
68
|
+
$desc.valueStart = $($desc.min ?? 0);
|
|
69
|
+
}
|
|
70
|
+
if(!$desc.valueEnd?.__$isObservable) {
|
|
71
|
+
$desc.valueEnd = $($desc.max ?? 100);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const $pctStart = $desc.valueStart.transform(v => toPercent(v, $desc));
|
|
75
|
+
const $pctEnd = $desc.valueEnd.transform(v => toPercent(v, $desc));
|
|
76
|
+
|
|
77
|
+
const track = buildRangeTrack($desc, $pctStart, $pctEnd);
|
|
78
|
+
const thumbStart = buildThumb($desc, instance, $pctStart, $desc.valueStart, 'start');
|
|
79
|
+
const thumbEnd = buildThumb($desc, instance, $pctEnd, $desc.valueEnd, 'end');
|
|
80
|
+
const marks = $desc.showMarks ? buildMarks($desc) : null;
|
|
81
|
+
|
|
82
|
+
const sliderEl = Div({class: 'slider-inner'}, [track, thumbStart, thumbEnd, marks].filter(Boolean));
|
|
83
|
+
|
|
84
|
+
setupDrag($desc, instance, sliderEl, $desc.valueStart, 'start', thumbStart);
|
|
85
|
+
setupDrag($desc, instance, sliderEl, $desc.valueEnd, 'end', thumbEnd);
|
|
86
|
+
|
|
87
|
+
return Div({ class: 'slider-wrapper' }, [sliderEl]);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const buildTrack = ($desc, $pct) => {
|
|
91
|
+
const isVertical = $desc.vertical;
|
|
92
|
+
let style;
|
|
93
|
+
|
|
94
|
+
if(isVertical?.__$Observable) {
|
|
95
|
+
// TODO: @DIM trouver un meilleur DX pour ce dernier
|
|
96
|
+
const width = Observable.computed(() => {
|
|
97
|
+
if(isVertical.val()) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return $pct.val()+'%';
|
|
101
|
+
}, [isVertical, $pct]);
|
|
102
|
+
const height = Observable.computed(() => {
|
|
103
|
+
if(!isVertical.val()) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return $pct.val()+'%';
|
|
107
|
+
}, [isVertical, $pct]);
|
|
108
|
+
|
|
109
|
+
style = {
|
|
110
|
+
width,
|
|
111
|
+
height,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
} else {
|
|
115
|
+
if(isVertical) {
|
|
116
|
+
style = {
|
|
117
|
+
height: $pct.transform((v) => v+'%')
|
|
118
|
+
};
|
|
119
|
+
} else {
|
|
120
|
+
style = {
|
|
121
|
+
width: $pct.transform((v) => v+'%')
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const fill = Div({
|
|
127
|
+
class: 'slider-fill',
|
|
128
|
+
style
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return Div({class: 'slider-track'}, fill);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const buildRangeTrack = ($desc, $pctStart, $pctEnd) => {
|
|
135
|
+
const isVertical = $desc.vertical;
|
|
136
|
+
|
|
137
|
+
const $fillSize = Observable.computed(() => {
|
|
138
|
+
const s = toPercent($desc.valueStart.val() ?? $desc.min, $desc);
|
|
139
|
+
const e = toPercent($desc.valueEnd.val() ?? $desc.max, $desc);
|
|
140
|
+
return (e - s) + '%';
|
|
141
|
+
}, [$desc.valueStart, $desc.valueEnd]);
|
|
142
|
+
|
|
143
|
+
const $fillPos = $pctStart.transform(p => p + '%');
|
|
144
|
+
|
|
145
|
+
let style;
|
|
146
|
+
if(isVertical?.__$Observable) {
|
|
147
|
+
const positionDependencies = [isVertical, $pctStart];
|
|
148
|
+
const sizeDependencies = [isVertical, $desc.valueStart, $desc.valueEnd];
|
|
149
|
+
|
|
150
|
+
const bottom = $.computed(() => isVertical.val() ? $fillPos.val() : false, positionDependencies);
|
|
151
|
+
const height = $.computed(() => isVertical.val() ? $fillSize.val() : false, sizeDependencies);
|
|
152
|
+
const left = $.computed(() => isVertical.val() ? false : $fillPos.val(), positionDependencies);
|
|
153
|
+
const width = $.computed(() => isVertical.val() ? false : $fillSize.val(), sizeDependencies);
|
|
154
|
+
|
|
155
|
+
style = {bottom, height, left, width};
|
|
156
|
+
} else {
|
|
157
|
+
style = isVertical
|
|
158
|
+
? {bottom: $fillPos, height: $fillSize}
|
|
159
|
+
: {left: $fillPos, width: $fillSize};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const fill = Div({class: 'slider-fill', style});
|
|
163
|
+
|
|
164
|
+
return Div({class: 'slider-track'}, fill);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const buildThumb = ($desc, instance, $pct, $value, role) => {
|
|
168
|
+
const isVertical = $desc.vertical;
|
|
169
|
+
|
|
170
|
+
const thumbContent = $desc.renderThumb
|
|
171
|
+
? $desc.renderThumb($value, instance)
|
|
172
|
+
: Div({class: 'slider-thumb-inner'});
|
|
173
|
+
|
|
174
|
+
let style, position;
|
|
175
|
+
if(isVertical?.__$Observable) {
|
|
176
|
+
// TODO: @DIM trouver un meilleur DX pour ce dernier
|
|
177
|
+
const pos = (verticalMode) => Observable.computed(() => {
|
|
178
|
+
return (isVertical.val() === verticalMode)
|
|
179
|
+
? `calc(${ $pct.val() }% - 9px)`
|
|
180
|
+
: false;
|
|
181
|
+
}, [isVertical, $pct]);
|
|
182
|
+
|
|
183
|
+
const bottom = pos(true);
|
|
184
|
+
const left = pos(false);
|
|
185
|
+
|
|
186
|
+
position = isVertical.transform((v) => v ? 'right' : 'top');
|
|
187
|
+
style = {
|
|
188
|
+
left,
|
|
189
|
+
bottom,
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
} else {
|
|
193
|
+
if(isVertical) {
|
|
194
|
+
position = 'right';
|
|
195
|
+
style = {
|
|
196
|
+
bottom: $pct.transform(p => `calc(${p}% - 9px)`)
|
|
197
|
+
};
|
|
198
|
+
} else {
|
|
199
|
+
position = 'top';
|
|
200
|
+
style = {
|
|
201
|
+
left: $pct.transform(p => `calc(${p}% - 9px)`),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// TODO: @DIM no focus element available for Div Element
|
|
207
|
+
// thumb.nd.onKeyDown((e) => { ... });
|
|
208
|
+
|
|
209
|
+
const thumb = Div({
|
|
210
|
+
class: {'slider-thumb': true, 'is-start': role === 'start', 'is-end': role === 'end'},
|
|
211
|
+
style,
|
|
212
|
+
tabindex: $desc.disabled ? -1 : 0,
|
|
213
|
+
}, [thumbContent]);
|
|
214
|
+
|
|
215
|
+
if($desc.showTooltip) {
|
|
216
|
+
const tooltipContent = $desc.renderTooltip
|
|
217
|
+
? $desc.renderTooltip($value)
|
|
218
|
+
: $desc.tooltipFormat
|
|
219
|
+
? $value.transform($desc.tooltipFormat)
|
|
220
|
+
: $value.transform(v => String(Math.round(v)));
|
|
221
|
+
|
|
222
|
+
return thumb.nd.tooltip(Tooltip(tooltipContent)
|
|
223
|
+
.position(position)
|
|
224
|
+
.updatePositionOn($pct));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return thumb;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const buildValueDisplay = ($value, $desc) => {
|
|
231
|
+
const formatted = $desc.tooltipFormat
|
|
232
|
+
? $value.transform($desc.tooltipFormat)
|
|
233
|
+
: $value.transform(v => String(Math.round(v)));
|
|
234
|
+
return Span({class: 'slider-value'}, formatted);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const buildMarks = ($desc) => {
|
|
238
|
+
const marks = $desc.marks || [];
|
|
239
|
+
const min = $desc.min ?? 0;
|
|
240
|
+
const max = $desc.max ?? 100;
|
|
241
|
+
|
|
242
|
+
if(!marks.length) return null;
|
|
243
|
+
|
|
244
|
+
return Div({class: 'slider-marks'},
|
|
245
|
+
ForEachArray(marks, (mark) => {
|
|
246
|
+
const pct = ((mark.value - min) / (max - min)) * 100;
|
|
247
|
+
|
|
248
|
+
let style;
|
|
249
|
+
if($desc.vertical?.__$Observable) {
|
|
250
|
+
style = {
|
|
251
|
+
bottom: $desc.vertical.transform(v => v ? pct + '%' : false),
|
|
252
|
+
left: $desc.vertical.transform(v => v ? false : pct + '%'),
|
|
253
|
+
};
|
|
254
|
+
} else {
|
|
255
|
+
style = $desc.vertical ? {bottom: pct + '%'} : {left: pct + '%'};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return Div({
|
|
259
|
+
class: 'slider-mark',
|
|
260
|
+
style,
|
|
261
|
+
}, [
|
|
262
|
+
Div({class: 'slider-mark-dot'}),
|
|
263
|
+
mark.label ? Span({class: 'slider-mark-label'}, mark.label) : null,
|
|
264
|
+
].filter(Boolean));
|
|
265
|
+
})
|
|
266
|
+
);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const setupDrag = ($desc, instance, sliderEl, $value, role, thumbEl) => {
|
|
270
|
+
const isNotEditable = () => {
|
|
271
|
+
return $desc.disabled?.valueOf() || $desc.readonly?.valueOf();
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const onMove = (clientX, clientY) => {
|
|
275
|
+
const rect = sliderEl.getBoundingClientRect();
|
|
276
|
+
const isVert = $desc.vertical?.valueOf();
|
|
277
|
+
const raw = isVert
|
|
278
|
+
? 1 - (clientY - rect.top) / rect.height
|
|
279
|
+
: (clientX - rect.left) / rect.width;
|
|
280
|
+
|
|
281
|
+
const clamped = Math.max(0, Math.min(1, raw));
|
|
282
|
+
const min = $desc.min ?? 0;
|
|
283
|
+
const max = $desc.max ?? 100;
|
|
284
|
+
const step = $desc.step || 1;
|
|
285
|
+
const raw_val = min + clamped * (max - min);
|
|
286
|
+
const stepped = Math.round(raw_val / step) * step;
|
|
287
|
+
|
|
288
|
+
setValue($desc, instance, $value, stepped, role);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const onMouseMove = (e) => onMove(e.clientX, e.clientY);
|
|
292
|
+
const onMouseUp = () => {
|
|
293
|
+
document.removeEventListener('mousemove', onMouseMove);
|
|
294
|
+
document.removeEventListener('mouseup', onMouseUp);
|
|
295
|
+
instance.emit('complete', $value.val());
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const targetEl = thumbEl || sliderEl;
|
|
299
|
+
|
|
300
|
+
targetEl.nd.onMouseDown((e) => {
|
|
301
|
+
if(isNotEditable()) return;
|
|
302
|
+
e.preventDefault();
|
|
303
|
+
e.stopPropagation();
|
|
304
|
+
onMove(e.clientX, e.clientY);
|
|
305
|
+
document.addEventListener('mousemove', onMouseMove);
|
|
306
|
+
document.addEventListener('mouseup', onMouseUp);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const onTouchMove = (e) => {
|
|
310
|
+
const t = e.touches[0];
|
|
311
|
+
onMove(t.clientX, t.clientY);
|
|
312
|
+
};
|
|
313
|
+
const onTouchEnd = () => {
|
|
314
|
+
document.removeEventListener('touchmove', onTouchMove);
|
|
315
|
+
document.removeEventListener('touchend', onTouchEnd);
|
|
316
|
+
instance.emit('complete', $value.val());
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
targetEl.nd.onTouchStart((e) => {
|
|
320
|
+
if(isNotEditable()) return;
|
|
321
|
+
const touch = e.touches[0];
|
|
322
|
+
onMove(touch.clientX, touch.clientY);
|
|
323
|
+
document.addEventListener('touchmove', onTouchMove);
|
|
324
|
+
document.addEventListener('touchend', onTouchEnd);
|
|
325
|
+
});
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const setValue = ($desc, instance, $value, val, role) => {
|
|
329
|
+
const min = $desc.min ?? 0;
|
|
330
|
+
const max = $desc.max ?? 100;
|
|
331
|
+
const clamped = Math.max(min, Math.min(max, val));
|
|
332
|
+
|
|
333
|
+
if(role === 'start' && $desc.valueEnd?.__$isObservable) {
|
|
334
|
+
const end = $desc.valueEnd.val() ?? max;
|
|
335
|
+
if(clamped > end) return;
|
|
336
|
+
}
|
|
337
|
+
if(role === 'end' && $desc.valueStart?.__$isObservable) {
|
|
338
|
+
const start = $desc.valueStart.val() ?? min;
|
|
339
|
+
if(clamped < start) return;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if($desc.snapToMarks && $desc.marks?.length) {
|
|
343
|
+
const closest = $desc.marks.reduce((prev, curr) =>
|
|
344
|
+
Math.abs(curr.value - clamped) < Math.abs(prev.value - clamped) ? curr : prev
|
|
345
|
+
);
|
|
346
|
+
$value.set(closest.value);
|
|
347
|
+
instance.emit('change', closest.value);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
$value.set(clamped);
|
|
352
|
+
instance.emit('change', clamped);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const toPercent = (value, $desc) => {
|
|
356
|
+
const min = $desc.min ?? 0;
|
|
357
|
+
const max = $desc.max ?? 100;
|
|
358
|
+
return ((value - min) / (max - min)) * 100;
|
|
359
|
+
};
|