Spaces:
Running
Running
File size: 6,052 Bytes
9bd422a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | /**
* ExportHandler - Handles exporting model information as JSON
* Requirements: 12.1, 12.2, 12.3, 12.4
*/
const ExportHandler = (function () {
class ExportHandler {
/**
* @param {string} [exportBtnId='exportBtn'] - ID of the export button element
*/
constructor(exportBtnId = 'exportBtn') {
this._exportBtn = document.getElementById(exportBtnId);
this._unsubscribe = null;
this._init();
}
// βββ Private ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_init() {
if (!this._exportBtn) {
console.warn('[ExportHandler] Export button not found:', this._exportBtn);
return;
}
// Bind click handler
this._exportBtn.addEventListener('click', () => this._handleExport());
// Subscribe to currentModel changes to enable/disable button
this._unsubscribe = StateManager.subscribe('currentModel', (model) => {
this._setButtonEnabled(!!model);
});
// Set initial state
this._setButtonEnabled(!!StateManager.getCurrentModel());
}
/**
* Enable or disable the export button.
* @param {boolean} enabled
*/
_setButtonEnabled(enabled) {
if (!this._exportBtn) return;
this._exportBtn.disabled = !enabled;
}
/**
* Build the export payload from the current model.
* @param {object} model - ParsedModel from StateManager
* @returns {object}
*/
_buildExportData(model) {
const data = {};
// Metadata
data.metadata = model.metadata ? Object.assign({}, model.metadata) : {};
// Inputs
data.inputs = Array.isArray(model.inputs)
? model.inputs.map((i) => Object.assign({}, i))
: [];
// Outputs
data.outputs = Array.isArray(model.outputs)
? model.outputs.map((o) => Object.assign({}, o))
: [];
// Graph (nodes + edges)
if (model.graph) {
data.graph = {
name: model.graph.name || '',
nodes: Array.isArray(model.graph.nodes)
? model.graph.nodes.map((n) => Object.assign({}, n))
: [],
edges: Array.isArray(model.graph.edges)
? model.graph.edges.map((e) => Object.assign({}, e))
: [],
};
} else {
data.graph = { name: '', nodes: [], edges: [] };
}
// Initializers
data.initializers = Array.isArray(model.initializers)
? model.initializers.map((init) => Object.assign({}, init))
: [];
return data;
}
/**
* Derive the download file name from the model's fileName metadata.
* e.g. "model.onnx" β "model_info.json"
* @param {object} metadata
* @returns {string}
*/
_buildFileName(metadata) {
const fileName = (metadata && metadata.fileName) || 'model';
// Strip directory path, keep base name
const base = fileName.split('/').pop().split('\\').pop();
// Remove extension
const withoutExt = base.replace(/\.[^.]+$/, '');
return `${withoutExt}_info.json`;
}
/**
* Trigger a browser download of the given JSON string.
* @param {string} jsonStr
* @param {string} fileName
*/
_triggerDownload(jsonStr, fileName) {
const blob = new Blob([jsonStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = url;
anchor.download = fileName;
anchor.style.display = 'none';
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
// Release the object URL after a short delay
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
/**
* Show a brief success or error notification.
* Falls back to console if no ErrorDisplay is available.
* @param {string} message
* @param {'success'|'error'} type
*/
_showNotification(message, type) {
if (window.ErrorDisplay && typeof window.ErrorDisplay.show === 'function') {
window.ErrorDisplay.show(message, type === 'error' ? 'error' : 'info');
} else {
if (type === 'error') {
console.error('[ExportHandler]', message);
} else {
console.info('[ExportHandler]', message);
}
}
}
// βββ Export Handler ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_handleExport() {
try {
const model = StateManager.getCurrentModel();
if (!model) {
this._showNotification(
CONFIG.ERRORS.EXPORT_ERROR + ' No model is currently loaded.',
'error'
);
return;
}
const exportData = this._buildExportData(model);
const jsonStr = JSON.stringify(exportData, null, 2);
const fileName = this._buildFileName(model.metadata);
this._triggerDownload(jsonStr, fileName);
this._showNotification(CONFIG.SUCCESS.MODEL_EXPORTED, 'success');
} catch (err) {
console.error('[ExportHandler] Export failed:', err);
this._showNotification(CONFIG.ERRORS.EXPORT_ERROR, 'error');
}
}
// βββ Public API ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Programmatically trigger an export (useful for testing).
*/
export() {
this._handleExport();
}
/**
* Clean up subscriptions.
*/
destroy() {
if (typeof this._unsubscribe === 'function') {
this._unsubscribe();
this._unsubscribe = null;
}
}
}
return ExportHandler;
})();
// Export for global access in vanilla JS context
window.ExportHandler = ExportHandler;
|