Skip to content

Media Picker

createMediaPicker searches the library and picks items. Each result and each picked item shows as a Media Card row with its artwork, and each picked item has a Remove button.

Decision

When Use
Picking movies, series, or other library items, such as titles to exclude or a collection to follow Media Picker
Values that have no artwork Chip Select
Picking users User Selector

Usage

import { createMediaPicker } from './configurationpage?name=myplugin_stolon.js';

var excluded = createMediaPicker({
    label: 'Excluded Titles',
    itemTypes: ['Movie', 'Series'],
    value: config.ExcludedItems,
    onChange: function (items) { config.ExcludedItems = items; tracker.check(); }
});
container.appendChild(excluded.element);

A page with its own source passes searchFn, which returns a promise of items.

var picker = createMediaPicker({
    searchFn: function (query) { return shared.apiRequest('Search?term=' + encodeURIComponent(query)); }
});

Classes: stolon-media-picker, plus the Search Field, Combo Box, and Media Card classes. The handle is { element, input, getValue, setValue, setHeight, setSize, destroy }.

Parts: stolon-media-picker-results, stolon-media-picker-option, stolon-media-picker-message, stolon-media-picker-items, stolon-media-picker-item, stolon-media-picker-empty.

States and Modifiers

  • The default source asks Items through ApiClient.getJSON with searchTerm, IncludeItemTypes, Recursive, Limit, Fields, and EnableImageTypes, and reads .Items. Without ApiClient or searchFn a search finds nothing.
  • An item is { Id, Name, Type, ProductionYear }, with ImageTags when the source gives it. getValue returns the same shape. An item whose ImageTags has no Primary tag shows the placeholder icon.
  • label names the field and the input, and description adds a sentence under it.
  • itemTypes defaults to ['Movie', 'Series'], maxResults to 20, minChars to 2, and delay to 250 milliseconds. single: true keeps one item and replaces it on the next pick.
  • The defaults are "Search the library" (placeholder), "Clear" (clearLabel), "No items selected." (emptyText), "No matches." (noResultsText), "Could not search the library." (errorText), and "Remove {value}" (removeText). typeLabels names each type on the second line, such as "Collection" for BoxSet.
  • The arrow keys move through the results, Enter picks the active one, and a click outside closes them. The first Escape closes the results and keeps the text, and the next clears it.
  • An answer that is not the latest, or that comes after the results closed, is dropped.
  • The results float like the Combo Box dropdown, so a scrolling list or a dialog never clips them. The search field keeps its own search icon.
  • emptyText is the picked List's empty text, shown after the list while nothing is picked.
  • The picked items are a List of bordered rows, so height, maxHeight, and minHeight take what a List takes, and setHeight(height, maxHeight) changes them later. By default the list grows with its items.
  • Remove moves focus to the next Remove button, or to the input when the list empties. setValue does not call onChange.
  • size is small, medium (default), or large, and sizes its fields on the size scale. setSize(size) changes it later.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createMediaPicker

function createMediaPicker(options?: MediaPickerOptions): MediaPickerHandle

A library search that picks media items. Each result and each picked item shows as a media card with its artwork, and each picked item has a Remove button.

Option Type What it does
value MediaPickerItem[] | null picked items, as [{ Id, Name, Type, ProductionYear }] [optional]
itemTypes string[] item types to search (default ['Movie', 'Series'])
single boolean keep at most one item, replacing it on the next pick (default false)
maxResults number most results shown (default 20)
minChars number fewest typed characters that start a search (default 2)
delay number milliseconds to wait after typing before the search runs (default 250)
label string the field label, which also names the input [optional]
description string a sentence under the field [optional]
placeholder string the input placeholder (default 'Search the library')
clearLabel string title and aria-label of the clear button (default 'Clear')
emptyText string sentence shown while nothing is picked (default 'No items selected.')
noResultsText string sentence shown when a search finds nothing (default 'No matches.')
errorText string sentence shown when a search fails (default 'Could not search the library.')
removeText string title and aria-label of a remove button, where {value} is the item's name (default 'Remove {value}')
typeLabels Record<string, string> names of item types on the second line, such as { BoxSet: 'Collection' } [optional]
height ListHeight the picked list's height, 'auto', 'fill', a number of pixels, or a CSS length, as a List takes it (default 'auto')
maxHeight number | string | null the most the picked list grows before it scrolls [optional]
minHeight number | string | null the least a filling picked list takes [optional]
size 'small' | 'medium' | 'large' 'small', 'medium', or 'large' for its fields (default 'medium')
searchFn (query: string): Promise<MediaPickerItem[] | null | undefined> function(query) returning a promise of items, which replaces the Items endpoint [optional]
onChange (items: MediaPickerItem[]): void function(items) called when a pick or a removal changes the items [optional]
Handle member Type
input HTMLInputElement
getValue (): MediaPickerItem[]
setValue (items: MediaPickerItem[] | null): void
setHeight (height: ListHeight, maxHeight?: number | string | null): void
setSize (size?: 'small' | 'medium' | 'large'): void
destroy (): void
Type What it is
MediaPickerItem A library item in a media picker, as the Items endpoint returns it. ImageTags, when given, tells whether a Primary image exists.
Class What it looks like
stolon-media-picker Library search over a list of picked media rows, built by createMediaPicker
stolon-combo-dropdown.stolon-media-picker-results Results dropdown tall enough for several rows with artwork
stolon-combo-option.stolon-media-picker-option One result, a borderless media row with small artwork

Example

Example Code

<!-- title: Media Picker -->
<!-- min-height: 420 -->
<div id="mount"></div>
<span id="pickStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
const MEDIA = window.__galleryData.MEDIA;
const mount = document.getElementById('mount');

const picker = base.createMediaPicker({
    label: 'Excluded Titles',
    description: 'Type two letters, such as ex.',
    itemTypes: ['Movie', 'Series', 'Episode'],
    value: [MEDIA[0]],
    searchFn: (query) => Promise.resolve(MEDIA.filter((m) => m.Name.toLowerCase().indexOf(query.toLowerCase()) !== -1)),
    onChange: (items) => shared.setStatus('pickStatus', items.length + ' picked.', false, { timeout: 0 })
});
mount.appendChild(picker.element);
</script>