source_GridView_gridQuery.bs

import "pkg:/source/utils/misc.bs"

' The library grid's LoadItemsTask2 query — what BaseGridView asks for, kept apart from the
' Task node that runs it.
'
' BaseGridView runs every load and every page on a NEW node (see replaceTask), so the node
' cannot be the memory of the query: the next page must ask for exactly what the first page
' asked for, and that has to survive the node being replaced. The query is a plain AA held
' on the component instead.
'
' It lives in source/ rather than in BaseGridView.bs because a component codebehind's
' functions are scoped to that component and a Rooibos suite cannot call them without a
' callFunc seam on the XML. Same move as source/home/latestRows.bs.
namespace gridQuery

  ' The query for a fresh load: the defaults, then whatever the presenter sets for its view.
  ' Presenters receive a plain AA and assign fields on it exactly as they would on a
  ' LoadItemsTask2 node. Built from scratch every time, so nothing a presenter set for one
  ' view carries into the next.
  '
  ' @param state - { nameStartsWith, searchTerm, sortField, sortAscending, filter, filterOptions }
  ' @param parentItem - the library (or folder) the grid shows
  ' @param view - the current view mode, passed to the presenter
  ' @param presenter - the grid's presenter, or invalid
  ' @return LoadItemsTask2 input fields, without startIndex (see forPage)
  function build(state as object, parentItem as object, view as dynamic, presenter as dynamic) as object
    query = {
      nameStartsWith: state.nameStartsWith,
      searchTerm: state.searchTerm,
      sortField: state.sortField,
      sortAscending: state.sortAscending,
      filter: state.filter,
      filterOptions: state.filterOptions,
      ' Presenters can override any of these
      itemId: parentItem.Id,
      isRecursive: true,
      itemType: "",
      view: "",
      studioIds: "",
      genreIds: ""
    }
    if isValid(presenter)
      presenter.configureLoadTask(query, parentItem, view)
    end if
    return query
  end function

  ' The fields for one run of `query`, starting at `startIndex`. A copy: `query` itself is
  ' left untouched, so it stays the record of what every page of this load asks for.
  '
  ' @param query - a query from build()
  ' @param startIndex - the first item to fetch
  ' @return LoadItemsTask2 input fields
  function forPage(query as object, startIndex as integer) as object
    fields = {}
    fields.append(query)
    fields.startIndex = startIndex
    return fields
  end function

end namespace