@digitransit-component/digitransit-component-favourite-bar 5.0.9 → 6.0.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/LICENSE-AGPL.txt +661 -0
- package/LICENSE-EUPL.txt +291 -0
- package/lib/index.cjs +1 -1
- package/lib/index.cjs.map +1 -1
- package/lib/index.development.cjs +619 -2
- package/lib/index.js +610 -0
- package/lib/index.js.map +1 -0
- package/package.json +20 -9
package/lib/index.js
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
import PropTypes from 'prop-types';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { withTranslation, useTranslation, I18nextProvider } from 'react-i18next';
|
|
4
|
+
import cx from 'classnames';
|
|
5
|
+
import differenceWith from 'lodash/differenceWith.js';
|
|
6
|
+
import isEqual from 'lodash/isEqual.js';
|
|
7
|
+
import isEmpty from 'lodash/isEmpty.js';
|
|
8
|
+
import Shimmer from '@hsl-fi/shimmer';
|
|
9
|
+
import SuggestionItem from '@digitransit-component/digitransit-component-suggestion-item';
|
|
10
|
+
import Icon, { defaultColors } from '@digitransit-component/digitransit-component-icon';
|
|
11
|
+
import 'lodash/uniqWith.js';
|
|
12
|
+
import memoize from 'lodash/memoize.js';
|
|
13
|
+
import escapeRegExp from 'lodash/escapeRegExp.js';
|
|
14
|
+
import cloneDeep from 'lodash/cloneDeep.js';
|
|
15
|
+
import { createInstance } from 'i18next';
|
|
16
|
+
|
|
17
|
+
function _typeof(o) {
|
|
18
|
+
"@babel/helpers - typeof";
|
|
19
|
+
|
|
20
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
|
|
21
|
+
return typeof o;
|
|
22
|
+
} : function (o) {
|
|
23
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
24
|
+
}, _typeof(o);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function toPrimitive(t, r) {
|
|
28
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
29
|
+
var e = t[Symbol.toPrimitive];
|
|
30
|
+
if (void 0 !== e) {
|
|
31
|
+
var i = e.call(t, r);
|
|
32
|
+
if ("object" != _typeof(i)) return i;
|
|
33
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
34
|
+
}
|
|
35
|
+
return ("string" === r ? String : Number)(t);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toPropertyKey(t) {
|
|
39
|
+
var i = toPrimitive(t, "string");
|
|
40
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function _defineProperty(e, r, t) {
|
|
44
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
45
|
+
value: t,
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true
|
|
49
|
+
}) : e[r] = t, e;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Location properties.
|
|
54
|
+
* @typedef {LocationProperties}
|
|
55
|
+
* @property {string} layer Suggestion layer.
|
|
56
|
+
* @property {lat} [number]
|
|
57
|
+
* @property {lon} [number]
|
|
58
|
+
* @property {string} address Address.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Returns localadmin (city name) for suggestions
|
|
63
|
+
*
|
|
64
|
+
* @name getLocaladmin
|
|
65
|
+
* @param {Object} suggestion suggestion's properties from geocoding or a favourite stop/station.
|
|
66
|
+
* Expects last part of an address (after ',') to contain the city name of the suggestion location.
|
|
67
|
+
* @returns {String} City name or empty string
|
|
68
|
+
*/
|
|
69
|
+
const getLocaladmin = suggestion => suggestion.localadmin || suggestion.localadmin || suggestion.address && suggestion.address.lastIndexOf(',') < suggestion.address.length - 2 && suggestion.address.substring(suggestion.address.lastIndexOf(',') + 2) || '';
|
|
70
|
+
const getStopCode = ({
|
|
71
|
+
id,
|
|
72
|
+
code
|
|
73
|
+
}) => {
|
|
74
|
+
if (code) {
|
|
75
|
+
return code;
|
|
76
|
+
}
|
|
77
|
+
if (id === undefined || typeof id.indexOf === 'undefined' || id.indexOf('#') === -1) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
// id from pelias
|
|
81
|
+
return id.substring(id.indexOf('#') + 1);
|
|
82
|
+
};
|
|
83
|
+
const formatFavouritePlaceLabel = (name, address) => [name || address && address.split(',')[0], typeof address === 'string' ? address.replace(new RegExp(`${escapeRegExp(name)}([ ,]|$)+`), '') : ''];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Format suggestion-pair [name, address] by `suggestion.layer`.
|
|
87
|
+
*
|
|
88
|
+
* @name getNameLabel
|
|
89
|
+
* @param {LocationProperties} suggestion
|
|
90
|
+
* @param {boolean} [plain]
|
|
91
|
+
*
|
|
92
|
+
* @returns {Array.<string>} Formatted label.
|
|
93
|
+
*/
|
|
94
|
+
memoize((suggestion, plain = false) => {
|
|
95
|
+
switch (suggestion.layer) {
|
|
96
|
+
case 'currentPosition':
|
|
97
|
+
return [suggestion.labelId, suggestion.address];
|
|
98
|
+
case 'selectFromMap':
|
|
99
|
+
case 'ownLocations':
|
|
100
|
+
case 'back':
|
|
101
|
+
return [suggestion.labelId];
|
|
102
|
+
case 'favouritePlace':
|
|
103
|
+
return formatFavouritePlaceLabel(suggestion.name, suggestion.address);
|
|
104
|
+
case 'favouriteVehicleRentalStation':
|
|
105
|
+
case 'bikestation':
|
|
106
|
+
return [suggestion.name];
|
|
107
|
+
case 'favouriteRoute':
|
|
108
|
+
case 'route-BUS':
|
|
109
|
+
case 'route-TRAM':
|
|
110
|
+
case 'route-RAIL':
|
|
111
|
+
case 'route-SUBWAY':
|
|
112
|
+
case 'route-FERRY':
|
|
113
|
+
case 'route-FUNICULAR':
|
|
114
|
+
case 'route-AIRPLANE':
|
|
115
|
+
return !plain && suggestion.shortName ? [suggestion.gtfsId, suggestion.mode.toLowerCase(), suggestion.longName] : [suggestion.shortName, suggestion.longName, suggestion.agency ? suggestion.agency.name : undefined];
|
|
116
|
+
case 'venue':
|
|
117
|
+
case 'address':
|
|
118
|
+
case 'street':
|
|
119
|
+
return [suggestion.name, suggestion.label.replace(new RegExp(`${escapeRegExp(suggestion.name)}(,)?( )?`), '')];
|
|
120
|
+
case 'favouriteStop':
|
|
121
|
+
case 'stop':
|
|
122
|
+
return plain ? [suggestion.name || suggestion.label || suggestion.address && suggestion.address.split(',')[0], getLocaladmin(suggestion)] : [suggestion.name, suggestion.id, getStopCode(suggestion), getLocaladmin(suggestion)];
|
|
123
|
+
case 'favouriteStation':
|
|
124
|
+
case 'station':
|
|
125
|
+
default:
|
|
126
|
+
return [suggestion.name || suggestion.label || suggestion.address && suggestion.address.split(',')[0], getLocaladmin(suggestion)];
|
|
127
|
+
}
|
|
128
|
+
}, (item, plain) => {
|
|
129
|
+
const i = cloneDeep(item);
|
|
130
|
+
i.plain = plain;
|
|
131
|
+
return i;
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
function styleInject(css, ref) {
|
|
135
|
+
if ( ref === void 0 ) ref = {};
|
|
136
|
+
var insertAt = ref.insertAt;
|
|
137
|
+
|
|
138
|
+
if (typeof document === 'undefined') { return; }
|
|
139
|
+
|
|
140
|
+
var head = document.head || document.getElementsByTagName('head')[0];
|
|
141
|
+
var style = document.createElement('style');
|
|
142
|
+
style.type = 'text/css';
|
|
143
|
+
|
|
144
|
+
if (insertAt === 'top') {
|
|
145
|
+
if (head.firstChild) {
|
|
146
|
+
head.insertBefore(style, head.firstChild);
|
|
147
|
+
} else {
|
|
148
|
+
head.appendChild(style);
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
head.appendChild(style);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (style.styleSheet) {
|
|
155
|
+
style.styleSheet.cssText = css;
|
|
156
|
+
} else {
|
|
157
|
+
style.appendChild(document.createTextNode(css));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
var css_248z = ".styles_button-shared__RSwxE, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 {\n background-color: white;\n display: flex;\n flex-direction: row;\n justify-content: center;\n align-items: center;\n border-radius: 5px;\n border: 1px solid #ddd;\n cursor: pointer;\n}\n.styles_button-shared__RSwxE:hover, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf:hover, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1:hover {\n box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);\n}\n\n.styles_shimmer-shared__MGBAa, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 > div {\n display: flex;\n align-items: center;\n overflow: hidden;\n}\n.styles_shimmer-shared__MGBAa .styles_shimmer__bIP9J, .styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div .styles_shimmer__bIP9J, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 > div .styles_shimmer__bIP9J {\n border-radius: 5px;\n}\n\n.styles_favourite-container__WTJm3 {\n display: flex;\n position: relative;\n z-index: 1;\n width: 100%;\n height: 50px;\n flex-shrink: 1;\n flex-direction: row;\n justify-content: space-between;\n align-items: center;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 {\n width: auto;\n min-width: 0;\n flex: 1;\n flex-grow: 1;\n height: 100%;\n position: relative;\n padding: 0;\n margin-right: 8px;\n overflow: hidden;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD {\n display: flex;\n width: 50px;\n height: 100%;\n justify-content: center;\n align-items: center;\n flex-shrink: 1;\n}\n@media screen and (min-width: 375px) {\n .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD {\n flex-shrink: 0;\n }\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD svg {\n width: 20px;\n height: 20px;\n position: relative;\n left: 2px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_place__4mHYj svg {\n width: 24px;\n height: 24px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_home__msJXd svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_school__Hta8J svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_sport__YFDd0 svg, .styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_shopping__jMKed svg {\n width: 18px;\n height: 18px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_icon__mAwUD.styles_work__uZX4R svg {\n width: 20px;\n height: 20px;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV {\n white-space: nowrap;\n overflow: hidden;\n text-align: start;\n width: 100%;\n flex-direction: row;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_name__Fvsvr {\n font-family: inherit;\n font-size: 15px;\n margin-right: 6px;\n font-weight: var(--font-weight-medium);\n font-stretch: normal;\n font-style: normal;\n letter-spacing: -0.6px;\n color: #333;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_name__Fvsvr:empty {\n margin-top: 0;\n}\n.styles_favourite-container__WTJm3 .styles_favourite-content__6gqt1 .styles_favourite-location__RiyEV .styles_address__iBEhu {\n font-family: inherit;\n font-size: 12px;\n margin-right: 6px;\n font-weight: normal;\n font-stretch: normal;\n font-style: normal;\n line-height: 1.67;\n letter-spacing: -0.48px;\n color: #666;\n white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf {\n height: 100%;\n width: 42px;\n flex-shrink: 0;\n overflow: hidden;\n padding: 0;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf > div {\n justify-content: center;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_plus__ryt4n svg {\n width: 16px;\n height: 16px;\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_arrow__EbmBZ svg {\n width: 19px;\n height: 19px;\n transform: rotate(90deg);\n}\n.styles_favourite-container__WTJm3 .styles_expandButton__mkemf.styles_arrow__EbmBZ.styles_rotate__Nw2-A svg {\n transform: rotate(-90deg);\n}\n\n.styles_favourite-suggestion-container__MVZBj {\n position: relative;\n display: flex;\n justify-content: center;\n padding-top: 9px;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_divider__podkI {\n display: block;\n height: 6px;\n background-color: #f2f5f7;\n border-top: 1px solid rgba(0, 0, 0, 0.1);\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-list__boIJY {\n max-height: 30vh;\n list-style-type: none;\n padding: 0;\n position: absolute;\n z-index: 20;\n width: 100%;\n margin: 0 auto;\n border-radius: 5px;\n box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.16);\n border: solid 1px rgba(0, 0, 0, 0.1);\n background-color: #fff;\n overflow-x: hidden;\n overflow-y: scroll;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-item__ptExh.styles_custom__87c-n {\n font-size: 15px;\n font-weight: var(--font-weight-medium);\n letter-spacing: -0.5;\n}\n.styles_favourite-suggestion-container__MVZBj .styles_favourite-suggestion-item__ptExh:hover {\n background-color: #f2f5f7;\n}";
|
|
162
|
+
var styles = {"button-shared":"styles_button-shared__RSwxE","favourite-container":"styles_favourite-container__WTJm3","expandButton":"styles_expandButton__mkemf","favourite-content":"styles_favourite-content__6gqt1","shimmer-shared":"styles_shimmer-shared__MGBAa","shimmer":"styles_shimmer__bIP9J","icon":"styles_icon__mAwUD","place":"styles_place__4mHYj","home":"styles_home__msJXd","school":"styles_school__Hta8J","sport":"styles_sport__YFDd0","shopping":"styles_shopping__jMKed","work":"styles_work__uZX4R","favourite-location":"styles_favourite-location__RiyEV","name":"styles_name__Fvsvr","address":"styles_address__iBEhu","plus":"styles_plus__ryt4n","arrow":"styles_arrow__EbmBZ","rotate":"styles_rotate__Nw2-A","favourite-suggestion-container":"styles_favourite-suggestion-container__MVZBj","divider":"styles_divider__podkI","favourite-suggestion-list":"styles_favourite-suggestion-list__boIJY","favourite-suggestion-item":"styles_favourite-suggestion-item__ptExh","custom":"styles_custom__87c-n"};
|
|
163
|
+
styleInject(css_248z);
|
|
164
|
+
|
|
165
|
+
/* eslint sort-keys: "error" */
|
|
166
|
+
const translations = {
|
|
167
|
+
de: {
|
|
168
|
+
translation: {
|
|
169
|
+
'add-favourite': 'Füge Favorit hinzu',
|
|
170
|
+
'add-home': 'Setze Zuhause',
|
|
171
|
+
'add-place': 'Setze Ort',
|
|
172
|
+
'add-work': 'Setze Arbeitsort',
|
|
173
|
+
edit: 'Bearbeiten',
|
|
174
|
+
home: 'Zuhause',
|
|
175
|
+
'open-favourites': 'Öffne Favoriten',
|
|
176
|
+
work: 'Arbeit'
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
en: {
|
|
180
|
+
translation: {
|
|
181
|
+
'add-destination': 'Set as destination',
|
|
182
|
+
'add-favourite': 'Add favourite',
|
|
183
|
+
'add-home': 'Add home',
|
|
184
|
+
'add-place': 'Add place',
|
|
185
|
+
'add-work': 'Add work',
|
|
186
|
+
edit: 'Edit',
|
|
187
|
+
'favourites-list': 'Favourites list',
|
|
188
|
+
home: 'Home',
|
|
189
|
+
'open-favourites': 'Open favourites',
|
|
190
|
+
work: 'Work'
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
fi: {
|
|
194
|
+
translation: {
|
|
195
|
+
'add-destination': 'Aseta määränpääksi',
|
|
196
|
+
'add-favourite': 'Lisää suosikki',
|
|
197
|
+
'add-home': 'Lisää koti',
|
|
198
|
+
'add-place': 'Lisää paikka',
|
|
199
|
+
'add-work': 'Lisää työ',
|
|
200
|
+
edit: 'Muokkaa',
|
|
201
|
+
'favourites-list': 'Suosikit lista',
|
|
202
|
+
home: 'Koti',
|
|
203
|
+
'open-favourites': 'Avaa suosikit',
|
|
204
|
+
work: 'Työ'
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
pl: {
|
|
208
|
+
translation: {
|
|
209
|
+
'add-destination': 'Wybierz jako punkt docelowy',
|
|
210
|
+
'add-favourite': 'Dodaj ulubione',
|
|
211
|
+
'add-home': 'Dodaj dom',
|
|
212
|
+
'add-place': 'Dodaj miejsce',
|
|
213
|
+
'add-work': 'Dodaj pracę',
|
|
214
|
+
edit: 'Edytuj',
|
|
215
|
+
'favourites-list': 'Lista ulubionych',
|
|
216
|
+
home: 'Dom',
|
|
217
|
+
'open-favourites': 'Pokaż ulubione',
|
|
218
|
+
work: 'Praca'
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
sv: {
|
|
222
|
+
translation: {
|
|
223
|
+
'add-destination': 'Ställa in som destination',
|
|
224
|
+
'add-favourite': 'Lägg till favorit',
|
|
225
|
+
'add-home': 'Lägg till hem',
|
|
226
|
+
'add-place': 'Lägg till plats',
|
|
227
|
+
'add-work': 'Lägg till arbete',
|
|
228
|
+
edit: 'Redigera',
|
|
229
|
+
'favourites-list': 'Favoritlista',
|
|
230
|
+
home: 'Hem',
|
|
231
|
+
'open-favourites': 'Öppna favoriter',
|
|
232
|
+
work: 'Arbete'
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const i18n = createInstance({
|
|
238
|
+
fallbackLng: 'fi',
|
|
239
|
+
// debug: true,
|
|
240
|
+
resources: translations,
|
|
241
|
+
defaultNS: 'translation',
|
|
242
|
+
react: {
|
|
243
|
+
useSuspense: true
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
i18n.init();
|
|
247
|
+
|
|
248
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
249
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
250
|
+
const isKeyboardSelectionEvent = event => {
|
|
251
|
+
const space = [13, ' ', 'Spacebar'];
|
|
252
|
+
const enter = [32, 'Enter'];
|
|
253
|
+
const key = event && (event.key || event.which || event.keyCode) || '';
|
|
254
|
+
if (!key || !space.concat(enter).includes(key)) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
event.preventDefault();
|
|
258
|
+
return true;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Utility to format location [name, address] pair.
|
|
263
|
+
* @param {LocationProperties} favourite
|
|
264
|
+
* @returns {Array.<string>}
|
|
265
|
+
*/
|
|
266
|
+
const formatFavourite = favourite => favourite && (favourite.name || favourite.address) ? formatFavouritePlaceLabel(favourite.name, favourite.address) : [];
|
|
267
|
+
const FavouriteLocation = ({
|
|
268
|
+
clickItem,
|
|
269
|
+
iconId,
|
|
270
|
+
text,
|
|
271
|
+
label,
|
|
272
|
+
isLoading,
|
|
273
|
+
color,
|
|
274
|
+
lang
|
|
275
|
+
}) => {
|
|
276
|
+
const [t] = useTranslation();
|
|
277
|
+
const ariaLabel = label === '' ? text : `${text} ${label} ${t('add-destination', {
|
|
278
|
+
lng: lang
|
|
279
|
+
})}`;
|
|
280
|
+
return /*#__PURE__*/React.createElement("button", {
|
|
281
|
+
type: "button",
|
|
282
|
+
tabIndex: "0",
|
|
283
|
+
className: cx(styles['favourite-content']),
|
|
284
|
+
onClick: clickItem,
|
|
285
|
+
onKeyDown: e => isKeyboardSelectionEvent(e) && clickItem(),
|
|
286
|
+
"aria-label": ariaLabel
|
|
287
|
+
}, /*#__PURE__*/React.createElement(Shimmer, {
|
|
288
|
+
active: isLoading,
|
|
289
|
+
className: styles.shimmer
|
|
290
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
291
|
+
className: cx(styles.icon, styles[iconId])
|
|
292
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
293
|
+
img: iconId,
|
|
294
|
+
color: color
|
|
295
|
+
})), /*#__PURE__*/React.createElement("div", {
|
|
296
|
+
className: styles['favourite-location']
|
|
297
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
298
|
+
className: styles.name
|
|
299
|
+
}, text), /*#__PURE__*/React.createElement("div", {
|
|
300
|
+
className: styles.address
|
|
301
|
+
}, label))));
|
|
302
|
+
};
|
|
303
|
+
process.env.NODE_ENV !== "production" ? FavouriteLocation.propTypes = {
|
|
304
|
+
clickItem: PropTypes.func.isRequired,
|
|
305
|
+
iconId: PropTypes.string.isRequired,
|
|
306
|
+
text: PropTypes.string.isRequired,
|
|
307
|
+
label: PropTypes.string.isRequired,
|
|
308
|
+
isLoading: PropTypes.bool,
|
|
309
|
+
color: PropTypes.string.isRequired,
|
|
310
|
+
lang: PropTypes.string.isRequired
|
|
311
|
+
} : void 0;
|
|
312
|
+
FavouriteLocation.defaultProps = {
|
|
313
|
+
isLoading: false
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* FavouriteBar renders favourites. FavouriteBar displays the first two favourites, the rest are shown in a list.
|
|
318
|
+
* @example
|
|
319
|
+
* <FavouriteBar
|
|
320
|
+
* favourites={favourites}
|
|
321
|
+
* onClickFavourite={onClickFavourite}
|
|
322
|
+
* onAddPlace={this.addPlace}
|
|
323
|
+
* onAddHome={this.addHome}
|
|
324
|
+
* onAddWork={this.addWork}
|
|
325
|
+
* lang={this.props.lang}
|
|
326
|
+
* isLoading={this.props.isLoading}
|
|
327
|
+
* />
|
|
328
|
+
*/
|
|
329
|
+
class FavouriteBar extends React.Component {
|
|
330
|
+
constructor(props) {
|
|
331
|
+
super(props);
|
|
332
|
+
_defineProperty(this, "toggleList", () => {
|
|
333
|
+
const eventDiff = new Date().getTime() - this.state.timestamp;
|
|
334
|
+
if (eventDiff > 200) {
|
|
335
|
+
this.setState(prevState => ({
|
|
336
|
+
listOpen: !prevState.listOpen,
|
|
337
|
+
timestamp: new Date().getTime()
|
|
338
|
+
}), () => {
|
|
339
|
+
if (this.state.listOpen) {
|
|
340
|
+
this.firstItemRef.current?.focus();
|
|
341
|
+
} else {
|
|
342
|
+
this.expandListRef.current?.focus();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
_defineProperty(this, "translate", id => {
|
|
348
|
+
return this.props.t(id, {
|
|
349
|
+
lng: this.props.lang
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
_defineProperty(this, "handleClickOutside", event => {
|
|
353
|
+
if (this.suggestionListRef.current && this.expandListRef.current && !this.suggestionListRef.current.contains(event.target) && !this.expandListRef.current.contains(event.target)) {
|
|
354
|
+
this.setState({
|
|
355
|
+
listOpen: false
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
_defineProperty(this, "suggestionSelected", index => {
|
|
360
|
+
const {
|
|
361
|
+
favourites
|
|
362
|
+
} = this.state;
|
|
363
|
+
if (index < favourites.length) {
|
|
364
|
+
this.props.onClickFavourite(favourites[index]);
|
|
365
|
+
} else if (index === favourites.length) {
|
|
366
|
+
this.props.onAddPlace();
|
|
367
|
+
} else if (index === favourites.length + 1) {
|
|
368
|
+
this.props.onEdit();
|
|
369
|
+
}
|
|
370
|
+
this.toggleList();
|
|
371
|
+
});
|
|
372
|
+
_defineProperty(this, "handleKeyDown", (event, index) => {
|
|
373
|
+
const {
|
|
374
|
+
listOpen,
|
|
375
|
+
favourites
|
|
376
|
+
} = this.state;
|
|
377
|
+
const key = event && (event.key || event.which || event.keyCode) || '';
|
|
378
|
+
if (isKeyboardSelectionEvent(event)) {
|
|
379
|
+
if (!listOpen) {
|
|
380
|
+
this.toggleList();
|
|
381
|
+
} else {
|
|
382
|
+
this.suggestionSelected(index);
|
|
383
|
+
}
|
|
384
|
+
} else if (key === 'Escape' || key === 27) {
|
|
385
|
+
if (listOpen) {
|
|
386
|
+
this.toggleList();
|
|
387
|
+
}
|
|
388
|
+
} else if (key === 'Tab' && !event.shiftKey && index === favourites.length + this.getCustomSuggestions().length - 1) {
|
|
389
|
+
this.setState({
|
|
390
|
+
listOpen: false
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
_defineProperty(this, "renderSuggestion", (item, index, ariaLabelSuffix = '') => {
|
|
395
|
+
const id = `favourite-suggestion-list--item-${index}`;
|
|
396
|
+
return /*#__PURE__*/React.createElement("li", {
|
|
397
|
+
key: `favourite-suggestion-item-${index}`
|
|
398
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
399
|
+
role: "button",
|
|
400
|
+
type: "button",
|
|
401
|
+
tabIndex: "0",
|
|
402
|
+
id: id,
|
|
403
|
+
className: cx(styles['favourite-suggestion-item']),
|
|
404
|
+
onClick: () => this.suggestionSelected(index),
|
|
405
|
+
onKeyDown: e => this.handleKeyDown(e, index),
|
|
406
|
+
ref: index === 0 ? this.firstItemRef : ''
|
|
407
|
+
// eslint-disable-next-line jsx-a11y/no-autofocus
|
|
408
|
+
,
|
|
409
|
+
autoFocus: index === 0,
|
|
410
|
+
"aria-label": `${item?.name || ''} ${item?.address || ''} ${ariaLabelSuffix}`
|
|
411
|
+
}, /*#__PURE__*/React.createElement(SuggestionItem, {
|
|
412
|
+
item: item,
|
|
413
|
+
colors: this.props.colors,
|
|
414
|
+
fontWeights: this.props.fontWeights
|
|
415
|
+
})));
|
|
416
|
+
});
|
|
417
|
+
_defineProperty(this, "getCustomSuggestions", () => {
|
|
418
|
+
const customSuggestions = [{
|
|
419
|
+
name: this.translate('add-place'),
|
|
420
|
+
selectedIconId: 'favourite'
|
|
421
|
+
}];
|
|
422
|
+
if (this.props.favourites.length === 0) {
|
|
423
|
+
return customSuggestions;
|
|
424
|
+
}
|
|
425
|
+
return [...customSuggestions, {
|
|
426
|
+
name: this.translate('edit'),
|
|
427
|
+
selectedIconId: 'edit'
|
|
428
|
+
}];
|
|
429
|
+
});
|
|
430
|
+
this.state = {
|
|
431
|
+
listOpen: false,
|
|
432
|
+
firstFavourite: props.favourites[0] || null,
|
|
433
|
+
secondFavourite: props.favourites[1] || null,
|
|
434
|
+
favourites: props.favourites.slice(2, props.favourites.length),
|
|
435
|
+
timestamp: 0
|
|
436
|
+
};
|
|
437
|
+
this.expandListRef = /*#__PURE__*/React.createRef();
|
|
438
|
+
this.suggestionListRef = /*#__PURE__*/React.createRef();
|
|
439
|
+
this.firstItemRef = /*#__PURE__*/React.createRef();
|
|
440
|
+
}
|
|
441
|
+
componentDidMount() {
|
|
442
|
+
document.addEventListener('mousedown', this.handleClickOutside);
|
|
443
|
+
}
|
|
444
|
+
static getDerivedStateFromProps(nextProps, prevState) {
|
|
445
|
+
const {
|
|
446
|
+
favourites
|
|
447
|
+
} = prevState;
|
|
448
|
+
const nextFavourites = nextProps.favourites;
|
|
449
|
+
if (!isEmpty(differenceWith(nextFavourites, favourites, isEqual)) || !isEmpty(differenceWith(favourites, nextFavourites, isEqual)) || !isEqual(nextFavourites, favourites) || isEmpty(nextFavourites)) {
|
|
450
|
+
return {
|
|
451
|
+
firstFavourite: nextFavourites[0] || null,
|
|
452
|
+
secondFavourite: nextFavourites[1] || null,
|
|
453
|
+
favourites: nextFavourites.slice(2, nextFavourites.length)
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
componentWillUnmount() {
|
|
459
|
+
document.removeEventListener('mousedown', this.handleClickOutside);
|
|
460
|
+
}
|
|
461
|
+
render() {
|
|
462
|
+
const {
|
|
463
|
+
onClickFavourite,
|
|
464
|
+
isLoading,
|
|
465
|
+
fontWeights
|
|
466
|
+
} = this.props;
|
|
467
|
+
const {
|
|
468
|
+
primary
|
|
469
|
+
} = this.props.colors;
|
|
470
|
+
const {
|
|
471
|
+
listOpen,
|
|
472
|
+
favourites,
|
|
473
|
+
firstFavourite,
|
|
474
|
+
secondFavourite
|
|
475
|
+
} = this.state;
|
|
476
|
+
const expandIcon = this.props.favourites.length === 0 ? 'plus' : 'arrow';
|
|
477
|
+
const [name1, address1] = formatFavourite(firstFavourite);
|
|
478
|
+
const [name2, address2] = formatFavourite(secondFavourite);
|
|
479
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
480
|
+
style: {
|
|
481
|
+
'--font-weight-medium': fontWeights.medium
|
|
482
|
+
}
|
|
483
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
484
|
+
className: styles['favourite-container']
|
|
485
|
+
}, /*#__PURE__*/React.createElement(FavouriteLocation, {
|
|
486
|
+
text: name1 || this.translate('add-home'),
|
|
487
|
+
label: address1 || '',
|
|
488
|
+
clickItem: () => firstFavourite ? onClickFavourite(firstFavourite) : this.props.onAddHome(),
|
|
489
|
+
iconId: firstFavourite && firstFavourite.selectedIconId ? FavouriteBar.FavouriteIconIdToNameMap[firstFavourite.selectedIconId] : 'home',
|
|
490
|
+
isLoading: isLoading,
|
|
491
|
+
color: primary,
|
|
492
|
+
lang: this.props.lang
|
|
493
|
+
}), /*#__PURE__*/React.createElement(FavouriteLocation, {
|
|
494
|
+
text: name2 || this.translate('add-work'),
|
|
495
|
+
label: address2 || '',
|
|
496
|
+
clickItem: () => secondFavourite ? onClickFavourite(secondFavourite) : this.props.onAddWork(),
|
|
497
|
+
iconId: secondFavourite && secondFavourite.selectedIconId ? FavouriteBar.FavouriteIconIdToNameMap[secondFavourite.selectedIconId] : 'work',
|
|
498
|
+
isLoading: isLoading,
|
|
499
|
+
color: primary,
|
|
500
|
+
lang: this.props.lang
|
|
501
|
+
}), /*#__PURE__*/React.createElement("button", {
|
|
502
|
+
type: "button",
|
|
503
|
+
className: cx(styles.expandButton, styles[expandIcon], listOpen && styles.rotate),
|
|
504
|
+
ref: this.expandListRef,
|
|
505
|
+
id: "favourite-expand-button",
|
|
506
|
+
onKeyDown: e => this.handleKeyDown(e),
|
|
507
|
+
onClick: () => this.toggleList(),
|
|
508
|
+
"aria-expanded": this.state.listOpen,
|
|
509
|
+
"aria-label": this.translate('open-favourites')
|
|
510
|
+
}, /*#__PURE__*/React.createElement(Shimmer, {
|
|
511
|
+
active: isLoading
|
|
512
|
+
}, /*#__PURE__*/React.createElement(Icon, {
|
|
513
|
+
img: expandIcon,
|
|
514
|
+
color: primary
|
|
515
|
+
})))), /*#__PURE__*/React.createElement("div", {
|
|
516
|
+
className: styles['favourite-suggestion-container']
|
|
517
|
+
}, listOpen && /*#__PURE__*/React.createElement("ul", {
|
|
518
|
+
className: styles['favourite-suggestion-list'],
|
|
519
|
+
id: "favourite-suggestion-list",
|
|
520
|
+
ref: this.suggestionListRef,
|
|
521
|
+
"aria-label": this.translate('favourites-list')
|
|
522
|
+
}, favourites.map((item, index) => {
|
|
523
|
+
const favouriteLabel = formatFavouritePlaceLabel(item.name, item.address);
|
|
524
|
+
const address = favouriteLabel[1];
|
|
525
|
+
return this.renderSuggestion(_objectSpread(_objectSpread({}, item), {}, {
|
|
526
|
+
address
|
|
527
|
+
}), index, `, ${this.translate('add-destination')}`);
|
|
528
|
+
}), favourites.length > 0 && /*#__PURE__*/React.createElement("div", {
|
|
529
|
+
"aria-hidden": true,
|
|
530
|
+
className: styles.divider
|
|
531
|
+
}), this.getCustomSuggestions().map((item, index) => this.renderSuggestion(item, favourites.length + index, undefined)))));
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
_defineProperty(FavouriteBar, "defaultProps", {
|
|
535
|
+
onClickFavourite: () => ({}),
|
|
536
|
+
onAddPlace: () => ({}),
|
|
537
|
+
onEdit: () => ({}),
|
|
538
|
+
onAddHome: () => ({}),
|
|
539
|
+
onAddWork: () => ({}),
|
|
540
|
+
lang: 'fi',
|
|
541
|
+
isLoading: false,
|
|
542
|
+
colors: defaultColors,
|
|
543
|
+
fontWeights: {
|
|
544
|
+
medium: 500
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
_defineProperty(FavouriteBar, "FavouriteIconIdToNameMap", {
|
|
548
|
+
'icon-icon_place': 'place',
|
|
549
|
+
'icon-icon_home': 'home',
|
|
550
|
+
'icon-icon_work': 'work',
|
|
551
|
+
'icon-icon_sport': 'sport',
|
|
552
|
+
'icon-icon_school': 'school',
|
|
553
|
+
'icon-icon_shopping': 'shopping',
|
|
554
|
+
// Map two buggy ids temporarily. This code can be removed at some point.
|
|
555
|
+
icon_home: 'home',
|
|
556
|
+
icon_work: 'work'
|
|
557
|
+
});
|
|
558
|
+
process.env.NODE_ENV !== "production" ? FavouriteBar.propTypes = {
|
|
559
|
+
/** Required. Array of favourites, favourite object contains following properties.
|
|
560
|
+
* @type {Array<object>}
|
|
561
|
+
* @property {string} address
|
|
562
|
+
* @property {string} gtfsId
|
|
563
|
+
* @property {string} gid
|
|
564
|
+
* @property {number} lat
|
|
565
|
+
* @property {number} lon
|
|
566
|
+
* @property {string} name
|
|
567
|
+
* @property {string} selectedIconId
|
|
568
|
+
* @property {string} favouriteId
|
|
569
|
+
*/
|
|
570
|
+
favourites: PropTypes.arrayOf(PropTypes.shape({
|
|
571
|
+
address: PropTypes.string,
|
|
572
|
+
gtfsId: PropTypes.string,
|
|
573
|
+
gid: PropTypes.string,
|
|
574
|
+
lat: PropTypes.number,
|
|
575
|
+
name: PropTypes.string,
|
|
576
|
+
lon: PropTypes.number,
|
|
577
|
+
selectedIconId: PropTypes.string,
|
|
578
|
+
favouriteId: PropTypes.string
|
|
579
|
+
})).isRequired,
|
|
580
|
+
/** Optional. Function for clicking favourites. */
|
|
581
|
+
onClickFavourite: PropTypes.func,
|
|
582
|
+
/** Optional. Function for selecting "Add place" from suggestions. */
|
|
583
|
+
onAddPlace: PropTypes.func,
|
|
584
|
+
/** Optional. Function for selected "Edit" from suggestions. */
|
|
585
|
+
onEdit: PropTypes.func,
|
|
586
|
+
/** Optional. Function for "Add home" button. */
|
|
587
|
+
onAddHome: PropTypes.func,
|
|
588
|
+
/** Optional. Function for "Add work" button. */
|
|
589
|
+
onAddWork: PropTypes.func,
|
|
590
|
+
/** Optional. Language, fi, en or sv. */
|
|
591
|
+
lang: PropTypes.string,
|
|
592
|
+
/** Optional. Whether to show loading animation, true or false. */
|
|
593
|
+
isLoading: PropTypes.bool,
|
|
594
|
+
/** Optional color palette */
|
|
595
|
+
colors: PropTypes.objectOf(PropTypes.string),
|
|
596
|
+
/** Optional. */
|
|
597
|
+
fontWeights: PropTypes.shape({
|
|
598
|
+
/** Default value is 500. */
|
|
599
|
+
medium: PropTypes.number
|
|
600
|
+
}),
|
|
601
|
+
/** Translation function */
|
|
602
|
+
t: PropTypes.func.isRequired
|
|
603
|
+
} : void 0;
|
|
604
|
+
const FavouriteBarWithTranslation = withTranslation()(FavouriteBar);
|
|
605
|
+
var index = props => /*#__PURE__*/React.createElement(I18nextProvider, {
|
|
606
|
+
i18n: i18n
|
|
607
|
+
}, /*#__PURE__*/React.createElement(FavouriteBarWithTranslation, props));
|
|
608
|
+
|
|
609
|
+
export { index as default };
|
|
610
|
+
//# sourceMappingURL=index.js.map
|