1/*
2 * Copyright (c) 2023-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
16if (!('finalizeConstruction' in ViewPU.prototype)) {
17    Reflect.set(ViewPU.prototype, 'finalizeConstruction', () => {
18    });
19}
20if (PUV2ViewBase.contextStack === undefined) {
21    Reflect.set(PUV2ViewBase, 'contextStack', []);
22}
23const curves = requireNativeModule('ohos.curves');
24const hilog = requireNapi('ohos.hilog');
25const START_TIME = 250;
26const END_TIME = 200;
27const BORDER_RADIUS = 12;
28const ZINDEX_NUM = 9;
29
30export var MarginType;
31(function (MarginType) {
32    MarginType[MarginType.DEFAULT_MARGIN = 0] = 'DEFAULT_MARGIN';
33    MarginType[MarginType.FIT_MARGIN = 1] = 'FIT_MARGIN';
34})(MarginType || (MarginType = {}));
35
36export class ExceptionPrompt extends ViewPU {
37    constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) {
38        super(parent, __localStorage, elmtId, extraInfo);
39        if (typeof paramsLambda === 'function') {
40            this.paramsGenerator_ = paramsLambda;
41        }
42        this.__options = new SynchedPropertyObjectOneWayPU(params.options, this, 'options');
43        this.touchBackgroundColor = {
44            'id': -1,
45            'type': 10001,
46            params: ['sys.color.ohos_id_color_sub_background_transparent'],
47            'bundleName': '__harDefaultBundleName__',
48            'moduleName': '__harDefaultModuleName__'
49        };
50        this.maxAppFontScale = 1;
51        this.isFollowingSystemFontScale = false;
52        this.onTipClick = () => {
53        };
54        this.onActionTextClick = () => {
55        };
56        this.setInitiallyProvidedValue(params);
57        this.finalizeConstruction();
58    }
59
60    setInitiallyProvidedValue(params) {
61        if (params.touchBackgroundColor !== undefined) {
62            this.touchBackgroundColor = params.touchBackgroundColor;
63        }
64        if (params.maxAppFontScale !== undefined) {
65            this.maxAppFontScale = params.maxAppFontScale;
66        }
67        if (params.isFollowingSystemFontScale !== undefined) {
68            this.isFollowingSystemFontScale = params.isFollowingSystemFontScale;
69        }
70        if (params.onTipClick !== undefined) {
71            this.onTipClick = params.onTipClick;
72        }
73        if (params.onActionTextClick !== undefined) {
74            this.onActionTextClick = params.onActionTextClick;
75        }
76    }
77
78    updateStateVars(params) {
79        this.__options.reset(params.options);
80    }
81
82    purgeVariableDependenciesOnElmtId(rmElmtId) {
83        this.__options.purgeDependencyOnElmtId(rmElmtId);
84    }
85
86    aboutToBeDeleted() {
87        this.__options.aboutToBeDeleted();
88        SubscriberManager.Get().delete(this.id__());
89        this.aboutToBeDeletedInternal();
90    }
91
92    get options() {
93        return this.__options.get();
94    }
95
96    set options(newValue) {
97        this.__options.set(newValue);
98    }
99
100    TextBuilder(parent = null) {
101        this.observeComponentCreation2((elmtId, isInitialRender) => {
102            Flex.create({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign
103                .Center });
104            Flex.padding({
105                left: {
106                    'id': -1,
107                    'type': 10002,
108                    params: ['sys.float.ohos_id_notification_margin_start'],
109                    'bundleName': '__harDefaultBundleName__',
110                    'moduleName': '__harDefaultModuleName__'
111                },
112                right: {
113                    'id': -1,
114                    'type': 10002,
115                    params: ['sys.float.ohos_id_text_paragraph_margin_s'],
116                    'bundleName': '__harDefaultBundleName__',
117                    'moduleName': '__harDefaultModuleName__'
118                },
119                top: {
120                    'id': -1,
121                    'type': 10002,
122                    params: ['sys.float.ohos_id_default_padding_start'],
123                    'bundleName': '__harDefaultBundleName__',
124                    'moduleName': '__harDefaultModuleName__'
125                },
126                bottom: {
127                    'id': -1,
128                    'type': 10002,
129                    params: ['sys.float.ohos_id_default_padding_end'],
130                    'bundleName': '__harDefaultBundleName__',
131                    'moduleName': '__harDefaultModuleName__'
132                }
133            });
134        }, Flex);
135        this.observeComponentCreation2((elmtId, isInitialRender) => {
136            Row.create();
137            Row.padding({
138                right: {
139                    'id': -1,
140                    'type': 10002,
141                    params: ['sys.float.ohos_id_default_padding_end'],
142                    'bundleName': '__harDefaultBundleName__',
143                    'moduleName': '__harDefaultModuleName__'
144                }
145            });
146            Row.width('100%');
147            Row.onClick(() => {
148                this.onTipClick();
149            });
150        }, Row);
151        this.observeComponentCreation2((elmtId, isInitialRender) => {
152            Image.create(this.options.icon ?? {
153                'id': -1,
154                'type': 20000,
155                params: ['sys.media.ohos_ic_public_fail'],
156                'bundleName': '__harDefaultBundleName__',
157                'moduleName': '__harDefaultModuleName__'
158            });
159            Image.width('24vp');
160            Image.height('24vp');
161            Image.fillColor({
162                'id': -1,
163                'type': 10001,
164                params: ['sys.color.ohos_id_color_warning'],
165                'bundleName': '__harDefaultBundleName__',
166                'moduleName': '__harDefaultModuleName__'
167            });
168        }, Image);
169        this.observeComponentCreation2((elmtId, isInitialRender) => {
170            Text.create(this.options.tip);
171            Text.fontSize({
172                'id': -1,
173                'type': 10002,
174                params: ['sys.float.ohos_id_text_size_body1'],
175                'bundleName': '__harDefaultBundleName__',
176                'moduleName': '__harDefaultModuleName__'
177            });
178            Text.minFontScale(1);
179            Text.maxFontScale(Math.min(this.updateFontScale(), 2));
180            Text.fontColor({
181                'id': -1,
182                'type': 10001,
183                params: ['sys.color.ohos_id_color_warning'],
184                'bundleName': '__harDefaultBundleName__',
185                'moduleName': '__harDefaultModuleName__'
186            });
187            Text.textOverflow({ overflow: TextOverflow.Ellipsis });
188            Text.maxLines(2);
189            Text.margin({
190                left: {
191                    'id': -1,
192                    'type': 10002,
193                    params: ['sys.float.ohos_id_dialog_margin_end'],
194                    'bundleName': '__harDefaultBundleName__',
195                    'moduleName': '__harDefaultModuleName__'
196                },
197                right: {
198                    'id': -1,
199                    'type': 10002,
200                    params: ['sys.float.ohos_id_dialog_margin_end'],
201                    'bundleName': '__harDefaultBundleName__',
202                    'moduleName': '__harDefaultModuleName__'
203                }
204            });
205        }, Text);
206        Text.pop();
207        Row.pop();
208        this.observeComponentCreation2((elmtId, isInitialRender) => {
209            If.create();
210            if (this.options.actionText) {
211                this.ifElseBranchUpdateFunction(0, () => {
212                    this.observeComponentCreation2((elmtId, isInitialRender) => {
213                        Button.createWithChild({ stateEffect: true, type: ButtonType.Normal });
214                        Button.backgroundColor(this.touchBackgroundColor);
215                        Button.width(this.options.actionText ? 144 : 0);
216                        Button.borderRadius({
217                            'id': -1,
218                            'type': 10002,
219                            params: ['sys.float.ohos_id_corner_radius_subtab'],
220                            'bundleName': '__harDefaultBundleName__',
221                            'moduleName': '__harDefaultModuleName__'
222                        });
223                        Button.padding({
224                            right: {
225                                'id': -1,
226                                'type': 10002,
227                                params: ['sys.float.ohos_id_elements_margin_vertical_l'],
228                                'bundleName': '__harDefaultBundleName__',
229                                'moduleName': '__harDefaultModuleName__'
230                            },
231                            left: {
232                                'id': -1,
233                                'type': 10002,
234                                params: ['sys.float.ohos_id_elements_margin_vertical_l'],
235                                'bundleName': '__harDefaultBundleName__',
236                                'moduleName': '__harDefaultModuleName__'
237                            }
238                        });
239                        Button.onTouch((event) => {
240                            if (event.type === TouchType.Down) {
241                                this.touchBackgroundColor = {
242                                    'id': -1,
243                                    'type': 10001,
244                                    params: ['sys.color.ohos_id_color_click_effect'],
245                                    'bundleName': '__harDefaultBundleName__',
246                                    'moduleName': '__harDefaultModuleName__'
247                                };
248                                this.onActionTextClick();
249                            } else if (event.type === TouchType.Up) {
250                                this.touchBackgroundColor = {
251                                    'id': -1,
252                                    'type': 10001,
253                                    params: ['sys.color.ohos_id_color_sub_background_transparent'],
254                                    'bundleName': '__harDefaultBundleName__',
255                                    'moduleName': '__harDefaultModuleName__'
256                                };
257                            }
258                        });
259                    }, Button);
260                    this.observeComponentCreation2((elmtId, isInitialRender) => {
261                        Row.create();
262                    }, Row);
263                    this.observeComponentCreation2((elmtId, isInitialRender) => {
264                        Text.create(this.options.actionText);
265                        Text.fontSize({
266                            'id': -1,
267                            'type': 10002,
268                            params: ['sys.float.ohos_id_text_size_body2'],
269                            'bundleName': '__harDefaultBundleName__',
270                            'moduleName': '__harDefaultModuleName__'
271                        });
272                        Text.minFontScale(1);
273                        Text.maxFontScale(Math.min(this.updateFontScale(), 2));
274                        Text.fontColor({
275                            'id': -1,
276                            'type': 10001,
277                            params: ['sys.color.ohos_id_color_text_secondary'],
278                            'bundleName': '__harDefaultBundleName__',
279                            'moduleName': '__harDefaultModuleName__'
280                        });
281                        Text.maxLines(2);
282                        Text.padding(0);
283                        Text.margin({
284                            right: {
285                                'id': -1,
286                                'type': 10002,
287                                params: ['sys.float.ohos_id_text_paragraph_margin_s'],
288                                'bundleName': '__harDefaultBundleName__',
289                                'moduleName': '__harDefaultModuleName__'
290                            }
291                        });
292                        Text.textOverflow({ overflow: TextOverflow.Ellipsis });
293                    }, Text);
294                    Text.pop();
295                    this.observeComponentCreation2((elmtId, isInitialRender) => {
296                        Image.create({
297                            'id': -1,
298                            'type': 20000,
299                            params: ['sys.media.ohos_ic_public_arrow_right'],
300                            'bundleName': '__harDefaultBundleName__',
301                            'moduleName': '__harDefaultModuleName__'
302                        });
303                        Image.width('12vp');
304                        Image.height('24vp');
305                        Image.fillColor({
306                            'id': -1,
307                            'type': 10001,
308                            params: ['sys.color.ohos_id_color_tertiary'],
309                            'bundleName': '__harDefaultBundleName__',
310                            'moduleName': '__harDefaultModuleName__'
311                        });
312                    }, Image);
313                    Row.pop();
314                    Button.pop();
315                });
316            } else {
317                this.ifElseBranchUpdateFunction(1, () => {
318                });
319            }
320        }, If);
321        If.pop();
322        Flex.pop();
323    }
324
325    initialRender() {
326        PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.push(this);
327        this.observeComponentCreation2((elmtId, isInitialRender) => {
328            Row.create();
329            Row.width('100%');
330            Row.position({ y: this.options.marginTop });
331            Row.zIndex(ZINDEX_NUM);
332        }, Row);
333        this.observeComponentCreation2((elmtId, isInitialRender) => {
334            Column.create();
335            Column.padding(this.options.marginType === MarginType.DEFAULT_MARGIN ? {
336                left: {
337                    'id': -1,
338                    'type': 10002,
339                    params: ['sys.float.ohos_id_card_margin_start'],
340                    'bundleName': '__harDefaultBundleName__',
341                    'moduleName': '__harDefaultModuleName__'
342                },
343                right: {
344                    'id': -1,
345                    'type': 10002,
346                    params: ['sys.float.ohos_id_card_margin_end'],
347                    'bundleName': '__harDefaultBundleName__',
348                    'moduleName': '__harDefaultModuleName__'
349                }
350            } : {
351                left: {
352                    'id': -1,
353                    'type': 10002,
354                    params: ['sys.float.ohos_id_max_padding_start'],
355                    'bundleName': '__harDefaultBundleName__',
356                    'moduleName': '__harDefaultModuleName__'
357                },
358                right: {
359                    'id': -1,
360                    'type': 10002,
361                    params: ['sys.float.ohos_id_max_padding_end'],
362                    'bundleName': '__harDefaultBundleName__',
363                    'moduleName': '__harDefaultModuleName__'
364                }
365            });
366            Column.transition(TransitionEffect.OPACITY.animation({
367                curve: curves.cubicBezierCurve(0.33, 0, 0.67, 1),
368                duration: this.options.isShown ? START_TIME : END_TIME
369            }));
370            Column.visibility(this.options.isShown ? Visibility.Visible : Visibility.None);
371        }, Column);
372        this.observeComponentCreation2((elmtId, isInitialRender) => {
373            Column.create();
374            Column.width('100%');
375            Column.borderRadius(BORDER_RADIUS);
376            Column.backgroundColor({
377                'id': -1,
378                'type': 10001,
379                params: ['sys.color.comp_background_warning_secondary'],
380                'bundleName': '__harDefaultBundleName__',
381                'moduleName': '__harDefaultModuleName__'
382            });
383            Column.zIndex(ZINDEX_NUM);
384        }, Column);
385        this.TextBuilder.bind(this)();
386        Column.pop();
387        Column.pop();
388        Row.pop();
389        PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.pop();
390    }
391
392    aboutToAppear() {
393        try {
394            let uiContent = this.getUIContext();
395            this.isFollowingSystemFontScale = uiContent.isFollowingSystemFontScale();
396            this.maxAppFontScale = uiContent.getMaxFontScale();
397        } catch (err) {
398            let code = err.code;
399            let message = err.message;
400            hilog.error(0x3900, 'Ace', `Failed to init fontsizescale info, cause, code: ${code}, message: ${message}`);
401        }
402    }
403
404    updateFontScale() {
405        let uiContent = this.getUIContext();
406        let systemFontScale = uiContent.getHostContext()?.config?.fontSizeScale ?? 1;
407        if (!this.isFollowingSystemFontScale) {
408            return 1;
409        }
410        return Math.min(systemFontScale, this.maxAppFontScale);
411    }
412
413    rerender() {
414        PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.push(this);
415        this.updateDirtyElements();
416        PUV2ViewBase.contextStack && PUV2ViewBase.contextStack.pop();
417    }
418}
419
420export default {
421    MarginType,
422    ExceptionPrompt
423}