|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 LabKey Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +/** |
| 7 | + * A plugin for EHR.panel.BulkEditPanel that makes the Observation/Score field's editor depend on the |
| 8 | + * selected Category, mirroring EHR.grid.plugin.ClinicalObservationsCellEditing so that the data type |
| 9 | + * and options offered in the bulk edit dialog match the clinical observations grid. |
| 10 | + * |
| 11 | + * It is attached to the bulk edit panel via the clinical observations grid's formConfig.bulkEditPlugins |
| 12 | + * (see EHR.grid.ClinicalObservationGridPanel), so the generic bulk edit panel stays free of any |
| 13 | + * observation-specific logic. |
| 14 | + */ |
| 15 | +Ext4.define('EHR.plugin.ClinicalObservationsBulkEdit', { |
| 16 | + extend: 'Ext.AbstractPlugin', |
| 17 | + alias: 'plugin.clinicalobservationsbulkedit', |
| 18 | + |
| 19 | + init: function(panel){ |
| 20 | + this.panel = panel; |
| 21 | + this.observationTypesStore = EHR.DataEntryUtils.getObservationTypesStore(); |
| 22 | + panel.on('afterrender', this.setupObservationDependency, this, {single: true}); |
| 23 | + |
| 24 | + this.callParent(arguments); |
| 25 | + }, |
| 26 | + |
| 27 | + setupObservationDependency: function(){ |
| 28 | + var panel = this.panel; |
| 29 | + var categoryField = panel.down('[name=category]'); |
| 30 | + var observationField = panel.down('[name=observation]'); |
| 31 | + if (!categoryField || !observationField){ |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // Capture the base config of the original Observation/Score field so it can be rebuilt with a |
| 36 | + // category-specific editor (see reconfigureObservationField). |
| 37 | + this.observationFieldBaseCfg = { |
| 38 | + name: observationField.name, |
| 39 | + fieldLabel: observationField.fieldLabel, |
| 40 | + labelWidth: observationField.labelWidth, |
| 41 | + width: observationField.width, |
| 42 | + // honor metadata that marks the field as never-enableable (see BulkEditPanel.getFieldConfigs) |
| 43 | + originalDisabled: observationField.originalDisabled |
| 44 | + }; |
| 45 | + |
| 46 | + categoryField.on('change', function(field, newValue){ |
| 47 | + this.reconfigureObservationField(newValue); |
| 48 | + }, this); |
| 49 | + |
| 50 | + // Initialize based on any pre-populated category value (e.g. when all selected records share a category) |
| 51 | + var initialCategory = categoryField.getValue(); |
| 52 | + if (initialCategory){ |
| 53 | + this.reconfigureObservationField(initialCategory); |
| 54 | + } |
| 55 | + }, |
| 56 | + |
| 57 | + reconfigureObservationField: function(category){ |
| 58 | + var store = this.observationTypesStore; |
| 59 | + // hasLoadedOnce (set in EHR.DataEntryUtils.getObservationTypesStore) distinguishes a pending |
| 60 | + // initial load from one that already returned no rows; for the latter we fall through to the |
| 61 | + // textfield default rather than waiting on a load event that will never fire |
| 62 | + if (!store.hasLoadedOnce){ |
| 63 | + store.on('load', function(){ |
| 64 | + // the dialog may have been closed before the store finished loading |
| 65 | + if (this.panel && !this.panel.isDestroyed){ |
| 66 | + this.reconfigureObservationField(category); |
| 67 | + } |
| 68 | + }, this, {single: true}); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + var panel = this.panel; |
| 73 | + var observationField = panel.down('[name=observation]'); |
| 74 | + if (!observationField){ |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + //note: we proceed even if the category cannot be found, to support records saved under a no-longer-supported category |
| 79 | + var rec = category ? store.findRecord('value', category) : null; |
| 80 | + var rawEditorConfig = (rec && rec.get('editorconfig')) || null; |
| 81 | + |
| 82 | + // the category combo fires change on every keystroke, so skip the rebuild when the resolved |
| 83 | + // editor is unchanged; this avoids churn and preserves any value the user already entered |
| 84 | + if (this.appliedEditorConfig !== undefined && this.appliedEditorConfig === rawEditorConfig){ |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + var container = observationField.ownerCt; |
| 89 | + var index = container.items.indexOf(observationField); |
| 90 | + //preserve the user's enable/disable toggle state across category changes |
| 91 | + var wasDisabled = observationField.isDisabled(); |
| 92 | + |
| 93 | + var editorConfig = rawEditorConfig ? Ext4.decode(rawEditorConfig) : null; |
| 94 | + editorConfig = editorConfig || { |
| 95 | + xtype: 'textfield' |
| 96 | + }; |
| 97 | + |
| 98 | + var cfg = Ext4.apply({}, editorConfig); |
| 99 | + Ext4.apply(cfg, this.observationFieldBaseCfg); |
| 100 | + delete cfg.value; |
| 101 | + delete cfg.defaultValue; |
| 102 | + cfg.allowBlank = true; |
| 103 | + cfg.disabled = wasDisabled; |
| 104 | + |
| 105 | + cfg = EHR.DataEntryUtils.ensureLookupPlugin(cfg, false); |
| 106 | + |
| 107 | + container.remove(observationField, true); |
| 108 | + var newField = Ext4.widget(cfg); |
| 109 | + panel.addLabelToggle(newField); |
| 110 | + container.insert(index, newField); |
| 111 | + |
| 112 | + // the databind plugin only registers listeners on the fields present at init, so register the |
| 113 | + // replacement explicitly to keep record syncing and validation consistent with the original field |
| 114 | + var databind = panel.getPlugin('ehr-databind'); |
| 115 | + if (databind){ |
| 116 | + databind.addFieldListener(newField); |
| 117 | + } |
| 118 | + |
| 119 | + this.appliedEditorConfig = rawEditorConfig; |
| 120 | + } |
| 121 | +}); |
0 commit comments