runtimeStepperJs top-level constant

String const runtimeStepperJs

Implementation

const String runtimeStepperJs = r'''
function stepperRoot(groupId) {
  return document.querySelector('[data-arcane-stepper="' + cssEscape(groupId) +
    '"][data-arcane-step-active]');
}

function goToStep(groupId, target) {
  const root = stepperRoot(groupId);
  if (!root) return;
  const cur = parseInt(root.getAttribute('data-arcane-step-active') || '0', 10);
  const count = parseInt(root.getAttribute('data-arcane-step-count') || '1', 10);
  let next = cur;
  if (target === 'next') next = Math.min(cur + 1, count - 1);
  else if (target === 'prev') next = Math.max(cur - 1, 0);
  else {
    const n = parseInt(target, 10);
    if (!isNaN(n)) next = Math.max(0, Math.min(n, count - 1));
  }
  if (next === cur) return;
  root.setAttribute('data-arcane-step-active', next.toString());
  const steps = document.querySelectorAll(
    '[data-arcane-stepper="' + cssEscape(groupId) + '"][data-arcane-step]'
  );
  for (let i = 0; i < steps.length; i++) {
    const stepEl = steps[i];
    const idx = parseInt(stepEl.getAttribute('data-arcane-step') || '0', 10);
    let state = 'pending';
    if (idx < next) state = 'completed';
    else if (idx === next) state = 'active';
    stepEl.setAttribute('data-arcane-state', state);
  }
  fireEvent(root, 'arcane:step-change', { groupId: groupId, step: next });
}

ARCANE.stepper = {
  go: goToStep
};
''';