WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
let incId = 1;
 
import GBridge from '../bridge/bridge-weex.js'
const noop = function () { };
 
class GImage {
 
    static GBridge = null;
 
    constructor() {
        this._id = incId++;
        this._width = 0;
        this._height = 0;
        this._src = undefined;
        this._onload = noop;
        this._onerror = noop;
        this.complete = false;
    }
 
    get width() {
        return this._width;
    }
    set width(v) {
        this._width = v;
    }
 
    get height() {
        return this._height;
    }
 
    set height(v) {
        this._height = v;
    }
 
    get src() {
        return this._src;
    }
 
    set src(v) {
 
        if (v.startsWith('//')) {
            v = 'http:' + v;
        }
 
        this._src = v;
        try {
            GBridge.perloadImage([this._src, this._id], (data) => {
                if (process.env.NODE_ENV === 'development') {
                    console.log('****GBridge.perloadImage****')
                }
                if (typeof data === 'string') {
                    data = JSON.parse(data);
                }
                
                if (data.error) {
                    var evt = { type: 'error', target: this };
                    this.onerror(evt);
                } else {
                    this.complete = true;
                    this.width = typeof data.width === 'number' ? data.width : 0;
                    this.height = typeof data.height === 'number' ? data.height : 0;
                    var evt = { type: 'load', target: this };
                    this.onload(evt);
                }
            });
        } catch (error) {
            console.log('perloadImage fail', error)
        }
    }
 
    addEventListener(name, listener) {
        if (name === 'load') {
            this.onload = listener;
        } else if (name === 'error') {
            this.onerror = listener;
        }
    }
 
    removeEventListener(name, listener) {
        if (name === 'load') {
            this.onload = noop;
        } else if (name === 'error') {
            this.onerror = noop;
        }
    }
 
    get onload() {
        return this._onload;
    }
 
    set onload(v) {
        this._onload = v;
    }
 
    get onerror() {
        return this._onerror;
    }
 
    set onerror(v) {
        this._onerror = v;
    }
}
 
export default GImage;