|
1 | | -import { EventData } from "data/observable"; |
2 | | -import { Property, PropertyChangeData, PropertyMetadataSettings } from "ui/core/dependency-observable"; |
3 | | -import { PropertyMetadata } from "ui/core/proxy"; |
4 | | -import { Cache } from "ui/image-cache"; |
5 | | -import { ScrollView } from "ui/scroll-view"; |
| 1 | +/*! ***************************************************************************** |
| 2 | +Copyright (c) 2017 Tangra Inc. |
6 | 3 |
|
7 | | -const IMAGESCROLLER = "ImageScroller"; |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
8 | 7 |
|
9 | | -function onItemsPropertyChanged(data: PropertyChangeData) { |
10 | | - const imageSwipe = data.object as ImageSwipeBase; |
11 | | - imageSwipe.refresh(); |
12 | | -} |
| 8 | +http://www.apache.org/licenses/LICENSE-2.0 |
13 | 9 |
|
14 | | -function onPagePropertyChanged(data: PropertyChangeData) { |
15 | | - const imageScroller = data.object as ImageSwipeBase; |
16 | | - imageScroller.loadCurrentPage(); |
17 | | - imageScroller.notify({ |
18 | | - eventName: ImageSwipeBase.pageChangedEvent, |
19 | | - object: imageScroller, |
20 | | - page: data.newValue |
21 | | - } as PageChangeEventData); |
22 | | -} |
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +***************************************************************************** */ |
| 16 | +import { CoercibleProperty, Property } from "ui/core/view"; |
| 17 | +import { Cache } from "ui/image-cache"; |
| 18 | +import { ItemsSource } from "ui/list-picker"; |
| 19 | +import { ScrollView } from "ui/scroll-view"; |
| 20 | +import { ImageSwipe as ImageSwipeDefinition } from "."; |
23 | 21 |
|
24 | | -export interface PageChangeEventData extends EventData { |
25 | | - eventName: string; |
26 | | - object: ImageSwipeBase; |
27 | | - page: number; |
28 | | -} |
| 22 | +export * from "ui/scroll-view"; |
29 | 23 |
|
30 | | -export abstract class ImageSwipeBase extends ScrollView { |
| 24 | +export class ImageSwipeBase extends ScrollView implements ImageSwipeDefinition { |
31 | 25 | public static pageChangedEvent: string = "pageChanged"; |
32 | 26 |
|
33 | | - public static itemsProperty = new Property( |
34 | | - "items", |
35 | | - IMAGESCROLLER, |
36 | | - new PropertyMetadata( |
37 | | - undefined, |
38 | | - PropertyMetadataSettings.AffectsLayout, |
39 | | - onItemsPropertyChanged |
40 | | - ) |
41 | | - ); |
42 | | - |
43 | | - public static imageUrlPropertyProperty = new Property( |
44 | | - "imageUrlProperty", |
45 | | - IMAGESCROLLER, |
46 | | - new PropertyMetadata( |
47 | | - "", |
48 | | - PropertyMetadataSettings.AffectsLayout |
49 | | - ) |
50 | | - ); |
51 | | - |
52 | | - public static pageNumberProperty = new Property( |
53 | | - "pageNumber", |
54 | | - IMAGESCROLLER, |
55 | | - new PropertyMetadata( |
56 | | - 0, |
57 | | - PropertyMetadataSettings.AffectsLayout, |
58 | | - onPagePropertyChanged |
59 | | - ) |
60 | | - ); |
61 | | - |
62 | 27 | public static _imageCache: Cache; |
63 | | - // private _itemsChanged: (args: EventData) => void; |
64 | 28 |
|
65 | | - get items(): any { |
66 | | - return this._getValue(ImageSwipeBase.itemsProperty); |
67 | | - } |
68 | | - set items(value: any) { |
69 | | - this._setValue(ImageSwipeBase.itemsProperty, value); |
70 | | - } |
71 | | - |
72 | | - get imageUrlProperty(): string { |
73 | | - return this._getValue(ImageSwipeBase.imageUrlPropertyProperty); |
74 | | - } |
75 | | - set imageUrlProperty(value: string) { |
76 | | - this._setValue(ImageSwipeBase.imageUrlPropertyProperty, value); |
77 | | - } |
78 | | - |
79 | | - get pageNumber(): number { |
80 | | - return this._getValue(ImageSwipeBase.pageNumberProperty); |
81 | | - } |
82 | | - set pageNumber(value: number) { |
83 | | - this._setValue(ImageSwipeBase.pageNumberProperty, value); |
84 | | - } |
| 29 | + public items: any[] | ItemsSource; |
| 30 | + public pageNumber: number; |
| 31 | + public imageUrlProperty: string; |
| 32 | + public isItemsSourceIn: boolean; |
85 | 33 |
|
86 | 34 | constructor() { |
87 | 35 | super(); |
88 | | - // this._itemsChanged = (args: EventData) => { this.refresh(); }; |
89 | 36 | if (!ImageSwipeBase._imageCache) { |
90 | 37 | ImageSwipeBase._imageCache = new Cache(); |
91 | 38 | ImageSwipeBase._imageCache.maxRequests = 3; |
92 | 39 | } |
93 | 40 | } |
94 | 41 |
|
95 | | - public abstract refresh(); |
96 | | - public abstract loadCurrentPage(); |
97 | | - |
98 | 42 | public _getDataItem(index: number): any { |
99 | | - return this.items.getItem ? this.items.getItem(index) : this.items[index]; |
| 43 | + return this.isItemsSourceIn ? (this.items as ItemsSource).getItem(index) : this.items[index]; |
100 | 44 | } |
101 | 45 | } |
| 46 | + |
| 47 | +export const pageNumberProperty = new CoercibleProperty<ImageSwipeBase, number>({ |
| 48 | + name: "pageNumber", |
| 49 | + defaultValue: 0, |
| 50 | + valueConverter: (v) => parseInt(v, 10), |
| 51 | + coerceValue: (target, value) => { |
| 52 | + const items = target.items; |
| 53 | + if (items && items.length !== 0) { |
| 54 | + const max = items.length - 1; |
| 55 | + if (value < 0) { |
| 56 | + value = 0; |
| 57 | + } |
| 58 | + if (value > max) { |
| 59 | + value = max; |
| 60 | + } |
| 61 | + } |
| 62 | + else { |
| 63 | + value = null; |
| 64 | + } |
| 65 | + |
| 66 | + return value; |
| 67 | + } |
| 68 | +}); |
| 69 | +pageNumberProperty.register(ImageSwipeBase); |
| 70 | + |
| 71 | +export const itemsProperty = new Property<ImageSwipeBase, any[] | ItemsSource>({ |
| 72 | + name: "items", |
| 73 | + valueChanged: (target, oldValue, newValue) => { |
| 74 | + const getItem = newValue && (newValue as ItemsSource).getItem; |
| 75 | + |
| 76 | + target.isItemsSourceIn = typeof getItem === "function"; |
| 77 | + } |
| 78 | +}); |
| 79 | +itemsProperty.register(ImageSwipeBase); |
| 80 | + |
| 81 | +export const imageUrlPropertyProperty = new Property<ImageSwipeBase, string>({ |
| 82 | + name: "imageUrlProperty", |
| 83 | + defaultValue: "" |
| 84 | +}); |
| 85 | +imageUrlPropertyProperty.register(ImageSwipeBase); |
0 commit comments