runtimePaginationJs top-level constant

String const runtimePaginationJs

Implementation

const String runtimePaginationJs = r'''
function paginationRoot(groupId) {
  return document.querySelector('[data-arcane-pagination="' + cssEscape(groupId) +
    '"][data-arcane-page-active]');
}

function goToPage(groupId, target) {
  const root = paginationRoot(groupId);
  if (!root) return;
  const cur = parseInt(root.getAttribute('data-arcane-page-active') || '1', 10);
  const count = parseInt(root.getAttribute('data-arcane-page-count') || '1', 10);
  let next = cur;
  if (target === 'next') next = Math.min(cur + 1, count);
  else if (target === 'prev') next = Math.max(cur - 1, 1);
  else if (target === 'first') next = 1;
  else if (target === 'last') next = count;
  else {
    const n = parseInt(target, 10);
    if (!isNaN(n)) next = Math.max(1, Math.min(n, count));
  }
  if (next === cur) return;
  root.setAttribute('data-arcane-page-active', next.toString());
  fireEvent(root, 'arcane:page-change', { groupId: groupId, page: next });
}

ARCANE.pagination = {
  go: goToPage
};
''';