• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 Page from './index';
17
18let image;
19
20/**
21 * This class provide a Image object.
22 */
23export class Image {
24  private _src: string;
25  private _height: number;
26  private _width: number;
27  private _onload: (...args: any | null) => void;
28  private _onerror: (...args: any | null) => void;
29
30  constructor(page: Page) {
31    image = page.requireModule('system.image');
32    this._src = '';
33    this._height = 0;
34    this._width = 0;
35    this._onload = null;
36    this._onerror = null;
37  }
38
39  /**
40   * Src of this Image.
41   * @type {string}
42   */
43  public get src() {
44    return this._src;
45  }
46
47  public set src(src) {
48    this._src = src;
49    image.getImage({
50      src: this._src,
51      width: this._width,
52      height: this._height,
53      success: data => {
54        if (this.onload && typeof this.onload === 'function') {
55          this.onload(data);
56        }
57      },
58      fail: data => {
59        if (this.onerror && typeof this.onerror === 'function') {
60          this.onerror(data);
61        }
62      }
63    });
64  }
65
66  /**
67   * Width of this Image.
68   * @type {number}
69   */
70  public get width() {
71    return this._width;
72  }
73
74  public set width(width) {
75    this._width = width;
76  }
77
78  /**
79   * Height of this Image.
80   * @type {number}
81   */
82  public get height() {
83    return this._height;
84  }
85
86  public set height(height) {
87    this._height = height;
88  }
89
90  /**
91   * Triggered when the image is successfully loaded.
92   * @type {Function}
93   */
94  public get onload() {
95    return this._onload;
96  }
97
98  public set onload(onload: (...args: any | null) => void) {
99    this._onload = onload;
100  }
101
102  /**
103   * Triggered when the image fails to be loaded.
104   * @type {Function}
105   */
106  public get onerror() {
107    return this._onerror;
108  }
109
110  public set onerror(onerror: (...args: any | null) => void) {
111    this._onerror = onerror;
112  }
113}
114