Spaces:
Paused
Paused
File size: 12,728 Bytes
aceb1b2 | 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | /**
* Instance Display Manager
*
* Handles client-side functionality for the instance display system.
* This includes image zoom, collapsible sections, and coordination
* with annotation schemas that reference display fields.
*/
(function() {
'use strict';
/**
* InstanceDisplayManager handles all display field interactions
*/
class InstanceDisplayManager {
constructor() {
this.displayContainer = document.querySelector('.instance-display-container');
this.displayFields = {};
this.spanTargets = [];
if (this.displayContainer) {
this.init();
}
}
/**
* Initialize the display manager
*/
init() {
this.collectDisplayFields();
this.initImageZoom();
this.initCollapsibleSections();
this.initSpanTargets();
this.initPerTurnRatings();
console.log('[InstanceDisplay] Initialized with', Object.keys(this.displayFields).length, 'fields');
}
/**
* Collect all display fields from the DOM
*/
collectDisplayFields() {
const fields = this.displayContainer.querySelectorAll('.display-field');
fields.forEach(field => {
const key = field.dataset.fieldKey;
const type = field.dataset.fieldType;
if (key) {
this.displayFields[key] = {
element: field,
type: type,
isSpanTarget: field.dataset.spanTarget === 'true'
};
if (field.dataset.spanTarget === 'true') {
this.spanTargets.push(key);
}
}
});
}
/**
* Initialize image zoom functionality
*/
initImageZoom() {
const zoomContainers = document.querySelectorAll('.image-zoom-container');
zoomContainers.forEach(container => {
const img = container.querySelector('img');
const zoomIn = container.querySelector('.zoom-in');
const zoomOut = container.querySelector('.zoom-out');
const zoomReset = container.querySelector('.zoom-reset');
if (!img) return;
let scale = 1;
const minScale = 0.5;
const maxScale = 5;
const scaleStep = 1.25;
const updateScale = (newScale) => {
scale = Math.max(minScale, Math.min(maxScale, newScale));
img.style.transform = `scale(${scale})`;
img.style.transformOrigin = 'center center';
};
if (zoomIn) {
zoomIn.addEventListener('click', (e) => {
e.preventDefault();
updateScale(scale * scaleStep);
});
}
if (zoomOut) {
zoomOut.addEventListener('click', (e) => {
e.preventDefault();
updateScale(scale / scaleStep);
});
}
if (zoomReset) {
zoomReset.addEventListener('click', (e) => {
e.preventDefault();
updateScale(1);
});
}
// Also support scroll wheel zoom when hovering
container.addEventListener('wheel', (e) => {
if (e.ctrlKey || e.metaKey) {
e.preventDefault();
const delta = e.deltaY > 0 ? 1 / scaleStep : scaleStep;
updateScale(scale * delta);
}
});
});
}
/**
* Initialize collapsible sections with persistent state
*/
initCollapsibleSections() {
const collapsibles = document.querySelectorAll('.collapsible-text-container');
collapsibles.forEach(container => {
const toggle = container.querySelector('.collapsible-toggle');
const content = container.querySelector('.collapse');
if (!toggle || !content) return;
// Get the field key from the parent display-field or collapse ID
const displayField = container.closest('.display-field');
const fieldKey = displayField ? displayField.dataset.fieldKey : content.id;
const storageKey = `potato_collapse_${fieldKey}`;
// Restore state from localStorage
const savedState = localStorage.getItem(storageKey);
if (savedState !== null) {
const shouldBeExpanded = savedState === 'expanded';
const isCurrentlyExpanded = content.classList.contains('show');
if (shouldBeExpanded !== isCurrentlyExpanded) {
// Need to toggle the state
if (shouldBeExpanded) {
content.classList.add('show');
toggle.setAttribute('aria-expanded', 'true');
} else {
content.classList.remove('show');
toggle.setAttribute('aria-expanded', 'false');
}
}
}
// Save state when toggled
content.addEventListener('shown.bs.collapse', () => {
toggle.setAttribute('aria-expanded', 'true');
localStorage.setItem(storageKey, 'expanded');
});
content.addEventListener('hidden.bs.collapse', () => {
toggle.setAttribute('aria-expanded', 'false');
localStorage.setItem(storageKey, 'collapsed');
});
});
}
/**
* Initialize span target fields
*/
initSpanTargets() {
// Span targets need special handling for the span annotation system
// This sets up the necessary attributes and event listeners
this.spanTargets.forEach(key => {
const field = this.displayFields[key];
if (!field) return;
const textContent = field.element.querySelector('.text-content');
if (textContent) {
// Ensure the text content has the necessary attributes
// for the span annotation system to work
if (!textContent.id) {
textContent.id = `text-content-${key}`;
}
}
});
}
/**
* Initialize per-turn rating widgets in dialogue displays.
* Supports both single-schema and multi-schema (multi-dimension) per-turn ratings.
* Each schema stores its values in its own hidden input.
*/
initPerTurnRatings() {
const containers = document.querySelectorAll('.has-per-turn-ratings');
containers.forEach(container => {
const fieldKey = container.dataset.fieldKey;
// Collect all hidden inputs keyed by schema name
const hiddenInputs = {};
container.querySelectorAll('.per-turn-hidden').forEach(input => {
const schemaName = input.dataset.schemaName;
if (schemaName) {
hiddenInputs[schemaName] = input;
}
});
// Fall back to single hidden input (legacy format)
const singleHiddenInput = container.querySelector('.annotation-data-input:not(.per-turn-hidden)');
// Rating values keyed by schema: {schemaName: {turnIndex: value}}
const ratingValuesBySchema = {};
// Handle click on rating values
container.querySelectorAll('.ptr-value').forEach(el => {
el.addEventListener('click', (e) => {
e.preventDefault();
const turn = el.dataset.turn;
const value = parseInt(el.dataset.value, 10);
const schema = el.dataset.schema || '';
// Initialize schema bucket
if (!ratingValuesBySchema[schema]) {
ratingValuesBySchema[schema] = {};
}
const schemaValues = ratingValuesBySchema[schema];
// Toggle: clicking same value deselects
if (schemaValues[turn] === value) {
delete schemaValues[turn];
} else {
schemaValues[turn] = value;
}
// Update visual state for this turn + schema combination
const selector = `.ptr-value[data-turn="${turn}"][data-schema="${schema}"]`;
container.querySelectorAll(selector).forEach(v => {
const vVal = parseInt(v.dataset.value, 10);
if (schemaValues[turn] && vVal <= schemaValues[turn]) {
v.classList.add('ptr-selected');
} else {
v.classList.remove('ptr-selected');
}
});
// Update the corresponding hidden input
if (schema && hiddenInputs[schema]) {
hiddenInputs[schema].value = JSON.stringify(schemaValues);
} else if (singleHiddenInput) {
singleHiddenInput.value = JSON.stringify(schemaValues);
}
console.log('[InstanceDisplay] Per-turn rating:', fieldKey,
schema ? `schema=${schema}` : '', 'turn', turn, '=',
schemaValues[turn] || 'cleared');
});
});
});
}
/**
* Get a display field by key
* @param {string} key - The field key
* @returns {Object|null} The field info or null
*/
getField(key) {
return this.displayFields[key] || null;
}
/**
* Get the source URL for a field (for images/videos/audio)
* @param {string} key - The field key
* @returns {string|null} The source URL or null
*/
getSourceUrl(key) {
const field = this.getField(key);
if (!field) return null;
const sourceElement = field.element.querySelector('[data-source-url]');
return sourceElement ? sourceElement.dataset.sourceUrl : null;
}
/**
* Get all span target field keys
* @returns {string[]} Array of field keys that are span targets
*/
getSpanTargets() {
return [...this.spanTargets];
}
/**
* Check if multiple span targets exist (multi-span mode)
* @returns {boolean}
*/
isMultiSpanMode() {
return this.spanTargets.length > 1;
}
/**
* Get the primary text content element for span annotation
* Falls back to legacy #text-content if instance_display not configured
* @returns {HTMLElement|null}
*/
getPrimaryTextElement() {
// First check for instance_display span targets
if (this.spanTargets.length > 0) {
const key = this.spanTargets[0];
const field = this.displayFields[key];
if (field) {
return field.element.querySelector('.text-content');
}
}
// Fall back to legacy element
return document.getElementById('text-content');
}
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
window.instanceDisplayManager = new InstanceDisplayManager();
});
} else {
window.instanceDisplayManager = new InstanceDisplayManager();
}
// Export for module systems
if (typeof module !== 'undefined' && module.exports) {
module.exports = InstanceDisplayManager;
}
})();
|