forked from NativeScript/NativeScript
/
repeater.ts
175 lines (139 loc) · 5.67 KB
/
repeater.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Repeater as RepeaterDefinition, ItemsSource } from ".";
import { Label } from "../label";
import { LayoutBase, CustomLayoutView, View, Template, Property, layout, CSSType } from "../layouts/layout-base";
import { StackLayout } from "../layouts/stack-layout";
import { ObservableArray, ChangedData } from "../../data/observable-array";
import { addWeakEventListener, removeWeakEventListener } from "../core/weak-event-listener";
import { parse } from "../builder";
import { profile } from "../../profiling";
export * from "../layouts/layout-base";
export module knownTemplates {
export const itemTemplate = "itemTemplate";
}
@CSSType("Repeater")
export class Repeater extends CustomLayoutView implements RepeaterDefinition {
private _isDirty = false;
public ios;
public android;
constructor() {
super();
// TODO: Do we need this as property?
this.itemsLayout = new StackLayout();
}
public items: any[] | ItemsSource;
public itemTemplate: string | Template;
public itemsLayout: LayoutBase;
@profile
public onLoaded() {
if (this._isDirty) {
this.refresh();
}
super.onLoaded();
}
public _requestRefresh() {
this._isDirty = true;
if (this.isLoaded) {
this.refresh();
}
}
public refresh() {
if (this.itemsLayout) {
this.itemsLayout.removeChildren();
}
if (!this.items) {
return;
}
const length = this.items.length;
for (let i = 0; i < length; i++) {
const viewToAdd = this.itemTemplate ? parse(this.itemTemplate, this) : this._getDefaultItemContent(i);
const dataItem = this._getDataItem(i);
viewToAdd.bindingContext = dataItem;
this.itemsLayout.addChild(viewToAdd);
}
this._isDirty = false;
}
public _onItemsChanged(data: ChangedData<any>) {
// TODO: use the event args and optimize this code by remove/add single items instead of full rebuild.
this._requestRefresh();
}
public _getDefaultItemContent(index: number): View {
const lbl = new Label();
lbl.bind({
targetProperty: "text",
sourceProperty: "$value"
});
return lbl;
}
private _getDataItem(index: number): any {
let items = <ItemsSource>this.items;
return items.getItem ? items.getItem(index) : this.items[index];
}
get _childrenCount(): number {
return this.itemsLayout ? 1 : 0;
}
public eachChildView(callback: (child: View) => boolean) {
if (this.itemsLayout) {
callback(this.itemsLayout);
}
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
const insets = this.getSafeAreaInsets();
const paddingLeft = this.effectiveBorderLeftWidth + this.effectivePaddingLeft + insets.left;
const paddingTop = this.effectiveBorderTopWidth + this.effectivePaddingTop + insets.top;
const paddingRight = this.effectiveBorderRightWidth + this.effectivePaddingRight + insets.right;
const paddingBottom = this.effectiveBorderBottomWidth + this.effectivePaddingBottom + insets.bottom;
const childLeft = paddingLeft;
const childTop = paddingTop;
const childRight = right - left - paddingRight;
const childBottom = bottom - top - paddingBottom;
View.layoutChild(this, this.itemsLayout, childLeft, childTop, childRight, childBottom);
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
const result = View.measureChild(this, this.itemsLayout, widthMeasureSpec, heightMeasureSpec);
const width = layout.getMeasureSpecSize(widthMeasureSpec);
const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
const height = layout.getMeasureSpecSize(heightMeasureSpec);
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
const widthAndState = View.resolveSizeAndState(result.measuredWidth, width, widthMode, 0);
const heightAndState = View.resolveSizeAndState(result.measuredHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
}
Repeater.prototype.recycleNativeView = "auto";
/**
* Represents the item template property of each ListView instance.
*/
export const itemTemplateProperty = new Property<Repeater, string | Template>({
name: "itemTemplate", affectsLayout: true, valueChanged: (target) => {
target._requestRefresh();
}
});
itemTemplateProperty.register(Repeater);
/**
* Represents the property backing the items property of each ListView instance.
*/
export const itemsProperty = new Property<Repeater, any[] | ItemsSource>({
name: "items", affectsLayout: true, valueChanged: (target, oldValue, newValue) => {
if (oldValue instanceof ObservableArray) {
removeWeakEventListener(oldValue, ObservableArray.changeEvent, target._onItemsChanged, target);
}
if (newValue instanceof ObservableArray) {
addWeakEventListener(newValue, ObservableArray.changeEvent, target._onItemsChanged, target);
}
target._requestRefresh();
}
});
itemsProperty.register(Repeater);
export const itemsLayoutProperty = new Property<Repeater, LayoutBase>({
name: "itemsLayout", affectsLayout: true, valueChanged: (target, oldValue, newValue) => {
if (oldValue) {
target._removeView(oldValue);
oldValue.removeChildren();
}
if (newValue) {
target._addView(newValue);
}
target._requestRefresh();
}
});
itemsLayoutProperty.register(Repeater);