1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
6
+ < title > OpenExchangeRates hyperHTML Demo</ title >
7
+ < link rel ="stylesheet " href ="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.1/css/bulma.min.css ">
8
+ < script defer src ="https://unpkg.com/hyperhtml@latest/min.js "> </ script >
9
+ < script defer src ="OpenExchangeRates.js "> </ script >
10
+ < script >
11
+ addEventListener ( 'DOMContentLoaded' , ( ) => {
12
+ var hyper = hyperHTML ;
13
+ var oer = OpenExchangeRates ( API_KEY ) ;
14
+ var render = hyper ( document . querySelector ( '#exchange' ) ) ;
15
+ var db = JSON . parse ( localStorage . getItem ( 'exchange' ) ||
16
+ '{"from": "USD", "to": "EUR"}' ) ;
17
+ update ( ) ;
18
+ addEventListener ( 'beforeunload' , ( ) => {
19
+ localStorage . setItem ( 'exchange' , JSON . stringify ( db ) ) ;
20
+ } ) ;
21
+
22
+ function convert ( amount , from , to ) {
23
+ oer . convert (
24
+ parseFloat ( db . amount = amount ) ,
25
+ db . from = from ,
26
+ db . to = to
27
+ ) . then ( value => update ( db . converted = value ) ) ;
28
+ }
29
+
30
+ function options ( selected ) {
31
+ return oer . currencies ( ) . then ( currencies =>
32
+ Object . keys ( currencies ) . map ( currency => hyper `
33
+ <option value=${ currency } selected=${ currency === selected } >
34
+ ${ currency }
35
+ </option>`
36
+ ) ) ;
37
+ }
38
+
39
+ function update ( ) {
40
+ render `
41
+ <div class="field has-addons">
42
+ <p class="control">
43
+ <input
44
+ value=${ db . amount }
45
+ oninput=${ e => convert ( e . currentTarget . value , db . from , db . to ) }
46
+ class="input" type="text" placeholder="amount">
47
+ </p>
48
+ <p class="control">
49
+ <span class="select">
50
+ <select disabled
51
+ onchange=${ e => convert ( db . amount , e . currentTarget . value , db . to ) } >
52
+ ${ options ( db . from ) }
53
+ </select>
54
+ </span>
55
+ </p>
56
+ </div>
57
+ <div class="field has-addons">
58
+ <p class="control">
59
+ <input value=${ db . converted }
60
+ class="input" type="text" placeholder="converted">
61
+ </p>
62
+ <p class="control">
63
+ <span class="select">
64
+ <select
65
+ onchange=${ e => convert ( db . amount , db . from , e . currentTarget . value ) } >
66
+ ${ options ( db . to ) }
67
+ </select>
68
+ </span>
69
+ </p>
70
+ </div>` ;
71
+ }
72
+ } , { once : true } ) ;
73
+ </ script >
74
+ < style >
75
+ section {
76
+ position : absolute;
77
+ right : 0 ;
78
+ left : 0 ;
79
+ bottom : 25% ;
80
+ }
81
+ # exchange {
82
+ display : inline-block;
83
+ }
84
+ p .logo {
85
+ margin-top : 25% ;
86
+ }
87
+ </ style >
88
+ </ head >
89
+ < body class ="has-text-centered ">
90
+ < span id ="logo "> </ span >
91
+ < p class ="logo is-size-1 "> 💱</ p >
92
+ < p class ="is-size-7 has-text-grey-light ">
93
+ powered by < a href ="https://viperhtml.js.org/ "> hyper(HTML)</ a >
94
+ </ p >
95
+ < section >
96
+ < div id ="exchange " class ="container "> </ div >
97
+ </ section >
98
+ < script > Object . defineProperty ( this , 'API_KEY' ,
99
+ { get :( ) => atob ( 'MGQxOTU1NWU1N2NiNDJjZGFkOTk4YmQwNjNiMzczN2I=' ) } ) </ script >
100
+ </ body >
101
+ </ html >
0 commit comments