|
| 1 | +/* eslint-disable no-redeclare */ |
1 | 2 | import { Observable } from '@nativescript/core/data/observable'; |
2 | 3 | import { ImageSource } from '@nativescript/core/image-source'; |
3 | 4 | import { ImageAsset } from '@nativescript/core/image-asset'; |
4 | 5 | import { RESOURCE_PREFIX, isDataURI, isFileOrResourcePath } from '@nativescript/core/utils/utils'; |
5 | 6 | import { isAndroid } from '@nativescript/core/platform'; |
6 | 7 |
|
7 | 8 | function createGetter(key: string, options: NativePropertyOptions) { |
8 | | - // console.log('createGetter', key, options); |
9 | 9 | const nativeGetterName = ((isAndroid ? options.android : options.ios) || options).nativeGetterName || 'get' + key.charAt(0).toUpperCase() + key.slice(1); |
10 | 10 | const converter = options.converter; |
11 | 11 | return function () { |
12 | 12 | let result; |
13 | | - // console.log('getter', key, nativeGetterName); |
14 | 13 | if (this.native && this.native[nativeGetterName]) { |
15 | 14 | result = this.native[nativeGetterName](); |
16 | 15 | } else { |
17 | 16 | result = this.options[key] || options.defaultValue; |
18 | 17 | } |
19 | 18 | result = converter ? converter.fromNative.call(this, result, key) : result; |
20 | | - // console.log('getter', key, options, nativeGetterName, !!getConverter, result); |
21 | 19 | return result; |
22 | 20 | }; |
23 | 21 | } |
24 | 22 | function createSetter(key, options: NativePropertyOptions) { |
25 | 23 | const nativeSetterName = ((isAndroid ? options.android : options.ios) || options).nativeSetterName || 'set' + key.charAt(0).toUpperCase() + key.slice(1); |
26 | 24 | return function (newVal) { |
27 | | - // console.log('setter', key, newVal, Array.isArray(newVal), typeof newVal, nativeSetterName, options.converter); |
28 | 25 | this.options[key] = newVal; |
29 | 26 | if (this.native && this.native[nativeSetterName]) { |
30 | 27 | const actualVal = options.converter ? options.converter.toNative.call(this, newVal, key) : newVal; |
31 | 28 | this.native[nativeSetterName](actualVal); |
32 | 29 | this._buildStyle = null; |
33 | 30 | } |
34 | | - // this.notify({ object: this, eventName: Observable.propertyChangeEvent, propertyName: key, value: actualVal }); |
35 | 31 | }; |
36 | 32 | } |
37 | 33 |
|
38 | | -// function hasSetter(obj, prop) { |
39 | | -// const descriptor = Object.getOwnPropertyDescriptor(obj, prop); |
40 | | -// return descriptor && !!descriptor['set']; |
41 | | -// } |
42 | 34 | function nativePropertyGenerator(target: Object, key: string, options?: NativePropertyOptions) { |
43 | 35 | Object.defineProperty(target, key, { |
44 | 36 | get: createGetter(key, options), |
@@ -115,75 +107,28 @@ export function _createImageSourceFromSrc(value: string | ImageSource | ImageAss |
115 | 107 | if (typeof value === 'string' || value instanceof String) { |
116 | 108 | value = value.trim(); |
117 | 109 |
|
118 | | - const source = new ImageSource(); |
| 110 | + let source: ImageSource; |
119 | 111 | if (isDataURI(value)) { |
120 | 112 | const base64Data = value.split(',')[1]; |
121 | 113 | if (base64Data !== undefined) { |
122 | | - // if (sync) { |
123 | | - source.loadFromBase64(base64Data); |
124 | | - // return source; |
125 | | - // imageLoaded(); |
126 | | - // } else { |
127 | | - // return source.fromBase64(base64Data).then(() => source); |
128 | | - // .then(imageLoaded); |
129 | | - // } |
| 114 | + source = ImageSource.fromBase64Sync(base64Data); |
130 | 115 | } |
131 | 116 | } else if (isFileOrResourcePath(value)) { |
132 | 117 | if (value.indexOf(RESOURCE_PREFIX) === 0) { |
133 | 118 | const resPath = value.substr(RESOURCE_PREFIX.length); |
134 | | - // if (sync) { |
135 | | - source.loadFromResource(resPath); |
136 | | - // return source.fromResource(resPath).then(() => source); |
137 | | - // imageLoaded(); |
138 | | - // } else { |
139 | | - // this.imageSource = null; |
140 | | - // source.fromResource(resPath) |
141 | | - // // .then(imageLoaded); |
142 | | - // } |
| 119 | + source = ImageSource.fromResourceSync(resPath); |
143 | 120 | } else { |
144 | | - // if (sync) { |
145 | | - source.loadFromFile(value); |
146 | | - // return source.fromFile(value).then(() => source); |
147 | | - // imageLoaded(); |
148 | | - // } else { |
149 | | - // this.imageSource = null; |
150 | | - // source.fromFile(value).then(imageLoaded); |
151 | | - // } |
| 121 | + source = ImageSource.fromFileSync(value); |
152 | 122 | } |
153 | 123 | } else { |
154 | | - // this.imageSource = null; |
155 | | - // return fromUrl(value); |
156 | | - // fromUrl(value); |
157 | | - // .then( |
158 | | - // r => { |
159 | | - // if (this['_url'] === value) { |
160 | | - // this.imageSource = r; |
161 | | - // this.isLoading = false; |
162 | | - // } |
163 | | - // }, |
164 | | - // err => { |
165 | | - // // catch: Response content may not be converted to an Image |
166 | | - // this.isLoading = false; |
167 | | - // if (traceEnabled()) { |
168 | | - // if (typeof err === 'object' && err.message) { |
169 | | - // err = err.message; |
170 | | - // } |
171 | | - // traceWrite(err, traceCategories.Debug); |
172 | | - // } |
173 | | - // } |
174 | | - // ); |
175 | 124 | } |
176 | 125 | return source; |
177 | 126 | } else if (value instanceof ImageSource) { |
178 | | - // Support binding the imageSource trough the src property |
179 | | - // return Promise.resolve(value); |
180 | 127 | return value; |
181 | 128 | } else if (value instanceof ImageAsset) { |
182 | | - // const result = await fromAsset(value); |
183 | | - return null; |
| 129 | + return new ImageSource(value.nativeImage); |
184 | 130 | } else { |
185 | 131 | return new ImageSource(value); |
186 | | - // return Promise.resolve(fromNativeSource(value)); |
187 | 132 | } |
188 | 133 | } |
189 | 134 | export function capitalize(s) { |
|
0 commit comments