@ne1410s/address 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 ne1410s
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # @ne1410s/address
2
+
3
+ ## An address lookup utility.
4
+
5
+ ```html
6
+ <ne14-address reveal="Hey!">
7
+ <p>Hello world</p>
8
+ </ne14-address>
9
+ ```
10
+
11
+ - **Use:** `<script src="PATH_TO_UMD_SCRIPT"></script>`
12
+ - **Extend:** _npm i -S @ne1410s/address_
13
+
14
+ ### Attributes
15
+
16
+ ```html
17
+ <ne14-address corner="3" reveal="Definition here">YO' STUFF</ne14-address>
18
+ ```
19
+
20
+ - **corner**: If specified, the reveal text is positioned in a corner of the screen:
21
+ - 1: Top-left, 2: Top-right, 3: Bottom-right, 4: Bottom-left
22
+ - **reveal**: The definition text that gets shown on hover
23
+
24
+ ### Events
25
+
26
+ ```javascript
27
+ const address = document.querySelector('ne14-address');
28
+
29
+ address.addEventListener('...', () => {
30
+ console.log('Event fired!');
31
+ });
32
+ ```
33
+
34
+ - _There are no bespoke events raised in the internal workings of this element_
35
+
36
+ ### Methods
37
+
38
+ - _There are no bespoke methods exposed in this element_
39
+
40
+ ### Properties
41
+
42
+ - `set` **corner** (number): Sets the attribute with the corresponding value
43
+ - `set` **reveal** (string): Sets the attribute with the corresponding value
44
+
45
+ ### CSS Variables
46
+
47
+ Some degree of custom styling can be provided, by way of css variables:
48
+
49
+ ```css
50
+ ne14-address {
51
+ --src-border: 1px solid green;
52
+ --reveal-trg-bg: rebeccapurple;
53
+ }
54
+ ```
55
+
56
+ - **`--src-border-radius`** _Source item border radius. Defaults to: `3px`_
57
+ - **`--src-border`** _Source item border. Defaults to: `1px solid black`_
58
+ - **`--src-bg`** _Source item background. Defaults to: `inherit`_
59
+ - **`--src-fg`** _Source item foreground. Defaults to: `inherit`_
60
+ - **`--trg-border-radius`** _Target item border radius. Defaults to: `3px`_
61
+ - **`--reveal-src-border`** _Source item border (in reveal mode). Defaults to: `--src-border`_
62
+ - **`--reveal-src-bg`** _Source item background (in reveal mode). Defaults to: `inherit`_
63
+ - **`--reveal-src-fg`** _Source item foreground (in reveal mode). Defaults to: `inherit`_
64
+ - **`--reveal-trg-border`** _Target item border (in reveal mode). Defaults to: `1px solid black`_
65
+ - **`--reveal-trg-bg`** _Target item background (in reveal mode). Defaults to: `white`_
66
+ - **`--reveal-trg-fg`** _Target item foreground (in reveal mode). Defaults to: `inherit`_
@@ -0,0 +1,10 @@
1
+ import { CustomElementBase } from '@ne1410s/cust-elems';
2
+ export declare class AddressLookup extends CustomElementBase {
3
+ private static readonly Css;
4
+ private static readonly Html;
5
+ private addressCompleter;
6
+ constructor();
7
+ initialise(): void;
8
+ private onPlaceChanged;
9
+ private getMatchingFieldId;
10
+ }
@@ -0,0 +1,2 @@
1
+ import { AddressLookup } from './address-lookup/address-lookup';
2
+ export { AddressLookup };
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var custElems = require('@ne1410s/cust-elems');
6
+
7
+ var markupUrl = "data:text/html;base64,PHNwYW4gY2xhc3M9InJvb3QiPg0KICA8Zm9ybSBhY3Rpb249IiIgbWV0aG9kPSJnZXQiIGF1dG9jb21wbGV0ZT0ib2ZmIj4NCiAgICA8bGFiZWwgY2xhc3M9InN1cGVyLXNsaW0tZmllbGQtbGVmdCI+DQogICAgICA8c3BhbiBjbGFzcz0iZm9ybS1sYWJlbCI+Tm8uKjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0iaG91c2UtbnVtYmVyIiBuYW1lPSJob3VzZS1udW1iZXIiIHJlcXVpcmVkIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZpZWxkLXJpZ2h0Ij4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5TdHJlZXQqPC9zcGFuPg0KICAgICAgPGlucHV0DQogICAgICAgIGlkPSJzdHJlZXQiDQogICAgICAgIG5hbWU9InN0cmVldCINCiAgICAgICAgcmVxdWlyZWQNCiAgICAgICAgYXV0b2NvbXBsZXRlPSJvZmYiDQogICAgICAgIHBsYWNlaG9sZGVyPSJUeXBlIHlvdXIgYWRkcmVzcy4uLiINCiAgICAgIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZ1bGwtZmllbGQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPkFkZHJlc3MgTGluZSAyPC9zcGFuPg0KICAgICAgPGlucHV0IGlkPSJhZGRyZXNzMiIgbmFtZT0iYWRkcmVzczIiIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZ1bGwtZmllbGQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPlRvd24gLyBDaXR5Kjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0idG93biIgbmFtZT0idG93biIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxsYWJlbCBjbGFzcz0ic2xpbS1maWVsZC1sZWZ0Ij4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5Db3VudHkqPC9zcGFuPg0KICAgICAgPGlucHV0IGlkPSJjb3VudHkiIG5hbWU9ImNvdW50eSIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxsYWJlbCBjbGFzcz0iZmllbGQtcmlnaHQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPlBvc3Rjb2RlKjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0icG9zdGNvZGUiIG5hbWU9InBvc3Rjb2RlIiByZXF1aXJlZCAvPg0KICAgIDwvbGFiZWw+DQogICAgPGxhYmVsIGNsYXNzPSJmdWxsLWZpZWxkIj4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5Db3VudHJ5Kjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0iY291bnRyeSIgbmFtZT0iY291bnRyeSIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxidXR0b24gdHlwZT0iYnV0dG9uIiBjbGFzcz0ibXktYnV0dG9uIj5TYXZlIGFkZHJlc3M8L2J1dHRvbj4NCiAgICA8aW5wdXQgdHlwZT0icmVzZXQiIHZhbHVlPSJDbGVhciBmb3JtIiAvPg0KICA8L2Zvcm0+DQo8L3NwYW4+DQo=";
8
+
9
+ var stylesUrl = "data:text/css;base64,";
10
+
11
+ class AddressLookup extends custElems.CustomElementBase {
12
+ constructor() {
13
+ super(AddressLookup.Css, AddressLookup.Html);
14
+ }
15
+ initialise() {
16
+ const searchBox = this.root.querySelector('#street');
17
+ this.addressCompleter = new google.maps.places.Autocomplete(searchBox, {
18
+ componentRestrictions: { country: ['uk'] },
19
+ fields: ['address_components'],
20
+ types: ['address'],
21
+ });
22
+ this.addressCompleter.addListener('place_changed', this.onPlaceChanged);
23
+ }
24
+ onPlaceChanged() {
25
+ const place = this.addressCompleter.getPlace();
26
+ for (const component of place.address_components) {
27
+ const inputFieldId = this.getMatchingFieldId(component.types[0]);
28
+ if (inputFieldId) {
29
+ const input = document.querySelector(inputFieldId);
30
+ input.value = component.long_name;
31
+ }
32
+ }
33
+ }
34
+ getMatchingFieldId(componentType) {
35
+ switch (componentType) {
36
+ case 'street_number':
37
+ return '#house-number';
38
+ case 'route':
39
+ return '#street';
40
+ case 'postal_code':
41
+ return '#postcode';
42
+ case 'locality':
43
+ return '#town';
44
+ case 'postal_town':
45
+ return '#town';
46
+ case 'administrative_area_level_2':
47
+ return '#county';
48
+ case 'country':
49
+ return '#country';
50
+ default:
51
+ return '';
52
+ }
53
+ }
54
+ }
55
+ AddressLookup.Css = custElems.reduceCss(custElems.decode(stylesUrl));
56
+ AddressLookup.Html = custElems.reduceHtml(custElems.decode(markupUrl));
57
+
58
+ // If custom elements is supported
59
+ if ('customElements' in window) {
60
+ // If not already defined
61
+ if (!window.customElements.get('ne14-address')) {
62
+ window.customElements.define('ne14-address', AddressLookup);
63
+ const apiKey = 'AIzaSyDcwGyRxRbcNGWOFQVT87A1mkxEOfm8t0w';
64
+ const callback = 'apiCallback'; // `ne_address.${AddressLookup.name}.${AddressLookup.Cb.name}`;
65
+ const mapsScriptUrl = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&callback=${callback}&libraries=places&v=weekly`;
66
+ const mapsScript = document.createElement('script');
67
+ mapsScript.setAttribute('async', '');
68
+ mapsScript.setAttribute('src', mapsScriptUrl);
69
+ window[callback] = () => {
70
+ Array.from(document.querySelectorAll('ne14-address')).forEach((el) => {
71
+ el.initialise();
72
+ });
73
+ };
74
+ window.addEventListener('load', () => document.body.appendChild(mapsScript));
75
+ }
76
+ }
77
+
78
+ exports.AddressLookup = AddressLookup;
@@ -0,0 +1,74 @@
1
+ import { reduceCss, decode, reduceHtml, CustomElementBase } from '@ne1410s/cust-elems';
2
+
3
+ var markupUrl = "data:text/html;base64,PHNwYW4gY2xhc3M9InJvb3QiPg0KICA8Zm9ybSBhY3Rpb249IiIgbWV0aG9kPSJnZXQiIGF1dG9jb21wbGV0ZT0ib2ZmIj4NCiAgICA8bGFiZWwgY2xhc3M9InN1cGVyLXNsaW0tZmllbGQtbGVmdCI+DQogICAgICA8c3BhbiBjbGFzcz0iZm9ybS1sYWJlbCI+Tm8uKjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0iaG91c2UtbnVtYmVyIiBuYW1lPSJob3VzZS1udW1iZXIiIHJlcXVpcmVkIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZpZWxkLXJpZ2h0Ij4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5TdHJlZXQqPC9zcGFuPg0KICAgICAgPGlucHV0DQogICAgICAgIGlkPSJzdHJlZXQiDQogICAgICAgIG5hbWU9InN0cmVldCINCiAgICAgICAgcmVxdWlyZWQNCiAgICAgICAgYXV0b2NvbXBsZXRlPSJvZmYiDQogICAgICAgIHBsYWNlaG9sZGVyPSJUeXBlIHlvdXIgYWRkcmVzcy4uLiINCiAgICAgIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZ1bGwtZmllbGQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPkFkZHJlc3MgTGluZSAyPC9zcGFuPg0KICAgICAgPGlucHV0IGlkPSJhZGRyZXNzMiIgbmFtZT0iYWRkcmVzczIiIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZ1bGwtZmllbGQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPlRvd24gLyBDaXR5Kjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0idG93biIgbmFtZT0idG93biIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxsYWJlbCBjbGFzcz0ic2xpbS1maWVsZC1sZWZ0Ij4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5Db3VudHkqPC9zcGFuPg0KICAgICAgPGlucHV0IGlkPSJjb3VudHkiIG5hbWU9ImNvdW50eSIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxsYWJlbCBjbGFzcz0iZmllbGQtcmlnaHQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPlBvc3Rjb2RlKjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0icG9zdGNvZGUiIG5hbWU9InBvc3Rjb2RlIiByZXF1aXJlZCAvPg0KICAgIDwvbGFiZWw+DQogICAgPGxhYmVsIGNsYXNzPSJmdWxsLWZpZWxkIj4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5Db3VudHJ5Kjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0iY291bnRyeSIgbmFtZT0iY291bnRyeSIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxidXR0b24gdHlwZT0iYnV0dG9uIiBjbGFzcz0ibXktYnV0dG9uIj5TYXZlIGFkZHJlc3M8L2J1dHRvbj4NCiAgICA8aW5wdXQgdHlwZT0icmVzZXQiIHZhbHVlPSJDbGVhciBmb3JtIiAvPg0KICA8L2Zvcm0+DQo8L3NwYW4+DQo=";
4
+
5
+ var stylesUrl = "data:text/css;base64,";
6
+
7
+ class AddressLookup extends CustomElementBase {
8
+ constructor() {
9
+ super(AddressLookup.Css, AddressLookup.Html);
10
+ }
11
+ initialise() {
12
+ const searchBox = this.root.querySelector('#street');
13
+ this.addressCompleter = new google.maps.places.Autocomplete(searchBox, {
14
+ componentRestrictions: { country: ['uk'] },
15
+ fields: ['address_components'],
16
+ types: ['address'],
17
+ });
18
+ this.addressCompleter.addListener('place_changed', this.onPlaceChanged);
19
+ }
20
+ onPlaceChanged() {
21
+ const place = this.addressCompleter.getPlace();
22
+ for (const component of place.address_components) {
23
+ const inputFieldId = this.getMatchingFieldId(component.types[0]);
24
+ if (inputFieldId) {
25
+ const input = document.querySelector(inputFieldId);
26
+ input.value = component.long_name;
27
+ }
28
+ }
29
+ }
30
+ getMatchingFieldId(componentType) {
31
+ switch (componentType) {
32
+ case 'street_number':
33
+ return '#house-number';
34
+ case 'route':
35
+ return '#street';
36
+ case 'postal_code':
37
+ return '#postcode';
38
+ case 'locality':
39
+ return '#town';
40
+ case 'postal_town':
41
+ return '#town';
42
+ case 'administrative_area_level_2':
43
+ return '#county';
44
+ case 'country':
45
+ return '#country';
46
+ default:
47
+ return '';
48
+ }
49
+ }
50
+ }
51
+ AddressLookup.Css = reduceCss(decode(stylesUrl));
52
+ AddressLookup.Html = reduceHtml(decode(markupUrl));
53
+
54
+ // If custom elements is supported
55
+ if ('customElements' in window) {
56
+ // If not already defined
57
+ if (!window.customElements.get('ne14-address')) {
58
+ window.customElements.define('ne14-address', AddressLookup);
59
+ const apiKey = 'AIzaSyDcwGyRxRbcNGWOFQVT87A1mkxEOfm8t0w';
60
+ const callback = 'apiCallback'; // `ne_address.${AddressLookup.name}.${AddressLookup.Cb.name}`;
61
+ const mapsScriptUrl = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&callback=${callback}&libraries=places&v=weekly`;
62
+ const mapsScript = document.createElement('script');
63
+ mapsScript.setAttribute('async', '');
64
+ mapsScript.setAttribute('src', mapsScriptUrl);
65
+ window[callback] = () => {
66
+ Array.from(document.querySelectorAll('ne14-address')).forEach((el) => {
67
+ el.initialise();
68
+ });
69
+ };
70
+ window.addEventListener('load', () => document.body.appendChild(mapsScript));
71
+ }
72
+ }
73
+
74
+ export { AddressLookup };
@@ -0,0 +1,107 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ne_address = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ class CustomElementBase extends HTMLElement {
8
+ constructor(css, html, mode = 'closed') {
9
+ super();
10
+ this.root = this.attachShadow({ mode });
11
+ this.root.innerHTML = html;
12
+ const style = document.createElement('style');
13
+ style.textContent = css;
14
+ this.root.appendChild(style);
15
+ }
16
+ }
17
+
18
+ function decode(b64) {
19
+ const bIndex = (b64 + '').indexOf('base64,');
20
+ return bIndex === -1 ? b64 : window.atob(b64.substring(bIndex + 7));
21
+ }
22
+ function reduceCss(cssIn) {
23
+ return cssIn
24
+ .replace(/\s+/g, ' ')
25
+ .replace(/([,{}:;])\s/g, '$1')
26
+ .replace(/\s([{])/g, '$1');
27
+ }
28
+ function reduceHtml(htmlIn) {
29
+ return htmlIn.replace(/\s+/g, ' ').replace(/(^|>)\s+(<|$)/g, '$1$2');
30
+ }
31
+
32
+ var markupUrl = "data:text/html;base64,PHNwYW4gY2xhc3M9InJvb3QiPg0KICA8Zm9ybSBhY3Rpb249IiIgbWV0aG9kPSJnZXQiIGF1dG9jb21wbGV0ZT0ib2ZmIj4NCiAgICA8bGFiZWwgY2xhc3M9InN1cGVyLXNsaW0tZmllbGQtbGVmdCI+DQogICAgICA8c3BhbiBjbGFzcz0iZm9ybS1sYWJlbCI+Tm8uKjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0iaG91c2UtbnVtYmVyIiBuYW1lPSJob3VzZS1udW1iZXIiIHJlcXVpcmVkIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZpZWxkLXJpZ2h0Ij4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5TdHJlZXQqPC9zcGFuPg0KICAgICAgPGlucHV0DQogICAgICAgIGlkPSJzdHJlZXQiDQogICAgICAgIG5hbWU9InN0cmVldCINCiAgICAgICAgcmVxdWlyZWQNCiAgICAgICAgYXV0b2NvbXBsZXRlPSJvZmYiDQogICAgICAgIHBsYWNlaG9sZGVyPSJUeXBlIHlvdXIgYWRkcmVzcy4uLiINCiAgICAgIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZ1bGwtZmllbGQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPkFkZHJlc3MgTGluZSAyPC9zcGFuPg0KICAgICAgPGlucHV0IGlkPSJhZGRyZXNzMiIgbmFtZT0iYWRkcmVzczIiIC8+DQogICAgPC9sYWJlbD4NCiAgICA8bGFiZWwgY2xhc3M9ImZ1bGwtZmllbGQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPlRvd24gLyBDaXR5Kjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0idG93biIgbmFtZT0idG93biIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxsYWJlbCBjbGFzcz0ic2xpbS1maWVsZC1sZWZ0Ij4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5Db3VudHkqPC9zcGFuPg0KICAgICAgPGlucHV0IGlkPSJjb3VudHkiIG5hbWU9ImNvdW50eSIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxsYWJlbCBjbGFzcz0iZmllbGQtcmlnaHQiPg0KICAgICAgPHNwYW4gY2xhc3M9ImZvcm0tbGFiZWwiPlBvc3Rjb2RlKjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0icG9zdGNvZGUiIG5hbWU9InBvc3Rjb2RlIiByZXF1aXJlZCAvPg0KICAgIDwvbGFiZWw+DQogICAgPGxhYmVsIGNsYXNzPSJmdWxsLWZpZWxkIj4NCiAgICAgIDxzcGFuIGNsYXNzPSJmb3JtLWxhYmVsIj5Db3VudHJ5Kjwvc3Bhbj4NCiAgICAgIDxpbnB1dCBpZD0iY291bnRyeSIgbmFtZT0iY291bnRyeSIgcmVxdWlyZWQgLz4NCiAgICA8L2xhYmVsPg0KICAgIDxidXR0b24gdHlwZT0iYnV0dG9uIiBjbGFzcz0ibXktYnV0dG9uIj5TYXZlIGFkZHJlc3M8L2J1dHRvbj4NCiAgICA8aW5wdXQgdHlwZT0icmVzZXQiIHZhbHVlPSJDbGVhciBmb3JtIiAvPg0KICA8L2Zvcm0+DQo8L3NwYW4+DQo=";
33
+
34
+ var stylesUrl = "data:text/css;base64,";
35
+
36
+ class AddressLookup extends CustomElementBase {
37
+ constructor() {
38
+ super(AddressLookup.Css, AddressLookup.Html);
39
+ }
40
+ initialise() {
41
+ const searchBox = this.root.querySelector('#street');
42
+ this.addressCompleter = new google.maps.places.Autocomplete(searchBox, {
43
+ componentRestrictions: { country: ['uk'] },
44
+ fields: ['address_components'],
45
+ types: ['address'],
46
+ });
47
+ this.addressCompleter.addListener('place_changed', this.onPlaceChanged);
48
+ }
49
+ onPlaceChanged() {
50
+ const place = this.addressCompleter.getPlace();
51
+ for (const component of place.address_components) {
52
+ const inputFieldId = this.getMatchingFieldId(component.types[0]);
53
+ if (inputFieldId) {
54
+ const input = document.querySelector(inputFieldId);
55
+ input.value = component.long_name;
56
+ }
57
+ }
58
+ }
59
+ getMatchingFieldId(componentType) {
60
+ switch (componentType) {
61
+ case 'street_number':
62
+ return '#house-number';
63
+ case 'route':
64
+ return '#street';
65
+ case 'postal_code':
66
+ return '#postcode';
67
+ case 'locality':
68
+ return '#town';
69
+ case 'postal_town':
70
+ return '#town';
71
+ case 'administrative_area_level_2':
72
+ return '#county';
73
+ case 'country':
74
+ return '#country';
75
+ default:
76
+ return '';
77
+ }
78
+ }
79
+ }
80
+ AddressLookup.Css = reduceCss(decode(stylesUrl));
81
+ AddressLookup.Html = reduceHtml(decode(markupUrl));
82
+
83
+ // If custom elements is supported
84
+ if ('customElements' in window) {
85
+ // If not already defined
86
+ if (!window.customElements.get('ne14-address')) {
87
+ window.customElements.define('ne14-address', AddressLookup);
88
+ const apiKey = 'AIzaSyDcwGyRxRbcNGWOFQVT87A1mkxEOfm8t0w';
89
+ const callback = 'apiCallback'; // `ne_address.${AddressLookup.name}.${AddressLookup.Cb.name}`;
90
+ const mapsScriptUrl = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&callback=${callback}&libraries=places&v=weekly`;
91
+ const mapsScript = document.createElement('script');
92
+ mapsScript.setAttribute('async', '');
93
+ mapsScript.setAttribute('src', mapsScriptUrl);
94
+ window[callback] = () => {
95
+ Array.from(document.querySelectorAll('ne14-address')).forEach((el) => {
96
+ el.initialise();
97
+ });
98
+ };
99
+ window.addEventListener('load', () => document.body.appendChild(mapsScript));
100
+ }
101
+ }
102
+
103
+ exports.AddressLookup = AddressLookup;
104
+
105
+ Object.defineProperty(exports, '__esModule', { value: true });
106
+
107
+ }));
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@ne1410s/address",
3
+ "version": "0.0.1",
4
+ "description": "An address lookup tool.",
5
+ "main": "dist/ne14_address.cjs.min.js",
6
+ "module": "dist/ne14_address.esm.min.js",
7
+ "browser": "dist/ne14_address.umd.min.js",
8
+ "displayName": "ne_address",
9
+ "types": "dist/index.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "dev": "npx rollup -c rollup.browser.config.js -m -w",
15
+ "clean": "npx rimraf dist",
16
+ "is-pretty": "npx prettier --list-different . --ignore-path .gitignore",
17
+ "prettify": "npx prettier --write . --ignore-path .gitignore",
18
+ "build": "npm run clean && npx rollup -c",
19
+ "test": "npm run is-pretty",
20
+ "publish-local": "npm run test && npm run build && npm pack",
21
+ "publish-patch": "npm run test && npm run build && npm version patch && npm publish --access public",
22
+ "publish-minor": "npm run test && npm run build && npm version minor && npm publish --access public",
23
+ "publish-major": "npm run test && npm run build && npm version major && npm publish --access public"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/ne1410s/elem.address.git"
28
+ },
29
+ "keywords": [
30
+ "ES",
31
+ "address",
32
+ "lookup"
33
+ ],
34
+ "author": "ne1410s <pgj646@gmail.com> (http://ne1410s.co.uk)",
35
+ "license": "MIT",
36
+ "bugs": {
37
+ "url": "https://github.com/ne1410s/elem.address/issues"
38
+ },
39
+ "homepage": "https://github.com/ne1410s/elem.address#readme",
40
+ "devDependencies": {
41
+ "@rollup/plugin-commonjs": "^22.0.2",
42
+ "@rollup/plugin-json": "^4.1.0",
43
+ "@rollup/plugin-node-resolve": "^13.3.0",
44
+ "@rollup/plugin-url": "^7.0.0",
45
+ "@types/google.maps": "^3.49.2",
46
+ "prettier": "^2.7.1",
47
+ "rimraf": "^3.0.1",
48
+ "rollup": "^2.78.0",
49
+ "rollup-plugin-typescript2": "^0.32.1",
50
+ "typescript": "^4.7.4"
51
+ },
52
+ "dependencies": {
53
+ "@ne1410s/cust-elems": "^0.3.0"
54
+ }
55
+ }