1/*
2 * Copyright (c) 2021-2023 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
16import { BreadData } from '../../../databases/model/FileData';
17
18@Styles
19function pressedStyles() {
20  .backgroundColor($r('app.color.hicloud_hmos_bg'))
21  .borderRadius($r('app.float.common_borderRadius8'))
22}
23
24@Styles
25function normalStyles() {
26  .backgroundColor($r('app.color.transparent_color'))
27  .borderRadius($r('app.float.common_borderRadius8'))
28}
29
30@Component
31export struct BreadCrumb {
32  @Link @Watch('onDireListUpdated') direList: BreadData[];
33  scroller: Scroller = new Scroller();
34
35  // 监听面包屑变化,滚动到指定位置
36  onDireListUpdated(): void {
37    setTimeout(() => {
38      this.scroller.scrollEdge(Edge.End);
39    }, 10);
40  }
41
42  build() {
43    Row() {
44      Row() {
45        Text($r('app.string.myPhone'))
46          .fontSize($r('app.float.common_font_size12'))
47          .opacity($r('app.float.common_opacity9'))
48          .height($r('app.float.common_size30'))
49          .fontColor(this.direList.length ? $r('app.color.detail_path_text_color') : $r('app.color.black'))
50          .textOverflow({ overflow: TextOverflow.Ellipsis })
51          .stateStyles({
52            pressed: pressedStyles,
53            normal: normalStyles
54          })
55        if (this.direList.length) {
56          Image($r('app.media.ic_arrow_right'))
57            .objectFit(ImageFit.Contain)
58            .height($r('app.float.common_size15'))
59            .width($r('app.float.common_size15'))
60            .interpolation(ImageInterpolation.Medium)
61        }
62      }
63      .onClick(() => {
64        if (!this.direList.length) {
65          return;
66        }
67        this.direList = [];
68      })
69
70      Scroll(this.scroller) {
71        Row() {
72          ForEach(this.direList, (item, index) => {
73            BreadCrumbItem({
74              direItem: item,
75              index: index,
76              direList: $direList,
77            })
78          }, item => item.url.toString())
79        }
80      }
81      .layoutWeight(1)
82      .align(Alignment.TopStart)
83      .scrollBar(BarState.Off)
84      .scrollable(ScrollDirection.Horizontal)
85    }
86    .padding({ left: $r('app.float.common_padding16'), right: $r('app.float.common_padding16') })
87  }
88}
89
90@Component
91struct BreadCrumbItem {
92  direItem: BreadData;
93  index: number;
94  @Link direList: BreadData[];
95
96  getTitle(breadCrumb: string) {
97    const breadCrumbMaxLength = 10;
98    return breadCrumb.length > breadCrumbMaxLength ? breadCrumb.substring(0, breadCrumbMaxLength) + '...' : breadCrumb;
99  }
100
101  isLast(): boolean {
102    return this.index === this.direList.length - 1;
103  }
104
105  build() {
106    Row() {
107      Text(this.getTitle('' + this.direItem.title))
108        .fontSize($r('app.float.common_font_size12'))
109        .opacity($r('app.float.common_opacity9'))
110        .fontColor(this.isLast() ? $r('app.color.black') : $r('app.color.detail_path_text_color'))
111        .height($r('app.float.common_size30'))
112        .margin({ left: $r('app.float.common_size4'), right: $r('app.float.common_size4') })
113        .textOverflow({ overflow: TextOverflow.Ellipsis })
114        .textAlign(TextAlign.Center)
115        .maxLines(1)
116        .stateStyles({
117          pressed: pressedStyles,
118          normal: normalStyles
119        })
120      if (!this.isLast()) {
121        Image($r('app.media.ic_arrow_right'))
122          .objectFit(ImageFit.Contain)
123          .autoResize(false)
124          .height($r('app.float.common_size15'))
125          .width($r('app.float.common_size15'))
126      }
127    }
128    .height($r('app.float.common_size30'))
129    .onClick(() => {
130      if (this.isLast()) {
131        return;
132      }
133      this.direList.splice(this.index + 1);
134    })
135  }
136}
137
138