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 { replacePlaceholders } from '../utils/Template';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_cilet css = `
20fb726d48Sopenharmony_ci<style>
21fb726d48Sopenharmony_ci      :host{
22fb726d48Sopenharmony_ci          display: inline-flex;
23fb726d48Sopenharmony_ci          overflow: visible;
24fb726d48Sopenharmony_ci          cursor: pointer;
25fb726d48Sopenharmony_ci          position: relative;
26fb726d48Sopenharmony_ci          border-radius: 16px;
27fb726d48Sopenharmony_ci          outline: none;
28fb726d48Sopenharmony_ci          user-select:none;
29fb726d48Sopenharmony_ci          width: 75%;
30fb726d48Sopenharmony_ci          -webkit-user-select:none ;
31fb726d48Sopenharmony_ci          -moz-user-select:none;
32fb726d48Sopenharmony_ci      }
33fb726d48Sopenharmony_ci      :host(:not([border])),
34fb726d48Sopenharmony_ci      :host([border='true']){
35fb726d48Sopenharmony_ci          border: 1px solid var(--bark-prompt,#dcdcdc);
36fb726d48Sopenharmony_ci      }
37fb726d48Sopenharmony_ci      .multipleSelect{
38fb726d48Sopenharmony_ci          display: flex;
39fb726d48Sopenharmony_ci          width: 100%;
40fb726d48Sopenharmony_ci          z-index: 98;
41fb726d48Sopenharmony_ci          position: relative;
42fb726d48Sopenharmony_ci          padding: 3px 6px;
43fb726d48Sopenharmony_ci          font-size: 1rem;
44fb726d48Sopenharmony_ci          transition: all .3s;
45fb726d48Sopenharmony_ci          outline: none;
46fb726d48Sopenharmony_ci          user-select:none;
47fb726d48Sopenharmony_ci          align-items: center;
48fb726d48Sopenharmony_ci          justify-content: space-between;
49fb726d48Sopenharmony_ci          -webkit-user-select:none ;
50fb726d48Sopenharmony_ci          -moz-user-select:none;
51fb726d48Sopenharmony_ci      }
52fb726d48Sopenharmony_ci      input{
53fb726d48Sopenharmony_ci          display: inline-flex;
54fb726d48Sopenharmony_ci          width:100%;
55fb726d48Sopenharmony_ci          z-index: 8999;
56fb726d48Sopenharmony_ci          color: var(--dark-color2,rgba(0,0,0,0.9));
57fb726d48Sopenharmony_ci          background-color: transparent;
58fb726d48Sopenharmony_ci          border: 0;
59fb726d48Sopenharmony_ci          user-select:none;
60fb726d48Sopenharmony_ci          outline: none;
61fb726d48Sopenharmony_ci          cursor: pointer;
62fb726d48Sopenharmony_ci          -webkit-user-select:none ;
63fb726d48Sopenharmony_ci          -moz-user-select:none;
64fb726d48Sopenharmony_ci      }
65fb726d48Sopenharmony_ci       .body{
66fb726d48Sopenharmony_ci          max-height: {1};
67fb726d48Sopenharmony_ci          width: 100%;
68fb726d48Sopenharmony_ci          display: block;
69fb726d48Sopenharmony_ci          overflow: auto;
70fb726d48Sopenharmony_ci          position: absolute;
71fb726d48Sopenharmony_ci          bottom: 100%;
72fb726d48Sopenharmony_ci          padding-top: 5px;
73fb726d48Sopenharmony_ci          margin-top: 2px;
74fb726d48Sopenharmony_ci          transition: all 0.2s;
75fb726d48Sopenharmony_ci          flex-direction: column;
76fb726d48Sopenharmony_ci          transform-origin: bottom center;
77fb726d48Sopenharmony_ci          box-shadow: 0 5px 15px 0px #00000033;
78fb726d48Sopenharmony_ci          background-color: var(--dark-background4,#fff);
79fb726d48Sopenharmony_ci          border-radius: 2px;
80fb726d48Sopenharmony_ci          opacity: 0;
81fb726d48Sopenharmony_ci          z-index: 99;
82fb726d48Sopenharmony_ci          visibility: hidden;
83fb726d48Sopenharmony_ci      }
84fb726d48Sopenharmony_ci      :host([placement="bottom"]) .body{
85fb726d48Sopenharmony_ci          bottom:unset;
86fb726d48Sopenharmony_ci          top: 100%;
87fb726d48Sopenharmony_ci          transition: none;
88fb726d48Sopenharmony_ci          transform: none;
89fb726d48Sopenharmony_ci      }
90fb726d48Sopenharmony_ci      .body-bottom{
91fb726d48Sopenharmony_ci          top: 100%;
92fb726d48Sopenharmony_ci          transform-origin: top center;
93fb726d48Sopenharmony_ci          bottom: auto;
94fb726d48Sopenharmony_ci      }
95fb726d48Sopenharmony_ci      .multipleRoot input::-webkit-input-placeholder {
96fb726d48Sopenharmony_ci          color: var(--dark-color,#aab2bd);
97fb726d48Sopenharmony_ci      }
98fb726d48Sopenharmony_ci      :host([disabled]) {
99fb726d48Sopenharmony_ci         pointer-events: none;
100fb726d48Sopenharmony_ci         cursor: not-allowed;
101fb726d48Sopenharmony_ci         background-color: var(--dark-background1,#f5f5f5);
102fb726d48Sopenharmony_ci      }
103fb726d48Sopenharmony_ci      </style>
104fb726d48Sopenharmony_ci`;
105fb726d48Sopenharmony_ciconst initHtmlStyle = (height: string): string => {
106fb726d48Sopenharmony_ci  return replacePlaceholders(css, height);
107fb726d48Sopenharmony_ci};
108fb726d48Sopenharmony_ci
109fb726d48Sopenharmony_ci@element('lit-allocation-select')
110fb726d48Sopenharmony_ciexport class LitAllocationSelect extends BaseElement {
111fb726d48Sopenharmony_ci  private selectAllocationInputEl: HTMLInputElement | null | undefined;
112fb726d48Sopenharmony_ci  private selectAllocationInputContent: HTMLDivElement | undefined;
113fb726d48Sopenharmony_ci  private selectAllocationOptions: unknown;
114fb726d48Sopenharmony_ci  private processDataList: Array<string> = [];
115fb726d48Sopenharmony_ci
116fb726d48Sopenharmony_ci  static get observedAttributes(): string[] {
117fb726d48Sopenharmony_ci    return ['value', 'disabled', 'placeholder'];
118fb726d48Sopenharmony_ci  }
119fb726d48Sopenharmony_ci
120fb726d48Sopenharmony_ci  get defaultPlaceholder(): string {
121fb726d48Sopenharmony_ci    return this.getAttribute('placeholder') || '';
122fb726d48Sopenharmony_ci  }
123fb726d48Sopenharmony_ci
124fb726d48Sopenharmony_ci  get placeholder(): string {
125fb726d48Sopenharmony_ci    return this.getAttribute('placeholder') || this.defaultPlaceholder;
126fb726d48Sopenharmony_ci  }
127fb726d48Sopenharmony_ci
128fb726d48Sopenharmony_ci  set placeholder(selectAllocationValue) {
129fb726d48Sopenharmony_ci    this.setAttribute('placeholder', selectAllocationValue);
130fb726d48Sopenharmony_ci  }
131fb726d48Sopenharmony_ci
132fb726d48Sopenharmony_ci  get value(): string {
133fb726d48Sopenharmony_ci    return this.getAttribute('value') || '';
134fb726d48Sopenharmony_ci  }
135fb726d48Sopenharmony_ci
136fb726d48Sopenharmony_ci  set value(selectAllocationValue: string) {
137fb726d48Sopenharmony_ci    this.setAttribute('value', selectAllocationValue);
138fb726d48Sopenharmony_ci  }
139fb726d48Sopenharmony_ci
140fb726d48Sopenharmony_ci  set processData(value: Array<string>) {
141fb726d48Sopenharmony_ci    this.processDataList = value; // @ts-ignore
142fb726d48Sopenharmony_ci    this.selectAllocationOptions.innerHTML = '';
143fb726d48Sopenharmony_ci    value.forEach((item) => {
144fb726d48Sopenharmony_ci      let option = document.createElement('div');
145fb726d48Sopenharmony_ci      option.className = 'option';
146fb726d48Sopenharmony_ci      option.innerHTML = item;
147fb726d48Sopenharmony_ci      option.style.padding = '8px 10px'; // @ts-ignore
148fb726d48Sopenharmony_ci      this.selectAllocationOptions.appendChild(option);
149fb726d48Sopenharmony_ci      this.selectAllocationInputEl?.focus();
150fb726d48Sopenharmony_ci    });
151fb726d48Sopenharmony_ci  }
152fb726d48Sopenharmony_ci
153fb726d48Sopenharmony_ci  get placement(): string {
154fb726d48Sopenharmony_ci    return this.getAttribute('placement') || '';
155fb726d48Sopenharmony_ci  }
156fb726d48Sopenharmony_ci
157fb726d48Sopenharmony_ci  set placement(selectAllocationValuePlacement: string) {
158fb726d48Sopenharmony_ci    if (selectAllocationValuePlacement) {
159fb726d48Sopenharmony_ci      this.setAttribute('placement', selectAllocationValuePlacement);
160fb726d48Sopenharmony_ci    } else {
161fb726d48Sopenharmony_ci      this.removeAttribute('placement');
162fb726d48Sopenharmony_ci    }
163fb726d48Sopenharmony_ci  }
164fb726d48Sopenharmony_ci
165fb726d48Sopenharmony_ci  get listHeight(): string {
166fb726d48Sopenharmony_ci    return this.getAttribute('list-height') || '256px';
167fb726d48Sopenharmony_ci  }
168fb726d48Sopenharmony_ci
169fb726d48Sopenharmony_ci  set listHeight(value) {
170fb726d48Sopenharmony_ci    this.setAttribute('list-height', value);
171fb726d48Sopenharmony_ci  }
172fb726d48Sopenharmony_ci
173fb726d48Sopenharmony_ci  attributeChangedCallback(name: unknown, oldValue: unknown, newValue: unknown): void {
174fb726d48Sopenharmony_ci    switch (name) {
175fb726d48Sopenharmony_ci      case 'value': // @ts-ignore
176fb726d48Sopenharmony_ci        this.selectAllocationInputEl!.value = newValue;
177fb726d48Sopenharmony_ci        break;
178fb726d48Sopenharmony_ci      case 'placeholder': // @ts-ignore
179fb726d48Sopenharmony_ci        this.selectAllocationInputEl!.placeholder = newValue;
180fb726d48Sopenharmony_ci        break;
181fb726d48Sopenharmony_ci    }
182fb726d48Sopenharmony_ci  }
183fb726d48Sopenharmony_ci
184fb726d48Sopenharmony_ci  initElements(): void {
185fb726d48Sopenharmony_ci    this.selectAllocationInputContent = this.shadowRoot!.querySelector('.multipleSelect') as HTMLDivElement;
186fb726d48Sopenharmony_ci    this.addEventListener('click', () => {
187fb726d48Sopenharmony_ci      // @ts-ignore
188fb726d48Sopenharmony_ci      if (this.selectAllocationOptions.style.visibility === 'visible') {
189fb726d48Sopenharmony_ci        // @ts-ignore
190fb726d48Sopenharmony_ci        this.selectAllocationOptions.style.visibility = 'hidden'; // @ts-ignore
191fb726d48Sopenharmony_ci        this.selectAllocationOptions.style.opacity = '0';
192fb726d48Sopenharmony_ci      } else {
193fb726d48Sopenharmony_ci        this.showProcessList();
194fb726d48Sopenharmony_ci      }
195fb726d48Sopenharmony_ci      this.selectAllocationInputContent!.dispatchEvent(new CustomEvent('inputClick', {}));
196fb726d48Sopenharmony_ci    });
197fb726d48Sopenharmony_ci    this.addEventListener('focusout', (e) => {
198fb726d48Sopenharmony_ci      // @ts-ignore
199fb726d48Sopenharmony_ci      this.selectAllocationOptions.style.visibility = 'hidden'; // @ts-ignore
200fb726d48Sopenharmony_ci      this.selectAllocationOptions.style.opacity = '0';
201fb726d48Sopenharmony_ci    });
202fb726d48Sopenharmony_ci    this.initData();
203fb726d48Sopenharmony_ci  }
204fb726d48Sopenharmony_ci
205fb726d48Sopenharmony_ci  showProcessList(): void {
206fb726d48Sopenharmony_ci    setTimeout(() => {
207fb726d48Sopenharmony_ci      if (this.processDataList.length > 0) {
208fb726d48Sopenharmony_ci        // @ts-ignore
209fb726d48Sopenharmony_ci        this.selectAllocationOptions.style.visibility = 'visible'; // @ts-ignore
210fb726d48Sopenharmony_ci        this.selectAllocationOptions.style.opacity = '1';
211fb726d48Sopenharmony_ci      }
212fb726d48Sopenharmony_ci    }, 200);
213fb726d48Sopenharmony_ci  }
214fb726d48Sopenharmony_ci
215fb726d48Sopenharmony_ci  initHtml(): string {
216fb726d48Sopenharmony_ci    return `
217fb726d48Sopenharmony_ci        ${initHtmlStyle(this.listHeight)}
218fb726d48Sopenharmony_ci        <div class="multipleSelect" tabindex="0">
219fb726d48Sopenharmony_ci            <div class="multipleRoot" id="select" style="width:100%">
220fb726d48Sopenharmony_ci            <input id="singleInput" placeholder="${this.placeholder}"/>
221fb726d48Sopenharmony_ci        </div>
222fb726d48Sopenharmony_ci            <lit-icon class="icon" name='down' color="#c3c3c3"></lit-icon>
223fb726d48Sopenharmony_ci        </div>
224fb726d48Sopenharmony_ci        <div class="body body-bottom">
225fb726d48Sopenharmony_ci            <slot></slot>
226fb726d48Sopenharmony_ci            <slot name="footer"></slot>
227fb726d48Sopenharmony_ci        </div>
228fb726d48Sopenharmony_ci        `;
229fb726d48Sopenharmony_ci  }
230fb726d48Sopenharmony_ci
231fb726d48Sopenharmony_ci  connectedCallback(): void {
232fb726d48Sopenharmony_ci    this.selectAllocationInputEl!.onkeydown = (ev): void => {
233fb726d48Sopenharmony_ci      // @ts-ignore
234fb726d48Sopenharmony_ci      if (ev.key === '0' && ev.target.value.length === 1 && ev.target.value === '0') {
235fb726d48Sopenharmony_ci        ev.preventDefault();
236fb726d48Sopenharmony_ci      }
237fb726d48Sopenharmony_ci    };
238fb726d48Sopenharmony_ci  }
239fb726d48Sopenharmony_ci
240fb726d48Sopenharmony_ci  initData(): void {
241fb726d48Sopenharmony_ci    this.selectAllocationInputEl = this.shadowRoot!.querySelector('input');
242fb726d48Sopenharmony_ci    this.selectAllocationOptions = this.shadowRoot!.querySelector('.body') as HTMLDivElement;
243fb726d48Sopenharmony_ci    this.selectAllocationInputEl?.addEventListener('input', () => {
244fb726d48Sopenharmony_ci      let filter = [...this.shadowRoot!.querySelectorAll<HTMLDivElement>('.option')].filter((a: HTMLDivElement) => {
245fb726d48Sopenharmony_ci        if (a.textContent!.indexOf(this.selectAllocationInputEl!.value) <= -1) {
246fb726d48Sopenharmony_ci          a.style.display = 'none';
247fb726d48Sopenharmony_ci        } else {
248fb726d48Sopenharmony_ci          a.style.display = 'block';
249fb726d48Sopenharmony_ci        }
250fb726d48Sopenharmony_ci      });
251fb726d48Sopenharmony_ci      this.value = this.selectAllocationInputEl!.value;
252fb726d48Sopenharmony_ci      this.selectAllocationInputContent!.dispatchEvent(new CustomEvent('valuable', {}));
253fb726d48Sopenharmony_ci    });
254fb726d48Sopenharmony_ci    this.shadowRoot?.querySelectorAll('.option').forEach((a) => {
255fb726d48Sopenharmony_ci      a.addEventListener('mousedown', (e) => {
256fb726d48Sopenharmony_ci        a.dispatchEvent(
257fb726d48Sopenharmony_ci          new CustomEvent('onSelected', {
258fb726d48Sopenharmony_ci            detail: {
259fb726d48Sopenharmony_ci              selected: true,
260fb726d48Sopenharmony_ci              text: a.textContent,
261fb726d48Sopenharmony_ci            },
262fb726d48Sopenharmony_ci          })
263fb726d48Sopenharmony_ci        );
264fb726d48Sopenharmony_ci      });
265fb726d48Sopenharmony_ci      a.addEventListener('onSelected', (e: unknown) => {
266fb726d48Sopenharmony_ci        // @ts-ignore
267fb726d48Sopenharmony_ci        this.selectAllocationInputEl!.value = e.detail.text; // @ts-ignore
268fb726d48Sopenharmony_ci        this.value = e.detail.text;
269fb726d48Sopenharmony_ci        this.selectAllocationInputContent!.dispatchEvent(new CustomEvent('valuable', {}));
270fb726d48Sopenharmony_ci      });
271fb726d48Sopenharmony_ci    });
272fb726d48Sopenharmony_ci  }
273fb726d48Sopenharmony_ci}
274