-
Notifications
You must be signed in to change notification settings - Fork 218
/
index.html
563 lines (479 loc) · 13.9 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<html>
<head>
<title>libphonenumber-js</title>
<!-- React 16 -->
<script crossorigin src="https://unpkg.com/react@16.10.2/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16.10.2/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/babel-core@5.8.38/browser.min.js"></script>
<!-- PropTypes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.7.2/prop-types.min.js"></script>
<script src="./lib/input-format-react.min.js"></script>
<script>
window.InputFormatReactInput = window.InputFormat
</script>
<script src="./lib/input-format.min.js"></script>
<script src="./libphonenumber-max.js"></script>
<meta charset="utf-8"/>
</head>
<style>
body
{
padding: 40px;
}
body, input, button, textarea
{
font-family: Arial, Helvetica;
font-size: 20px;
}
input[type="tel"],
input[type="text"]
{
padding-bottom: 4px;
border: none;
border-bottom: 1px solid #afafaf;
outline: none;
}
input[type="tel"]:focus,
input[type="text"]:focus
{
border-bottom-color: #0093C4;
}
h2
{
margin-top: 4rem;
margin-bottom: 2rem;
font-size: 2rem;
font-weight: normal;
}
.section
{
margin-top: 20px;
}
a
{
text-decoration: none;
color: #0093C4;
}
a:hover
{
text-decoration: underline;
}
a.main
{
display: block;
font-size: 32px;
}
p
{
margin-top: 10px;
margin-bottom: 40px;
}
pre
{
margin-top: 50px;
margin-bottom: 40px;
padding: 20px;
border: 1px solid #cfcfcf;
border-radius: 4px;
background: #fafafa;
}
button
{
display: block;
margin-top: 10px;
background : none;
border : none;
outline : none;
padding : 0;
white-space: nowrap;
color: #0093C4;
}
.section__line
{
margin-top: 10px;
margin-bottom: 10px;
}
.find-numbers-text, .find-numbers-highlighted
{
display: block;
box-sizing: border-box;
width: 100%;
line-height: 1.35em;
}
.find-numbers-text
{
min-height: 250px;
padding: 15px;
margin-bottom: 40px;
}
.find-numbers-highlighted
{
margin-top: 20px;
margin-bottom: 20px;
}
.numbers-found li
{
margin-top: 15px;
margin-bottom: 15px;
}
mark
{
background-color: #b1d5e5;
border-radius: 5px;
padding-left: 5px;
padding-right: 5px;
padding-top: 3px;
padding-bottom: 3px;
}
.key-value__key
{
font-weight: bolder;
padding-right: 0.5em;
}
.key-value__key:after
{
display: inline;
content: ':';
font-weight: normal;
}
</style>
<body>
<a target="_blank" class="main" href="https://gitlab.com/catamphetamine/libphonenumber-js"><code>libphonenumber-js</code></a>
<p>A simpler and smaller rewrite of Google Android's <a target="_blank" href="https://github.com/googlei18n/libphonenumber">libphonenumber</a> library.</p>
<!--
<p>This is a demo for the new API. <a href="https://catamphetamine.gitlab.io/libphonenumber-js/legacy.html">See legacy API demo</a>.</p>
-->
<p>
Note: This demo uses "max" metadata set. <a href="https://gitlab.com/catamphetamine/libphonenumber-js#min-vs-max-vs-mobile-vs-core">See readme on "min" vs "max" vs "mobile"</a>.
</p>
<div id="content"></div>
<script type="text/babel">
function KeyValue({ name, value }) {
return (
<div className="key-value section__line">
<span className="key-value__key">{name}</span>
<span className="key-value__value"><code>{value}</code></span>
</div>
)
}
function PhoneNumber({ children: number }) {
const keyValues = [
['Number', number.number],
['Country', number.country || '—'],
['National', number.formatNational()],
['International', number.formatInternational()],
['URI', number.getURI()],
['Type', number.getType() || '—'],
['Possible', number.isPossible() ? 'true' : 'false'],
['Valid', number.isValid() ? 'true' : 'false']
]
return (
<div className="phone-number">
{keyValues.map(kv => <KeyValue key={kv[0]} name={kv[0]} value={kv[1]}/>)}
</div>
)
}
class Content extends React.Component {
constructor() {
super()
const { query } = parseLocationUrl(window.location.href)
this.state = {
parseCountry: query.parseCountry || 'US',
parseValue: query.parseValue || '213-373-4253',
asYouTypeCountry: query.asYouTypeCountry || 'US',
asYouTypeValue: query.asYouTypeValue || '', // '+12133734253',
findNumbersCountry: query.findNumbersCountry || 'US',
findNumbersText: query.findNumbersText || 'For more details call +7 (800) 555-35-35 internationally or reach the local US branch at (213) 373-4253 ext. 1234 for paid customer support.'
}
}
// Update the URL.
componentDidUpdate() {
const createUrl = () => {
const location = parseLocationUrl(window.location.href)
return getLocationUrl({
...location,
query: this.state
})
}
window.history.replaceState({}, '', createUrl())
}
render()
{
const ReactInput = InputFormatReactInput
const templateParser = InputFormat.templateParser
const templateFormatter = InputFormat.templateFormatter
const parseDigit = InputFormat.parseDigit
let asYouType
let parsedNumber
let numbersFound = []
const {
parseCountry,
parseValue,
asYouTypeCountry,
asYouTypeValue,
findNumbersCountry,
findNumbersText
} = this.state
parsedNumber = libphonenumber.parsePhoneNumberFromString(parseValue, parseCountry)
try {
asYouType = new libphonenumber.AsYouType(asYouTypeCountry)
asYouType.input(asYouTypeValue)
} catch (error) {
console.error(error)
}
try {
numbersFound = libphonenumber.findNumbers(findNumbersText, findNumbersCountry, { v2: true })
} catch (error) {
console.error(error)
}
return (
<div>
<h2 id="parse">Parse</h2>
<input
type="text"
value={this.state.parseCountry}
onChange={event => this.setState({ parseCountry: event.target.value.toUpperCase().trim() })}
style={{ width: '40px', marginRight: '10px' }}/>
<input
type="text"
value={this.state.parseValue}
onChange={event => this.setState({ parseValue: event.target.value })}/>
<div className="section">
{parsedNumber &&
<PhoneNumber>{parsedNumber}</PhoneNumber>
}
</div>
<h2 id="as-you-type">As you type</h2>
{/* type="tel" introduces bugs on Samsung Android phones */}
{/* https://github.com/catamphetamine/react-phone-number-input/issues/59 */}
<ReactInput
placeholder="Enter a phone number"
value={this.state.asYouTypeValue}
onChange={value => this.setState({ asYouTypeValue: value })}
parse={(character, value) => {
// Leading plus is allowed
if (character === '+') {
if (!value) {
return character
}
}
// Digits are allowed
return parseDigit(character)
}}
format={(value) => {
let text
let template
try {
const asYouType = new libphonenumber.AsYouType(this.state.asYouTypeCountry)
text = asYouType.input(value)
template = asYouType.getTemplate()
} catch (error) {
if (error.message.indexOf('Unknown country') === 0) {
text = value
template = value.replace(/./g, 'x')
} else {
throw error
}
}
return { text, template }
}}/>
<div className="section">
<div className="section__line">
Default country:
<input
type="text"
value={this.state.asYouTypeCountry}
onChange={event => this.setState({ asYouTypeCountry: event.target.value.toUpperCase().trim() })}
style={{ width: '40px', marginLeft: '8px' }}/>
</div>
<div className="section"/>
{asYouType && asYouType.getTemplate() &&
<KeyValue name="Template" value={asYouType.getTemplate()}/>
}
{asYouType && asYouType.getNumber() &&
<PhoneNumber>{asYouType.getNumber()}</PhoneNumber>
}
</div>
<h2 id="find-numbers">Find phone numbers</h2>
<div className="section">
<div className="section__line">
Default country:
<input
type="text"
value={this.state.findNumbersCountry}
onChange={event => this.setState({ findNumbersCountry: event.target.value.toUpperCase().trim() })}
style={{ width: '40px', marginLeft: '8px' }}/>
</div>
</div>
<div className="section"/>
<textarea
value={this.state.findNumbersText}
onChange={event => this.setState({ findNumbersText: event.target.value })}
className="find-numbers-text"/>
{ numbersFound.length === 0 && 'No phone numbers found' }
{ numbersFound.length > 0 && 'Phone numbers found: ' + numbersFound.length }
{ numbersFound.length > 0 &&
<ContentEditable
html={highlight_phones(this.state.findNumbersText, numbersFound)}
onChange={event => this.setState({ findNumbersText: strip_marks(event.target.value) })}
readOnly
className="find-numbers-highlighted" />
}
{ numbersFound.length > 0 &&
<ul className="numbers-found">
{numbersFound.map((number, i) => (
<li key={i}>
<strong>Number</strong>: <code>{number.number.number}</code>,{' '}
<strong>Country</strong>: <code>{number.number.country}</code>,{' '}
<strong>Starts at</strong>: <code>{number.startsAt}</code>,{' '}
<strong>Ends at</strong>: <code>{number.endsAt}</code>
</li>
))}
</ul>
}
</div>
)
}
}
// https://stackoverflow.com/questions/22677931/react-js-onchange-event-for-contenteditable/27255103#27255103
class ContentEditable extends React.Component {
render() {
return <div id="contenteditable"
onInput={this.emitChange}
onBlur={this.emitChange}
className={this.props.className}
contentEditable={!this.props.readOnly}
dangerouslySetInnerHTML={{__html: this.props.html}}></div>;
}
getDOMNode() {
return ReactDOM.findDOMNode(this);
}
shouldComponentUpdate(nextProps) {
return nextProps.html !== this.getDOMNode().innerHTML;
}
componentDidUpdate() {
if ( this.props.html !== this.getDOMNode().innerHTML ) {
this.getDOMNode().innerHTML = this.props.html;
}
}
emitChange() {
var html = this.getDOMNode().innerHTML;
if (this.props.onChange && html !== this.lastHtml) {
this.props.onChange({
target: {
value: html
}
});
}
this.lastHtml = html;
}
}
ReactDOM.render
(
<Content />,
document.getElementById('content')
)
function highlight_phones(html, phones)
{
let endedAt = 0
return phones.reduce((result, { startsAt, endsAt }) => {
const previousEndsAt = endedAt
endedAt = endsAt
return result +
html.slice(previousEndsAt, startsAt) +
'<mark>' +
html.slice(startsAt, endsAt) +
'</mark>'
}, '') + html.slice(endedAt)
}
function strip_marks(html)
{
return html.replace(/<\/?mark>/g, '')
}
function parseLocationUrl(url) {
let origin;
let pathname;
if (url[0] === '/') {
origin = '';
pathname = url;
} else {
const pathnameStartsAt = url.indexOf('/', url.indexOf('//') + '//'.length);
if (pathnameStartsAt > 0) {
origin = url.slice(0, pathnameStartsAt);
pathname = url.slice(pathnameStartsAt);
} else {
origin = url;
pathname = '/';
}
}
let search = '';
let hash = '';
const searchIndex = pathname.indexOf('?');
if (searchIndex >= 0) {
search = pathname.slice(searchIndex);
pathname = pathname.slice(0, searchIndex);
}
const hashIndex = search.indexOf('#');
if (hashIndex >= 0) {
hash = search.slice(hashIndex);
search = search.slice(0, hashIndex);
}
const query = parseSearch(search || '?');
return {
origin,
pathname,
// search,
query,
hash
};
}
function parseSearch(search) {
search = search.slice('?'.length);
// Return so that it doesn't return `{ '': undefined }`.
if (search.length === 0) {
return {};
}
const query = {};
for (const part of search.split('&')) {
let [key, value] = part.split('=');
if (value) {
key = decodeURIComponent(key);
value = decodeURIComponent(value);
query[key] = value;
}
}
return query;
}
function getLocationUrl(location) {
if (location.hasOwnProperty('search')) {
throw new Error('A `location` argument having a `search` property was passed to `getLocationUrl()`. Instead of passing a `location` having a `search` string, pass a `location` having a `query` object.');
}
const origin = location.origin || '';
const pathname = location.pathname;
// `query` has higher priority than `search`.
const search = stringifyLocationQuery(location.query || {});
const hash = location.hash || '';
return `${origin}${pathname}${search}${hash}`;
}
function stringifyLocationQuery(query) {
let result = '';
for (const key in query) {
if (!isEmpty(query[key])) {
if (result) {
result += '&';
} else {
result += '?';
}
result += encodeURIComponent(key);
result += '=';
result += encodeURIComponent(query[key]);
}
}
return result;
}
function isEmpty(value) {
return value === undefined || value === null || value === '';
}
</script>
</body>
</html>