• 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() {
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: any) {
44        if (val.startsWith('h')) {
45            this.shadowRoot!.querySelector('div')!.style.flexDirection = 'row'
46        } else if (val.startsWith('v')) {
47            this.shadowRoot!.querySelector('div')!.style.flexDirection = 'column'
48        }
49    }
50
51    connectedCallback() {
52
53    }
54
55    disconnectedCallback() {
56
57    }
58
59    attributeChangedCallback(name: any, oldValue: any, newValue: any) {
60        (this as any)[name] = newValue;
61    }
62
63    set style(v: any) {
64        // this.shadowRoot.querySelector('#root').setAttribute('style',v)
65    }
66}
67
68if (!customElements.get('lit-slicer')) {
69    customElements.define('lit-slicer', LitSlicer);
70}
71
72
73export class LitSlicerTrack extends HTMLElement {
74    private line: HTMLElement | null | undefined;
75    private draging: boolean = false;
76    private normalWidth: number = 0;
77
78    static get observedAttributes() {
79        return ["range-left", "range-right"]
80    }
81
82    get rangeLeft() {
83        return parseInt(this.getAttribute('range-left') || '200');
84    }
85
86    set rangeLeft(val: number) {
87        this.setAttribute('range-left', `${val}`);
88    }
89
90    get rangeRight() {
91        return parseInt(this.getAttribute('range-right') || '300');
92    }
93
94    set rangeRight(val: number) {
95        this.setAttribute('range-right', `${val}`);
96    }
97
98    constructor() {
99        super();
100        const shadowRoot = this.attachShadow({mode: 'open'});
101        shadowRoot.innerHTML = `
102        <style>
103        :host{
104            flex
105        }
106        .rootH{
107            width:var(--lit-slicer-track--width,5px);
108            background-color: var(--lit-slicer-track--background-color,#d1d1d1);
109            height: 100%;
110            cursor: ew-resize;
111        }
112        .rootV{
113            height:var(--lit-slicer-track--height,5px);
114            background-color: var(--lit-slicer-track--background-color,#d1d1d1);
115            width: 100%;
116            cursor: ns-resize;
117        }
118        </style>
119        <div id="root">
120        </div>
121        `
122    }
123
124    //当 custom element首次被插入文档DOM时,被调用。
125    connectedCallback() {
126        this.line = this.shadowRoot?.querySelector('#root')
127        let parentDirection = this.parentElement!.getAttribute("direction") || 'horizontal'
128        if (parentDirection.startsWith("h")) {
129            this.line!.className = 'rootH'
130            let previousElementSibling = this.previousElementSibling as HTMLElement;
131            let preX: number, preY: number, preWidth: number;
132            this.line!.onmousedown = (e) => {
133                this.draging = true;
134                preX = e.pageX;
135                preWidth = previousElementSibling!.clientWidth;
136                if (this.normalWidth == 0) this.normalWidth = previousElementSibling!.clientWidth;
137                previousElementSibling!.style.width = preWidth + 'px'
138                document.body.style.userSelect = 'none';
139                document.body.style.webkitUserSelect = 'none';
140                // @ts-ignore
141                document.body.style.msUserSelect = 'none';
142                document.onmousemove = (e1) => {
143                    if (this.draging) {
144                        if ((preWidth + e1.pageX - preX) >= (this.normalWidth - this.rangeLeft) && (preWidth + e1.pageX - preX) <= (this.normalWidth + this.rangeRight)) {
145                            previousElementSibling!.style.width = (preWidth + e1.pageX - preX) + 'px'
146                        }
147                    }
148                }
149                document.onmouseleave = (e2) => {
150                    this.draging = false;
151                    document.body.style.userSelect = 'auto';
152                    document.body.style.webkitUserSelect = 'auto';
153                    // @ts-ignore
154                    document.body.style.msUserSelect = 'auto';
155                }
156                document.onmouseup = (e3) => {
157                    this.draging = false;
158                    document.body.style.userSelect = 'auto';
159                    document.body.style.webkitUserSelect = 'auto';
160                    // @ts-ignore
161                    document.body.style.msUserSelect = 'auto';
162                }
163            }
164        } else {
165            this.line!.className = 'rootV'
166            let previousElementSibling = this.previousElementSibling as HTMLElement;
167            let preY: number, preHeight: number;
168            this.line!.onmousedown = (e) => {
169                this.draging = true;
170                preY = e.pageY;
171                preHeight = previousElementSibling?.clientHeight;
172                previousElementSibling!.style!.height = preHeight + 'px'
173                document.onmousemove = (e1) => {
174                    if (this.draging) {
175                        previousElementSibling.style.height = (preHeight + e1.pageY - preY) + 'px'
176                    }
177                }
178                document.onmouseleave = (e2) => {
179                    this.draging = false;
180                }
181                document.onmouseup = (e3) => {
182                    this.draging = false;
183                }
184            }
185        }
186
187    }
188
189    //当 custom element从文档DOM中删除时,被调用。
190    disconnectedCallback() {
191        this.line!.onmousedown = null;
192    }
193
194    //当 custom element被移动到新的文档时,被调用。
195    adoptedCallback() {
196    }
197
198    //当 custom element增加、删除、修改自身属性时,被调用。
199    attributeChangedCallback(name: any, oldValue: any, newValue: any) {
200    }
201}
202
203if (!customElements.get('lit-slicer-track')) {
204    customElements.define('lit-slicer-track', LitSlicerTrack);
205}