/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BaseElement, element } from '../BaseElement.js';
@element('lit-slider')
export class LitSlider extends BaseElement {
private litSliderStyle: LitSliderStyle | undefined | null;
private litSlider: HTMLInputElement | undefined | null;
private litSliderCon: HTMLDivElement | undefined | null;
private litResult: HTMLInputElement | undefined | null;
private slotEl: HTMLSlotElement | undefined | null;
private currentValue: number = 0;
private defaultTimeText: string | undefined | null;
static get observedAttributes() {
return ['percent', 'disabled-X', 'custom-slider', 'custom-line', 'custom-button'];
}
get sliderStyle(): LitSliderStyle {
if (this.litSliderStyle) {
return this.litSliderStyle;
} else {
return {
minRange: 0,
maxRange: 100,
defaultValue: '0',
resultUnit: '',
stepSize: 1,
lineColor: 'var(--dark-color3,#46B1E3)',
buttonColor: '#999999',
};
}
}
set sliderStyle(value: LitSliderStyle) {
this.litSliderStyle = value;
this.currentValue = Number(value.defaultValue);
this.litSliderStyle.defaultValue = value.defaultValue;
if (this.litSliderStyle.resultUnit === 'h:m:s') {
let timeData = this.litSliderStyle.defaultValue.split(':');
let timeSize = Number(timeData[0]) * 3600 + Number(timeData[1]) * 60 + Number(timeData[2]);
this.defaultTimeText = timeSize.toString();
let defaultSize =
((timeSize - this.litSliderStyle.minRange) * 100) /
(this.litSliderStyle.maxRange - this.litSliderStyle.minRange);
this.litSlider!.style.backgroundSize = defaultSize + '%';
} else {
this.defaultTimeText = this.litSliderStyle.defaultValue;
this.litSlider!.style.backgroundSize = '0%';
if (Number(this.litSliderStyle.defaultValue)) {
let defaultSize =
((Number(this.litSliderStyle.defaultValue) - this.litSliderStyle.minRange) /
(this.litSliderStyle.maxRange - this.litSliderStyle.minRange)) *
100;
this.litSlider!.style.backgroundSize = defaultSize + '%';
}
}
let htmlInputElement = this.shadowRoot?.querySelector('#slider') as HTMLInputElement;
let attribute = htmlInputElement.getAttribute('type');
if (attribute === 'range') {
htmlInputElement!.setAttribute('value', this.defaultTimeText!);
htmlInputElement!.setAttribute('min', this.litSliderStyle!.minRange.toString());
htmlInputElement!.setAttribute('max', this.litSliderStyle!.maxRange.toString());
htmlInputElement!.setAttribute('step', this.litSliderStyle!.stepSize.toString());
}
}
get disabledX() {
return this.getAttribute('disabled-X') || '';
}
set disabledX(value: string) {
if (value) {
this.setAttribute('disabled-X', '');
} else {
this.removeAttribute('disabled-X');
}
}
get customSlider() {
return this.getAttribute('custom-slider') || '';
}
set customSlider(value: string) {
if (value) {
this.setAttribute('custom-slider', '');
} else {
this.removeAttribute('custom-slider');
}
}
get customLine() {
return this.getAttribute('custom-line') || '';
}
set customLine(value: string) {
this.setAttribute('custom-line', value);
}
get customButton() {
return this.getAttribute('custom-button') || '';
}
set customButton(value: string) {
this.setAttribute('custom-button', value);
}
get percent() {
return this.getAttribute('percent') || '';
}
set percent(value: string) {
this.setAttribute('percent', value);
let resultNumber =
((Number(value) - this.sliderStyle!.minRange) * 100) / (this.sliderStyle!.maxRange - this.sliderStyle!.minRange);
this.litSlider!.style.backgroundSize = resultNumber + '%';
}
get resultUnit() {
return this.getAttribute('resultUnit') || '';
}
set resultUnit(value: string) {
this.setAttribute('resultUnit', value);
}
initElements(): void {
this.litSlider = this.shadowRoot?.querySelector('#slider') as HTMLInputElement;
}
initHtml(): string {
return `