• 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/AlertMessage/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      showInSubWindow: true
109    });
110    this.dlpAlertDialog.open();
111  }
112
113  private splitContent() {
114    if (this.textContent) {
115      if (this.textContent.length > Constants.TIPS_NAME_LENGTH) {
116        return this.textContent.split('').join(Constants.TIPS_SPACE_ZERO)
117      } else if (this.textContent.length === Constants.TIPS_NAME_LENGTH) {
118        return this.textContent.split('').join(Constants.TIPS_SPACE_EM)
119      } else {
120        return Constants.TIPS_SPACE_EN + this.textContent;
121      }
122    }
123    return this.textContent;
124  }
125
126  private getFontWeight() {
127    try {
128      return (getContext(this) as common.UIAbilityContext).resourceManager.getNumber(
129        $r('sys.float.font_weight_semibold'));
130    } catch (error) {
131      HiLog.error(TAG, `getFontWeight callback getNumber failed: ${JSON.stringify(error)}.`);
132      return FontWeight.Regular;
133    }
134  }
135
136  async aboutToAppear() {
137    AccountManager.connectAbility(getContext(this) as common.UIAbilityContext);
138  }
139
140  build() {
141    Row() {
142      Stack() {
143        Circle({ width: Constants.HEADER_ROW_FONT_SIZE, height: Constants.HEADER_ROW_FONT_SIZE })
144          .fill(this.rollColor())
145          .opacity(Constants.FOOTER_OPACITY_MID);
146        Text(this.authAccount?.[0]?.toLocaleUpperCase())
147          .fontSize(Constants.STAFF_FONT_SIZE)
148          .fontWeight(this.getFontWeight())
149          .width(Constants.HEADER_ROW_FONT_SIZE)
150          .height(Constants.HEADER_ROW_FONT_SIZE)
151          .borderRadius(Constants.ENCRYPTION_STAFF_BORDER_RADIUS)
152          .fontColor($r('sys.color.ohos_fa_text_contrary'))
153          .textAlign(TextAlign.Center)
154          .opacity(Constants.FOOTER_OPACITY_MID)
155          .padding({ bottom: Constants.STAFF_FONT_PADDING });
156      }
157      .margin({
158        left: SystemUtils.isRTL() ? Constants.ADD_STAFF_ITEM_MARGIN_RIGHT : Constants.ADD_STAFF_ITEM_MARGIN_LEFT,
159        right: SystemUtils.isRTL() ? Constants.ADD_STAFF_ITEM_MARGIN_LEFT : Constants.ADD_STAFF_ITEM_MARGIN_RIGHT,
160      });
161      Text(this.splitContent())
162        .textOverflow({
163          overflow: TextOverflow.Ellipsis
164        })
165        .maxLines(Constants.PP_TEXT_MAX_LINE)
166        .fontSize($r('sys.float.ohos_id_text_size_button3'))
167        .height(Constants.HEADER_ROW_FONT_SIZE);
168    }
169    .onClick(async () => {
170      try {
171        let accountInfo: DomainAccountInfo = await AccountManager.getDomainAccountByAccountName(this.authAccount);
172        this.accountTipsArray = await AccountTipsConfig.getConfigTips();
173        this.handlePopupTips = !this.handlePopupTips;
174        this.accountTipsArray?.forEach((item: AccountTips) => {
175          item.value = accountInfo[item.key];
176        })
177      } catch (error) {
178        HiLog.error(TAG, `getFontWeight callback getNumber failed: ${JSON.stringify(error)}.`);
179      }
180    })
181    .hitTestBehavior(HitTestMode.Block)
182    .bindPopup(this.handlePopupTips, {
183      builder: this.popupBuilderTips,
184      placement: SystemUtils.isRTL() ? Placement.BottomRight : Placement.BottomLeft,
185      enableArrow: true,
186      showInSubWindow: false,
187      onStateChange: (e) => {
188        if (!e.isVisible) {
189          this.handlePopupTips = false;
190        }
191      }
192    })
193    .height(Constants.ENCRYPTION_ADD_STAFF_HEIGHT)
194    .alignItems(VerticalAlign.Center)
195    .backgroundColor(this.isActive && this.handlePopupTips ? '#1A0A59F7' : $r('sys.color.ohos_id_color_button_normal'))
196    .borderRadius(Constants.ENCRYPTION_SUCCESS_CHANGE_TOP)
197    .padding(
198      {
199        top: Constants.ENCRYPTION_STAFF_PAD,
200        bottom: Constants.ENCRYPTION_STAFF_PAD,
201        left: Constants.ENCRYPTION_STAFF_ITEM_MARGIN,
202        right: Constants.ENCRYPTION_STAFF_ITEM_MARGIN
203      }
204    )
205    .margin({
206      right: Constants.ENCRYPTION_ADD_STAFF_MARGIN_RIGHT,
207      top: Constants.ENCRYPTION_ADD_STAFF_MARGIN_BOTTOM
208    })
209  }
210}
211
212export { staffItem };
213