eLabSDK2.Inventory.Sample.SampleForm
Hierarchy
-
unknown↳
SampleForm
Methods
filterSampleTypes
Static filterSampleTypes(filter): void
Filter which sample types are available for selection when adding a new sample.
This allows you to control which sample types appear in the sample type selector
when creating a new sample. The filter function is called for each sample type,
and should return true to show the type or false to hide it. Use this to restrict
sample type options based on user permissions, location, project requirements, or
any other business logic.
Parameters
| Name | Type | Description |
|---|---|---|
filter | Function | Function that receives a sample type object and returns boolean. Return true to show the sample type, false to hide it. |
Returns
void
Example
// Hide specific sample types by name
eLabSDK2.Inventory.Sample.SampleForm.filterSampleTypes((sampleType) => {
const hiddenTypes = ['Internal Use Only', 'Deprecated'];
return !hiddenTypes.includes(sampleType.name);
});
Example
// Show only sample types for current user's department
eLabSDK2.Inventory.Sample.SampleForm.filterSampleTypes((sampleType) => {
const userDepartment = getUserDepartment();
return sampleType.department === userDepartment;
});
Example
// Filter based on permissions
eLabSDK2.Inventory.Sample.SampleForm.filterSampleTypes((sampleType) => {
// Only show "Hazardous" sample types to users with special permissions
if (sampleType.category === 'Hazardous') {
return eLabSDK2.System.User.hasPermissions('inventory', ['MANAGE_HAZARDOUS']);
}
return true;
});
Example
// Filter by sample type ID
eLabSDK2.Inventory.Sample.SampleForm.filterSampleTypes((sampleType) => {
const allowedTypeIds = [1, 5, 10, 15];
return allowedTypeIds.includes(sampleType.sampleTypeID);
});
getSampleFieldFromSampleForm
Static getSampleFieldFromSampleForm(fieldName): HTMLInputElement
Retrieve a form field DOM element by its field name from the sample form.
This searches the sample form for a field matching the given field name and returns
the input element. The search is case-insensitive and looks for both standard fields
and metadata fields. Use this to programmatically access, read, or modify form field
values. Returns undefined if the field is not found or if multiple matching fields exist.
Parameters
| Name | Type | Description |
|---|---|---|
fieldName | string | The display name of the field to retrieve (case-insensitive, e.g., 'Name', 'Barcode', 'Concentration'). |
Returns
HTMLInputElement
The HTMLInputElement for the field if found and unique, or undefined if not found or ambiguous.
Example
// Get and modify the barcode field
const barcodeField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Barcode');
if (barcodeField) {
barcodeField.value = generateBarcode();
console.log(`Barcode set to: ${barcodeField.value}`);
}
Example
// Read current field value
const nameField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Name');
if (nameField) {
console.log(`Current sample name: ${nameField.value}`);
}
Example
// Set multiple fields programmatically
const fields = ['Project', 'Owner', 'Department'];
fields.forEach(fieldName => {
const field = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm(fieldName);
if (field) {
field.value = getDefaultValue(fieldName);
}
});
Example
// Validate field before submission
const concentrationField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Concentration');
if (concentrationField) {
const value = parseFloat(concentrationField.value);
if (value < 0) {
alert('Concentration cannot be negative');
concentrationField.focus();
}
}
onAddNewSampleFormLoaded
Static onAddNewSampleFormLoaded(callback, id): void
Register a callback to execute when the new sample form is fully loaded and ready.
This event fires when the 'Add New Sample' form has completed loading all data and
rendering the form fields. The callback receives comprehensive form information including
the sample type, available fields, and form state. Use this to customize the form,
pre-fill values, add validation, or integrate with external systems during sample creation.
Parameters
| Name | Type | Description |
|---|---|---|
callback | (event: CustomEvent<SampleFormInterface>) => void | Function to execute when the form is loaded. Receives a CustomEvent with SampleFormInterface as detail containing form configuration and field information. |
id | string | Unique identifier for this callback registration. |
Returns
void
Example
// Log form details when loaded
eLabSDK2.Inventory.Sample.SampleForm.onAddNewSampleFormLoaded((event) => {
const formData = event.detail;
console.log(`Sample form loaded for type: ${formData.sampleType.name}`);
console.log(`Available fields: ${formData.fields.length}`);
}, 'log-form-load');
Example
// Pre-fill form fields with default values
eLabSDK2.Inventory.Sample.SampleForm.onAddNewSampleFormLoaded((event) => {
// Set default location
const locationField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Location');
if (locationField) {
locationField.value = 'Lab 101';
}
// Set default project
const projectField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Project');
if (projectField) {
projectField.value = 'Default Project';
}
}, 'pre-fill-defaults');
Example
// Add custom validation when form loads
eLabSDK2.Inventory.Sample.SampleForm.onAddNewSampleFormLoaded((event) => {
const formData = event.detail;
// Add validation message
console.log('Custom validation active for:', formData.sampleType.name);
}, 'setup-validation');
onAfterAddSampleFormLoad
Static onAfterAddSampleFormLoad(callback, id): void
Register a callback to execute after the 'Add Sample' form DOM has been fully rendered.
This event fires after the form has completed rendering and all form fields are visible
and accessible in the DOM. Use this for tasks that require the form elements to exist,
such as modifying field appearance, attaching event listeners, pre-filling values, or
initializing third-party form components.
Parameters
| Name | Type | Description |
|---|---|---|
callback | Function | Function to execute after the DOM is rendered. Receives detail information as parameter. |
id | string | Unique identifier for this callback registration. |
Returns
void
Example
// Pre-fill fields after form renders
eLabSDK2.Inventory.Sample.SampleForm.onAfterAddSampleFormLoad((detail) => {
console.log('Sample form rendered:', detail);
// Set default values
const projectField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Project');
if (projectField) {
projectField.value = 'Default Project';
}
}, 'set-defaults');
Example
// Attach custom event listeners
eLabSDK2.Inventory.Sample.SampleForm.onAfterAddSampleFormLoad(() => {
// Listen for changes to concentration field
const concField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Concentration');
if (concField) {
concField.addEventListener('change', (e) => {
validateConcentration(e.target.value);
});
}
}, 'attach-listeners');
Example
// Initialize date pickers or other UI components
eLabSDK2.Inventory.Sample.SampleForm.onAfterAddSampleFormLoad(() => {
const dateFields = document.querySelectorAll('.date-input');
dateFields.forEach(field => {
$(field).datepicker({ dateFormat: 'yy-mm-dd' });
});
}, 'init-date-pickers');
onAfterEditSampleFormLoad
Static onAfterEditSampleFormLoad(callback, id): void
Register a callback to execute after the 'Edit Sample' form DOM has been fully rendered.
This event fires after the edit form has completed rendering and all form fields are
populated with the current sample data. Use this for tasks that require both the DOM
elements and the sample data to be available, such as highlighting changes, comparing
with previous values, or adding edit-specific UI elements.
Parameters
| Name | Type | Description |
|---|---|---|
callback | Function | Function to execute after the DOM is rendered. Receives detail information as parameter. |
id | string | Unique identifier for this callback registration. |
Returns
void
Example
// Highlight modified fields after form loads
eLabSDK2.Inventory.Sample.SampleForm.onAfterEditSampleFormLoad((detail) => {
console.log('Sample edit form rendered:', detail);
// Compare with original values and highlight changes
const modifiedFields = getModifiedFields();
modifiedFields.forEach(fieldName => {
const field = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm(fieldName);
if (field) {
field.classList.add('field-modified');
}
});
}, 'highlight-changes');
Example
// Add change tracking after form loads
eLabSDK2.Inventory.Sample.SampleForm.onAfterEditSampleFormLoad(() => {
const allFields = document.querySelectorAll('.field-input, .meta-input');
allFields.forEach(field => {
// Store original value
field.dataset.originalValue = field.value;
// Track changes
field.addEventListener('change', (e) => {
const hasChanged = e.target.value !== e.target.dataset.originalValue;
e.target.classList.toggle('changed', hasChanged);
});
});
}, 'track-changes');
Example
// Show edit history in a custom section
eLabSDK2.Inventory.Sample.SampleForm.onAfterEditSampleFormLoad(async (detail) => {
const history = await fetchEditHistory(detail.sampleID);
displayEditHistory(history);
}, 'show-edit-history');
onBeforeAddSampleFormLoad
Static onBeforeAddSampleFormLoad(callback, id): void
Register a callback to execute before the 'Add Sample' form loads data and renders the DOM.
This event fires at the beginning of the form loading process, before the form fields
are rendered or populated with data. Use this for early initialization tasks such as
setting up validation rules, preparing external data sources, or configuring the form
behavior before it becomes visible to the user.
Parameters
| Name | Type | Description |
|---|---|---|
callback | Function | Function to execute before form load begins. Receives detail information as parameter. |
id | string | Unique identifier for this callback registration. |
Returns
void
Example
// Initialize custom validation before form loads
eLabSDK2.Inventory.Sample.SampleForm.onBeforeAddSampleFormLoad((detail) => {
console.log('Sample form loading:', detail);
setupCustomValidation();
prepareFieldDefaults();
}, 'init-validation');
Example
// Fetch external data before form displays
eLabSDK2.Inventory.Sample.SampleForm.onBeforeAddSampleFormLoad(async () => {
// Fetch default values from external API
const defaults = await fetchDefaultSampleValues();
storeDefaultsForLaterUse(defaults);
}, 'fetch-external-data');
Example
// Track analytics
eLabSDK2.Inventory.Sample.SampleForm.onBeforeAddSampleFormLoad(() => {
trackEvent('sample_form_opened', { type: 'create' });
window.formLoadStartTime = Date.now();
}, 'analytics-tracking');
onBeforeEditSampleFormLoad
Static onBeforeEditSampleFormLoad(callback, id): void
Register a callback to execute before the 'Edit Sample' form loads data and renders the DOM.
This event fires at the beginning of the sample editing form load process, before the
form fields are rendered or populated with existing sample data. Use this for early
initialization tasks specific to editing operations, such as fetching related data,
setting up edit-specific validation, or configuring form behavior.
Parameters
| Name | Type | Description |
|---|---|---|
callback | Function | Function to execute before form load begins. Receives detail information as parameter. |
id | string | Unique identifier for this callback registration. |
Returns
void
Example
// Track which sample is being edited
eLabSDK2.Inventory.Sample.SampleForm.onBeforeEditSampleFormLoad((detail) => {
console.log('Editing sample:', detail);
trackEvent('sample_edit_started', { sampleId: detail.sampleID });
}, 'track-edit');
Example
// Fetch historical data before form loads
eLabSDK2.Inventory.Sample.SampleForm.onBeforeEditSampleFormLoad(async (detail) => {
const history = await fetchSampleHistory(detail.sampleID);
storeHistoryForComparison(history);
}, 'fetch-history');
Example
// Set up edit-specific validation rules
eLabSDK2.Inventory.Sample.SampleForm.onBeforeEditSampleFormLoad(() => {
setupEditValidation({
preventNameChange: true,
requireApproval: true
});
}, 'setup-edit-validation');
registerAction
Static registerAction(action): void
Register a custom action button in the sample form.
This adds a custom action button to the sample creation/editing form, typically appearing
in the form toolbar or action area. Use this to add custom functionality like validating
external data, triggering integrations, or performing calculations before sample creation.
Actions can be made conditional based on form state or user permissions.
Parameters
| Name | Type | Description |
|---|---|---|
action | Action | { id: string, label: string, icon?: string, onClick: () => void, showCondition?: () => boolean } |
Returns
void
Example
// Add barcode generator action
eLabSDK2.Inventory.Sample.SampleForm.registerAction({
id: 'generate-barcode',
label: 'Generate Barcode',
icon: 'fas fa-barcode',
onClick: () => {
const barcodeField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Barcode');
if (barcodeField) {
barcodeField.value = generateUniqueBarcode();
eLabSDK2.UI.Toast.showToast('Barcode generated', 2000);
}
}
});
Example
// Add external system lookup action
eLabSDK2.Inventory.Sample.SampleForm.registerAction({
id: 'lookup-cas-number',
label: 'Lookup CAS Number',
icon: 'fas fa-search',
onClick: async () => {
const nameField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('Name');
if (nameField && nameField.value) {
const casNumber = await lookupCASNumber(nameField.value);
const casField = eLabSDK2.Inventory.Sample.SampleForm.getSampleFieldFromSampleForm('CAS Number');
if (casField && casNumber) {
casField.value = casNumber;
}
}
}
});
setSampleFormData
Static setSampleFormData(data): void
Programmatically set field values in the sample form.
This allows you to pre-populate or update field values in the sample creation/editing
form. Each data object specifies a field name and the value to set. Use this to
auto-fill form fields based on context, copy values from other samples, or integrate
with external systems that provide sample data.
Parameters
| Name | Type | Description |
|---|---|---|
data | SampleFormInputData[] | Array of field data objects to set in the form. |
Returns
void
Example
// Pre-fill multiple form fields
eLabSDK2.Inventory.Sample.SampleForm.setSampleFormData([
{ fieldName: 'Project', value: 'Cancer Research Project' },
{ fieldName: 'Owner', value: 'John Doe' },
{ fieldName: 'Location', value: 'Freezer A, Shelf 2' }
]);
Example
// Copy values from existing sample
const sourceSample = await eLabSDK2.Inventory.Sample.getSampleByID(12345);
eLabSDK2.Inventory.Sample.SampleForm.setSampleFormData([
{ fieldName: 'Concentration', value: sourceSample.meta.concentration },
{ fieldName: 'Buffer', value: sourceSample.meta.buffer },
{ fieldName: 'Storage Conditions', value: sourceSample.meta.storageConditions }
]);
Example
// Integrate with external system
async function importSampleData(externalId) {
const externalData = await fetchFromExternalLIMS(externalId);
eLabSDK2.Inventory.Sample.SampleForm.setSampleFormData([
{ fieldName: 'Name', value: externalData.compoundName },
{ fieldName: 'CAS Number', value: externalData.casNumber },
{ fieldName: 'Molecular Weight', value: externalData.mw },
{ fieldName: 'Purity', value: externalData.purity }
]);
}
© 2023 eLabNext
Updated about 18 hours ago