//---------------------------------------------------------
// Review form: the complete script at the end of the Script Panel Tutorial workflow
// (script-panel.md). Run it from the tutorial, or with Script > Open VivoScript.
//
// A review form across several tabs: an Overview tab and one tab per organ. It shows
// - tabs, each with its own columns
// - a repeatable section the user can add copies of, and reading it back as an array
// - the status line, kept up to date as the user edits (valueChanged)
// - making fields read-only, and editable again, from a handler
// - waiting for the user, and refusing to go on until the form is valid
// The tutorial's earlier steps explain each part.
//---------------------------------------------------------
var organs = ['Liver', 'Kidney', 'Spleen'];
var panel = VQ.scriptPanel();
panel.clear(); // the panel keeps its contents between scripts
panel.setTitle('Organ review');
// The default tab is named ''. Its text is "Main" unless the script changes it.
panel.setTabLabel('', 'Overview');
panel.setColumns(2);
panel.setFields([
{ name: 'reader', label: 'Reader', type: 'text', value: 'Example reader', tooltip: 'Required' },
{ name: 'date', label: 'Date', type: 'text', value: '20260101', tooltip: 'As YYYYMMDD' },
{ name: 'complete', label: 'Review complete', type: 'bool', span: 2 },
// Read-only until "Review complete" is ticked (see the valueChanged handler below)
{ name: 'summary', label: 'Summary', type: 'textarea', span: 2, readOnly: true,
tooltip: 'Tick "Review complete" to write a summary' }
]);
// One tab per organ. Naming a tab that doesn't exist yet creates it, at the end.
// All tabs share one name scope, so the organ is part of each field's name.
organs.forEach(function (organ) {
panel.setColumns(2, organ);
panel.setFields([
{ name: organ + 'Uptake', label: 'Uptake (%ID/g)', type: 'double', min: 0, max: 100, decimals: 3 },
{ name: organ + 'Grade', label: 'Grade', type: 'item', items: ['Normal', 'Mild', 'Severe'] },
// A repeatable: the user presses Add for another copy. Its value is an array with
// one object per copy; there is always at least one copy.
{ name: organ + 'Notes', label: 'Notes', type: 'repeatable', columns: 2, children: [
{ name: 'region', label: 'Region', type: 'item', items: ['Whole organ', 'Cortex', 'Medulla'] },
{ name: 'note', label: 'Note', type: 'text' }
] }
], organ);
});
// What stops the user submitting; shown in the status line, above the Submit button
function problems(v) {
var p = [];
if (String(v.reader).trim() === '') p.push('Reader is empty');
if (!/^\d{8}$/.test(v.date)) p.push("Date '" + v.date + "' is not a date as YYYYMMDD");
organs.forEach(function (organ) {
var noted = v[organ + 'Notes'].some(function (n) { return n.note.trim() !== ''; });
if (v[organ + 'Grade'] === 'Severe' && !noted) p.push(organ + ' is graded Severe but has no note');
});
return p;
}
function showProblems() {
var p = problems(panel.values());
if (p.length) {
panel.setStatus("Can't submit yet:\n- " + p.join('\n- '), 'error');
} else {
panel.setStatus('Ready: check each tab, then press Submit.');
}
return p;
}
// Runs whenever the user edits a value (not when the script calls setValue), while the
// script waits. For an edit inside a repeatable, name is the repeatable and value its array.
panel.valueChanged.connect(function (name, value) {
if (name === 'complete') {
panel.setReadOnly('summary', !value);
}
showProblems();
});
// To follow the tab the user is looking at, connect to the tab widget:
// VQ.getWidget('ScriptPanel.tabs').currentChanged.connect(function () { ... panel.currentTab() ... });
panel.showPanel();
panel.setCurrentTab('');
// Fill in what the script works out. setValue shows each value at once.
organs.forEach(function (organ, i) {
panel.setStatus('Measuring ' + organ + '...');
// ... the real measurement goes here ...
panel.setValue(organ + 'Uptake', 1.5 * (i + 1));
});
// A repeatable is set as a whole, with one object per copy
panel.setValue('LiverNotes', [ { region: 'Whole organ', note: 'Homogeneous' } ]);
showProblems();
// Let the user review and edit, until the form is valid and they press Submit.
// wait() returns false if the script is stopped while waiting.
while (true) {
if (!panel.wait('Submit')) {
VQ.abort();
}
if (showProblems().length === 0) {
break;
}
VQ.showMessage("Some answers need fixing: see the list above the Submit button.");
}
// Read everything back: one flat object with the values of every tab
var values = panel.values();
VQ.debug("Review values: " + JSON.stringify(values));
// Leave the panel as a read-only record of what was submitted
panel.fieldNames().forEach(function (name) {
panel.setReadOnly(name, true);
});
panel.setStatus('Submitted. These are the values the script used.');
// Automated tests (VQ_TEST_MODE) close VivoQuant here
if (VQ.isTestMode()) {
VQ.quit();
}