# ui/uilistview (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uilistview/

Recycling list view.

## UIListView extends Component

Recycling list view.
- Takes just one template node and an item count, creates real nodes only for the visible range, and
  reclaims and reuses nodes that go out of range. Even with hundreds ~ thousands of items, the node count stays at one screen's worth.
- Attaches to the same node as UIScrollView. (auto-attached via require)
  Item content is filled in code with setBindItemEvent. (data-driven)
- When it nears the end, it sends a setReachEndEvent notification once. (infinite scroll / load more)
  When the item count grows via setItemCount, the notification is re-armed.
- Instead of a template, you can give a function that creates item nodes (setCreateItemEvent). If the item size is not set, it reads the size of the first created item.
- Usage:
    const listView = scrollNode.addComponent(UIListView);
    listView.setTemplateNode(templateNode);
    listView.setBindItemEvent((itemNode, itemIndex) => { ... });
    listView.setItemCount(500);
    listView.setReachEndEvent(() => { ... });

Example: Show a 500 row list with a single template

```js
import { Engine, EngineConfiguration, UIScene, UIListView, UILabel, WorldNode, Paint, Color, Pivot, Vector2 } from "./build/vanilla.js";

class ListScene extends UIScene {
	create() {
		super.create();
		const listNode = new WorldNode();
		listNode.setPivot(Pivot.topLeft.clone());
		listNode.setAnchor(Pivot.topLeft.clone());
		listNode.setContentSize(Vector2.create(320, 240));
		listNode.setLocalPosition(Vector2.create(160, 60));

		const templateNode = new WorldNode();
		templateNode.setPivot(Pivot.topLeft.clone());
		templateNode.setAnchor(Pivot.topLeft.clone());
		templateNode.setContentSize(Vector2.create(320, 36));
		const rowPaint = templateNode.addComponent(Paint);
		rowPaint.setColor(new Color(1, 1, 1, 0.08));
		const rowLabel = templateNode.addComponent(UILabel);
		rowLabel.setFontSize(16);
		listNode.addChild(templateNode);

		const listView = listNode.addComponent(UIListView);
		listView.setTemplateNode(templateNode);
		listView.setSpacing(4);
		listView.setBindItemEvent((itemNode, itemIndex) => {
			const itemLabel = itemNode.getComponent(UILabel);
			itemLabel.setText("Row " + itemIndex);
		});
		listView.setItemCount(500);
		listView.setReachEndEvent(() => {
			const itemCount = listView.getItemCount();
			listView.setItemCount(itemCount + 100);
		});
		const root = this.getRoot();
		root.addChild(listNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new ListScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `require()` (method, since 0.3.0): Dependent components.
  - returns Function[]: Components that must be attached to the same node. (UIScrollView, addComponent attaches it first if missing)
- `setTemplateNode(templateNode)` (method, since 0.3.0): Sets the template.
  - Serializes the node and clones it when needed. The original is hidden.
    The item size is read from the template's contentSize value along the scroll axis.
  - Clones with a new UIDocument that has only the engine's default types. For a template with game components, use setTemplateNodeWithDocument.
  - Items use the template's images as they are. (the document remembers the image of each component that writes an imagePath when it writes the template, UIDocument.serializeNode)
  - `templateNode` import("../core/node/worldnode.js").WorldNode: Node used as the item template. (Serialized with UIDocument and this node is turned off, if the item size is not set the contentSize along the main axis of this node is used, a previously given create function is cleared)
- `setTemplateNodeWithDocument(templateNode, uiDocument)` (method, since 0.4.0): Sets the template. (serializes and clones with a UIDocument that has the game components registered)
  - `templateNode` import("../core/node/worldnode.js").WorldNode: The node that items are based on. (same as setTemplateNode)
  - `uiDocument` UIDocument: The document that writes and reads the template. (one with the template's component types registered, items are read with this document every time one is made)

  Example: Build a list from a template with a game component

  ```js
  import { UIDocument, UIListView, WorldNode, Component, Vector2 } from "./build/vanilla.js";
  
  class Badge extends Component {
  }
  
  const uiDocument = new UIDocument();
  uiDocument.registerComponentType("Badge", Badge);
  const listNode = new WorldNode();
  listNode.setContentSize(Vector2.create(200, 300));
  const listView = listNode.addComponent(UIListView);
  const templateNode = new WorldNode();
  templateNode.setContentSize(Vector2.create(200, 40));
  templateNode.addComponent(Badge);
  listView.setTemplateNodeWithDocument(templateNode, uiDocument);
  console.log(templateNode.isActive(), listView.getItemSize());
  ```

- `setCreateItemEvent(createItemEvent)` (method, since 0.4.0): Sets the item node creation function. (instead of a template, () => WorldNode. Created nodes are attached as children of the content node)
  - `createItemEvent` Function | null: Function that creates and returns an item node. (() => WorldNode, giving a function drops a previously given template, null clears the create function)

  Example: Create nodes only for visible rows with a create function

  ```js
  import { UIListView, UIScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";
  
  const listNode = new WorldNode();
  listNode.setContentSize(Vector2.create(200, 100));
  const listView = listNode.addComponent(UIListView);
  listView.setCreateItemEvent(() => {
  	const itemNode = new WorldNode();
  	itemNode.setPivot(Pivot.topLeft.clone());
  	itemNode.setAnchor(Pivot.topLeft.clone());
  	itemNode.setContentSize(Vector2.create(200, 20));
  	return itemNode;
  });
  listView.setItemCount(100);
  listView.tick(0);
  console.log(listView.getItemSize(), listView.getActiveItemCount());
  const scrollView = listNode.getComponent(UIScrollView);
  scrollView.setScrollOffset(Vector2.create(0, -500));
  listView.tick(0);
  console.log(listView.getItemNode(0), listView.getItemNode(25) !== null);
  ```

- `setItemCount(itemCount)` (method, since 0.3.0): Sets the item count. (updates the scroll area + re-arms the end notification)
  - `itemCount` number: Item count. (Below 0 becomes 0, an increase re-arms the end notification, the scroll area is measured again and the visible items are bound again)
- `tick(timeDelta)` (method, since 0.3.0): Update. (computes the visible range → reclaim / create)
  - `timeDelta` number: Time since the previous frame. (Seconds, unused)
- `refresh()` (method, since 0.3.0): Rebinds all visible items. (when the data content changes)
- `getItemNode(itemIndex)` (method, since 0.3.0): Returns the real node of a given item. (null if off-screen)
  - `itemIndex` number: Item index. (From 0)
  - returns import("../core/node/worldnode.js").WorldNode | null: Node showing that item. (null when it is outside the visible range and buffer so no node exists, nodes are reused for other items once they leave the range)
- `setBindItemEvent(bindItemEvent)` (method, since 0.3.0): Sets the item bind function.
  - `bindItemEvent` Function: Function that fills an item node. ((itemNode, itemIndex) => void, called when a node is newly taken for that item, and on refresh and setItemCount)
- `setReachEndEvent(reachEndEvent)` (method, since 0.3.0): Sets the end notification function. (Infinite scroll, load more)
  - `reachEndEvent` Function: Function called once when the remaining length falls within setReachEndThreshold. (() => void, can be called again after setItemCount adds items)

  Example: Append the next 20 when reaching the end

  ```js
  import { UIListView, UIScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";
  
  const listNode = new WorldNode();
  listNode.setContentSize(Vector2.create(200, 100));
  const listView = listNode.addComponent(UIListView);
  listView.setItemSize(20);
  listView.setCreateItemEvent(() => {
  	const itemNode = new WorldNode();
  	itemNode.setPivot(Pivot.topLeft.clone());
  	itemNode.setAnchor(Pivot.topLeft.clone());
  	itemNode.setContentSize(Vector2.create(200, 20));
  	return itemNode;
  });
  let reachEndCount = 0;
  listView.setReachEndEvent(() => {
  	reachEndCount += 1;
  	const itemCount = listView.getItemCount();
  	listView.setItemCount(itemCount + 20);
  });
  listView.setReachEndThreshold(40);
  listView.setItemCount(20);
  listView.tick(0);
  console.log(reachEndCount, listView.getItemCount());
  const scrollView = listNode.getComponent(UIScrollView);
  scrollView.setScrollOffset(Vector2.create(0, -280));
  listView.tick(0);
  console.log(reachEndCount, listView.getItemCount());
  ```

- `setReachEndThreshold(reachEndThreshold)` (method, since 0.3.0): Sets the end notification distance.
  - `reachEndThreshold` number: Notifies when within this length of the end. (Pixels, default 120)
- `setItemSize(itemSize)` (method, since 0.3.0): Sets the item size.
  - `itemSize` number: Item size along the main axis. (Pixels, 0 or less reads the size of the template or of the first created item, the scroll area is measured again)
- `setSpacing(spacing)` (method, since 0.3.0): Sets the item spacing.
  - `spacing` number: Spacing between items. (Pixels, default 0, the scroll area is measured again)
- `setHorizontal(isHorizontal)` (method, since 0.3.0): Sets the layout direction.
  - `isHorizontal` boolean: True lays items out horizontally. (Default vertical, a change remeasures the scroll area and places the visible items again on the new axis, set it before the template or item size is read, the scroll view axis is set separately)
- `setBufferItemCount(bufferItemCount)` (method, since 0.3.0): Sets the number of off screen buffer items.
  - `bufferItemCount` number: Number of extra items to keep before and after the visible range. (Below 0 becomes 0, default 1)
- `getItemCount()` (method, since 0.3.0): Returns the item count.
  - returns number: Item count set with setItemCount.
- `getItemSize()` (method, since 0.3.0): Returns the item size.
  - returns number: Item size along the main axis. (Pixels, 0 if not known yet)
- `getSpacing()` (method, since 0.3.0): Returns the item spacing.
  - returns number: Spacing between items. (Pixels)
- `isHorizontal()` (method, since 0.4.0): Returns whether the layout is horizontal.
  - returns boolean: True if items are laid out horizontally. (Default false)
- `getActiveItemCount()` (method, since 0.3.0): Returns the number of visible item nodes.
  - returns number: Number of nodes currently assigned to items. (Visible range and buffer items, recycled nodes are not counted)
