mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00
Fix: ListView not showing first items of VirtArray
This commit is contained in:
@ -9,7 +9,7 @@ export class ChangeType implements observableArrayDef.ChangeType {
|
|||||||
static Splice = "splice";
|
static Splice = "splice";
|
||||||
}
|
}
|
||||||
|
|
||||||
var CHANGE = "change";
|
const CHANGE = "change";
|
||||||
|
|
||||||
export class ObservableArray<T> extends observable.Observable implements observableArrayDef.ObservableArray<T> { // implements Array<T> {
|
export class ObservableArray<T> extends observable.Observable implements observableArrayDef.ObservableArray<T> { // implements Array<T> {
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
*/
|
*/
|
||||||
concat(): T[] {
|
concat(): T[] {
|
||||||
this._addArgs.index = this._array.length;
|
this._addArgs.index = this._array.length;
|
||||||
var result = this._array.concat.apply(this._array, arguments);
|
const result = this._array.concat.apply(this._array, arguments);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
pop(): T {
|
pop(): T {
|
||||||
this._deleteArgs.index = this._array.length - 1;
|
this._deleteArgs.index = this._array.length - 1;
|
||||||
|
|
||||||
var result = this._array.pop();
|
const result = this._array.pop();
|
||||||
|
|
||||||
this._deleteArgs.removed = [result];
|
this._deleteArgs.removed = [result];
|
||||||
|
|
||||||
@ -127,10 +127,9 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
this._addArgs.index = this._array.length;
|
this._addArgs.index = this._array.length;
|
||||||
|
|
||||||
if (arguments.length === 1 && Array.isArray(arguments[0])) {
|
if (arguments.length === 1 && Array.isArray(arguments[0])) {
|
||||||
|
const source = <Array<T>>arguments[0];
|
||||||
|
|
||||||
var source = <Array<T>>arguments[0];
|
for (let i = 0, l = source.length; i < l; i++) {
|
||||||
|
|
||||||
for (var i = 0, l = source.length; i < l; i++) {
|
|
||||||
this._array.push(source[i]);
|
this._array.push(source[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,7 +146,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
}
|
}
|
||||||
|
|
||||||
_notifyLengthChange() {
|
_notifyLengthChange() {
|
||||||
var lengthChangedData = this._createPropertyChangeData("length", this._array.length);
|
const lengthChangedData = this._createPropertyChangeData("length", this._array.length);
|
||||||
this.notify(lengthChangedData);
|
this.notify(lengthChangedData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,7 +161,7 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
* Removes the first element from an array and returns it.
|
* Removes the first element from an array and returns it.
|
||||||
*/
|
*/
|
||||||
shift(): T {
|
shift(): T {
|
||||||
var result = this._array.shift();
|
const result = this._array.shift();
|
||||||
|
|
||||||
this._deleteArgs.index = 0;
|
this._deleteArgs.index = 0;
|
||||||
this._deleteArgs.removed = [result];
|
this._deleteArgs.removed = [result];
|
||||||
@ -197,8 +196,8 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
* @param items Elements to insert into the array in place of the deleted elements.
|
* @param items Elements to insert into the array in place of the deleted elements.
|
||||||
*/
|
*/
|
||||||
splice(start: number, deleteCount?: number): T[] {
|
splice(start: number, deleteCount?: number): T[] {
|
||||||
var length = this._array.length;
|
const length = this._array.length;
|
||||||
var result = this._array.splice.apply(this._array, arguments);
|
const result = this._array.splice.apply(this._array, arguments);
|
||||||
|
|
||||||
this.notify(<observableArrayDef.ChangedData<T>>{
|
this.notify(<observableArrayDef.ChangedData<T>>{
|
||||||
eventName: CHANGE, object: this,
|
eventName: CHANGE, object: this,
|
||||||
@ -219,8 +218,8 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
* @param items Elements to insert at the start of the Array.
|
* @param items Elements to insert at the start of the Array.
|
||||||
*/
|
*/
|
||||||
unshift(): number {
|
unshift(): number {
|
||||||
var length = this._array.length;
|
const length = this._array.length;
|
||||||
var result = this._array.unshift.apply(this._array, arguments);
|
const result = this._array.unshift.apply(this._array, arguments);
|
||||||
|
|
||||||
this._addArgs.index = 0;
|
this._addArgs.index = 0;
|
||||||
this._addArgs.addedCount = result - length;
|
this._addArgs.addedCount = result - length;
|
||||||
@ -237,8 +236,8 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
|
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
|
||||||
*/
|
*/
|
||||||
indexOf(searchElement: T, fromIndex?: number): number {
|
indexOf(searchElement: T, fromIndex?: number): number {
|
||||||
var index = fromIndex ? fromIndex : 0;
|
const index = fromIndex ? fromIndex : 0;
|
||||||
for (var i = index, l = this._array.length; i < l; i++) {
|
for (let i = index, l = this._array.length; i < l; i++) {
|
||||||
if (this._array[i] === searchElement) {
|
if (this._array[i] === searchElement) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -252,9 +251,9 @@ export class ObservableArray<T> extends observable.Observable implements observa
|
|||||||
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
|
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
|
||||||
*/
|
*/
|
||||||
lastIndexOf(searchElement: T, fromIndex?: number): number {
|
lastIndexOf(searchElement: T, fromIndex?: number): number {
|
||||||
var index = fromIndex ? fromIndex : this._array.length - 1;
|
const index = fromIndex ? fromIndex : this._array.length - 1;
|
||||||
|
|
||||||
for (var i = index; i >= 0; i--) {
|
for (let i = index; i >= 0; i--) {
|
||||||
if (this._array[i] === searchElement) {
|
if (this._array[i] === searchElement) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,19 @@
|
|||||||
import * as observable from "data/observable";
|
import { Observable } from "data/observable";
|
||||||
import * as types from "utils/types";
|
|
||||||
import * as virtualArrayDef from "data/virtual-array";
|
import * as virtualArrayDef from "data/virtual-array";
|
||||||
|
|
||||||
var CHANGE = "change", UPDATE = "update", DELETE = "delete", ADD = "add";
|
const CHANGE = "change";
|
||||||
|
const UPDATE = "update";
|
||||||
|
const DELETE = "delete";
|
||||||
|
const ADD = "add";
|
||||||
|
|
||||||
export class ChangeType implements virtualArrayDef.ChangeType {
|
export class ChangeType implements virtualArrayDef.ChangeType {
|
||||||
static Add = "add";
|
static Add = ADD;
|
||||||
static Delete = "delete";
|
static Delete = DELETE;
|
||||||
static Update = "update";
|
static Update = UPDATE;
|
||||||
static Splice = "splice";
|
static Splice = CHANGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class VirtualArray<T> extends observable.Observable {
|
export class VirtualArray<T> extends Observable implements virtualArrayDef.VirtualArray<T> {
|
||||||
public static changeEvent = CHANGE;
|
public static changeEvent = CHANGE;
|
||||||
public static itemsLoadingEvent = "itemsLoading";
|
public static itemsLoadingEvent = "itemsLoading";
|
||||||
|
|
||||||
@ -36,8 +38,8 @@ export class VirtualArray<T> extends observable.Observable {
|
|||||||
set length(value: number) {
|
set length(value: number) {
|
||||||
if (this._length !== value) {
|
if (this._length !== value) {
|
||||||
|
|
||||||
var index = this._length;
|
const index = this._length;
|
||||||
var count = value - this._length;
|
const count = value - this._length;
|
||||||
|
|
||||||
this._length = value;
|
this._length = value;
|
||||||
|
|
||||||
@ -60,9 +62,9 @@ export class VirtualArray<T> extends observable.Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getItem(index: number): T {
|
getItem(index: number): T {
|
||||||
var item = this._cache[index];
|
const item = this._cache[index];
|
||||||
|
|
||||||
if (types.isUndefined(item)) {
|
if (item === undefined) {
|
||||||
if (index >= 0 && index < this.length && this._requestedIndexes.indexOf(index) < 0 && this._loadedIndexes.indexOf(index) < 0) {
|
if (index >= 0 && index < this.length && this._requestedIndexes.indexOf(index) < 0 && this._loadedIndexes.indexOf(index) < 0) {
|
||||||
this.requestItems(index);
|
this.requestItems(index);
|
||||||
}
|
}
|
||||||
@ -78,10 +80,9 @@ export class VirtualArray<T> extends observable.Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
load(index: number, items: T[]): void {
|
load(index: number, items: T[]): void {
|
||||||
var i: number;
|
for (let i = 0; i < items.length; i++) {
|
||||||
for (i = 0; i < items.length; i++) {
|
|
||||||
|
|
||||||
var itemIndex = index + i;
|
const itemIndex = index + i;
|
||||||
|
|
||||||
this._cache[itemIndex] = items[i];
|
this._cache[itemIndex] = items[i];
|
||||||
|
|
||||||
@ -94,7 +95,7 @@ export class VirtualArray<T> extends observable.Observable {
|
|||||||
|
|
||||||
// Remove requested but never loaded indexes.
|
// Remove requested but never loaded indexes.
|
||||||
if (this._requestedIndexes.length > 0) {
|
if (this._requestedIndexes.length > 0) {
|
||||||
for (i = 0; i < this.loadSize - items.length; i++) {
|
for (let i = 0; i < this.loadSize - items.length; i++) {
|
||||||
this._requestedIndexes.splice(this._requestedIndexes.indexOf(index + i), 1);
|
this._requestedIndexes.splice(this._requestedIndexes.indexOf(index + i), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,14 +110,14 @@ export class VirtualArray<T> extends observable.Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private requestItems(index: number): void {
|
private requestItems(index: number): void {
|
||||||
var indexesToLoad = [];
|
const indexesToLoad = [];
|
||||||
|
|
||||||
var pageIndex = this._loadSize > 0 ? this._loadSize * Math.floor(index / this._loadSize) : index;
|
const pageIndex = this._loadSize > 0 ? this._loadSize * Math.floor(index / this._loadSize) : index;
|
||||||
var count = 0;
|
let count = 0;
|
||||||
var start = -1;
|
let start = -1;
|
||||||
|
|
||||||
for (var i = 0; i < this.loadSize; i++) {
|
for (let i = 0; i < this.loadSize; i++) {
|
||||||
var itemIndex = pageIndex + i;
|
const itemIndex = pageIndex + i;
|
||||||
|
|
||||||
if (itemIndex >= this._length) {
|
if (itemIndex >= this._length) {
|
||||||
break;
|
break;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { ListView as ListViewDefinition, ItemsSource } from "ui/list-view";
|
import { ListView as ListViewDefinition, ItemsSource } from "ui/list-view";
|
||||||
import { CoercibleProperty, CssProperty, Style, View, Template, KeyedTemplate, Length, Property, Color, lengthComparer } from "ui/core/view";
|
import { CoercibleProperty, CssProperty, Style, View, Template, KeyedTemplate, Length, Property, Color, lengthComparer, Observable } from "ui/core/view";
|
||||||
import { parse, parseMultipleTemplates } from "ui/builder";
|
import { parse, parseMultipleTemplates } from "ui/builder";
|
||||||
import { Label } from "ui/label";
|
import { Label } from "ui/label";
|
||||||
import { ObservableArray, ChangedData } from "data/observable-array";
|
import { ObservableArray, ChangedData } from "data/observable-array";
|
||||||
@ -129,11 +129,11 @@ export abstract class ListViewBase extends View implements ListViewDefinition {
|
|||||||
*/
|
*/
|
||||||
export const itemsProperty = new Property<ListViewBase, any[] | ItemsSource>({
|
export const itemsProperty = new Property<ListViewBase, any[] | ItemsSource>({
|
||||||
name: "items", valueChanged: (target, oldValue, newValue) => {
|
name: "items", valueChanged: (target, oldValue, newValue) => {
|
||||||
if (oldValue instanceof ObservableArray) {
|
if (oldValue instanceof Observable) {
|
||||||
removeWeakEventListener(oldValue, ObservableArray.changeEvent, target._onItemsChanged, target);
|
removeWeakEventListener(oldValue, ObservableArray.changeEvent, target._onItemsChanged, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newValue instanceof ObservableArray) {
|
if (newValue instanceof Observable) {
|
||||||
addWeakEventListener(newValue, ObservableArray.changeEvent, target._onItemsChanged, target);
|
addWeakEventListener(newValue, ObservableArray.changeEvent, target._onItemsChanged, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user