100aff185Sopenharmony_ci/*
200aff185Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400aff185Sopenharmony_ci * you may not use this file except in compliance with the License.
500aff185Sopenharmony_ci * You may obtain a copy of the License at
600aff185Sopenharmony_ci *
700aff185Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800aff185Sopenharmony_ci *
900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and
1300aff185Sopenharmony_ci * limitations under the License.
1400aff185Sopenharmony_ci */
1500aff185Sopenharmony_ciimport { BroadCast, BroadCastConstants, Constants, DateUtil, Log, ScreenManager, TimelineData } from '@ohos/common';
1600aff185Sopenharmony_ci
1700aff185Sopenharmony_ciconst TAG: string = 'timeline_TimelineScrollBar';
1800aff185Sopenharmony_ci
1900aff185Sopenharmony_ci@Component
2000aff185Sopenharmony_ciexport struct TimelineScrollBar {
2100aff185Sopenharmony_ci  scroller: Scroller | null = null;
2200aff185Sopenharmony_ci  @Consume @Watch('updateTotalNum') yearData: TimelineData[];
2300aff185Sopenharmony_ci  @State isClickScrollBar: boolean = false;
2400aff185Sopenharmony_ci  @Consume dateText: string;
2500aff185Sopenharmony_ci  @Consume broadCast: BroadCast;
2600aff185Sopenharmony_ci  @State totalNum: number = 0;
2700aff185Sopenharmony_ci  gridHeight: number = ScreenManager.getInstance()
2800aff185Sopenharmony_ci    .getWinHeight() - Constants.ActionBarHeight - Constants.TOOL_BAR_SIZE - Constants.SCROLL_MARGIN * 2;
2900aff185Sopenharmony_ci
3000aff185Sopenharmony_ci  aboutToAppear(): void {
3100aff185Sopenharmony_ci    this.updateTotalNum();
3200aff185Sopenharmony_ci  }
3300aff185Sopenharmony_ci
3400aff185Sopenharmony_ci  nodeGap(count: number): number {
3500aff185Sopenharmony_ci    Log.debug(TAG, `nodeGap start ${count} --- ${this.gridHeight} ---- ${this.totalNum}`)
3600aff185Sopenharmony_ci    let gapS = Math.ceil(this.gridHeight * count / this.totalNum - 48);
3700aff185Sopenharmony_ci    if (gapS >= Constants.SCROLL_BAR_SIDE_MIN_GAP) {
3800aff185Sopenharmony_ci      return gapS;
3900aff185Sopenharmony_ci    }
4000aff185Sopenharmony_ci
4100aff185Sopenharmony_ci    return -1;
4200aff185Sopenharmony_ci  }
4300aff185Sopenharmony_ci
4400aff185Sopenharmony_ci  updateTotalNum() {
4500aff185Sopenharmony_ci    let totalCount = 0;
4600aff185Sopenharmony_ci    for (let year of this.yearData) {
4700aff185Sopenharmony_ci      totalCount = totalCount + year.count;
4800aff185Sopenharmony_ci    }
4900aff185Sopenharmony_ci    this.totalNum = totalCount;
5000aff185Sopenharmony_ci  }
5100aff185Sopenharmony_ci
5200aff185Sopenharmony_ci  build() {
5300aff185Sopenharmony_ci    Stack({ alignContent: Alignment.End }) {
5400aff185Sopenharmony_ci      if (this.isClickScrollBar) {
5500aff185Sopenharmony_ci        Column() {
5600aff185Sopenharmony_ci          ForEach(this.yearData, (year: TimelineData, index?: number) => {
5700aff185Sopenharmony_ci            if (this.nodeGap(year.count) > 0 || this.yearData.indexOf(year) == 0 ||
5800aff185Sopenharmony_ci              this.yearData.indexOf(year) == this.yearData.length - 1) {
5900aff185Sopenharmony_ci              Row() {
6000aff185Sopenharmony_ci                Text(DateUtil.getLocalizedYear(year.startDate))
6100aff185Sopenharmony_ci                  .fontSize($r('sys.float.ohos_id_text_size_body3'))
6200aff185Sopenharmony_ci                  .fontFamily($r('app.string.id_text_font_family_medium'))
6300aff185Sopenharmony_ci                  .fontColor($r('sys.color.ohos_id_color_text_primary'))
6400aff185Sopenharmony_ci                  .key('TimelineScrollBar_Text_' + index)
6500aff185Sopenharmony_ci              }
6600aff185Sopenharmony_ci              .key('TimelineScrollBar_Row_' + index)
6700aff185Sopenharmony_ci              .height($r('app.float.scroll_bar_side_text_height_small'))
6800aff185Sopenharmony_ci              .backgroundColor($r('app.color.scroll_bar_side_text_small_color'))
6900aff185Sopenharmony_ci              .borderRadius($r('app.float.scroll_bar_side_text_radio'))
7000aff185Sopenharmony_ci              .padding({
7100aff185Sopenharmony_ci                left: $r('app.float.scroll_bar_side_text_padding_horizontal_small'),
7200aff185Sopenharmony_ci                right: $r('app.float.scroll_bar_side_text_padding_horizontal_small'),
7300aff185Sopenharmony_ci                top: $r('app.float.scroll_bar_side_text_padding_vertical_small'),
7400aff185Sopenharmony_ci                bottom: $r('app.float.scroll_bar_side_text_padding_vertical_small')
7500aff185Sopenharmony_ci              })
7600aff185Sopenharmony_ci              .shadow({
7700aff185Sopenharmony_ci                radius: $r('app.float.scroll_bar_side_text_shadow_radio'),
7800aff185Sopenharmony_ci                color: $r('app.color.scroll_bar_side_text_shadow_color'),
7900aff185Sopenharmony_ci                offsetX: $r('app.float.scroll_bar_side_text_shadow_offsetX'),
8000aff185Sopenharmony_ci                offsetY: $r('app.float.scroll_bar_side_text_shadow_offsetY'),
8100aff185Sopenharmony_ci              })
8200aff185Sopenharmony_ci              .margin({
8300aff185Sopenharmony_ci                bottom: this.nodeGap(year.count)
8400aff185Sopenharmony_ci              })
8500aff185Sopenharmony_ci            }
8600aff185Sopenharmony_ci          }, (year: TimelineData) => JSON.stringify(year))
8700aff185Sopenharmony_ci        }
8800aff185Sopenharmony_ci        .height('100%')
8900aff185Sopenharmony_ci        .margin({
9000aff185Sopenharmony_ci          right: $r('app.float.scroll_bar_margin_small'),
9100aff185Sopenharmony_ci          top: $r('sys.float.ohos_id_max_padding_start'),
9200aff185Sopenharmony_ci          bottom: $r('sys.float.ohos_id_max_padding_end')
9300aff185Sopenharmony_ci        })
9400aff185Sopenharmony_ci      }
9500aff185Sopenharmony_ci
9600aff185Sopenharmony_ci      ScrollBar({ scroller: this.scroller as Scroller, direction: ScrollBarDirection.Vertical,
9700aff185Sopenharmony_ci        state: BarState.Auto }) {
9800aff185Sopenharmony_ci        Row() {
9900aff185Sopenharmony_ci          if (this.isClickScrollBar) {
10000aff185Sopenharmony_ci            Row() {
10100aff185Sopenharmony_ci              Row() {
10200aff185Sopenharmony_ci                Text(this.dateText)
10300aff185Sopenharmony_ci                  .fontSize($r('sys.float.ohos_id_text_size_sub_title1'))
10400aff185Sopenharmony_ci                  .fontFamily($r('app.string.id_text_font_family_medium'))
10500aff185Sopenharmony_ci                  .fontColor($r('app.color.title_text_color'))
10600aff185Sopenharmony_ci                  .fontWeight(FontWeight.Medium)
10700aff185Sopenharmony_ci              }
10800aff185Sopenharmony_ci              .height($r('app.float.scroll_bar_side_text_height'))
10900aff185Sopenharmony_ci              .backgroundColor($r('app.color.scroll_bar_side_text_color'))
11000aff185Sopenharmony_ci              .borderRadius($r('app.float.scroll_bar_side_text_radio'))
11100aff185Sopenharmony_ci              .padding({
11200aff185Sopenharmony_ci                left: $r('app.float.scroll_bar_side_text_padding_horizontal'),
11300aff185Sopenharmony_ci                right: $r('app.float.scroll_bar_side_text_padding_horizontal'),
11400aff185Sopenharmony_ci                top: $r('app.float.scroll_bar_side_text_padding_vertical'),
11500aff185Sopenharmony_ci                bottom: $r('app.float.scroll_bar_side_text_padding_vertical')
11600aff185Sopenharmony_ci              })
11700aff185Sopenharmony_ci              .shadow({
11800aff185Sopenharmony_ci                radius: $r('app.float.scroll_bar_side_text_shadow_radio'),
11900aff185Sopenharmony_ci                color: $r('app.color.scroll_bar_side_text_shadow_color'),
12000aff185Sopenharmony_ci                offsetX: $r('app.float.scroll_bar_side_text_shadow_offsetX'),
12100aff185Sopenharmony_ci                offsetY: $r('app.float.scroll_bar_side_text_shadow_offsetY'),
12200aff185Sopenharmony_ci              })
12300aff185Sopenharmony_ci
12400aff185Sopenharmony_ci              Row() {
12500aff185Sopenharmony_ci                Image($r('app.media.scroll_press_light'))
12600aff185Sopenharmony_ci              }
12700aff185Sopenharmony_ci              .width($r('app.float.scroll_bar_big_width'))
12800aff185Sopenharmony_ci              .height($r('app.float.scroll_bar_big_height'))
12900aff185Sopenharmony_ci              .margin({ left: $r('app.float.scroll_bar_side_gap') })
13000aff185Sopenharmony_ci            }
13100aff185Sopenharmony_ci            .width($r('app.float.scroll_press_all_width'))
13200aff185Sopenharmony_ci            .justifyContent(FlexAlign.End)
13300aff185Sopenharmony_ci          } else {
13400aff185Sopenharmony_ci            Row() {
13500aff185Sopenharmony_ci              Image($r('app.media.scroll_light'))
13600aff185Sopenharmony_ci                .width($r('app.float.scroll_bar_small_width'))
13700aff185Sopenharmony_ci                .height($r('app.float.scroll_bar_small_height'))
13800aff185Sopenharmony_ci                .borderRadius(25)
13900aff185Sopenharmony_ci            }
14000aff185Sopenharmony_ci          }
14100aff185Sopenharmony_ci        }
14200aff185Sopenharmony_ci      }
14300aff185Sopenharmony_ci      .height('100%')
14400aff185Sopenharmony_ci      .width(this.isClickScrollBar ? $r('app.float.scroll_press_all_width') : $r('app.float.scroll_bar_small_width'))
14500aff185Sopenharmony_ci      .onTouch((event?: TouchEvent) => {
14600aff185Sopenharmony_ci        if (this.dateText == '') {
14700aff185Sopenharmony_ci          Log.debug(TAG, `dateText is null`)
14800aff185Sopenharmony_ci          this.broadCast.emit(BroadCastConstants.INIT_DATE_TEXT, [])
14900aff185Sopenharmony_ci        }
15000aff185Sopenharmony_ci        if ((event as TouchEvent).type == TouchType.Move && !this.isClickScrollBar) {
15100aff185Sopenharmony_ci          Log.debug(TAG, `scrollBar first TouchType.Move`);
15200aff185Sopenharmony_ci          this.isClickScrollBar = true;
15300aff185Sopenharmony_ci        } else if ((event as TouchEvent).type == TouchType.Up || (event as TouchEvent).type == TouchType.Cancel) {
15400aff185Sopenharmony_ci          Log.debug(TAG, `scrollBar TouchType.Up or Cancel. type=${(event as TouchEvent).type}`);
15500aff185Sopenharmony_ci          this.isClickScrollBar = false;
15600aff185Sopenharmony_ci        }
15700aff185Sopenharmony_ci      })
15800aff185Sopenharmony_ci    }
15900aff185Sopenharmony_ci    .hitTestBehavior(HitTestMode.Transparent)
16000aff185Sopenharmony_ci    .position({ x: '100%', y: 0 })
16100aff185Sopenharmony_ci    .markAnchor({
16200aff185Sopenharmony_ci      x: '100%',
16300aff185Sopenharmony_ci      y: 0
16400aff185Sopenharmony_ci    })
16500aff185Sopenharmony_ci  }
16600aff185Sopenharmony_ci}