Debian Codegraph @

packages/labs/eleventy-plugin-lit/demo/_js/components.js

raw
1 /**
2 * @license
3 * Copyright 2021 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 import {LitElement, html, css} from 'lit';
8
9 export class CalcWC extends LitElement {
10 static styles = css`
11 :host {
12 display: inline-block;
13 margin: 5px;
14 border: 1px solid gray;
15 width: 120px;
16 }
17
18 .row {
19 display: flex;
20 }
21
22 .top {
23 flex-direction: column;
24 }
25
26 .input {
27 flex: 1;
28 max-width: 100%;
29 height: 30px;
30 box-sizing: border-box;
31 border: 0px;
32 text-align: right;
33 font-size: 2em;
34 color: gray;
35 }
36
37 .clear {
38 margin-left: auto;
39 background: transparent;
40 border: 0;
41 color: gray;
42 padding: 5px;
43 }
44
45 .digit,
46 .operation {
47 flex: 1;
48 height: 30px;
49 border: 0;
50 background: gray;
51 color: white;
52 transition: background 0.2s;
53 }
54
55 .digit:active,
56 .operation:active {
57 background: teal;
58 }
59
60 .operation {
61 background: black;
62 }
63 `;
64
65 static properties = {
66 value: {type: Number},
67 };
68
69 constructor() {
70 super();
71 this.value = 0;
72 }
73
74 operations = [];
75
76 render() {
77 const disableClear = this.value == '0';
78 return html`
79 <div @click=${this.handleClick}>
80 <div class="top row">
81 <input value=${this.value} class="input" />
82 <button class="clear" ?disabled=${disableClear}>clear</button>
83 </div>
84 <div class="row">
85 <button class="digit">7</button>
86 <button class="digit">8</button>
87 <button class="digit">9</button>
88 <button class="operation">÷</button>
89 </div>
90 <div class="row">
91 <button class="digit">4</button>
92 <button class="digit">5</button>
93 <button class="digit">6</button>
94 <button class="operation">×</button>
95 </div>
96 <div class="row">
97 <button class="digit">1</button>
98 <button class="digit">2</button>
99 <button class="digit">3</button>
100 <button class="operation">-</button>
101 </div>
102 <div class="row">
103 <button class="digit">.</button>
104 <button class="digit">0</button>
105 <button class="digit">=</button>
106 <button class="operation">+</button>
107 </div>
108 </div>
109 `;
110 }
111
112 handleClick(e) {
113 const value = e.target.textContent || 'error';
114 if (!isNaN(Number(value))) {
115 this.value = `${this.value === '0' ? '' : this.value}${value}`;
116 } else if (value === '.') {
117 if (this.value[this.value.length - 1] !== '.') {
118 this.value = `${this.value}${value}`;
119 }
120 } else if (value === 'clear') {
121 this.value = '0';
122 this.operations = [];
123 } else if (value === '=') {
124 this.operations.push(this.value);
125 this.value = this.equate();
126 this.operations = [];
127 } else {
128 this.operations.push(this.value, value);
129 this.value = '';
130 }
131 }
132
133 equate() {
134 const toSum = [];
135 let value = Number(this.operations.shift());
136 while (this.operations.length) {
137 const op = this.operations.shift();
138 const v = Number(this.operations.shift());
139 if (op === '×') {
140 value *= v;
141 } else if (op === '÷') {
142 value /= v;
143 } else if (op === '+') {
144 toSum.push(value);
145 value = v;
146 } else {
147 toSum.push(value);
148 value = -v;
149 }
150 }
151 return String(toSum.reduce((p, n) => p + n, value));
152 }
153 }
154 customElements.define('calc-wc', CalcWC);