aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Numpad.svelte
blob: bbafc19603d8905ff1d91a54ba1873a7ddf654ec (plain) (blame)
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
<script>
  import { onMount } from "svelte";
  import { fiat } from "$lib/store";
  import Left from "$comp/Left.svelte";
  import Icon from "$comp/Icon.svelte";
  import { f, s, focus, warning, sat, sats } from "$lib/utils";
  import { t } from "$lib/translations";

  export let amount = 0,
    currency,
    element,
    rate,
    submit = undefined;

  export let amountFiat = 0;

  let arrow = "<";

  $: update(amount, amountFiat);
  let update = (a, f) => {
    if ($fiat) {
      amount = Math.round(f / (rate / sats));
    } else {
      amountFiat = ((a * rate) / sats).toFixed(2);
    }
  };

  let loading = false;

  let symbol =
    {
      USD: "$",
      CAD: "$",
      AUD: "$",
      NZD: "$",
      MXN: "$",
      BRL: "R$",
      HKD: "$",
      TWD: "$",
      JPY: "¥",
      CNY: "¥",
      GBP: "£",
      EUR: "€",
      KRW: "₩",
    }[currency] || "";

  const numPad = ["1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "<"];

  const handleInput = (value) => {
    if (selecting) amount = 0;
    selecting = false;

    if ($fiat) {
      if (amountFiat === 0 && value !== "." && value !== "<" && value !== "0") {
        amountFiat = value;
      } else if ((amountFiat === 0 || amountFiat === "0") && value === "0") {
        return;
      } else if (amountFiat === 0 && value === ".") {
        amountFiat = "0.";
      } else if (
        amountFiat !== 0 &&
        amountFiat.includes(".") &&
        value === "."
      ) {
        return;
      } else if (value === "<") {
        if (amountFiat !== 0) {
          amountFiat = amountFiat.slice(0, amountFiat.length - 1);
          if (amountFiat.length === 0) {
            amountFiat = 0;
            amount = 0;
          }
        }
      } else if (
        value !== "." &&
        value !== "<" &&
        parseInt(amountFiat + value) > rate
      ) {
        warning($t("user.receive.lessThan1BTCWarning"));
      } else if (amountFiat !== 0 && amountFiat.match(/\.../)) {
        return;
      } else {
        amountFiat = amountFiat + value;
      }
    } else {
      if (value === ".") {
        return;
      } else if (!amount && value !== "<" && value !== "0") {
        amount = parseInt(value);
      } else if (value === "<") {
        if (amount !== 0) {
          amount = Math.floor(amount / 10);
          if (amount.length === 0) {
            amount = 0;
            amountFiat = 0;
          }
        }
      } else if (value !== "<" && parseInt(amount + value) > sats) {
        warning($t("user.receive.lessThan1BTCWarning"));
      } else {
        amount = parseInt(amount.toString() + value);
      }
    }
  };

  $: html = $fiat ? amountFiat : s(amount);

  let prev = "";

  let input = (e) => {
    if (prev === "0" && html !== "0.") {
      prev = "";
      html = html.replace("0", "");
    }

    let sel = getSelection();
    let i = sel.focusOffset;

    let clean = html
      .substr(0, 15)
      .replace(/[^0-9.,]+/g, "")
      .replace(".", "F")
      .replace(/\./g, "")
      .replace("F", ".");

    if ($fiat) clean = clean.replace(",", "");
    if (clean !== html) {
      html = clean;
      i = html.length;
    }

    if (amount > sats && html.length > prev.length) {
      html = prev;
      warning($t("user.receive.lessThan1BTCWarning"));
      i--;
    }

    if ($fiat) {
      amountFiat = html;
    } else {
      amount = parseInt(html.replace(/,/g, ""));
    }

    if (!amount) amount = null;

    setTimeout(() => {
      if (!$fiat) {
        let p = prev.split(",")[0].length;
        if (html.length > prev.length && p === 3) i++;
        if (html.length < prev.length && p === 1 && i > 1) i--;
      }

      let node = e.target.childNodes[0];
      let range = document.createRange();

      if (node) {
        range.setStart(node, i);
        range.setEnd(node, i);

        let sel = getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
      }

      prev = html.toString();
    }, 0);
  };

  let selecting;
  let select = (e) => {
    let range = document.createRange();
    range.selectNodeContents(e.target);
    let sel = getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
    selecting = true;
  };

  let blur = () => {
    if (!html) html = "0";
  };

  let keydown = (e) =>
    e.key === "Enter" && (e.preventDefault() || submit.click());

  let swap = () => {
    if ($fiat) {
      amount = parseInt((amountFiat / (rate / sats)).toFixed(0));
    } else {
      amountFiat =
        (amount * (rate / sats)).toFixed(2) > 0.0
          ? (amount * (rate / sats)).toFixed(2)
          : 0;
    }
    $fiat = !$fiat;
  };
</script>

<div class="flex justify-center items-center">
  <div class="space-y-5 w-full">
    <div class="text-center">
      <div
        class="text-5xl md:text-6xl font-semibold tracking-widest flex justify-center"
      >
        <div class="my-auto" class:text-5xl={!$fiat}>
          {#if $fiat}{symbol}{:else}
            <iconify-icon icon="ph:lightning-fill" class="text-yellow-300"
            ></iconify-icon>
          {/if}
        </div>
        <div
          use:focus
          contenteditable
          bind:innerHTML={html}
          on:focus={select}
          on:blur={blur}
          on:input={input}
          on:keydown={keydown}
          class="outline-none my-auto"
          bind:this={element}
        />
      </div>
      <div
        class="flex items-center justify-center text-2xl cursor-pointer"
        on:click|preventDefault|stopPropagation={swap}
      >
        {#if $fiat}
          <iconify-icon icon="ph:lightning-fill" class="text-yellow-300"
          ></iconify-icon>
          {s(amount)}
        {:else}
          {f(amountFiat, currency)} {currency}
        {/if}
      </div>
    </div>

    <div class="grid grid-cols-3 gap-2 w-full mx-auto">
      {#each numPad as value}
        {#if value === arrow}
          <button
            type="button"
            class="btn"
            on:click|preventDefault|stopPropagation={() => handleInput(value)}
          >
            <Left />
          </button>
        {:else}
          <button
            type="button"
            class="btn"
            on:click|preventDefault|stopPropagation={() => handleInput(value)}
            >{value}</button
          >
        {/if}
      {/each}
    </div>
  </div>
</div>