@wayke-se/ev-web 0.1.1-beta.1 → 1.0.0-beta.1
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 +342 -81
- package/dist/api.d.ts +50 -0
- package/dist/chargingCost.d.ts +28 -0
- package/dist/dom.d.ts +14 -0
- package/dist/i18n.d.ts +46 -0
- package/dist/index.d.ts +2 -567
- package/dist/index.js +14 -358
- package/dist/index.js.map +4 -4
- package/dist/mount.d.ts +9 -0
- package/dist/style.css +2 -0
- package/dist/style.css.map +7 -0
- package/dist/types.d.ts +105 -0
- package/dist/widget.d.ts +12 -0
- package/package.json +59 -38
- package/dist/GeneralSans-Bold-B2L2RQWE.woff +0 -0
- package/dist/GeneralSans-Bold-VZ4Y7PGT.woff2 +0 -0
- package/dist/GeneralSans-Regular-5M4NQDY6.woff2 +0 -0
- package/dist/GeneralSans-Regular-6E2LFRI2.woff +0 -0
- package/dist/GeneralSans-Semibold-LBITB72G.woff +0 -0
- package/dist/GeneralSans-Semibold-XUXDDWPR.woff2 +0 -0
- package/dist/OpenSauceOne-ExtraBold-3ZSFGTY7.woff2 +0 -0
- package/dist/OpenSauceOne-ExtraBold-HMQJUXJT.woff +0 -0
- package/dist/OpenSauceOne-Regular-2D5N6QGZ.woff2 +0 -0
- package/dist/OpenSauceOne-Regular-F62Q4XLM.woff +0 -0
- package/dist/StyreneA-Bold-Web-4CXEYQLA.woff2 +0 -0
- package/dist/StyreneA-Bold-Web-AGBUOKS2.woff +0 -0
- package/dist/StyreneA-Light-Web-DAMPSUFB.woff +0 -0
- package/dist/StyreneA-Light-Web-EE4AJECE.woff2 +0 -0
- package/dist/StyreneA-Medium-Web-53Q6X35A.woff +0 -0
- package/dist/StyreneA-Medium-Web-7KYQNCKQ.woff2 +0 -0
- package/dist/StyreneA-Regular-Web-7SAOGHB5.woff2 +0 -0
- package/dist/StyreneA-Regular-Web-XFBCXNPA.woff +0 -0
- package/dist/index.css +0 -2
- package/dist/index.css.map +0 -7
- package/dist/index.mjs +0 -359
- package/dist/index.mjs.map +0 -7
package/README.md
CHANGED
|
@@ -1,123 +1,384 @@
|
|
|
1
1
|
# Wayke EV Web
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A calculator that shows what it costs to charge an electric car from Wayke, per month. The visitor picks a yearly distance and can use their own electricity price. Made for the vehicle page of a dealer or partner website.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Upgrading from 0.x? See [Migrating from 0.x to 1.0](#migrating-from-0x-to-10).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Quick start
|
|
8
|
+
|
|
9
|
+
Add this where the calculator should be shown, and replace `<vehicle id>` with the id of the vehicle's ad at Wayke, such as `4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf`:
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<div id="wayke-ev"></div>
|
|
13
|
+
<script type="module">
|
|
14
|
+
import { mount } from 'https://cdn.wayke.se/public-assets/wayke-ev-web/1.0.0/index.js';
|
|
15
|
+
|
|
16
|
+
mount('#wayke-ev', { id: '<vehicle id>' });
|
|
17
|
+
</script>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
<details>
|
|
21
|
+
<summary>The same snippet for Wayke's test environment</summary>
|
|
22
|
+
|
|
23
|
+
For a staging site or `localhost`, registered in Wayke's test environment. See [Environments](#environments).
|
|
24
|
+
|
|
25
|
+
```html
|
|
26
|
+
<div id="wayke-ev"></div>
|
|
27
|
+
<script type="module">
|
|
28
|
+
import { mount } from 'https://cdn.wayke.se/public-assets/wayke-ev-web/1.0.0/index.js';
|
|
29
|
+
|
|
30
|
+
mount('#wayke-ev', { id: '<vehicle id>', environment: 'test' });
|
|
31
|
+
</script>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
Three things to know:
|
|
37
|
+
|
|
38
|
+
* **Your domain has to be registered with Wayke.** Wayke recognizes the site by its domain. Until the domain is registered, nothing is shown. Ask your contact at Wayke to register it, and see [Environments](#environments) for development.
|
|
39
|
+
* **Only battery-electric cars have a charging cost.** For any other vehicle nothing is shown, so the snippet can be on every vehicle page. See [When nothing is shown](#when-nothing-is-shown).
|
|
40
|
+
* **The calculator uses the font of your page.** Set the colors to match your site, see [Colors and fonts](#colors-and-fonts).
|
|
41
|
+
|
|
42
|
+
## Contents
|
|
43
|
+
|
|
44
|
+
* [Add to your site](#add-to-your-site): [CDN](#cdn), [npm](#npm)
|
|
45
|
+
* [Environments](#environments)
|
|
46
|
+
* [Options](#options)
|
|
47
|
+
* [Passing the figures yourself](#passing-the-figures-yourself)
|
|
48
|
+
* [When nothing is shown](#when-nothing-is-shown)
|
|
49
|
+
* [Colors and fonts](#colors-and-fonts)
|
|
50
|
+
* [Events](#events)
|
|
51
|
+
* [How the cost is calculated](#how-the-cost-is-calculated)
|
|
52
|
+
* [Development](#development)
|
|
53
|
+
* [Migrating from 0.x to 1.0](#migrating-from-0x-to-10)
|
|
54
|
+
|
|
55
|
+
## Add to your site
|
|
56
|
+
|
|
57
|
+
| Your site | Use |
|
|
58
|
+
|-----------|-----|
|
|
59
|
+
| Any website, without a build step, such as a CMS or static HTML | [CDN](#cdn) |
|
|
60
|
+
| A website with its own bundler (Vite, webpack, Next.js, ...) | [npm](#npm) |
|
|
61
|
+
|
|
62
|
+
### CDN
|
|
63
|
+
|
|
64
|
+
Every release is published to the Wayke CDN. Nothing needs to be installed.
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
https://cdn.wayke.se/public-assets/wayke-ev-web/<version>/index.js
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Use an exact version. The files of a version never change, so your site only changes when you update the version in the snippet. The latest version is shown on [npm](https://www.npmjs.com/package/@wayke-se/ev-web).
|
|
71
|
+
|
|
72
|
+
There are two ways to load the script. They show the same thing and take the same options.
|
|
73
|
+
|
|
74
|
+
**Option A: import** (recommended) is the snippet in [Quick start](#quick-start).
|
|
75
|
+
|
|
76
|
+
**Option B: script tag.** The script sets `window.WaykeEv` when it has loaded. Module scripts run after the page has been read, so wait for `DOMContentLoaded` before you use it. If the snippet is added after the page has loaded, for example by a tag manager, `DOMContentLoaded` has already happened, so use option A.
|
|
77
|
+
|
|
78
|
+
```html
|
|
79
|
+
<div id="wayke-ev"></div>
|
|
80
|
+
<script type="module" src="https://cdn.wayke.se/public-assets/wayke-ev-web/1.0.0/index.js"></script>
|
|
81
|
+
<script>
|
|
82
|
+
window.addEventListener('DOMContentLoaded', () => {
|
|
83
|
+
WaykeEv.mount('#wayke-ev', { id: '<vehicle id>' });
|
|
84
|
+
});
|
|
85
|
+
</script>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Stylesheet
|
|
89
|
+
|
|
90
|
+
The script adds its stylesheet, `index.css` in the same folder, to the top of `<head>`, and shows the calculator when it has loaded. Since it is added first, the styles of your site override it.
|
|
91
|
+
|
|
92
|
+
To load the stylesheet yourself, add a `<link>` to it with the same URL. The script finds it and does not add it again.
|
|
93
|
+
|
|
94
|
+
```html
|
|
95
|
+
<link rel="stylesheet" href="https://cdn.wayke.se/public-assets/wayke-ev-web/1.0.0/index.css">
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
To not load the stylesheet at all, add `disablecssinjection` to the script tag in option B. The calculator is then unstyled.
|
|
99
|
+
|
|
100
|
+
### npm
|
|
8
101
|
|
|
9
102
|
```bash
|
|
10
103
|
npm install @wayke-se/ev-web
|
|
11
104
|
```
|
|
12
105
|
|
|
13
|
-
```
|
|
14
|
-
import
|
|
15
|
-
import
|
|
106
|
+
```ts
|
|
107
|
+
import { mount } from '@wayke-se/ev-web';
|
|
108
|
+
import '@wayke-se/ev-web/style.css';
|
|
16
109
|
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
110
|
+
const calculator = mount('#wayke-ev', { id: '<vehicle id>' });
|
|
111
|
+
|
|
112
|
+
// When the element goes away, for example when a component unmounts:
|
|
113
|
+
calculator.unmount();
|
|
21
114
|
```
|
|
22
115
|
|
|
23
|
-
|
|
24
|
-
The associated css is injected into head by default once a `new WaykeEvWeb(...)` is called. This can be turned off by adding `disablecssinjection` to the script tag.
|
|
116
|
+
The first argument is a CSS selector or an element. The package is ESM only, has no dependencies, and comes with TypeScript types. It needs a browser, so call `mount` on the client, for example in `useEffect` in React:
|
|
25
117
|
|
|
118
|
+
```tsx
|
|
119
|
+
import { useEffect, useRef } from 'react';
|
|
120
|
+
import { mount } from '@wayke-se/ev-web';
|
|
121
|
+
import '@wayke-se/ev-web/style.css';
|
|
26
122
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
type="module"
|
|
30
|
-
src="https://cdn.wayke.se/public-assets/wayke-ev-web/x.x.x/index.js"
|
|
31
|
-
></script>
|
|
123
|
+
export const ChargingCost = ({ id }: { id: string }) => {
|
|
124
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
32
125
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
if (!ref.current) return;
|
|
128
|
+
const calculator = mount(ref.current, { id });
|
|
129
|
+
return () => calculator.unmount();
|
|
130
|
+
}, [id]);
|
|
131
|
+
|
|
132
|
+
return <div ref={ref} />;
|
|
133
|
+
};
|
|
41
134
|
```
|
|
42
135
|
|
|
43
|
-
##
|
|
136
|
+
## Environments
|
|
44
137
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
138
|
+
| `environment` | Shows | Your domain is registered in |
|
|
139
|
+
|---------------|-------|------------------------------|
|
|
140
|
+
| `production` (default) | The vehicles on wayke.se and drive.no | Wayke's production environment |
|
|
141
|
+
| `test` | Test vehicles | Wayke's test environment |
|
|
142
|
+
|
|
143
|
+
A vehicle id belongs to one environment. An id from production is unknown in test, and the other way around.
|
|
144
|
+
|
|
145
|
+
Each environment recognizes the site by the domain the request comes from, so the domain has to be registered in that environment. To develop on `localhost` or a staging domain, have it registered in test. Instead of the domain, a site can be identified with `apiKey`, see [Options](#options).
|
|
146
|
+
|
|
147
|
+
## Options
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
mount(target, options);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Either `id` or `data` is required. Everything else is optional.
|
|
154
|
+
|
|
155
|
+
| Option | Type | Default | |
|
|
156
|
+
|--------|------|---------|---|
|
|
157
|
+
| `id` | `string` | | The id of the vehicle's ad at Wayke. The calculator fetches the figures itself. |
|
|
158
|
+
| `data` | [`ChargingData`](#passing-the-figures-yourself) | | The figures, when your page already has them. No request is made. |
|
|
159
|
+
| `environment` | `'production' \| 'test'` | `'production'` | See [Environments](#environments). |
|
|
160
|
+
| `apiKey` | `string` | The origin of the page | Identifies the site instead of its domain. |
|
|
161
|
+
| `marketCode` | `'SE' \| 'NO'` | The market of the vehicle | Sweden counts the distance in mil and Norway in km. |
|
|
162
|
+
| `language` | `'sv' \| 'nb' \| 'en'` | `sv` in Sweden, `nb` in Norway | |
|
|
163
|
+
| `theme` | `{ primaryColor?, primaryColorForeground?, linkColor? }` | | See [Colors and fonts](#colors-and-fonts). |
|
|
164
|
+
| `hash` | `boolean` | `false` | Adds `#wayke-ev` to the url while the calculator dialog is open, so the browser's back button closes it. Useful on mobile, where the dialog covers the screen. |
|
|
165
|
+
| `onEvent` | `(event) => void` | | See [Events](#events). |
|
|
166
|
+
|
|
167
|
+
`mount` returns `{ unmount }`. `unmount()` removes the calculator and everything it added to the page.
|
|
168
|
+
|
|
169
|
+
`mount` throws if no element matches `target`, or if neither `id` nor `data` is given. It never throws for a vehicle or a network problem: see [When nothing is shown](#when-nothing-is-shown).
|
|
170
|
+
|
|
171
|
+
## Passing the figures yourself
|
|
172
|
+
|
|
173
|
+
If your page already reads the vehicle from Wayke's GraphQL API, pass the figures as `data`. The calculator then makes no request, and is on the page at once.
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
import { mount, type ChargingData } from '@wayke-se/ev-web';
|
|
177
|
+
|
|
178
|
+
const data: ChargingData = {
|
|
179
|
+
energyPer100Km: 16.34, // kWh per 100 km, charging losses included
|
|
180
|
+
costPerKm: 0.198, // at pricePerKwh, in the market's currency
|
|
181
|
+
pricePerKwh: 1.21, // Wayke's template electricity price
|
|
48
182
|
marketCode: 'SE',
|
|
49
|
-
|
|
50
|
-
|
|
183
|
+
batteryCapacityKwh: 75, // optional
|
|
184
|
+
rangeKm: 514, // optional
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
mount('#wayke-ev', { data });
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
The figures are attributes of the vehicle:
|
|
191
|
+
|
|
192
|
+
```graphql
|
|
193
|
+
query ($id: ID!) {
|
|
194
|
+
ads {
|
|
195
|
+
byAdId(ID: $id) {
|
|
196
|
+
iteration {
|
|
197
|
+
item {
|
|
198
|
+
... on Vehicle {
|
|
199
|
+
attributes {
|
|
200
|
+
chargingEnergyConsumption { floatValue } # energyPer100Km
|
|
201
|
+
chargingCostPerKilometer { floatValue unit } # costPerKm, unit "SEK/km" or "NOK/km"
|
|
202
|
+
chargingPricePerKwh { floatValue } # pricePerKwh
|
|
203
|
+
batteryCapacityNet { intValue } # batteryCapacityKwh
|
|
204
|
+
electricRangeWltp { minvalue } # rangeKm
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
The three `charging*` attributes are `null` for a vehicle without a charging cost. Do not mount the calculator then.
|
|
215
|
+
|
|
216
|
+
## When nothing is shown
|
|
217
|
+
|
|
218
|
+
The calculator takes no space until it has figures to show, and stays empty when it has none. Your page keeps its layout, and `onEvent` tells you why:
|
|
219
|
+
|
|
220
|
+
| Event | `reason` | Meaning |
|
|
221
|
+
|-------|----------|---------|
|
|
222
|
+
| `unavailable` | `no-charging-data` | The vehicle is not battery-electric (petrol, diesel and plug-in hybrids have no charging cost), or Wayke lacks the data to estimate its cost. |
|
|
223
|
+
| `unavailable` | `not-found` | No published vehicle has this id in this environment. |
|
|
224
|
+
| `error` | `unauthorized` | The domain of the page, or `apiKey`, is not registered with Wayke in this environment. |
|
|
225
|
+
| `error` | `rate-limited` | Too many requests from the site in a short time. |
|
|
226
|
+
| `error` | `network` | The request did not reach Wayke. |
|
|
227
|
+
| `error` | `server` | Wayke answered with an error. |
|
|
228
|
+
|
|
229
|
+
To show a section heading only when there is a calculator, wait for `ready`:
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
mount('#wayke-ev', {
|
|
233
|
+
id,
|
|
234
|
+
onEvent: (event) => {
|
|
235
|
+
if (event.type === 'ready') document.querySelector('#charging-section').hidden = false;
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Colors and fonts
|
|
241
|
+
|
|
242
|
+
The calculator has no font of its own and inherits the font of your page. Its accents are turquoise until you set your own colors:
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
mount('#wayke-ev', {
|
|
246
|
+
id,
|
|
51
247
|
theme: {
|
|
52
|
-
primaryColor: '#227a29',
|
|
53
|
-
primaryColorForeground: '#
|
|
54
|
-
linkColor: '#
|
|
248
|
+
primaryColor: '#227a29', // slider and radio buttons
|
|
249
|
+
primaryColorForeground: '#ffffff', // placed on top of primaryColor
|
|
250
|
+
linkColor: '#19b52f',
|
|
55
251
|
},
|
|
56
|
-
onEvent: (view, event, data) => console.log(view, event, data),
|
|
57
252
|
});
|
|
58
253
|
```
|
|
59
254
|
|
|
60
|
-
|
|
61
|
-
* `id` Id of the vehicle from Wayke or a swedish registration number
|
|
62
|
-
* `url` Should be one of the following urls below:
|
|
255
|
+
The theme is set per calculator, so two on the same page can differ.
|
|
63
256
|
|
|
64
|
-
|
|
65
|
-
| ----------- | --- |
|
|
66
|
-
| Test | https://api.wayketech.se |
|
|
67
|
-
| Production | https://api.wayke.se |
|
|
257
|
+
To change more than the colors, add your own CSS. All class names start with `waykeev-`, and the calculator is wrapped in `.waykeev-root`. Its own element styles sit inside `:where()`, so a class selector of yours always wins:
|
|
68
258
|
|
|
259
|
+
```css
|
|
260
|
+
.waykeev-root .waykeev-heading {
|
|
261
|
+
font-family: 'Your Heading Font', sans-serif;
|
|
262
|
+
}
|
|
263
|
+
```
|
|
69
264
|
|
|
70
|
-
|
|
265
|
+
The class names are not part of the public API and can change between versions, so check your CSS when you update.
|
|
71
266
|
|
|
72
|
-
|
|
73
|
-
- `rootId` Choose the element, id, that WakeEvWeb should append to, by default it will be appended to the body.
|
|
74
|
-
- `hash` Enables hash in route. Will prevent the user from navigating to the previous page when using the back button. It's recommended to enable this option for a better experience in mobile as the modal takes up the whole screen.
|
|
75
|
-
- `onEvent` Listen to events. See [Events](#events) below.
|
|
76
|
-
- `theme.primaryColor` Override primary color. Defaults to `#00b6d3`
|
|
77
|
-
- `theme.primaryColorForeground` Override color placed on top of the primary color. Defaults to `#fff`
|
|
78
|
-
- `theme.linkColor` Override color of links. Defaults to `#00b6d3`
|
|
267
|
+
The calculator never changes the styles of your page. Your page can change the calculator though: a rule on an element, such as `button { ... }` or `h2 { ... }`, also applies inside it. If something looks off, look for such a rule first.
|
|
79
268
|
|
|
80
|
-
|
|
269
|
+
## Events
|
|
81
270
|
|
|
82
|
-
|
|
83
|
-
| ------|------------------------------- | ------------------------------------------------------------------- |
|
|
84
|
-
| MAIN | INITIALIZED | `{ "id": string }` |
|
|
85
|
-
| MAIN | INITIAL_REQUEST_PENDING | `{ "id": string }` |
|
|
86
|
-
| MAIN | INITIAL_REQUEST_SUCCEEDED | `{ "id": string }` |
|
|
87
|
-
| MAIN | INITIAL_REQUEST_FAILED | `{ "id": string, "statusCode": number \| undefined }` |
|
|
88
|
-
| MAIN | DISTANCE_SLIDER | `{ "id": string }` |
|
|
89
|
-
| MAIN | DISTANCE_SELECT | `{ "id": string }` |
|
|
90
|
-
| MAIN | MODAL_OPENED | `{ "id": string }` |
|
|
91
|
-
| MODAL | MODAL_CLOSED | `{ "id": string }` |
|
|
92
|
-
| MODAL | REQUEST_CUSTOM_PRICE_PENDING | `{ "id": string }` |
|
|
93
|
-
| MODAL | REQUEST_CUSTOM_PRICE_SUCCEEDED | `{ "id": string }` |
|
|
94
|
-
| MODAL | REQUEST_CUSTOM_PRICE_FAILED | `{ "id": string, "statusCode": number \| undefined }` |
|
|
95
|
-
| MODAL | TOGGLE_PRICE | `{ "id": string, "priceOption": 'standardPrice' \| 'customPrice' }` |
|
|
96
|
-
| MODAL | CUSTOM_PRICE_CHANGE | `{ "id": string, "customPrice": number }` |
|
|
97
|
-
## CSS specificity
|
|
271
|
+
`onEvent` is called with one object. `type` tells the events apart.
|
|
98
272
|
|
|
99
|
-
|
|
273
|
+
| `type` | Other fields | When |
|
|
274
|
+
|--------|--------------|------|
|
|
275
|
+
| `ready` | `data` | The calculator is on the page. `data` is the figures it shows. Like every event, it is delivered after `mount` has returned. |
|
|
276
|
+
| `unavailable` | `reason` | Nothing is shown, see [When nothing is shown](#when-nothing-is-shown). |
|
|
277
|
+
| `error` | `reason` | Nothing is shown, see [When nothing is shown](#when-nothing-is-shown). |
|
|
278
|
+
| `distance-change` | `distanceKm`, `control` | The visitor changed the yearly distance. `distanceKm` is in km in both markets, `control` is `'slider'` or `'select'`. |
|
|
279
|
+
| `modal-open` | | The visitor opened the dialog for their own electricity price. |
|
|
280
|
+
| `modal-close` | | The dialog was closed. |
|
|
281
|
+
| `price-option-change` | `priceOption` | `'standard'` or `'custom'`. |
|
|
282
|
+
| `custom-price-change` | `pricePerKwh` | The visitor entered their own price. In the market's currency per kWh, so 1.5 for 150 öre. |
|
|
100
283
|
|
|
101
|
-
|
|
284
|
+
```ts
|
|
285
|
+
import { mount, type EvEvent } from '@wayke-se/ev-web';
|
|
102
286
|
|
|
103
|
-
|
|
287
|
+
const onEvent = (event: EvEvent) => {
|
|
288
|
+
if (event.type === 'distance-change') {
|
|
289
|
+
analytics.track('charging_distance', { km: event.distanceKm });
|
|
290
|
+
}
|
|
291
|
+
};
|
|
104
292
|
|
|
105
|
-
|
|
293
|
+
mount('#wayke-ev', { id, onEvent });
|
|
294
|
+
```
|
|
106
295
|
|
|
107
|
-
##
|
|
296
|
+
## How the cost is calculated
|
|
108
297
|
|
|
109
|
-
|
|
298
|
+
```
|
|
299
|
+
cost per month = energy per km × price per kWh × distance per year / 12
|
|
300
|
+
```
|
|
110
301
|
|
|
111
|
-
|
|
302
|
+
* **Energy per km** comes from Wayke. It is the energy drawn from the socket, with charging losses included, based on the car's WLTP figures: its battery capacity and range, or its WLTP consumption when Wayke lacks the capacity.
|
|
303
|
+
* **Price per kWh** is Wayke's template price for the market, with energy tax and VAT included. The grid fee is not included. In the dialog the visitor can enter their own price instead.
|
|
304
|
+
* **Distance per year** is chosen by the visitor: 500 to 5 000 mil in Sweden, 5 000 to 50 000 km in Norway.
|
|
112
305
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
306
|
+
An own price is calculated in the browser, without a request.
|
|
307
|
+
|
|
308
|
+
## Development
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
npm install
|
|
312
|
+
npm start # the development page on http://localhost:5000, against Wayke's test environment
|
|
313
|
+
npm test # lint, types, stylelint and unit tests
|
|
314
|
+
npm run build # dist/ for npm and dist-cdn/ for the CDN
|
|
117
315
|
```
|
|
118
316
|
|
|
119
|
-
|
|
317
|
+
The origin of the development page has to be registered in Wayke's test environment. To use another port, run `PORT=5050 npm start`. To identify the page with a key instead of its origin, run `WAYKE_API_KEY=<key> npm start`.
|
|
318
|
+
|
|
319
|
+
A push to `main` or `test` releases the version in `package.json` to the CDN and npm. A pre-release version, such as `1.1.0-beta.1`, gets the npm tag `next`, and so does everything released from `test`. Only a plain version on `main` becomes `latest`. A version that is already published is refused, so bump the version first. The release log prints the CDN url the files are served from.
|
|
320
|
+
|
|
321
|
+
## Migrating from 0.x to 1.0
|
|
322
|
+
|
|
323
|
+
1.0 is a rewrite. The calculator looks the same, and has become smaller and faster: the package went from about 1 MB to about 200 kB, of which the browser loads 50 kB, and an own electricity price no longer needs a request.
|
|
324
|
+
|
|
325
|
+
### What to change
|
|
326
|
+
|
|
327
|
+
**1. `new WaykeEvWeb(...)` is now `mount(...)`.**
|
|
328
|
+
|
|
329
|
+
```js
|
|
330
|
+
// 0.x
|
|
331
|
+
import WaykeEvWeb from '@wayke-se/ev-web';
|
|
332
|
+
import '@wayke-se/ev-web/dist/index.css';
|
|
333
|
+
|
|
334
|
+
new WaykeEvWeb({
|
|
335
|
+
url: 'https://api.wayke.se',
|
|
336
|
+
id: '4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf',
|
|
337
|
+
rootId: 'custom-id',
|
|
338
|
+
marketCode: 'SE',
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
// 1.0
|
|
342
|
+
import { mount } from '@wayke-se/ev-web';
|
|
343
|
+
import '@wayke-se/ev-web/style.css';
|
|
344
|
+
|
|
345
|
+
mount('#custom-id', { id: '4a18e9a4-f7ee-458e-8bb4-9b6b8b778bbf' });
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
**2. Options.**
|
|
349
|
+
|
|
350
|
+
| 0.x | 1.0 |
|
|
351
|
+
|-----|-----|
|
|
352
|
+
| `url: 'https://api.wayke.se'` | Remove it. Production is the default. |
|
|
353
|
+
| `url: 'https://api.wayketech.se'` | `environment: 'test'` |
|
|
354
|
+
| `rootId: 'custom-id'` | The first argument: `mount('#custom-id', ...)`. It is required, the calculator is no longer added to `<body>` without it. |
|
|
355
|
+
| `marketCode` | Optional. The market follows the vehicle. |
|
|
356
|
+
| `theme`, `hash` | Unchanged. |
|
|
357
|
+
| `onEvent: (view, event, data) => ...` | `onEvent: (event) => ...`, see below. |
|
|
358
|
+
|
|
359
|
+
**3. The stylesheet** is imported from `@wayke-se/ev-web/style.css` instead of `@wayke-se/ev-web/dist/index.css`.
|
|
360
|
+
|
|
361
|
+
**4. CDN.** The script now exports `mount`, and `window.WaykeEvWeb` is replaced by `window.WaykeEv.mount`. See [CDN](#cdn).
|
|
362
|
+
|
|
363
|
+
**5. Events.**
|
|
364
|
+
|
|
365
|
+
| 0.x | 1.0 |
|
|
366
|
+
|-----|-----|
|
|
367
|
+
| `INITIALIZED`, `INITIAL_REQUEST_PENDING` | Removed. |
|
|
368
|
+
| `INITIAL_REQUEST_SUCCEEDED` | `ready` |
|
|
369
|
+
| `INITIAL_REQUEST_FAILED` | `unavailable` or `error`, with a `reason` |
|
|
370
|
+
| `DISTANCE_SLIDER`, `DISTANCE_SELECT` | `distance-change` with `control` |
|
|
371
|
+
| `MODAL_OPENED`, `MODAL_CLOSED` | `modal-open`, `modal-close` |
|
|
372
|
+
| `TOGGLE_PRICE` | `price-option-change`. `priceOption` is `'standard'` or `'custom'`, it was `'standardPrice'` or `'customPrice'`. |
|
|
373
|
+
| `CUSTOM_PRICE_CHANGE` | `custom-price-change`. `pricePerKwh` is in kronor, `customPrice` was in öre. |
|
|
374
|
+
| `REQUEST_CUSTOM_PRICE_PENDING`, `_SUCCEEDED`, `_FAILED` | Removed. An own price is calculated in the browser. |
|
|
120
375
|
|
|
121
|
-
|
|
376
|
+
### What else is different
|
|
122
377
|
|
|
123
|
-
|
|
378
|
+
* **The id is the id of the vehicle's ad at Wayke.** A registration number is no longer accepted as `id`.
|
|
379
|
+
* **The fonts are gone.** 0.x came with Wayke's and Drive's fonts. 1.0 uses the font of your page, see [Colors and fonts](#colors-and-fonts).
|
|
380
|
+
* **More cars get a cost.** 0.x showed nothing for a car without a battery capacity at Wayke. 1.0 then uses the car's WLTP consumption.
|
|
381
|
+
* **Your domain has to be registered with Wayke**, as before. If nothing is shown after the upgrade, `onEvent` gets `error` with the reason `unauthorized`: ask your contact at Wayke to check the registration.
|
|
382
|
+
* **The package is ESM only.** `require('@wayke-se/ev-web')` no longer works. Every current bundler handles ESM.
|
|
383
|
+
* **`unmount()`** removes the calculator. 0.x had no way to do that.
|
|
384
|
+
* **The figures can be passed in** as `data`, see [Passing the figures yourself](#passing-the-figures-yourself).
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ChargingData, Environment, ErrorReason, UnavailableReason } from './types.js';
|
|
2
|
+
interface AdNode {
|
|
3
|
+
id: string;
|
|
4
|
+
iteration?: {
|
|
5
|
+
item?: {
|
|
6
|
+
attributes?: {
|
|
7
|
+
chargingEnergyConsumption?: {
|
|
8
|
+
floatValue?: number | null;
|
|
9
|
+
} | null;
|
|
10
|
+
chargingCostPerKilometer?: {
|
|
11
|
+
floatValue?: number | null;
|
|
12
|
+
unit?: string | null;
|
|
13
|
+
} | null;
|
|
14
|
+
chargingPricePerKwh?: {
|
|
15
|
+
floatValue?: number | null;
|
|
16
|
+
} | null;
|
|
17
|
+
batteryCapacityNet?: {
|
|
18
|
+
intValue?: number | null;
|
|
19
|
+
} | null;
|
|
20
|
+
batteryCapacityGross?: {
|
|
21
|
+
intValue?: number | null;
|
|
22
|
+
} | null;
|
|
23
|
+
electricRangeWltp?: {
|
|
24
|
+
minvalue?: number | null;
|
|
25
|
+
} | null;
|
|
26
|
+
} | null;
|
|
27
|
+
} | null;
|
|
28
|
+
} | null;
|
|
29
|
+
}
|
|
30
|
+
export declare class ApiError extends Error {
|
|
31
|
+
readonly reason: ErrorReason;
|
|
32
|
+
constructor(reason: ErrorReason);
|
|
33
|
+
}
|
|
34
|
+
export type ChargingDataResult = {
|
|
35
|
+
status: 'ok';
|
|
36
|
+
data: ChargingData;
|
|
37
|
+
} | {
|
|
38
|
+
status: 'unavailable';
|
|
39
|
+
reason: UnavailableReason;
|
|
40
|
+
};
|
|
41
|
+
/** Exported for the tests. */
|
|
42
|
+
export declare const toChargingData: (ad: AdNode | null | undefined) => ChargingDataResult;
|
|
43
|
+
interface FetchOptions {
|
|
44
|
+
id: string;
|
|
45
|
+
environment: Environment;
|
|
46
|
+
apiKey?: string;
|
|
47
|
+
signal?: AbortSignal;
|
|
48
|
+
}
|
|
49
|
+
export declare const fetchChargingData: ({ id, environment, apiKey, signal, }: FetchOptions) => Promise<ChargingDataResult>;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ChargingData, MarketCode } from './types.js';
|
|
2
|
+
export declare const DEFAULT_DISTANCE_KM = 15000;
|
|
3
|
+
export interface DistanceScale {
|
|
4
|
+
/** Sweden counts in Scandinavian miles (1 mil = 10 km), Norway in km. */
|
|
5
|
+
unit: 'mil' | 'km';
|
|
6
|
+
kmPerUnit: number;
|
|
7
|
+
/** In the market's unit. */
|
|
8
|
+
options: number[];
|
|
9
|
+
min: number;
|
|
10
|
+
max: number;
|
|
11
|
+
step: number;
|
|
12
|
+
}
|
|
13
|
+
export declare const distanceScale: (marketCode: MarketCode) => DistanceScale;
|
|
14
|
+
/** The price in whole öre/øre, which is what the visitor sees and types. */
|
|
15
|
+
export declare const toCents: (pricePerKwh: number) => number;
|
|
16
|
+
/**
|
|
17
|
+
* Cost per km at the visitor's own price per kWh. Undefined for a missing or
|
|
18
|
+
* invalid price.
|
|
19
|
+
*
|
|
20
|
+
* At the template price it is the template cost itself: Wayke computes that figure
|
|
21
|
+
* from an unrounded price, so energy × the rounded price would land a krona or two
|
|
22
|
+
* off per month and the two price options would disagree before anything is typed.
|
|
23
|
+
*/
|
|
24
|
+
export declare const costPerKmAtPrice: (data: ChargingData, pricePerKwh: number | undefined) => number | undefined;
|
|
25
|
+
/** Undefined when there is no cost per km. A cost that rounds to 0 is still a cost. */
|
|
26
|
+
export declare const monthlyCost: (costPerKm: number | undefined, distanceKmPerYear: number) => number | undefined;
|
|
27
|
+
/** True when the figures are enough to show a calculator. */
|
|
28
|
+
export declare const isChargingData: (data: Partial<ChargingData> | null | undefined) => data is ChargingData;
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type Attributes = Record<string, string | number | boolean | undefined>;
|
|
2
|
+
type Child = Node | string | false | null | undefined;
|
|
3
|
+
export declare const h: <K extends keyof HTMLElementTagNameMap>(tag: K, attributes?: Attributes, ...children: Child[]) => HTMLElementTagNameMap[K];
|
|
4
|
+
/** A decorative icon: one path in a 16 × 16 viewBox. */
|
|
5
|
+
export declare const icon: (path: string) => SVGSVGElement;
|
|
6
|
+
export declare const ICONS: {
|
|
7
|
+
bulb: string;
|
|
8
|
+
close: string;
|
|
9
|
+
help: string;
|
|
10
|
+
helpClose: string;
|
|
11
|
+
};
|
|
12
|
+
/** The brand symbol shown next to "Powered by". */
|
|
13
|
+
export declare const brandSymbol: (brand: string) => SVGSVGElement;
|
|
14
|
+
export {};
|
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Language, MarketCode } from './types.js';
|
|
2
|
+
declare const sv: {
|
|
3
|
+
title: string;
|
|
4
|
+
description: string;
|
|
5
|
+
modalDescription: string;
|
|
6
|
+
yearlyDistance: string;
|
|
7
|
+
adjustToYourPrice: string;
|
|
8
|
+
openCalculator: string;
|
|
9
|
+
closeModal: string;
|
|
10
|
+
costPerMonthUnit: string;
|
|
11
|
+
pricePerKwhUnit: string;
|
|
12
|
+
energyUnit: string;
|
|
13
|
+
unknownCost: string;
|
|
14
|
+
disclaimerStandard: string;
|
|
15
|
+
disclaimerCustom: string;
|
|
16
|
+
whichPrice: string;
|
|
17
|
+
standardPrice: string;
|
|
18
|
+
customPrice: string;
|
|
19
|
+
yourPrice: string;
|
|
20
|
+
yourPriceHelp: string;
|
|
21
|
+
showHelp: string;
|
|
22
|
+
hideHelp: string;
|
|
23
|
+
batteryCapacity: string;
|
|
24
|
+
range: string;
|
|
25
|
+
energyUse: string;
|
|
26
|
+
statedDistance: string;
|
|
27
|
+
standardPriceRow: string;
|
|
28
|
+
gridFee: string;
|
|
29
|
+
gridFeeValue: string;
|
|
30
|
+
poweredBy: string;
|
|
31
|
+
};
|
|
32
|
+
type Messages = typeof sv;
|
|
33
|
+
export type MessageKey = keyof Messages;
|
|
34
|
+
export interface Translator {
|
|
35
|
+
(key: MessageKey, values?: Record<string, string>): string;
|
|
36
|
+
/** Formats a number the way the language writes it, e.g. "1 500" and "20,4". */
|
|
37
|
+
number: (value: number, maximumFractionDigits?: number) => string;
|
|
38
|
+
}
|
|
39
|
+
export declare const defaultLanguage: (marketCode: MarketCode) => Language;
|
|
40
|
+
export declare const createTranslator: (language: Language) => Translator;
|
|
41
|
+
/** The brand behind the figures: Wayke in Sweden, Drive in Norway. */
|
|
42
|
+
export declare const brandOf: (marketCode: MarketCode) => {
|
|
43
|
+
name: string;
|
|
44
|
+
url: string;
|
|
45
|
+
};
|
|
46
|
+
export {};
|