• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16export class LitSlicer extends HTMLElement {
17  static get observedAttributes(): string[] {
18    return ['direction']; //direction = 'horizontal'或者'vertical'
19  }
20
21  constructor() {
22    super();
23    const shadowRoot = this.attachShadow({ mode: 'open' });
24    shadowRoot.innerHTML = `
25        <style>
26        :host{
27            display: flex;
28        }
29
30        #root{
31            display: flex;
32            width: 100%;
33            height: 100%;
34            overflow: auto;
35        }
36        </style>
37        <div id="root"  style="${this.style}">
38            <slot></slot>
39        </div>
40        `;
41  }
42
43  set direction(val: unknown) {
44    // @ts-ignore
45    if (val.startsWith('h')) {
46      this.shadowRoot!.querySelector('div')!.style.flexDirection = 'row'; // @ts-ignore
47    } else if (val.startsWith('v')) {
48      this.shadowRoot!.querySelector('div')!.style.flexDirection = 'column';
49    }
50  }
51
52  connectedCallback(): void {}
53
54  disconnectedCallback(): void {}
55
56  attributeChangedCallback(name: unknown, oldValue: unknown, newValue: unknown): void {
57    // @ts-ignore
58    (this as unknown)[name] = newValue;
59  }
60  // @ts-ignore
61  set style(v: unknown) {}
62}
63
64if (!customElements.get('lit-slicer')) {
65  // @ts-ignore
66  customElements.define('lit-slicer', LitSlicer);
67}
68
69export class LitSlicerTrack extends HTMLElement {
70  private line: HTMLElement | null | undefined;
71  private draging: boolean = false;
72  private normalWidth: number = 0;
73  private rightWidth: number = 0;
74
75  static get observedAttributes(): string[] {
76    return ['range-left', 'range-right'];
77  }
78
79  get rangeLeft(): number {
80    return parseInt(this.getAttribute('range-left') || '200');
81  }
82
83  set rangeLeft(val: number) {
84    this.setAttribute('range-left', `${val}`);
85  }
86
87  get rangeRight(): number {
88    return parseInt(this.getAttribute('range-right') || '300');
89  }
90
91  set rangeRight(val: number) {
92    this.setAttribute('range-right', `${val}`);
93  }
94
95  constructor() {
96    super();
97    const shadowRoot = this.attachShadow({ mode: 'open' });
98    shadowRoot.innerHTML = `
99        <style>
100        :host{
101            flex
102        }
103        .rootH{
104            width:var(--lit-slicer-track--width,5px);
105            background-color: var(--lit-slicer-track--background-color,#d1d1d1);
106            height: 100%;
107            cursor: ew-resize;
108        }
109        .rootV{
110            height:var(--lit-slicer-track--height,5px);
111            background-color: var(--lit-slicer-track--background-color,#d1d1d1);
112            width: 100%;
113            cursor: ns-resize;
114        }
115        </style>
116        <div id="root">
117        </div>
118        `;
119  }
120
121  //当 custom element首次被插入文档DOM时,被调用。
122  connectedCallback(): void {
123    this.line = this.shadowRoot?.querySelector('#root');
124    let parentDirection = this.parentElement!.getAttribute('direction') || 'horizontal';
125    if (parentDirection.startsWith('h')) {
126      this.line!.className = 'rootH';
127      let previousElementSibling = this.previousElementSibling as HTMLElement;
128      let nextElementSibling = this.nextElementSibling as HTMLElement;
129      let preX: number;
130      let preY: number;
131      let preWidth: number;
132      let nextWidth: number;
133      this.line!.onmousedown = (e): void => {
134        this.draging = true;
135        preX = e.pageX;
136        preWidth = previousElementSibling!.clientWidth;
137        nextWidth = nextElementSibling!.clientWidth;
138        if (this.normalWidth === 0) {
139          this.normalWidth = previousElementSibling!.clientWidth;
140          this.rightWidth = nextElementSibling!.clientWidth;
141        }
142        previousElementSibling!.style.width = preWidth + 'px';
143        nextElementSibling!.style.width = nextWidth + 'px';
144        document.body.style.userSelect = 'none';
145        document.body.style.webkitUserSelect = 'none';
146        // @ts-ignore
147        document.body.style.msUserSelect = 'none';
148        document.onmousemove = (e1): void => {
149          if (this.draging) {
150            if (
151              preWidth + e1.pageX - preX >= this.normalWidth - this.rangeLeft &&
152              preWidth + e1.pageX - preX <= this.normalWidth + this.rangeRight
153            ) {
154              previousElementSibling!.style.width = preWidth + e1.pageX - preX + 'px';
155              nextElementSibling!.style.width = nextWidth + preX - e1.pageX + 'px';
156            } else {
157              this.draging = false;
158            }
159          }
160        };
161        document.onmouseleave = (e2): void => {
162          this.draging = false;
163          document.body.style.userSelect = 'auto';
164          document.body.style.webkitUserSelect = 'auto';
165          // @ts-ignore
166          document.body.style.msUserSelect = 'auto';
167        };
168        document.onmouseup = (e3): void => {
169          this.draging = false;
170          document.body.style.userSelect = 'auto';
171          document.body.style.webkitUserSelect = 'auto';
172          // @ts-ignore
173          document.body.style.msUserSelect = 'auto';
174        };
175      };
176    } else {
177      this.isDirection();
178    }
179  }
180
181  isDirection(): void {
182    this.line!.className = 'rootV';
183    let previousElementSibling = this.previousElementSibling as HTMLElement;
184    let preY: number;
185    let preHeight: number;
186    this.line!.onmousedown = (e): void => {
187      this.draging = true;
188      preY = e.pageY;
189      preHeight = previousElementSibling?.clientHeight;
190      previousElementSibling!.style!.height = preHeight + 'px';
191      document.onmousemove = (e1): void => {
192        if (this.draging) {
193          previousElementSibling.style.height = preHeight + e1.pageY - preY + 'px';
194        }
195      };
196      document.onmouseleave = (e2): void => {
197        this.draging = false;
198      };
199      document.onmouseup = (e3): void => {
200        this.draging = false;
201      };
202    };
203  }
204
205  //当 custom element从文档DOM中删除时,被调用。
206  disconnectedCallback(): void {
207    this.line!.onmousedown = null;
208  }
209
210  //当 custom element被移动到新的文档时,被调用。
211  adoptedCallback(): void {}
212
213  //当 custom element增加、删除、修改自身属性时,被调用。
214  attributeChangedCallback(name: unknown, oldValue: unknown, newValue: unknown): void {}
215}
216
217if (!customElements.get('lit-slicer-track')) {
218  customElements.define('lit-slicer-track', LitSlicerTrack);
219}
220