@agnos-ui/core 0.1.1 → 0.3.0
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/README.md +3 -3
- package/accordion-BaWN0_n-.js +330 -0
- package/accordion-BuIgxZ0S.cjs +329 -0
- package/alert-C4jCg9Pl.cjs +8 -0
- package/alert-Dlf-BV98.js +9 -0
- package/common-DRdsw5m8.js +76 -0
- package/common-nJRMXbwj.cjs +75 -0
- package/components/accordion/accordion.d.ts +57 -64
- package/components/accordion/index.cjs +7 -0
- package/components/accordion/index.js +7 -1
- package/components/alert/alert.d.ts +3 -12
- package/components/alert/common.d.ts +4 -13
- package/components/alert/index.cjs +10 -0
- package/components/alert/index.js +10 -2
- package/components/modal/index.cjs +7 -0
- package/components/modal/index.js +7 -1
- package/components/modal/modal.d.ts +7 -49
- package/components/pagination/index.cjs +5 -0
- package/components/pagination/index.d.ts +0 -1
- package/components/pagination/index.js +5 -2
- package/components/pagination/pagination.d.ts +107 -128
- package/components/progressbar/index.cjs +5 -0
- package/components/progressbar/index.js +5 -1
- package/components/progressbar/progressbar.d.ts +6 -22
- package/components/rating/index.cjs +5 -0
- package/components/rating/index.js +5 -1
- package/components/rating/rating.d.ts +16 -11
- package/components/select/index.cjs +6 -0
- package/components/select/index.js +6 -1
- package/components/select/select.d.ts +45 -57
- package/components/slider/index.cjs +5 -0
- package/components/slider/index.js +5 -1
- package/components/slider/slider.d.ts +63 -5
- package/components/toast/index.cjs +5 -0
- package/components/toast/index.d.ts +1 -0
- package/components/toast/index.js +5 -0
- package/components/toast/toast.d.ts +47 -0
- package/config.cjs +38 -0
- package/config.d.ts +6 -1
- package/config.js +35 -50
- package/directive-BTSEYLF3.cjs +404 -0
- package/directive-DCYlDznf.js +405 -0
- package/func-DR0n-ShK.js +7 -0
- package/func-Qd3cD9a3.cjs +6 -0
- package/index.cjs +119 -0
- package/index.d.ts +3 -1
- package/index.js +119 -31
- package/modal-BI2qUu1M.js +251 -0
- package/modal-rzMpATf5.cjs +250 -0
- package/package.json +30 -22
- package/pagination--GkwduJn.js +263 -0
- package/pagination-EWSWQT1I.cjs +262 -0
- package/progressbar-DH7DHYMp.cjs +83 -0
- package/progressbar-DuRX7_my.js +84 -0
- package/promise-BMJ8qhA8.cjs +118 -0
- package/promise-CY2U8bTP.js +119 -0
- package/rating-BR5wD7y2.js +173 -0
- package/rating-CmuYUSxy.cjs +172 -0
- package/select-BCs6HQWn.js +358 -0
- package/select-CCIKn8WR.cjs +357 -0
- package/services/extendWidget.cjs +32 -0
- package/services/extendWidget.d.ts +10 -5
- package/services/extendWidget.js +31 -34
- package/services/floatingUI.cjs +131 -0
- package/services/floatingUI.d.ts +30 -14
- package/services/floatingUI.js +128 -102
- package/services/focustrack.cjs +47 -0
- package/services/focustrack.js +45 -44
- package/services/hash.cjs +15 -0
- package/services/hash.d.ts +2 -0
- package/services/hash.js +15 -0
- package/services/intersection.cjs +53 -0
- package/services/intersection.js +48 -50
- package/services/matchMedia.cjs +13 -0
- package/services/matchMedia.d.ts +7 -0
- package/services/matchMedia.js +13 -0
- package/services/navManager.cjs +196 -0
- package/services/navManager.d.ts +24 -17
- package/services/navManager.js +186 -162
- package/services/portal.cjs +43 -0
- package/services/portal.js +41 -42
- package/services/resizeObserver.cjs +32 -0
- package/services/resizeObserver.d.ts +14 -0
- package/services/resizeObserver.js +32 -0
- package/services/siblingsInert.cjs +40 -0
- package/services/siblingsInert.js +31 -31
- package/services/transitions/baseTransitions.cjs +171 -0
- package/services/transitions/baseTransitions.d.ts +16 -16
- package/services/transitions/baseTransitions.js +159 -170
- package/services/transitions/collapse.cjs +44 -0
- package/services/transitions/collapse.js +41 -49
- package/services/transitions/cssTransitions.cjs +32 -0
- package/services/transitions/cssTransitions.d.ts +2 -1
- package/services/transitions/cssTransitions.js +29 -39
- package/services/transitions/simpleClassTransition.cjs +31 -0
- package/services/transitions/simpleClassTransition.js +30 -41
- package/slider-CA_fszn7.js +536 -0
- package/slider-DsLvT87U.cjs +535 -0
- package/toast-8tWp6x89.js +63 -0
- package/toast-Aw8o0Iwe.cjs +62 -0
- package/types.cjs +12 -0
- package/types.d.ts +21 -1
- package/types.js +11 -13
- package/utils/directive.cjs +26 -0
- package/utils/directive.d.ts +155 -4
- package/utils/directive.js +25 -189
- package/utils/internal/dom.d.ts +57 -3
- package/utils/internal/promise.d.ts +2 -2
- package/utils/internal/ssrHTMLElement.d.ts +7 -0
- package/utils/internal/textDirection.d.ts +1 -1
- package/utils/stores.cjs +163 -0
- package/utils/stores.d.ts +23 -20
- package/utils/stores.js +151 -269
- package/utils/writables.cjs +13 -0
- package/utils/writables.js +12 -71
- package/writables-D46sFgGK.cjs +85 -0
- package/writables-DoU_XYTX.js +86 -0
- package/components/accordion/accordion.js +0 -267
- package/components/alert/alert.js +0 -22
- package/components/alert/common.js +0 -69
- package/components/commonProps.js +0 -1
- package/components/modal/modal.js +0 -186
- package/components/pagination/bootstrap.d.ts +0 -8
- package/components/pagination/bootstrap.js +0 -110
- package/components/pagination/pagination.js +0 -149
- package/components/progressbar/progressbar.js +0 -78
- package/components/rating/rating.js +0 -138
- package/components/select/select.js +0 -266
- package/components/slider/slider.js +0 -415
- package/services/transitions/bootstrap/collapse.d.ts +0 -2
- package/services/transitions/bootstrap/collapse.js +0 -15
- package/services/transitions/bootstrap/fade.d.ts +0 -1
- package/services/transitions/bootstrap/fade.js +0 -7
- package/services/transitions/bootstrap.d.ts +0 -2
- package/services/transitions/bootstrap.js +0 -2
- package/utils/internal/checks.js +0 -60
- package/utils/internal/dom.js +0 -61
- package/utils/internal/func.js +0 -11
- package/utils/internal/isFocusable.js +0 -35
- package/utils/internal/math.js +0 -13
- package/utils/internal/promise.js +0 -169
- package/utils/internal/scrollbars.js +0 -33
- package/utils/internal/sort.js +0 -28
- package/utils/internal/textDirection.js +0 -7
- package/utils/internal/traversal.js +0 -105
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
// Utility functions
|
|
2
|
-
function _applyPagination(page, maxSize) {
|
|
3
|
-
const pp = Math.ceil(page / maxSize) - 1;
|
|
4
|
-
const start = pp * maxSize;
|
|
5
|
-
// console.log('start', start, 'pp', pp, 'page', page, 'maxSize', maxSize);
|
|
6
|
-
const end = start + maxSize;
|
|
7
|
-
return [start, end];
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Appends ellipses and first/last page number to the displayed pages
|
|
11
|
-
* @param start - the first page number
|
|
12
|
-
* @param end - the last page number
|
|
13
|
-
* @param ellipses - apply ellipses
|
|
14
|
-
* @param pages - the pages to apply the function to
|
|
15
|
-
* @param pageCount - the total number of pages
|
|
16
|
-
*/
|
|
17
|
-
function _applyEllipses(start, end, ellipses, pages, pageCount) {
|
|
18
|
-
if (ellipses) {
|
|
19
|
-
if (start > 0) {
|
|
20
|
-
// The first page will always be included. If the displayed range
|
|
21
|
-
// starts after the third page, then add ellipsis. But if the range
|
|
22
|
-
// starts on the third page, then add the second page instead of
|
|
23
|
-
// an ellipsis, because the ellipsis would only hide a single page.
|
|
24
|
-
if (start > 2) {
|
|
25
|
-
pages.unshift(-1);
|
|
26
|
-
}
|
|
27
|
-
else if (start === 2) {
|
|
28
|
-
pages.unshift(2);
|
|
29
|
-
}
|
|
30
|
-
pages.unshift(1);
|
|
31
|
-
}
|
|
32
|
-
if (end < pageCount) {
|
|
33
|
-
// The last page will always be included. If the displayed range
|
|
34
|
-
// ends before the third-last page, then add ellipsis. But if the range
|
|
35
|
-
// ends on third-last page, then add the second-last page instead of
|
|
36
|
-
// an ellipsis, because the ellipsis would only hide a single page.
|
|
37
|
-
if (end < pageCount - 2) {
|
|
38
|
-
pages.push(-1);
|
|
39
|
-
}
|
|
40
|
-
else if (end === pageCount - 2) {
|
|
41
|
-
pages.push(pageCount - 1);
|
|
42
|
-
}
|
|
43
|
-
pages.push(pageCount);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Rotates page numbers based on maxSize items visible.
|
|
49
|
-
* Currently selected page stays in the middle:
|
|
50
|
-
*
|
|
51
|
-
* Ex. for selected page = 6:
|
|
52
|
-
* [5,*6*,7] for maxSize = 3
|
|
53
|
-
* [4,5,*6*,7] for maxSize = 4
|
|
54
|
-
*
|
|
55
|
-
* @param page - the page number
|
|
56
|
-
* @param maxSize - the max size
|
|
57
|
-
* @param pageCount - the page count
|
|
58
|
-
* @returns the rotated page numbers
|
|
59
|
-
*/
|
|
60
|
-
function _applyRotation(page, maxSize, pageCount) {
|
|
61
|
-
let start = 0;
|
|
62
|
-
let end = pageCount;
|
|
63
|
-
const leftOffset = Math.floor(maxSize / 2);
|
|
64
|
-
const rightOffset = maxSize % 2 === 0 ? leftOffset - 1 : leftOffset;
|
|
65
|
-
if (page <= leftOffset) {
|
|
66
|
-
// very beginning, no rotation -> [0..maxSize]
|
|
67
|
-
end = maxSize;
|
|
68
|
-
}
|
|
69
|
-
else if (pageCount - page < leftOffset) {
|
|
70
|
-
// very end, no rotation -> [len-maxSize..len]
|
|
71
|
-
start = pageCount - maxSize;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
// rotate
|
|
75
|
-
start = page - leftOffset - 1;
|
|
76
|
-
end = page + rightOffset;
|
|
77
|
-
}
|
|
78
|
-
return [start, end];
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* This is the pagination algorithm used by ng-bootstrap. While this is not the default implementation of our widget, we export it to ease the migration from ng-bootstrap pagination.
|
|
82
|
-
* @param maxSize - the max size
|
|
83
|
-
* @param rotate - rotate the pages
|
|
84
|
-
* @param ellipses - enable ellipses
|
|
85
|
-
* @returns the pages
|
|
86
|
-
*/
|
|
87
|
-
export function ngBootstrapPagination(maxSize, rotate, ellipses) {
|
|
88
|
-
return function (page, pageCount) {
|
|
89
|
-
let pages = [];
|
|
90
|
-
for (let i = 1; i <= pageCount; i++) {
|
|
91
|
-
pages.push(i);
|
|
92
|
-
}
|
|
93
|
-
// apply maxSize if necessary
|
|
94
|
-
if (maxSize > 0 && pageCount > maxSize) {
|
|
95
|
-
let start = 0;
|
|
96
|
-
let end = pageCount;
|
|
97
|
-
// either paginating or rotating page numbers
|
|
98
|
-
if (rotate) {
|
|
99
|
-
[start, end] = _applyRotation(page, maxSize, pageCount);
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
[start, end] = _applyPagination(page, maxSize);
|
|
103
|
-
}
|
|
104
|
-
pages = pages.slice(start, end);
|
|
105
|
-
// adding ellipses
|
|
106
|
-
_applyEllipses(start, end, ellipses, pages, pageCount);
|
|
107
|
-
}
|
|
108
|
-
return pages;
|
|
109
|
-
};
|
|
110
|
-
}
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { computed } from '@amadeus-it-group/tansu';
|
|
2
|
-
import { INVALID_VALUE } from '../../types';
|
|
3
|
-
import { bindableDerived, stateStores, writablesForProps } from '../../utils/stores';
|
|
4
|
-
import { clamp, isNumber } from '../../utils/internal/checks';
|
|
5
|
-
import { typeBoolean, typeFunction, typeNumber, typeString } from '../../utils/writables';
|
|
6
|
-
import { noop } from '../../utils/internal/func';
|
|
7
|
-
const defaultConfig = {
|
|
8
|
-
page: 1,
|
|
9
|
-
collectionSize: 0,
|
|
10
|
-
pageSize: 10,
|
|
11
|
-
disabled: false,
|
|
12
|
-
directionLinks: true,
|
|
13
|
-
boundaryLinks: false,
|
|
14
|
-
size: null,
|
|
15
|
-
onPageChange: noop,
|
|
16
|
-
pagesFactory: (page, pageCount) => {
|
|
17
|
-
// TODO extract this for testing
|
|
18
|
-
const pages = [];
|
|
19
|
-
for (let i = 1; i <= pageCount; i++) {
|
|
20
|
-
pages.push(i);
|
|
21
|
-
}
|
|
22
|
-
return pages;
|
|
23
|
-
},
|
|
24
|
-
ariaLabel: 'Page navigation',
|
|
25
|
-
activeLabel: '(current)',
|
|
26
|
-
ariaPageLabel: (processPage, pageCount) => `Page ${processPage} of ${pageCount}`,
|
|
27
|
-
ariaFirstLabel: 'Action link for first page',
|
|
28
|
-
ariaPreviousLabel: 'Action link for previous page',
|
|
29
|
-
ariaNextLabel: 'Action link for next page',
|
|
30
|
-
ariaLastLabel: 'Action link for last page',
|
|
31
|
-
className: '',
|
|
32
|
-
slotEllipsis: '…',
|
|
33
|
-
slotFirst: '«',
|
|
34
|
-
slotPrevious: '‹',
|
|
35
|
-
slotNext: '›',
|
|
36
|
-
slotLast: '»',
|
|
37
|
-
slotPages: undefined,
|
|
38
|
-
slotNumberLabel: ({ displayedPage }) => `${displayedPage}`,
|
|
39
|
-
};
|
|
40
|
-
/**
|
|
41
|
-
* Returns a shallow copy of the default pagination config
|
|
42
|
-
* @returns a copy of the default config
|
|
43
|
-
*/
|
|
44
|
-
export function getPaginationDefaultConfig() {
|
|
45
|
-
return { ...defaultConfig };
|
|
46
|
-
}
|
|
47
|
-
const configValidator = {
|
|
48
|
-
page: typeNumber,
|
|
49
|
-
collectionSize: typeNumber,
|
|
50
|
-
pageSize: typeNumber,
|
|
51
|
-
disabled: typeBoolean,
|
|
52
|
-
directionLinks: typeBoolean,
|
|
53
|
-
boundaryLinks: typeBoolean,
|
|
54
|
-
size: { normalizeValue: (value) => (value === 'lg' || value === 'sm' || value === null ? value : INVALID_VALUE) },
|
|
55
|
-
onPageChange: typeFunction,
|
|
56
|
-
pagesFactory: typeFunction,
|
|
57
|
-
ariaLabel: typeString,
|
|
58
|
-
activeLabel: typeString,
|
|
59
|
-
ariaPageLabel: typeFunction,
|
|
60
|
-
ariaFirstLabel: typeString,
|
|
61
|
-
ariaPreviousLabel: typeString,
|
|
62
|
-
ariaNextLabel: typeString,
|
|
63
|
-
ariaLastLabel: typeString,
|
|
64
|
-
className: typeString,
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* Create a PaginationWidget with given config props
|
|
68
|
-
* @param config - an optional pagination config
|
|
69
|
-
* @returns a PaginationWidget
|
|
70
|
-
*/
|
|
71
|
-
export function createPagination(config) {
|
|
72
|
-
const [{
|
|
73
|
-
// dirty inputs that need adjustment:
|
|
74
|
-
page$: _dirtyPage$,
|
|
75
|
-
// clean inputs with value validation:
|
|
76
|
-
collectionSize$, pageSize$, onPageChange$, pagesFactory$, ariaPageLabel$, ...stateProps }, patch,] = writablesForProps(defaultConfig, config, configValidator);
|
|
77
|
-
// computed
|
|
78
|
-
// nb total of Pages.
|
|
79
|
-
const pageCount$ = computed(() => {
|
|
80
|
-
let pageCount = Math.ceil(collectionSize$() / pageSize$());
|
|
81
|
-
// Here we choose to always display a page when collection size is 0
|
|
82
|
-
if (!isNumber(pageCount) || pageCount < 1) {
|
|
83
|
-
pageCount = 1;
|
|
84
|
-
}
|
|
85
|
-
return pageCount;
|
|
86
|
-
});
|
|
87
|
-
// current page
|
|
88
|
-
const page$ = bindableDerived(onPageChange$, [_dirtyPage$, pageCount$], ([dirtyPage, pageCount]) => clamp(dirtyPage, pageCount, 1));
|
|
89
|
-
const pages$ = computed(() => pagesFactory$()(page$(), pageCount$()));
|
|
90
|
-
const nextDisabled$ = computed(() => page$() === pageCount$() || stateProps.disabled$());
|
|
91
|
-
const previousDisabled$ = computed(() => page$() === 1 || stateProps.disabled$());
|
|
92
|
-
const pagesLabel$ = computed(() => {
|
|
93
|
-
const ariaPageLabel = ariaPageLabel$();
|
|
94
|
-
const pageCount = pageCount$();
|
|
95
|
-
return pages$().map((page) => ariaPageLabel(page, pageCount));
|
|
96
|
-
});
|
|
97
|
-
return {
|
|
98
|
-
...stateStores({
|
|
99
|
-
pageCount$,
|
|
100
|
-
page$,
|
|
101
|
-
pages$,
|
|
102
|
-
nextDisabled$,
|
|
103
|
-
previousDisabled$,
|
|
104
|
-
pagesLabel$,
|
|
105
|
-
...stateProps,
|
|
106
|
-
}),
|
|
107
|
-
patch,
|
|
108
|
-
actions: {
|
|
109
|
-
/**
|
|
110
|
-
* Set the current page pageNumber (starting from 1)
|
|
111
|
-
* @param pageNumber - Current page number to set.
|
|
112
|
-
* Value is normalized between 1 and the number of page
|
|
113
|
-
*/
|
|
114
|
-
select(pageNumber) {
|
|
115
|
-
patch({ page: pageNumber });
|
|
116
|
-
},
|
|
117
|
-
/**
|
|
118
|
-
* Select the first page
|
|
119
|
-
*/
|
|
120
|
-
first() {
|
|
121
|
-
patch({ page: 1 });
|
|
122
|
-
},
|
|
123
|
-
/**
|
|
124
|
-
* Select the previous page
|
|
125
|
-
*/
|
|
126
|
-
previous() {
|
|
127
|
-
patch({ page: page$() - 1 });
|
|
128
|
-
},
|
|
129
|
-
/**
|
|
130
|
-
* Select the next page
|
|
131
|
-
*/
|
|
132
|
-
next() {
|
|
133
|
-
patch({ page: page$() + 1 });
|
|
134
|
-
},
|
|
135
|
-
/**
|
|
136
|
-
* Select the last page
|
|
137
|
-
*/
|
|
138
|
-
last() {
|
|
139
|
-
patch({ page: pageCount$() });
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
api: {
|
|
143
|
-
isEllipsis(pageNumber) {
|
|
144
|
-
return pageNumber === -1;
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
directives: {},
|
|
148
|
-
};
|
|
149
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { clamp } from '../../utils/internal/checks';
|
|
2
|
-
import { typeBoolean, typeFunction, typeNumber, typeString } from '../../utils/writables';
|
|
3
|
-
import { stateStores, writablesForProps } from '../../utils/stores';
|
|
4
|
-
import { computed } from '@amadeus-it-group/tansu';
|
|
5
|
-
const defaultConfig = {
|
|
6
|
-
min: 0,
|
|
7
|
-
max: 100,
|
|
8
|
-
value: 0,
|
|
9
|
-
ariaLabel: 'Progressbar',
|
|
10
|
-
className: '',
|
|
11
|
-
slotContent: undefined,
|
|
12
|
-
slotDefault: undefined,
|
|
13
|
-
height: '',
|
|
14
|
-
striped: false,
|
|
15
|
-
animated: false,
|
|
16
|
-
ariaValueTextFn: () => undefined,
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Retrieve a shallow copy of the default Progressbar config
|
|
20
|
-
* @returns the default Progressbar config
|
|
21
|
-
*/
|
|
22
|
-
export function getProgressbarDefaultConfig() {
|
|
23
|
-
return { ...defaultConfig };
|
|
24
|
-
}
|
|
25
|
-
const configValidator = {
|
|
26
|
-
min: typeNumber,
|
|
27
|
-
max: typeNumber,
|
|
28
|
-
value: typeNumber,
|
|
29
|
-
ariaLabel: typeString,
|
|
30
|
-
className: typeString,
|
|
31
|
-
height: typeString,
|
|
32
|
-
striped: typeBoolean,
|
|
33
|
-
animated: typeBoolean,
|
|
34
|
-
ariaValueTextFn: typeFunction,
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* Create an ProgressbarWidget with given config props
|
|
38
|
-
* @param config - an optional progress bar config
|
|
39
|
-
* @returns an ProgressbarWidget
|
|
40
|
-
*/
|
|
41
|
-
export function createProgressbar(config) {
|
|
42
|
-
const [{
|
|
43
|
-
// dirty inputs that need adjustment:
|
|
44
|
-
max$: _dirtyMaximum$, value$: _dirtyValue$,
|
|
45
|
-
// clean inputs
|
|
46
|
-
min$, ariaValueTextFn$, ...stateProps }, patch,] = writablesForProps(defaultConfig, config, configValidator);
|
|
47
|
-
const max$ = computed(() => Math.max(min$(), _dirtyMaximum$()));
|
|
48
|
-
const value$ = computed(() => clamp(_dirtyValue$(), max$(), min$()));
|
|
49
|
-
const percentage$ = computed(() => {
|
|
50
|
-
const max = max$();
|
|
51
|
-
const min = min$();
|
|
52
|
-
if (max > min) {
|
|
53
|
-
return clamp(((value$() - min) * 100) / (max - min), 100, 0);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
return 0;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
const started$ = computed(() => value$() > min$());
|
|
60
|
-
const finished$ = computed(() => value$() === max$());
|
|
61
|
-
const ariaValueText$ = computed(() => ariaValueTextFn$()(value$(), min$(), max$()));
|
|
62
|
-
return {
|
|
63
|
-
...stateStores({
|
|
64
|
-
min$,
|
|
65
|
-
max$,
|
|
66
|
-
value$,
|
|
67
|
-
percentage$,
|
|
68
|
-
started$,
|
|
69
|
-
finished$,
|
|
70
|
-
ariaValueText$,
|
|
71
|
-
...stateProps,
|
|
72
|
-
}),
|
|
73
|
-
patch,
|
|
74
|
-
api: {},
|
|
75
|
-
directives: {},
|
|
76
|
-
actions: {},
|
|
77
|
-
};
|
|
78
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { computed, writable } from '@amadeus-it-group/tansu';
|
|
2
|
-
import { INVALID_VALUE } from '../../types';
|
|
3
|
-
import { bindableDerived, stateStores, writablesForProps } from '../../utils/stores';
|
|
4
|
-
import { clamp, isNumber } from '../../utils/internal/checks';
|
|
5
|
-
import { typeBoolean, typeFunction, typeNumber, typeString } from '../../utils/writables';
|
|
6
|
-
import { noop } from '../../utils/internal/func';
|
|
7
|
-
const defaultConfig = {
|
|
8
|
-
rating: 0,
|
|
9
|
-
tabindex: 0,
|
|
10
|
-
maxRating: 10,
|
|
11
|
-
disabled: false,
|
|
12
|
-
readonly: false,
|
|
13
|
-
resettable: true,
|
|
14
|
-
ariaValueTextFn: (rating, maxRating) => `${rating} out of ${maxRating}`,
|
|
15
|
-
onHover: noop,
|
|
16
|
-
onLeave: noop,
|
|
17
|
-
onRatingChange: noop,
|
|
18
|
-
className: '',
|
|
19
|
-
slotStar: ({ fill }) => String.fromCharCode(fill === 100 ? 9733 : 9734),
|
|
20
|
-
ariaLabel: 'Rating',
|
|
21
|
-
ariaLabelledBy: '',
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Returns a shallow copy of the default rating config.
|
|
25
|
-
* @returns a copy of the default config
|
|
26
|
-
*/
|
|
27
|
-
export function getRatingDefaultConfig() {
|
|
28
|
-
return { ...defaultConfig };
|
|
29
|
-
}
|
|
30
|
-
// TODO export normalize function in utils and test them.
|
|
31
|
-
const configValidator = {
|
|
32
|
-
rating: typeNumber,
|
|
33
|
-
tabindex: typeNumber,
|
|
34
|
-
maxRating: { normalizeValue: (value) => (isNumber(value) ? Math.max(0, value) : INVALID_VALUE) },
|
|
35
|
-
disabled: typeBoolean,
|
|
36
|
-
readonly: typeBoolean,
|
|
37
|
-
resettable: typeBoolean,
|
|
38
|
-
ariaValueTextFn: typeFunction,
|
|
39
|
-
onHover: typeFunction,
|
|
40
|
-
onLeave: typeFunction,
|
|
41
|
-
onRatingChange: typeFunction,
|
|
42
|
-
className: typeString,
|
|
43
|
-
ariaLabel: typeString,
|
|
44
|
-
ariaLabelledBy: typeString,
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Create a RatingWidget with given config props
|
|
48
|
-
* @param config - an optional alert config
|
|
49
|
-
* @returns a RatingWidget
|
|
50
|
-
*/
|
|
51
|
-
export function createRating(config) {
|
|
52
|
-
const [{
|
|
53
|
-
// dirty inputs that need adjustment:
|
|
54
|
-
rating$: _dirtyRating$, tabindex$: _dirtyTabindex$,
|
|
55
|
-
// clean inputs with value validation:
|
|
56
|
-
ariaValueTextFn$, onHover$, onLeave$, onRatingChange$, ...stateProps }, patch,] = writablesForProps(defaultConfig, config, configValidator);
|
|
57
|
-
const { maxRating$, disabled$, readonly$, resettable$ } = stateProps;
|
|
58
|
-
// clean inputs adjustment to valid range
|
|
59
|
-
const tabindex$ = computed(() => (disabled$() ? -1 : _dirtyTabindex$()));
|
|
60
|
-
const rating$ = bindableDerived(onRatingChange$, [_dirtyRating$, maxRating$], ([dirtyRating, maxRating]) => clamp(dirtyRating, maxRating));
|
|
61
|
-
// internal inputs
|
|
62
|
-
const _hoveredRating$ = writable(0);
|
|
63
|
-
// computed
|
|
64
|
-
const isInteractive$ = computed(() => !disabled$() && !readonly$());
|
|
65
|
-
const visibleRating$ = computed(() => {
|
|
66
|
-
const rating = rating$(); // call rating unconditionnally (for the bindableDerived to stay active)
|
|
67
|
-
const hoveredRating = _hoveredRating$();
|
|
68
|
-
return hoveredRating !== 0 ? hoveredRating : rating;
|
|
69
|
-
});
|
|
70
|
-
const ariaValueText$ = computed(() => ariaValueTextFn$()(visibleRating$(), maxRating$()));
|
|
71
|
-
const stars$ = computed(() => {
|
|
72
|
-
const visibleRating = visibleRating$();
|
|
73
|
-
return Array.from({ length: maxRating$() }, (_v, i) => ({
|
|
74
|
-
fill: Math.round(Math.max(Math.min(visibleRating - i, 1), 0) * 100),
|
|
75
|
-
index: i,
|
|
76
|
-
}));
|
|
77
|
-
});
|
|
78
|
-
return {
|
|
79
|
-
...stateStores({
|
|
80
|
-
ariaValueText$,
|
|
81
|
-
isInteractive$,
|
|
82
|
-
rating$,
|
|
83
|
-
stars$,
|
|
84
|
-
tabindex$,
|
|
85
|
-
visibleRating$,
|
|
86
|
-
...stateProps,
|
|
87
|
-
}),
|
|
88
|
-
patch,
|
|
89
|
-
actions: {
|
|
90
|
-
click: (index) => {
|
|
91
|
-
if (isInteractive$() && index > 0 && index <= maxRating$()) {
|
|
92
|
-
patch({ rating: rating$() === index && resettable$() ? 0 : index });
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
hover: (index) => {
|
|
96
|
-
if (isInteractive$() && index > 0 && index <= maxRating$()) {
|
|
97
|
-
_hoveredRating$.set(index);
|
|
98
|
-
onHover$()(index);
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
leave: () => {
|
|
102
|
-
if (isInteractive$()) {
|
|
103
|
-
onLeave$()(_hoveredRating$());
|
|
104
|
-
_hoveredRating$.set(0);
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
handleKey(event) {
|
|
108
|
-
if (isInteractive$()) {
|
|
109
|
-
const { key } = event;
|
|
110
|
-
switch (key) {
|
|
111
|
-
case 'ArrowLeft':
|
|
112
|
-
case 'ArrowDown':
|
|
113
|
-
patch({ rating: rating$() - 1 });
|
|
114
|
-
break;
|
|
115
|
-
case 'ArrowRight':
|
|
116
|
-
case 'ArrowUp':
|
|
117
|
-
patch({ rating: rating$() + 1 });
|
|
118
|
-
break;
|
|
119
|
-
case 'Home':
|
|
120
|
-
case 'PageDown':
|
|
121
|
-
patch({ rating: 0 });
|
|
122
|
-
break;
|
|
123
|
-
case 'End':
|
|
124
|
-
case 'PageUp':
|
|
125
|
-
patch({ rating: maxRating$() });
|
|
126
|
-
break;
|
|
127
|
-
default:
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
event.preventDefault();
|
|
131
|
-
event.stopPropagation();
|
|
132
|
-
}
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
directives: {},
|
|
136
|
-
api: {},
|
|
137
|
-
};
|
|
138
|
-
}
|