1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { BaseElement, element } from '../BaseElement';
17fb726d48Sopenharmony_ciimport { LitRadioGroup } from './LitRadioGroup';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ciconst initHtmlStyle: string = `
20fb726d48Sopenharmony_ci    <style>
21fb726d48Sopenharmony_ci        :host([dis=round]):host{ 
22fb726d48Sopenharmony_ci            font-family: Helvetica,serif;
23fb726d48Sopenharmony_ci            font-size: 14px;
24fb726d48Sopenharmony_ci            color: var(--dark-color1,#212121);
25fb726d48Sopenharmony_ci            text-align: left;
26fb726d48Sopenharmony_ci            line-height: 16px;
27fb726d48Sopenharmony_ci            font-weight: 400;
28fb726d48Sopenharmony_ci        }
29fb726d48Sopenharmony_ci        :host([dis=round]) lit-icon{
30fb726d48Sopenharmony_ci           display: none;
31fb726d48Sopenharmony_ci        }
32fb726d48Sopenharmony_ci        :host([dis=round]) #radio{
33fb726d48Sopenharmony_ci            position:absolute;
34fb726d48Sopenharmony_ci            clip:rect(0,0,0,0);
35fb726d48Sopenharmony_ci        }
36fb726d48Sopenharmony_ci        :host([dis=round]) :host(:focus-within) .selected label:hover .selected{
37fb726d48Sopenharmony_ci            z-index:1;
38fb726d48Sopenharmony_ci            border-color:#a671ef;
39fb726d48Sopenharmony_ci        }
40fb726d48Sopenharmony_ci        :host([dis=round]) label{
41fb726d48Sopenharmony_ci            box-sizing:border-box;
42fb726d48Sopenharmony_ci            cursor:pointer;
43fb726d48Sopenharmony_ci            display:flex;
44fb726d48Sopenharmony_ci            align-items:center;
45fb726d48Sopenharmony_ci        }
46fb726d48Sopenharmony_ci        :host([dis=round]) .selected{
47fb726d48Sopenharmony_ci            position:relative;
48fb726d48Sopenharmony_ci            display: flex;
49fb726d48Sopenharmony_ci            box-sizing: border-box;
50fb726d48Sopenharmony_ci            width: 16px;
51fb726d48Sopenharmony_ci            height: 16px;
52fb726d48Sopenharmony_ci            border-radius:50%;
53fb726d48Sopenharmony_ci            border: 2px solid var(--dark-color1,#4D4D4D);
54fb726d48Sopenharmony_ci            margin-right:1em;    
55fb726d48Sopenharmony_ci        }
56fb726d48Sopenharmony_ci        .selected{
57fb726d48Sopenharmony_ci            position:relative;
58fb726d48Sopenharmony_ci            box-sizing: border-box;
59fb726d48Sopenharmony_ci            margin-right:1em;    
60fb726d48Sopenharmony_ci        }
61fb726d48Sopenharmony_ci        :host([dis=round]) .selected::before{
62fb726d48Sopenharmony_ci            content:'';
63fb726d48Sopenharmony_ci            width:6px;
64fb726d48Sopenharmony_ci            height:6px;
65fb726d48Sopenharmony_ci            margin:auto;
66fb726d48Sopenharmony_ci            border-radius:50%;
67fb726d48Sopenharmony_ci            background:#a671ef;
68fb726d48Sopenharmony_ci            transform: scale(0);
69fb726d48Sopenharmony_ci        }
70fb726d48Sopenharmony_ci        :host([dis=round]) .blue::before{
71fb726d48Sopenharmony_ci            background: #0a59f7;
72fb726d48Sopenharmony_ci        }
73fb726d48Sopenharmony_ci       :host([dis=round]) #radio:focus-visible+label .selected::after{
74fb726d48Sopenharmony_ci            transform:scale(2.5);
75fb726d48Sopenharmony_ci        }
76fb726d48Sopenharmony_ci        :host([dis=round]) #radio:checked+label .selected::before{
77fb726d48Sopenharmony_ci            transform: scale(1);
78fb726d48Sopenharmony_ci        }
79fb726d48Sopenharmony_ci        :host([dis=round]) #radio:checked+label .selected{
80fb726d48Sopenharmony_ci            border-color:#a671ef;
81fb726d48Sopenharmony_ci        }
82fb726d48Sopenharmony_ci        :host([dis=round]) #radio:checked+label .blue{
83fb726d48Sopenharmony_ci            border-color: #0a59f7;
84fb726d48Sopenharmony_ci        }
85fb726d48Sopenharmony_ci        :host([dis=check]):host{
86fb726d48Sopenharmony_ci           opacity: 0.9;
87fb726d48Sopenharmony_ci           font-family: Helvetica,serif;
88fb726d48Sopenharmony_ci           font-size: 14px;
89fb726d48Sopenharmony_ci           text-align: left;
90fb726d48Sopenharmony_ci           font-weight: 400;
91fb726d48Sopenharmony_ci        }
92fb726d48Sopenharmony_ci        :host([dis=round]) lit-icon{
93fb726d48Sopenharmony_ci           visibility: visible;
94fb726d48Sopenharmony_ci        }
95fb726d48Sopenharmony_ci        :host([dis=check]) #radio{
96fb726d48Sopenharmony_ci            position:absolute;
97fb726d48Sopenharmony_ci            clip:rect(0,0,0,0);
98fb726d48Sopenharmony_ci        }
99fb726d48Sopenharmony_ci        :host([dis=check]) label{
100fb726d48Sopenharmony_ci            box-sizing:border-box;
101fb726d48Sopenharmony_ci            cursor:pointer;
102fb726d48Sopenharmony_ci            display:flex;
103fb726d48Sopenharmony_ci            align-items:center;
104fb726d48Sopenharmony_ci        }
105fb726d48Sopenharmony_ci        :host([dis=check]) .selected{
106fb726d48Sopenharmony_ci            position:relative;
107fb726d48Sopenharmony_ci            display:flex;
108fb726d48Sopenharmony_ci            justify-content: center;
109fb726d48Sopenharmony_ci            align-items: center;
110fb726d48Sopenharmony_ci            margin-right:5px;
111fb726d48Sopenharmony_ci            width: 16px;
112fb726d48Sopenharmony_ci            height:16px;
113fb726d48Sopenharmony_ci        }
114fb726d48Sopenharmony_ci        :host([dis=check]) .selected::before{
115fb726d48Sopenharmony_ci            position:absolute;
116fb726d48Sopenharmony_ci            content:'';
117fb726d48Sopenharmony_ci            width:74%;
118fb726d48Sopenharmony_ci            height:0.15em;
119fb726d48Sopenharmony_ci            background:#fff;
120fb726d48Sopenharmony_ci            transform:scale(0);
121fb726d48Sopenharmony_ci            border-radius: 0.15em;
122fb726d48Sopenharmony_ci        }
123fb726d48Sopenharmony_ci        :host([dis=check]) .selected::after{
124fb726d48Sopenharmony_ci            content:'';
125fb726d48Sopenharmony_ci            position:absolute;
126fb726d48Sopenharmony_ci            width:100%;
127fb726d48Sopenharmony_ci            height:100%;
128fb726d48Sopenharmony_ci            border-radius:50%;
129fb726d48Sopenharmony_ci            background:#1A83FF;
130fb726d48Sopenharmony_ci            opacity:0.2;
131fb726d48Sopenharmony_ci            transform:scale(0);
132fb726d48Sopenharmony_ci            z-index:-1;
133fb726d48Sopenharmony_ci        }
134fb726d48Sopenharmony_ci        :host([dis=check]) #radio:checked:not(:indeterminate)+label .selected .icon{
135fb726d48Sopenharmony_ci            transform: scale(1.5);
136fb726d48Sopenharmony_ci        }
137fb726d48Sopenharmony_ci        :host([dis=check]) #radio:indeterminate+label .selected::before{
138fb726d48Sopenharmony_ci            transform:scale(1);
139fb726d48Sopenharmony_ci        }
140fb726d48Sopenharmony_ci        :host([dis=check]) .icon{
141fb726d48Sopenharmony_ci            width: 90%;
142fb726d48Sopenharmony_ci            height: 55%;
143fb726d48Sopenharmony_ci            margin-left: 5px;
144fb726d48Sopenharmony_ci            transform: scale(0);
145fb726d48Sopenharmony_ci        }
146fb726d48Sopenharmony_ci        :host([checked][dis=check]) {
147fb726d48Sopenharmony_ci            background-color: #1A83FF;
148fb726d48Sopenharmony_ci            color:#ffffff
149fb726d48Sopenharmony_ci        }
150fb726d48Sopenharmony_ci        :host([disabled]){ 
151fb726d48Sopenharmony_ci            pointer-events: none;
152fb726d48Sopenharmony_ci        }
153fb726d48Sopenharmony_ci        </style>
154fb726d48Sopenharmony_ci    `;
155fb726d48Sopenharmony_ci
156fb726d48Sopenharmony_ci@element('lit-radio')
157fb726d48Sopenharmony_ciexport class LitRadioBox extends BaseElement {
158fb726d48Sopenharmony_ci  private group: LitRadioGroup | undefined | null;
159fb726d48Sopenharmony_ci  private parent: LitRadioGroup | undefined | null;
160fb726d48Sopenharmony_ci  private radio: HTMLInputElement | undefined | null;
161fb726d48Sopenharmony_ci
162fb726d48Sopenharmony_ci  static get observedAttributes(): string[] {
163fb726d48Sopenharmony_ci    return ['checked', 'value', 'disabled'];
164fb726d48Sopenharmony_ci  }
165fb726d48Sopenharmony_ci
166fb726d48Sopenharmony_ci  get disabled(): boolean {
167fb726d48Sopenharmony_ci    return this.getAttribute('disabled') !== null;
168fb726d48Sopenharmony_ci  }
169fb726d48Sopenharmony_ci
170fb726d48Sopenharmony_ci  get checked(): boolean {
171fb726d48Sopenharmony_ci    return this.getAttribute('checked') !== null;
172fb726d48Sopenharmony_ci  }
173fb726d48Sopenharmony_ci
174fb726d48Sopenharmony_ci  get name(): string | null {
175fb726d48Sopenharmony_ci    return this.getAttribute('name');
176fb726d48Sopenharmony_ci  }
177fb726d48Sopenharmony_ci
178fb726d48Sopenharmony_ci  set checked(radioValue: boolean) {
179fb726d48Sopenharmony_ci    if (radioValue === null || !radioValue) {
180fb726d48Sopenharmony_ci      this.removeAttribute('checked');
181fb726d48Sopenharmony_ci    } else {
182fb726d48Sopenharmony_ci      this.setAttribute('checked', '');
183fb726d48Sopenharmony_ci    }
184fb726d48Sopenharmony_ci  }
185fb726d48Sopenharmony_ci
186fb726d48Sopenharmony_ci  get value(): string {
187fb726d48Sopenharmony_ci    let slot = this.shadowRoot?.getElementById('slot');
188fb726d48Sopenharmony_ci    return slot!.textContent || this.textContent || '';
189fb726d48Sopenharmony_ci  }
190fb726d48Sopenharmony_ci
191fb726d48Sopenharmony_ci  set disabled(value: boolean) {
192fb726d48Sopenharmony_ci    if (value === null || value === false) {
193fb726d48Sopenharmony_ci      this.removeAttribute('disabled');
194fb726d48Sopenharmony_ci    } else {
195fb726d48Sopenharmony_ci      this.setAttribute('disabled', '');
196fb726d48Sopenharmony_ci    }
197fb726d48Sopenharmony_ci  }
198fb726d48Sopenharmony_ci
199fb726d48Sopenharmony_ci  set dis(dis: string) {
200fb726d48Sopenharmony_ci    this.setAttribute('dis', dis);
201fb726d48Sopenharmony_ci  }
202fb726d48Sopenharmony_ci
203fb726d48Sopenharmony_ci  set value(value: string) {
204fb726d48Sopenharmony_ci    this.setAttribute('value', value);
205fb726d48Sopenharmony_ci  }
206fb726d48Sopenharmony_ci
207fb726d48Sopenharmony_ci  initHtml(): string {
208fb726d48Sopenharmony_ci    return `
209fb726d48Sopenharmony_ci        ${initHtmlStyle}
210fb726d48Sopenharmony_ci        <input type="checkbox" id="radio" >
211fb726d48Sopenharmony_ci        <label id="label" for="radio">
212fb726d48Sopenharmony_ci            <span class="selected">
213fb726d48Sopenharmony_ci            <lit-icon name="checkmark" class="icon" size="8">
214fb726d48Sopenharmony_ci            </lit-icon>
215fb726d48Sopenharmony_ci            </span>
216fb726d48Sopenharmony_ci            <slot id='slot'></slot>
217fb726d48Sopenharmony_ci        </label>
218fb726d48Sopenharmony_ci        `;
219fb726d48Sopenharmony_ci  }
220fb726d48Sopenharmony_ci
221fb726d48Sopenharmony_ci  initElements(): void {
222fb726d48Sopenharmony_ci    this.radio = this.shadowRoot?.getElementById('radio') as HTMLInputElement;
223fb726d48Sopenharmony_ci  }
224fb726d48Sopenharmony_ci
225fb726d48Sopenharmony_ci  connectedCallback(): void {
226fb726d48Sopenharmony_ci    this.group = this.closest('lit-radio-group') as LitRadioGroup;
227fb726d48Sopenharmony_ci    this.parent = this.group || this.getRootNode();
228fb726d48Sopenharmony_ci    this.radio = this.shadowRoot?.getElementById('radio') as HTMLInputElement;
229fb726d48Sopenharmony_ci    this.checked = this.checked;
230fb726d48Sopenharmony_ci    this.radio.addEventListener('change', () => {
231fb726d48Sopenharmony_ci      const selector = this.group ? 'lit-radio[checked]' : `lit-radio[name='${this.name}'][checked]`;
232fb726d48Sopenharmony_ci      const siblingNode = this.parent?.querySelector(selector) as LitRadioBox;
233fb726d48Sopenharmony_ci      if (siblingNode) {
234fb726d48Sopenharmony_ci        siblingNode.checked = false;
235fb726d48Sopenharmony_ci      }
236fb726d48Sopenharmony_ci      this.checked = true;
237fb726d48Sopenharmony_ci    });
238fb726d48Sopenharmony_ci  }
239fb726d48Sopenharmony_ci
240fb726d48Sopenharmony_ci  attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
241fb726d48Sopenharmony_ci    if (name === 'checked' && this.radio) {
242fb726d48Sopenharmony_ci      this.radio.checked = newValue !== null;
243fb726d48Sopenharmony_ci    }
244fb726d48Sopenharmony_ci    if (name === 'value') {
245fb726d48Sopenharmony_ci      let slot = this.shadowRoot?.getElementById('slot');
246fb726d48Sopenharmony_ci      slot!.textContent = newValue;
247fb726d48Sopenharmony_ci    }
248fb726d48Sopenharmony_ci  }
249fb726d48Sopenharmony_ci}
250