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 this._width = data.width; 55 this._height = data.height; 56 if (this.onload && typeof this.onload === 'function') { 57 this.onload(); 58 } 59 }, 60 fail: data => { 61 if (this.onerror && typeof this.onerror === 'function') { 62 this.onerror(data); 63 } 64 } 65 }); 66 } 67 68 /** 69 * Width of this Image. 70 * @type {number} 71 */ 72 public get width() { 73 return this._width; 74 } 75 76 public set width(width) { 77 this._width = width; 78 } 79 80 /** 81 * Height of this Image. 82 * @type {number} 83 */ 84 public get height() { 85 return this._height; 86 } 87 88 public set height(height) { 89 this._height = height; 90 } 91 92 /** 93 * Triggered when the image is successfully loaded. 94 * @type {Function} 95 */ 96 public get onload() { 97 return this._onload; 98 } 99 100 public set onload(onload: (...args: any | null) => void) { 101 this._onload = onload; 102 } 103 104 /** 105 * Triggered when the image fails to be loaded. 106 * @type {Function} 107 */ 108 public get onerror() { 109 return this._onerror; 110 } 111 112 public set onerror(onerror: (...args: any | null) => void) { 113 this._onerror = onerror; 114 } 115} 116