7.31 kBJavaScriptView Raw
1import { ContainerView, CSSType } from '../core/view';
2import { Property, CoercibleProperty, CssProperty } from '../core/properties';
3import { Length } from '../styling/style-properties';
4import { Style } from '../styling/style';
5import { Color } from '../../color';
6import { Builder } from '../builder';
7import { Label } from '../label';
8import { Observable } from '../../data/observable';
9import { ObservableArray } from '../../data/observable-array';
10import { addWeakEventListener, removeWeakEventListener } from '../core/weak-event-listener';
11import { isFunction } from '../../utils/types';
12const autoEffectiveRowHeight = -1;
13let ListViewBase = class ListViewBase extends ContainerView {
14 constructor() {
15 super(...arguments);
16 this._itemIdGenerator = (_item, index) => index;
17 this._itemTemplateSelectorBindable = new Label();
18 this._defaultTemplate = {
19 key: 'default',
20 createView: () => {
21 if (__UI_USE_EXTERNAL_RENDERER__) {
22 if (isFunction(this.itemTemplate)) {
23 return this.itemTemplate();
24 }
25 }
26 else {
27 if (this.itemTemplate) {
28 return Builder.parse(this.itemTemplate, this);
29 }
30 }
31 return undefined;
32 },
33 };
34 this._itemTemplatesInternal = new Array(this._defaultTemplate);
35 this._effectiveRowHeight = autoEffectiveRowHeight;
36 }
37 get separatorColor() {
38 return this.style.separatorColor;
39 }
40 set separatorColor(value) {
41 this.style.separatorColor = value;
42 }
43 get itemTemplateSelector() {
44 return this._itemTemplateSelector;
45 }
46 set itemTemplateSelector(value) {
47 if (typeof value === 'string') {
48 this._itemTemplateSelectorBindable.bind({
49 sourceProperty: null,
50 targetProperty: 'templateKey',
51 expression: value,
52 });
53 this._itemTemplateSelector = (item, index, items) => {
54 item['$index'] = index;
55 if (this._itemTemplateSelectorBindable.bindingContext === item) {
56 this._itemTemplateSelectorBindable.bindingContext = null;
57 }
58 this._itemTemplateSelectorBindable.bindingContext = item;
59 return this._itemTemplateSelectorBindable.get('templateKey');
60 };
61 }
62 else if (typeof value === 'function') {
63 this._itemTemplateSelector = value;
64 }
65 }
66 get itemIdGenerator() {
67 return this._itemIdGenerator;
68 }
69 set itemIdGenerator(generatorFn) {
70 this._itemIdGenerator = generatorFn;
71 }
72 refresh() {
73 //
74 }
75 scrollToIndex(index) {
76 //
77 }
78 scrollToIndexAnimated(index) {
79 //
80 }
81 _getItemTemplate(index) {
82 let templateKey = 'default';
83 if (this.itemTemplateSelector) {
84 const dataItem = this._getDataItem(index);
85 templateKey = this._itemTemplateSelector(dataItem, index, this.items);
86 }
87 for (let i = 0, length = this._itemTemplatesInternal.length; i < length; i++) {
88 if (this._itemTemplatesInternal[i].key === templateKey) {
89 return this._itemTemplatesInternal[i];
90 }
91 }
92 // This is the default template
93 return this._itemTemplatesInternal[0];
94 }
95 _prepareItem(item, index) {
96 if (item) {
97 item.bindingContext = this._getDataItem(index);
98 }
99 }
100 _getDataItem(index) {
101 const thisItems = this.items;
102 return thisItems.getItem ? thisItems.getItem(index) : thisItems[index];
103 }
104 _getDefaultItemContent(index) {
105 const lbl = new Label();
106 lbl.bind({
107 targetProperty: 'text',
108 sourceProperty: '$value',
109 });
110 return lbl;
111 }
112 _onItemsChanged(args) {
113 this.refresh();
114 }
115 _onRowHeightPropertyChanged(oldValue, newValue) {
116 this.refresh();
117 }
118 isItemAtIndexVisible(index) {
119 return false;
120 }
121 updateEffectiveRowHeight() {
122 rowHeightProperty.coerce(this);
123 }
124};
125ListViewBase.itemLoadingEvent = 'itemLoading';
126ListViewBase.itemTapEvent = 'itemTap';
127ListViewBase.loadMoreItemsEvent = 'loadMoreItems';
128// TODO: get rid of such hacks.
129ListViewBase.knownFunctions = ['itemTemplateSelector', 'itemIdGenerator']; //See component-builder.ts isKnownFunction
130ListViewBase = __decorate([
131 CSSType('ListView')
132], ListViewBase);
133export { ListViewBase };
134ListViewBase.prototype.recycleNativeView = 'auto';
135/**
136 * Represents the property backing the items property of each ListView instance.
137 */
138export const itemsProperty = new Property({
139 name: 'items',
140 valueChanged: (target, oldValue, newValue) => {
141 if (oldValue instanceof Observable) {
142 removeWeakEventListener(oldValue, ObservableArray.changeEvent, target._onItemsChanged, target);
143 }
144 if (newValue instanceof Observable) {
145 addWeakEventListener(newValue, ObservableArray.changeEvent, target._onItemsChanged, target);
146 }
147 target.refresh();
148 },
149});
150itemsProperty.register(ListViewBase);
151/**
152 * Represents the item template property of each ListView instance.
153 */
154export const itemTemplateProperty = new Property({
155 name: 'itemTemplate',
156 valueChanged: (target) => {
157 target.refresh();
158 },
159});
160itemTemplateProperty.register(ListViewBase);
161/**
162 * Represents the items template property of each ListView instance.
163 */
164export const itemTemplatesProperty = new Property({
165 name: 'itemTemplates',
166 valueConverter: (value) => {
167 if (typeof value === 'string') {
168 if (__UI_USE_XML_PARSER__) {
169 return Builder.parseMultipleTemplates(value, null);
170 }
171 else {
172 return null;
173 }
174 }
175 return value;
176 },
177});
178itemTemplatesProperty.register(ListViewBase);
179const defaultRowHeight = 'auto';
180/**
181 * Represents the observable property backing the rowHeight property of each ListView instance.
182 */
183export const rowHeightProperty = new CoercibleProperty({
184 name: 'rowHeight',
185 defaultValue: defaultRowHeight,
186 equalityComparer: Length.equals,
187 coerceValue: (target, value) => {
188 // We coerce to default value if we don't have display density.
189 return target.nativeViewProtected ? value : defaultRowHeight;
190 },
191 valueChanged: (target, oldValue, newValue) => {
192 target._effectiveRowHeight = Length.toDevicePixels(newValue, autoEffectiveRowHeight);
193 target._onRowHeightPropertyChanged(oldValue, newValue);
194 },
195 valueConverter: Length.parse,
196});
197rowHeightProperty.register(ListViewBase);
198export const iosEstimatedRowHeightProperty = new Property({
199 name: 'iosEstimatedRowHeight',
200 valueConverter: (v) => Length.parse(v),
201});
202iosEstimatedRowHeightProperty.register(ListViewBase);
203export const separatorColorProperty = new CssProperty({
204 name: 'separatorColor',
205 cssName: 'separator-color',
206 equalityComparer: Color.equals,
207 valueConverter: (v) => new Color(v),
208});
209separatorColorProperty.register(Style);
210//# sourceMappingURL=list-view-common.js.map
\No newline at end of file