• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 account_osAccount from '@ohos.account.osAccount';
17import common from '@ohos.app.ability.common';
18import { BusinessError } from '@ohos.base';
19import Constants from '../../common/constant';
20import { AccountTips, AccountTipsConfig } from '../AccountTipsConfig';
21import { GetAlertMessage } from '../../common/GetAlertMessage';
22import { DlpAlertDialog } from '../../common/components/dlp_alert_dialog';
23import { HiLog } from '../../common/HiLog';
24import { SystemUtils } from '../../common/systemUtils';
25import AccountManager from '../../manager/AccountManager';
26import accountManager from '@ohos.enterprise.accountManager';
27import DomainAccountInfo from '../../bean/data/DomainAccountInfo';
28
29const TAG = 'staff';
30
31@Extend(Text)
32function tipsTextStyle() {
33  .fontSize($r('sys.float.ohos_id_text_size_body2'))
34  .fontColor($r('sys.color.ohos_id_color_text_primary'))
35  .textOverflow({
36    overflow: TextOverflow.Ellipsis
37  })
38  .maxLines(Constants.PP_TEXT_MAX_LINE)
39}
40
41@Component
42struct staffItem {
43  @Prop authAccount: string = '';
44  @Prop isActive: boolean = false;
45  @State color: Resource[] = [
46    $r('sys.color.multi_color_01'),
47    $r('sys.color.multi_color_02'),
48    $r('sys.color.multi_color_03'),
49    $r('sys.color.multi_color_04'),
50    $r('sys.color.multi_color_05'),
51    $r('sys.color.multi_color_06'),
52    $r('sys.color.multi_color_07'),
53    $r('sys.color.multi_color_08'),
54    $r('sys.color.multi_color_09'),
55    $r('sys.color.multi_color_10'),
56    $r('sys.color.multi_color_11'),
57  ];
58  @Prop changeIndex: number = 0;
59  @State domainTips: string = '';
60  @State accountNameTips: string = '';
61  @State handlePopupTips: boolean = false;
62
63  @Prop textContent: string = '';
64  @State accountTipsArray: AccountTips[] = [];
65  @State isHover: boolean = false;
66  dlpAlertDialog?: CustomDialogController;
67
68  @Builder popupBuilderTips() {
69    Column() {
70      ForEach(this.accountTipsArray, (item: AccountTips) => {
71        if (item.isShow) {
72          Row() {
73            if (AccountTipsConfig.isSysLanguage()) {
74              Text(item.description?.replace('%s', item.value)).tipsTextStyle()
75            } else {
76              Text(item.descriptionEn?.replace('%s', item.value)).tipsTextStyle()
77            }
78          }.height(Constants.TIPS_HEIGHT_ITEM)
79        }
80      }, (item: AccountTips) => item.key)
81    }
82    .alignItems(HorizontalAlign.Start)
83    .padding({
84      left: Constants.ROW_FONT_SIZE,
85      right: Constants.ROW_FONT_SIZE,
86      top: Constants.DA_MARGIN_TOP,
87      bottom: Constants.DA_MARGIN_TOP
88    })
89  }
90
91  private rollColor() {
92    while (this.changeIndex > this.color.length - 1) {
93      this.changeIndex = this.changeIndex - this.color.length
94    }
95    return this.color[this.changeIndex]
96  }
97
98  private showErrorDialog(title: Resource, message: Resource) {
99    this.dlpAlertDialog = new CustomDialogController({
100      builder: DlpAlertDialog({
101        title: title,
102        message: message,
103        action: () => {
104        }
105      }),
106      autoCancel: false,
107      customStyle: true,
108    });
109    this.dlpAlertDialog.open();
110  }
111
112  private splitContent() {
113    if (this.textContent) {
114      if (this.textContent.length > Constants.TIPS_NAME_LENGTH) {
115        return this.textContent.split('').join(Constants.TIPS_SPACE_ZERO)
116      } else if (this.textContent.length === Constants.TIPS_NAME_LENGTH) {
117        return this.textContent.split('').join(Constants.TIPS_SPACE_EM)
118      } else {
119        return Constants.TIPS_SPACE_EN + this.textContent;
120      }
121    }
122    return this.textContent;
123  }
124
125  private getFontWeight() {
126    try {
127      return (getContext(this) as common.UIAbilityContext).resourceManager.getNumber(
128        $r('sys.float.font_weight_semibold'));
129    } catch (error) {
130      HiLog.error(TAG, `getFontWeight callback getNumber failed: ${JSON.stringify(error)}.`);
131      return FontWeight.Regular;
132    }
133  }
134
135  async aboutToAppear() {
136    AccountManager.connectAbility(getContext(this) as common.UIAbilityContext);
137  }
138
139  build() {
140    Row() {
141      Stack() {
142        Circle({ width: Constants.HEADER_ROW_FONT_SIZE, height: Constants.HEADER_ROW_FONT_SIZE })
143          .fill(this.rollColor())
144          .opacity(Constants.FOOTER_OPACITY_MID);
145        Text(this.authAccount?.[0]?.toLocaleUpperCase())
146          .fontSize(Constants.STAFF_FONT_SIZE)
147          .fontWeight(this.getFontWeight())
148          .width(Constants.HEADER_ROW_FONT_SIZE)
149          .height(Constants.HEADER_ROW_FONT_SIZE)
150          .borderRadius(Constants.ENCRYPTION_STAFF_BORDER_RADIUS)
151          .fontColor($r('sys.color.ohos_fa_text_contrary'))
152          .textAlign(TextAlign.Center)
153          .opacity(Constants.FOOTER_OPACITY_MID)
154          .padding({ bottom: Constants.STAFF_FONT_PADDING });
155      }
156      .margin({
157        left: SystemUtils.isRTL() ? Constants.ADD_STAFF_ITEM_MARGIN_RIGHT : Constants.ADD_STAFF_ITEM_MARGIN_LEFT,
158        right: SystemUtils.isRTL() ? Constants.ADD_STAFF_ITEM_MARGIN_LEFT : Constants.ADD_STAFF_ITEM_MARGIN_RIGHT,
159      });
160      Text(this.splitContent())
161        .textOverflow({
162          overflow: TextOverflow.Ellipsis
163        })
164        .maxLines(Constants.PP_TEXT_MAX_LINE)
165        .fontSize($r('sys.float.ohos_id_text_size_button3'))
166        .height(Constants.HEADER_ROW_FONT_SIZE);
167    }
168    .onClick(async () => {
169      try {
170        let accountInfo: DomainAccountInfo = await AccountManager.getDomainAccountByAccountName(this.authAccount);
171        this.accountTipsArray = await AccountTipsConfig.getConfigTips();
172        this.handlePopupTips = !this.handlePopupTips;
173        this.accountTipsArray?.forEach((item: AccountTips) => {
174          item.value = accountInfo[item.key];
175        })
176      } catch (error) {
177        HiLog.error(TAG, `getFontWeight callback getNumber failed: ${JSON.stringify(error)}.`);
178      }
179    })
180    .hitTestBehavior(HitTestMode.Block)
181    .bindPopup(this.handlePopupTips, {
182      builder: this.popupBuilderTips,
183      placement: SystemUtils.isRTL() ? Placement.BottomRight : Placement.BottomLeft,
184      enableArrow: true,
185      showInSubWindow: false,
186      onStateChange: (e) => {
187        if (!e.isVisible) {
188          this.handlePopupTips = false;
189        }
190      }
191    })
192    .height(Constants.ENCRYPTION_ADD_STAFF_HEIGHT)
193    .alignItems(VerticalAlign.Center)
194    .backgroundColor(this.isActive && this.handlePopupTips ? '#1A0A59F7' : $r('sys.color.ohos_id_color_button_normal'))
195    .borderRadius(Constants.ENCRYPTION_SUCCESS_CHANGE_TOP)
196    .padding(
197      {
198        top: Constants.ENCRYPTION_STAFF_PAD,
199        bottom: Constants.ENCRYPTION_STAFF_PAD,
200        left: Constants.ENCRYPTION_STAFF_ITEM_MARGIN,
201        right: Constants.ENCRYPTION_STAFF_ITEM_MARGIN
202      }
203    )
204    .margin({
205      right: Constants.ENCRYPTION_ADD_STAFF_MARGIN_RIGHT,
206      top: Constants.ENCRYPTION_ADD_STAFF_MARGIN_BOTTOM
207    })
208  }
209}
210
211export { staffItem };
212