|
Line 0
Link Here
|
| 0 |
- |
1 |
/** |
|
|
2 |
* EDIFACT Display Module |
| 3 |
*/ |
| 4 |
const EdifactDisplay = (() => { |
| 5 |
"use strict"; |
| 6 |
|
| 7 |
const defaults = { |
| 8 |
modalId: "#EDI_modal", |
| 9 |
modalBodySelector: ".modal-body", |
| 10 |
jsonEndpoint: "/cgi-bin/koha/acqui/edimsg.pl", |
| 11 |
expandByDefault: true, |
| 12 |
}; |
| 13 |
|
| 14 |
const extractFocusOptions = element => { |
| 15 |
const options = {}; |
| 16 |
const basketno = element.dataset.basketno; |
| 17 |
const basketname = element.dataset.basketname; |
| 18 |
const invoicenumber = element.dataset.invoicenumber; |
| 19 |
|
| 20 |
if (basketno) options.basketno = basketno; |
| 21 |
if (basketname) options.basketname = basketname; |
| 22 |
if (invoicenumber) options.invoicenumber = invoicenumber; |
| 23 |
|
| 24 |
return Object.keys(options).length > 0 ? options : null; |
| 25 |
}; |
| 26 |
|
| 27 |
const init = (options = {}) => { |
| 28 |
const settings = { ...defaults, ...options }; |
| 29 |
|
| 30 |
document.body.addEventListener("click", e => { |
| 31 |
if (e.target.closest(".view_edifact_message")) { |
| 32 |
e.preventDefault(); |
| 33 |
const button = e.target.closest(".view_edifact_message"); |
| 34 |
const messageId = button.dataset.messageId; |
| 35 |
const customUrl = button.dataset.url; |
| 36 |
const focusOptions = extractFocusOptions(button); |
| 37 |
|
| 38 |
if (messageId) { |
| 39 |
showMessage(messageId, settings, focusOptions); |
| 40 |
} else if (customUrl) { |
| 41 |
showMessageFromUrl(customUrl, settings, focusOptions); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if (e.target.closest(".view_message_enhanced")) { |
| 46 |
e.preventDefault(); |
| 47 |
const link = e.target.closest(".view_message_enhanced"); |
| 48 |
const href = link.getAttribute("href"); |
| 49 |
const focusOptions = extractFocusOptions(link); |
| 50 |
|
| 51 |
showMessageFromUrl(href, settings, focusOptions); |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
initializeModal(settings); |
| 56 |
}; |
| 57 |
|
| 58 |
const showMessage = (messageId, settings, focusOptions) => { |
| 59 |
const url = `${settings.jsonEndpoint}?id=${encodeURIComponent(messageId)}&format=json`; |
| 60 |
showMessageFromUrl(url, settings, focusOptions); |
| 61 |
}; |
| 62 |
|
| 63 |
const showMessageFromUrl = (url, settings, focusOptions) => { |
| 64 |
const modal = $(settings.modalId); |
| 65 |
const modalContent = modal.find(".modal-content"); |
| 66 |
const modalBody = modal.find(settings.modalBodySelector); |
| 67 |
const loadingHtml = `<div class="edi-loading"> |
| 68 |
<img src="/intranet-tmpl/${window.theme || "prog"}/img/spinner-small.gif" alt="" /> |
| 69 |
Loading |
| 70 |
</div>`; |
| 71 |
|
| 72 |
// Remove any existing EDIFACT navbars |
| 73 |
modalContent.find(".edi-main-navbar, .edi-tree-toolbar").remove(); |
| 74 |
|
| 75 |
modalBody.html(loadingHtml); |
| 76 |
modal.modal("show"); |
| 77 |
|
| 78 |
const jsonUrl = url + (url.includes("?") ? "&" : "?") + "format=json"; |
| 79 |
|
| 80 |
$.ajax({ |
| 81 |
url: jsonUrl, |
| 82 |
type: "GET", |
| 83 |
dataType: "json", |
| 84 |
success: data => { |
| 85 |
// Build components separately |
| 86 |
const mainNavbar = createMainNavbar(); |
| 87 |
const treeToolbar = createTreeToolbar(); |
| 88 |
const treeView = buildEdiTree(data, settings, focusOptions); |
| 89 |
const rawView = buildEdiRaw(data, settings, focusOptions); |
| 90 |
rawView.classList.add("hidden"); |
| 91 |
|
| 92 |
// Add class for identification |
| 93 |
mainNavbar.classList.add("edi-main-navbar"); |
| 94 |
|
| 95 |
// Insert navbar between header and body |
| 96 |
modalBody.before(mainNavbar); |
| 97 |
|
| 98 |
// Insert tree toolbar between navbar and body |
| 99 |
modalBody.before(treeToolbar); |
| 100 |
|
| 101 |
// Insert views directly into modal body |
| 102 |
modalBody.empty(); |
| 103 |
modalBody.addClass("edi-content"); |
| 104 |
modalBody.append(treeView, rawView); |
| 105 |
|
| 106 |
// Re-run initialization since DOM structure changed |
| 107 |
setTimeout(() => { |
| 108 |
initializeViewToggle(modalContent[0], data); |
| 109 |
initializeExpandCollapse(modalContent[0]); |
| 110 |
initializeSearch(modalContent[0]); |
| 111 |
|
| 112 |
if (focusOptions) { |
| 113 |
applyFocusOptions(modalContent[0], focusOptions); |
| 114 |
} |
| 115 |
}, 0); |
| 116 |
}, |
| 117 |
error: () => { |
| 118 |
modalBody.html( |
| 119 |
'<div class="alert alert-danger">Failed to load message</div>' |
| 120 |
); |
| 121 |
}, |
| 122 |
}); |
| 123 |
}; |
| 124 |
|
| 125 |
const createMainNavbar = () => { |
| 126 |
const nav = document.createElement("nav"); |
| 127 |
nav.className = "navbar navbar-light bg-light border-bottom py-2"; |
| 128 |
nav.innerHTML = ` |
| 129 |
<div class="container-fluid align-items-center"> |
| 130 |
<!-- Left: View toggle buttons --> |
| 131 |
<div class="btn-group me-2" role="group" aria-label="Display style"> |
| 132 |
<button type="button" class="btn btn-outline-secondary active" data-view="tree" title="Tree view"> |
| 133 |
<i class="fa fa-sitemap fa-fw"></i> |
| 134 |
</button> |
| 135 |
<button type="button" class="btn btn-outline-secondary" data-view="raw" title="Raw view"> |
| 136 |
<i class="fa fa-file-text fa-fw"></i> |
| 137 |
</button> |
| 138 |
</div> |
| 139 |
|
| 140 |
<!-- Center: Search form --> |
| 141 |
<form class="d-flex flex-grow-1 mx-2 edi-search-form" role="search"> |
| 142 |
<input class="form-control me-2 edi-search-input" type="search" placeholder="Search segments..." aria-label="Search segments"> |
| 143 |
<button class="btn btn-outline-secondary me-1 edi-search-prev" type="button" title="Previous result" disabled> |
| 144 |
<i class="fa fa-chevron-up"></i> |
| 145 |
</button> |
| 146 |
<button class="btn btn-outline-secondary edi-search-next" type="button" title="Next result" disabled> |
| 147 |
<i class="fa fa-chevron-down"></i> |
| 148 |
</button> |
| 149 |
</form> |
| 150 |
|
| 151 |
<!-- Right: Search results --> |
| 152 |
<div class="text-muted small text-nowrap"> |
| 153 |
<span class="edi-search-count">0 results</span> |
| 154 |
</div> |
| 155 |
</div>`; |
| 156 |
return nav; |
| 157 |
}; |
| 158 |
|
| 159 |
const createTreeToolbar = () => { |
| 160 |
const toolbar = document.createElement("div"); |
| 161 |
toolbar.className = |
| 162 |
"navbar navbar-light bg-light border-bottom py-2 edi-tree-toolbar"; |
| 163 |
toolbar.innerHTML = ` |
| 164 |
<div class="container-fluid d-flex flex-row-reverse"> |
| 165 |
<div class="btn-group btn-group-sm" role="group" aria-label="Tree controls"> |
| 166 |
<button type="button" class="btn btn-outline-secondary expand-all-btn" title="Expand all sections"> |
| 167 |
<i class="fa fa-expand me-1"></i>Expand all |
| 168 |
</button> |
| 169 |
<button type="button" class="btn btn-outline-secondary collapse-all-btn" title="Collapse all sections"> |
| 170 |
<i class="fa fa-compress me-1"></i>Collapse all |
| 171 |
</button> |
| 172 |
</div> |
| 173 |
</div>`; |
| 174 |
return toolbar; |
| 175 |
}; |
| 176 |
|
| 177 |
const buildEdiRaw = (data, settings, focusOptions) => { |
| 178 |
const rawDiv = document.createElement("div"); |
| 179 |
rawDiv.className = "edi-raw-view"; |
| 180 |
|
| 181 |
const lines = []; |
| 182 |
|
| 183 |
if (data.header) { |
| 184 |
lines.push( |
| 185 |
`<div class="segment-line"><span class="segment-tag">UNB</span>${data.header.substring(3)}</div>` |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
data.messages.forEach((message, messageIndex) => { |
| 190 |
const isMessageFocused = |
| 191 |
focusOptions && |
| 192 |
messageMatchesFocusOptions(message, focusOptions); |
| 193 |
const focusClass = isMessageFocused |
| 194 |
? " edi-focused-segment-line" |
| 195 |
: ""; |
| 196 |
const messageAttr = ` data-message-index="${messageIndex}"`; |
| 197 |
|
| 198 |
if (message.header) { |
| 199 |
lines.push( |
| 200 |
`<div class="segment-line${focusClass}"${messageAttr}><span class="segment-tag">UNH</span>${message.header.substring(3)}</div>` |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
message.segments.forEach(segment => { |
| 205 |
const tag = segment.tag || ""; |
| 206 |
const content = (segment.raw || "").substring(tag.length); |
| 207 |
lines.push( |
| 208 |
`<div class="segment-line${focusClass}"${messageAttr}><span class="segment-tag">${escapeHtml(tag)}</span>${escapeHtml(content)}</div>` |
| 209 |
); |
| 210 |
}); |
| 211 |
|
| 212 |
if (message.trailer) { |
| 213 |
lines.push( |
| 214 |
`<div class="segment-line${focusClass}"${messageAttr}><span class="segment-tag">UNT</span>${message.trailer.substring(3)}</div>` |
| 215 |
); |
| 216 |
} |
| 217 |
}); |
| 218 |
|
| 219 |
if (data.trailer) { |
| 220 |
lines.push( |
| 221 |
`<div class="segment-line"><span class="segment-tag">UNZ</span>${data.trailer.substring(3)}</div>` |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
rawDiv.innerHTML = lines.join(""); |
| 226 |
return rawDiv; |
| 227 |
}; |
| 228 |
|
| 229 |
const initializeViewToggle = (container, data) => { |
| 230 |
const toggleButtons = container.querySelectorAll("button[data-view]"); |
| 231 |
const treeView = container.querySelector(".edi-tree"); |
| 232 |
const rawView = container.querySelector(".edi-raw-view"); |
| 233 |
const treeToolbar = container.querySelector(".edi-tree-toolbar"); |
| 234 |
|
| 235 |
container.edifactData = data; |
| 236 |
|
| 237 |
toggleButtons.forEach(button => { |
| 238 |
button.addEventListener("click", () => { |
| 239 |
const viewType = button.dataset.view; |
| 240 |
|
| 241 |
// Update button states |
| 242 |
toggleButtons.forEach(btn => btn.classList.remove("active")); |
| 243 |
button.classList.add("active"); |
| 244 |
|
| 245 |
// Toggle views and tree toolbar |
| 246 |
if (viewType === "tree") { |
| 247 |
treeView.classList.remove("hidden"); |
| 248 |
rawView.classList.add("hidden"); |
| 249 |
treeToolbar.classList.remove("d-none"); |
| 250 |
} else { |
| 251 |
treeView.classList.add("hidden"); |
| 252 |
rawView.classList.remove("hidden"); |
| 253 |
treeToolbar.classList.add("d-none"); |
| 254 |
} |
| 255 |
|
| 256 |
// Re-execute search if active |
| 257 |
if (container.searchFunctions) { |
| 258 |
const searchInput = |
| 259 |
container.querySelector(".edi-search-input"); |
| 260 |
if (searchInput?.value.trim()) { |
| 261 |
container.searchFunctions.performSearch( |
| 262 |
searchInput.value.trim() |
| 263 |
); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
// Re-apply focus highlighting |
| 268 |
if (container.focusOptions) { |
| 269 |
setTimeout(() => { |
| 270 |
const focusTarget = |
| 271 |
viewType === "tree" |
| 272 |
? container.querySelector( |
| 273 |
'[data-focus-message="true"].edi-focused-message' |
| 274 |
) |
| 275 |
: rawView.querySelector( |
| 276 |
".edi-focused-segment-line" |
| 277 |
); |
| 278 |
|
| 279 |
focusTarget?.scrollIntoView({ |
| 280 |
behavior: "smooth", |
| 281 |
block: "start", |
| 282 |
}); |
| 283 |
}, 100); |
| 284 |
} |
| 285 |
}); |
| 286 |
}); |
| 287 |
}; |
| 288 |
|
| 289 |
const initializeExpandCollapse = container => { |
| 290 |
const treeView = container.querySelector(".edi-tree"); |
| 291 |
|
| 292 |
container |
| 293 |
.querySelector(".expand-all-btn") |
| 294 |
?.addEventListener("click", () => { |
| 295 |
const collapseElements = treeView.querySelectorAll(".collapse"); |
| 296 |
collapseElements.forEach(el => { |
| 297 |
el.classList.add("show"); |
| 298 |
if (typeof bootstrap !== "undefined") { |
| 299 |
bootstrap.Collapse.getOrCreateInstance(el)?.show(); |
| 300 |
} |
| 301 |
}); |
| 302 |
}); |
| 303 |
|
| 304 |
container |
| 305 |
.querySelector(".collapse-all-btn") |
| 306 |
?.addEventListener("click", () => { |
| 307 |
const collapseElements = treeView.querySelectorAll(".collapse"); |
| 308 |
collapseElements.forEach(el => { |
| 309 |
if (typeof bootstrap !== "undefined") { |
| 310 |
bootstrap.Collapse.getOrCreateInstance(el).hide(); |
| 311 |
} else { |
| 312 |
el.classList.remove("show"); |
| 313 |
} |
| 314 |
}); |
| 315 |
}); |
| 316 |
}; |
| 317 |
|
| 318 |
const initializeSearch = container => { |
| 319 |
const searchInput = container.querySelector(".edi-search-input"); |
| 320 |
const searchCount = container.querySelector(".edi-search-count"); |
| 321 |
const searchPrev = container.querySelector(".edi-search-prev"); |
| 322 |
const searchNext = container.querySelector(".edi-search-next"); |
| 323 |
const treeView = container.querySelector(".edi-tree"); |
| 324 |
const rawView = container.querySelector(".edi-raw-view"); |
| 325 |
|
| 326 |
let currentResults = []; |
| 327 |
let currentIndex = -1; |
| 328 |
let searchTimeout = null; |
| 329 |
|
| 330 |
const performSearch = query => { |
| 331 |
clearSearch(false); |
| 332 |
|
| 333 |
if (!query || query.length < 2) { |
| 334 |
updateSearchUI(0, -1); |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
const activeView = container.querySelector(".edi-tree:not(.hidden)") |
| 339 |
? "tree" |
| 340 |
: "raw"; |
| 341 |
const searchTargets = |
| 342 |
activeView === "tree" |
| 343 |
? treeView.querySelectorAll(".segment") |
| 344 |
: rawView.querySelectorAll(".segment-line"); |
| 345 |
|
| 346 |
const regex = new RegExp(escapeRegExp(query), "i"); |
| 347 |
|
| 348 |
searchTargets.forEach(element => { |
| 349 |
const textContent = element.textContent || element.innerText; |
| 350 |
if (regex.test(textContent)) { |
| 351 |
highlightMatches(element, query); |
| 352 |
currentResults.push({ element, view: activeView }); |
| 353 |
} |
| 354 |
}); |
| 355 |
|
| 356 |
if (activeView === "tree") { |
| 357 |
smartExpandCollapseForSearch(currentResults); |
| 358 |
} |
| 359 |
|
| 360 |
updateSearchUI(currentResults.length, currentIndex); |
| 361 |
|
| 362 |
if (currentResults.length > 0) { |
| 363 |
navigateToResult(0); |
| 364 |
} |
| 365 |
}; |
| 366 |
|
| 367 |
const highlightMatches = (element, query) => { |
| 368 |
const regex = new RegExp(`(${escapeRegExp(query)})`, "gi"); |
| 369 |
const walker = document.createTreeWalker( |
| 370 |
element, |
| 371 |
NodeFilter.SHOW_TEXT |
| 372 |
); |
| 373 |
|
| 374 |
const textNodes = []; |
| 375 |
let node; |
| 376 |
while ((node = walker.nextNode())) { |
| 377 |
textNodes.push(node); |
| 378 |
} |
| 379 |
|
| 380 |
textNodes.forEach(textNode => { |
| 381 |
if (regex.test(textNode.textContent)) { |
| 382 |
const highlightedHTML = textNode.textContent.replace( |
| 383 |
regex, |
| 384 |
'<mark class="edi-search-highlight">$1</mark>' |
| 385 |
); |
| 386 |
const wrapper = document.createElement("span"); |
| 387 |
wrapper.innerHTML = highlightedHTML; |
| 388 |
textNode.parentNode.replaceChild(wrapper, textNode); |
| 389 |
} |
| 390 |
}); |
| 391 |
}; |
| 392 |
|
| 393 |
const smartExpandCollapseForSearch = results => { |
| 394 |
setTimeout(() => { |
| 395 |
const allCollapseElements = |
| 396 |
treeView.querySelectorAll(".collapse"); |
| 397 |
const elementsWithResults = new Set(); |
| 398 |
|
| 399 |
results.forEach(result => { |
| 400 |
let current = result.element; |
| 401 |
while (current && current !== treeView) { |
| 402 |
if (current.classList?.contains("collapse")) { |
| 403 |
elementsWithResults.add(current); |
| 404 |
} |
| 405 |
current = current.parentElement; |
| 406 |
} |
| 407 |
}); |
| 408 |
|
| 409 |
allCollapseElements.forEach(collapseElement => { |
| 410 |
const collapse = |
| 411 |
typeof bootstrap !== "undefined" |
| 412 |
? bootstrap.Collapse.getOrCreateInstance( |
| 413 |
collapseElement, |
| 414 |
{ toggle: false } |
| 415 |
) |
| 416 |
: null; |
| 417 |
|
| 418 |
if (elementsWithResults.has(collapseElement)) { |
| 419 |
collapse |
| 420 |
? collapse.show() |
| 421 |
: collapseElement.classList.add("show"); |
| 422 |
} else { |
| 423 |
collapse |
| 424 |
? collapse.hide() |
| 425 |
: collapseElement.classList.remove("show"); |
| 426 |
} |
| 427 |
}); |
| 428 |
}, 50); |
| 429 |
}; |
| 430 |
|
| 431 |
const navigateResults = direction => { |
| 432 |
if (currentResults.length === 0) return; |
| 433 |
|
| 434 |
let newIndex = currentIndex + direction; |
| 435 |
if (newIndex >= currentResults.length) newIndex = 0; |
| 436 |
if (newIndex < 0) newIndex = currentResults.length - 1; |
| 437 |
|
| 438 |
navigateToResult(newIndex); |
| 439 |
}; |
| 440 |
|
| 441 |
const navigateToResult = index => { |
| 442 |
if (index < 0 || index >= currentResults.length) return; |
| 443 |
|
| 444 |
currentResults[currentIndex]?.element.classList.remove( |
| 445 |
"edi-search-current" |
| 446 |
); |
| 447 |
|
| 448 |
currentIndex = index; |
| 449 |
const result = currentResults[currentIndex]; |
| 450 |
|
| 451 |
result.element.classList.add("edi-search-current"); |
| 452 |
result.element.scrollIntoView({ |
| 453 |
behavior: "smooth", |
| 454 |
block: "center", |
| 455 |
}); |
| 456 |
|
| 457 |
updateSearchUI(currentResults.length, currentIndex); |
| 458 |
}; |
| 459 |
|
| 460 |
const clearSearch = (clearInput = true) => { |
| 461 |
if (clearInput && searchInput) { |
| 462 |
searchInput.value = ""; |
| 463 |
} |
| 464 |
|
| 465 |
container |
| 466 |
.querySelectorAll(".edi-search-highlight, .edi-search-current") |
| 467 |
.forEach(el => { |
| 468 |
if (el.classList.contains("edi-search-highlight")) { |
| 469 |
el.parentNode.replaceChild( |
| 470 |
document.createTextNode(el.textContent), |
| 471 |
el |
| 472 |
); |
| 473 |
} else { |
| 474 |
el.classList.remove("edi-search-current"); |
| 475 |
} |
| 476 |
}); |
| 477 |
|
| 478 |
container.querySelectorAll("span:not([class])").forEach(wrapper => { |
| 479 |
if ( |
| 480 |
wrapper.children.length === 0 || |
| 481 |
(wrapper.children.length === 1 && |
| 482 |
wrapper.children[0].tagName === "MARK") |
| 483 |
) { |
| 484 |
wrapper.parentNode.replaceChild( |
| 485 |
document.createTextNode(wrapper.textContent), |
| 486 |
wrapper |
| 487 |
); |
| 488 |
} |
| 489 |
}); |
| 490 |
|
| 491 |
currentResults = []; |
| 492 |
currentIndex = -1; |
| 493 |
updateSearchUI(0, -1); |
| 494 |
}; |
| 495 |
|
| 496 |
const updateSearchUI = (totalResults, currentIdx) => { |
| 497 |
if (searchCount) { |
| 498 |
searchCount.textContent = |
| 499 |
totalResults === 0 |
| 500 |
? "0 results" |
| 501 |
: `${currentIdx + 1} of ${totalResults}`; |
| 502 |
} |
| 503 |
|
| 504 |
const hasResults = totalResults > 0; |
| 505 |
if (searchPrev) searchPrev.disabled = !hasResults; |
| 506 |
if (searchNext) searchNext.disabled = !hasResults; |
| 507 |
}; |
| 508 |
|
| 509 |
const escapeRegExp = string => |
| 510 |
string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 511 |
|
| 512 |
// Event listeners |
| 513 |
searchInput?.addEventListener("input", () => { |
| 514 |
clearTimeout(searchTimeout); |
| 515 |
const value = searchInput.value.trim(); |
| 516 |
|
| 517 |
// If field was cleared (native clear button or manual), clear search |
| 518 |
if (value === "") { |
| 519 |
clearSearch(false); // Don't clear input since it's already empty |
| 520 |
} else { |
| 521 |
searchTimeout = setTimeout(() => performSearch(value), 500); |
| 522 |
} |
| 523 |
}); |
| 524 |
|
| 525 |
searchInput?.addEventListener("keydown", e => { |
| 526 |
if (e.key === "Enter") { |
| 527 |
e.preventDefault(); |
| 528 |
navigateResults(e.shiftKey ? -1 : 1); |
| 529 |
} |
| 530 |
}); |
| 531 |
|
| 532 |
// Prevent form submission |
| 533 |
const searchForm = container.querySelector(".edi-search-form"); |
| 534 |
searchForm?.addEventListener("submit", e => e.preventDefault()); |
| 535 |
|
| 536 |
searchPrev?.addEventListener("click", () => navigateResults(-1)); |
| 537 |
searchNext?.addEventListener("click", () => navigateResults(1)); |
| 538 |
|
| 539 |
container.searchFunctions = { |
| 540 |
performSearch, |
| 541 |
clearSearch, |
| 542 |
navigateResults, |
| 543 |
}; |
| 544 |
}; |
| 545 |
|
| 546 |
const messageMatchesFocusOptions = (message, focusOptions) => { |
| 547 |
if (!message.segments || !focusOptions) return false; |
| 548 |
|
| 549 |
// Check if basketno is a whole number and pad it |
| 550 |
if (/^\d+$/.test(String(focusOptions.basketno))) { |
| 551 |
const strVal = String(focusOptions.basketno); |
| 552 |
if (11 > strVal.length) { |
| 553 |
focusOptions.basketno = strVal.padStart(11, "0"); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
return message.segments.some(segment => { |
| 558 |
// Check basketno/invoicenumber in BGM segments |
| 559 |
if (segment.tag === "BGM" && segment.elements?.length > 1) { |
| 560 |
const bgmValue = String(segment.elements[1]); |
| 561 |
|
| 562 |
if ( |
| 563 |
(focusOptions.basketno && |
| 564 |
bgmValue === String(focusOptions.basketno)) || |
| 565 |
(focusOptions.invoicenumber && |
| 566 |
bgmValue === String(focusOptions.invoicenumber)) |
| 567 |
) { |
| 568 |
return true; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
// Check basketname/invoicenumber in RFF segments |
| 573 |
if (segment.tag === "RFF" && segment.elements?.length >= 2) { |
| 574 |
const refType = segment.elements[0]; |
| 575 |
const refValue = String(segment.elements[1]); |
| 576 |
|
| 577 |
if ( |
| 578 |
focusOptions.basketname && |
| 579 |
refType === "ON" && |
| 580 |
refValue === String(focusOptions.basketname) |
| 581 |
) { |
| 582 |
return true; |
| 583 |
} |
| 584 |
|
| 585 |
if ( |
| 586 |
focusOptions.invoicenumber && |
| 587 |
(refType === "IV" || refType === "VN") && |
| 588 |
refValue === String(focusOptions.invoicenumber) |
| 589 |
) { |
| 590 |
return true; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
return false; |
| 595 |
}); |
| 596 |
}; |
| 597 |
|
| 598 |
const applyFocusOptions = (container, focusOptions) => { |
| 599 |
container.focusOptions = focusOptions; |
| 600 |
|
| 601 |
setTimeout(() => { |
| 602 |
const focusedMessage = container.querySelector( |
| 603 |
'[data-focus-message="true"]' |
| 604 |
); |
| 605 |
if (focusedMessage) { |
| 606 |
focusedMessage.classList.add("edi-focused-message"); |
| 607 |
|
| 608 |
const treeView = container.querySelector(".edi-tree"); |
| 609 |
if (treeView && !treeView.classList.contains("hidden")) { |
| 610 |
focusedMessage.scrollIntoView({ |
| 611 |
behavior: "smooth", |
| 612 |
block: "start", |
| 613 |
}); |
| 614 |
} |
| 615 |
|
| 616 |
container.setAttribute("data-has-focused-message", "true"); |
| 617 |
} |
| 618 |
|
| 619 |
const rawView = container.querySelector(".edi-raw-view"); |
| 620 |
if (rawView && !rawView.classList.contains("hidden")) { |
| 621 |
const firstFocusedLine = rawView.querySelector( |
| 622 |
".edi-focused-segment-line" |
| 623 |
); |
| 624 |
firstFocusedLine?.scrollIntoView({ |
| 625 |
behavior: "smooth", |
| 626 |
block: "start", |
| 627 |
}); |
| 628 |
} |
| 629 |
}, 200); |
| 630 |
}; |
| 631 |
|
| 632 |
const buildEdiTree = (data, settings, focusOptions) => { |
| 633 |
const rootUl = document.createElement("ul"); |
| 634 |
rootUl.className = "edi-tree list-unstyled"; |
| 635 |
|
| 636 |
const hasMatchingMessage = |
| 637 |
focusOptions && |
| 638 |
data.messages && |
| 639 |
data.messages.some(msg => |
| 640 |
messageMatchesFocusOptions(msg, focusOptions) |
| 641 |
); |
| 642 |
|
| 643 |
const effectiveFocusOptions = |
| 644 |
focusOptions && !hasMatchingMessage ? null : focusOptions; |
| 645 |
const effectiveSettings = |
| 646 |
focusOptions && !hasMatchingMessage |
| 647 |
? { ...settings, expandByDefault: true } |
| 648 |
: settings; |
| 649 |
|
| 650 |
const interchangeLi = buildInterchangeLevel( |
| 651 |
data, |
| 652 |
effectiveSettings, |
| 653 |
effectiveFocusOptions |
| 654 |
); |
| 655 |
rootUl.appendChild(interchangeLi); |
| 656 |
|
| 657 |
return rootUl; |
| 658 |
}; |
| 659 |
|
| 660 |
const buildInterchangeLevel = (data, settings, focusOptions) => { |
| 661 |
const interchangeLi = document.createElement("li"); |
| 662 |
const interchangeId = `interchange_${Date.now()}`; |
| 663 |
const shouldExpand = focusOptions ? true : settings.expandByDefault; |
| 664 |
|
| 665 |
const headerDiv = createSegmentDiv( |
| 666 |
"header", |
| 667 |
data.header, |
| 668 |
shouldExpand, |
| 669 |
true, |
| 670 |
interchangeId |
| 671 |
); |
| 672 |
interchangeLi.appendChild(headerDiv); |
| 673 |
|
| 674 |
const messagesUl = document.createElement("ul"); |
| 675 |
messagesUl.id = interchangeId; |
| 676 |
messagesUl.className = `collapse${shouldExpand ? " show" : ""}`; |
| 677 |
|
| 678 |
data.messages.forEach((message, i) => { |
| 679 |
const messageLi = buildMessageLevel( |
| 680 |
message, |
| 681 |
i, |
| 682 |
settings, |
| 683 |
focusOptions |
| 684 |
); |
| 685 |
messagesUl.appendChild(messageLi); |
| 686 |
}); |
| 687 |
|
| 688 |
const trailerDiv = createSegmentDiv( |
| 689 |
"trailer", |
| 690 |
data.trailer, |
| 691 |
false, |
| 692 |
true |
| 693 |
); |
| 694 |
|
| 695 |
interchangeLi.append(messagesUl, trailerDiv); |
| 696 |
return interchangeLi; |
| 697 |
}; |
| 698 |
|
| 699 |
const buildMessageLevel = (message, index, settings, focusOptions) => { |
| 700 |
const messageLi = document.createElement("li"); |
| 701 |
const messageId = `message_${index}_${Date.now()}`; |
| 702 |
|
| 703 |
const shouldFocusMessage = |
| 704 |
focusOptions && messageMatchesFocusOptions(message, focusOptions); |
| 705 |
const shouldExpand = focusOptions |
| 706 |
? shouldFocusMessage |
| 707 |
: settings.expandByDefault; |
| 708 |
|
| 709 |
const headerDiv = createSegmentDiv( |
| 710 |
"header", |
| 711 |
message.header || "UNH", |
| 712 |
shouldExpand, |
| 713 |
true, |
| 714 |
messageId |
| 715 |
); |
| 716 |
messageLi.appendChild(headerDiv); |
| 717 |
|
| 718 |
if (shouldFocusMessage) { |
| 719 |
messageLi.setAttribute("data-focus-message", "true"); |
| 720 |
} |
| 721 |
|
| 722 |
const segmentsUl = document.createElement("ul"); |
| 723 |
segmentsUl.id = messageId; |
| 724 |
segmentsUl.className = `collapse${shouldExpand ? " show" : ""}`; |
| 725 |
|
| 726 |
const groupedSegments = groupSegmentsByLineId(message.segments); |
| 727 |
|
| 728 |
groupedSegments.forEach((group, i) => { |
| 729 |
if (group.isLineGroup) { |
| 730 |
const lineGroupLi = buildLineGroup( |
| 731 |
group, |
| 732 |
settings, |
| 733 |
`${messageId}_line_${i}` |
| 734 |
); |
| 735 |
segmentsUl.appendChild(lineGroupLi); |
| 736 |
} else { |
| 737 |
const relatedGrouping = groupRelatedSegments(group.segments); |
| 738 |
|
| 739 |
relatedGrouping.forEach(relatedItem => { |
| 740 |
if (relatedItem.type === "group") { |
| 741 |
const groupLi = document.createElement("li"); |
| 742 |
const groupDiv = document.createElement("div"); |
| 743 |
groupDiv.className = "segment-group related-segments"; |
| 744 |
groupDiv.setAttribute( |
| 745 |
"data-relationship", |
| 746 |
relatedItem.relationship |
| 747 |
); |
| 748 |
|
| 749 |
relatedItem.segments.forEach(segment => { |
| 750 |
const segmentDiv = createSegmentDiv( |
| 751 |
"content", |
| 752 |
segment.raw, |
| 753 |
false, |
| 754 |
true |
| 755 |
); |
| 756 |
if (segment.line_id) |
| 757 |
segmentDiv.dataset.lineId = segment.line_id; |
| 758 |
groupDiv.appendChild(segmentDiv); |
| 759 |
}); |
| 760 |
|
| 761 |
groupLi.appendChild(groupDiv); |
| 762 |
segmentsUl.appendChild(groupLi); |
| 763 |
} else { |
| 764 |
const segmentLi = buildSegmentElement( |
| 765 |
relatedItem.segment, |
| 766 |
settings |
| 767 |
); |
| 768 |
segmentsUl.appendChild(segmentLi); |
| 769 |
} |
| 770 |
}); |
| 771 |
} |
| 772 |
}); |
| 773 |
|
| 774 |
const trailerDiv = createSegmentDiv( |
| 775 |
"trailer", |
| 776 |
message.trailer || "UNT", |
| 777 |
false, |
| 778 |
true |
| 779 |
); |
| 780 |
|
| 781 |
messageLi.append(segmentsUl, trailerDiv); |
| 782 |
return messageLi; |
| 783 |
}; |
| 784 |
|
| 785 |
const buildLineGroup = (group, settings, lineId) => { |
| 786 |
const lineGroupLi = document.createElement("li"); |
| 787 |
const linSegment = group.segments[0]; |
| 788 |
|
| 789 |
const headerDiv = createSegmentDiv( |
| 790 |
"header", |
| 791 |
linSegment.raw, |
| 792 |
settings.expandByDefault, |
| 793 |
true, |
| 794 |
lineId |
| 795 |
); |
| 796 |
lineGroupLi.appendChild(headerDiv); |
| 797 |
|
| 798 |
const lineSegmentsUl = document.createElement("ul"); |
| 799 |
lineSegmentsUl.id = lineId; |
| 800 |
lineSegmentsUl.className = `collapse${settings.expandByDefault ? " show" : ""}`; |
| 801 |
|
| 802 |
const lineSegments = group.segments.slice(1); |
| 803 |
const groupedSegments = groupRelatedSegments(lineSegments); |
| 804 |
|
| 805 |
groupedSegments.forEach(groupItem => { |
| 806 |
if (groupItem.type === "group") { |
| 807 |
const groupLi = document.createElement("li"); |
| 808 |
const groupDiv = document.createElement("div"); |
| 809 |
groupDiv.className = "segment-group related-segments"; |
| 810 |
groupDiv.setAttribute( |
| 811 |
"data-relationship", |
| 812 |
groupItem.relationship |
| 813 |
); |
| 814 |
|
| 815 |
groupItem.segments.forEach(segment => { |
| 816 |
const segmentDiv = createSegmentDiv( |
| 817 |
"content", |
| 818 |
segment.raw, |
| 819 |
false, |
| 820 |
true |
| 821 |
); |
| 822 |
if (segment.line_id) |
| 823 |
segmentDiv.dataset.lineId = segment.line_id; |
| 824 |
groupDiv.appendChild(segmentDiv); |
| 825 |
}); |
| 826 |
|
| 827 |
groupLi.appendChild(groupDiv); |
| 828 |
lineSegmentsUl.appendChild(groupLi); |
| 829 |
} else { |
| 830 |
const segmentLi = buildSegmentElement( |
| 831 |
groupItem.segment, |
| 832 |
settings |
| 833 |
); |
| 834 |
lineSegmentsUl.appendChild(segmentLi); |
| 835 |
} |
| 836 |
}); |
| 837 |
|
| 838 |
lineGroupLi.appendChild(lineSegmentsUl); |
| 839 |
return lineGroupLi; |
| 840 |
}; |
| 841 |
|
| 842 |
const buildSegmentElement = (segment, settings) => { |
| 843 |
const segmentLi = document.createElement("li"); |
| 844 |
const segmentDiv = createSegmentDiv( |
| 845 |
"content", |
| 846 |
segment.raw, |
| 847 |
false, |
| 848 |
true |
| 849 |
); |
| 850 |
|
| 851 |
if (segment.line_id) { |
| 852 |
segmentDiv.dataset.lineId = segment.line_id; |
| 853 |
} |
| 854 |
|
| 855 |
if (settings.showElementDetails && segment.elements) { |
| 856 |
const detailsDiv = document.createElement("div"); |
| 857 |
detailsDiv.className = "segment-details"; |
| 858 |
detailsDiv.innerHTML = `<small>Elements: ${JSON.stringify(segment.elements)}</small>`; |
| 859 |
segmentDiv.appendChild(detailsDiv); |
| 860 |
} |
| 861 |
|
| 862 |
segmentLi.appendChild(segmentDiv); |
| 863 |
return segmentLi; |
| 864 |
}; |
| 865 |
|
| 866 |
const groupRelatedSegments = segments => { |
| 867 |
const grouped = []; |
| 868 |
let i = 0; |
| 869 |
|
| 870 |
const relationships = { |
| 871 |
ALC: ["MOA", "TAX"], |
| 872 |
PRI: ["MOA"], |
| 873 |
QTY: ["MOA"], |
| 874 |
DTM: ["MOA"], |
| 875 |
TAX: ["MOA"], |
| 876 |
RFF: ["DTM"], |
| 877 |
}; |
| 878 |
|
| 879 |
while (i < segments.length) { |
| 880 |
const segment = segments[i]; |
| 881 |
const relatedTags = relationships[segment.tag]; |
| 882 |
|
| 883 |
if (relatedTags) { |
| 884 |
const relatedSegments = [segment]; |
| 885 |
let j = i + 1; |
| 886 |
|
| 887 |
while ( |
| 888 |
j < segments.length && |
| 889 |
relatedTags.includes(segments[j].tag) |
| 890 |
) { |
| 891 |
relatedSegments.push(segments[j]); |
| 892 |
j++; |
| 893 |
} |
| 894 |
|
| 895 |
if (relatedSegments.length > 1) { |
| 896 |
grouped.push({ |
| 897 |
type: "group", |
| 898 |
relationship: `${segment.tag}_group`, |
| 899 |
segments: relatedSegments, |
| 900 |
}); |
| 901 |
i = j; |
| 902 |
} else { |
| 903 |
grouped.push({ type: "single", segment }); |
| 904 |
i++; |
| 905 |
} |
| 906 |
} else { |
| 907 |
grouped.push({ type: "single", segment }); |
| 908 |
i++; |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
return grouped; |
| 913 |
}; |
| 914 |
|
| 915 |
const createSegmentDiv = ( |
| 916 |
type, |
| 917 |
content, |
| 918 |
expandByDefault, |
| 919 |
boldTag, |
| 920 |
targetId |
| 921 |
) => { |
| 922 |
const div = document.createElement("div"); |
| 923 |
div.className = `segment ${type}`; |
| 924 |
|
| 925 |
if (type === "header" && targetId) { |
| 926 |
div.setAttribute("data-bs-toggle", "collapse"); |
| 927 |
div.setAttribute("data-bs-target", `#${targetId}`); |
| 928 |
div.setAttribute( |
| 929 |
"aria-expanded", |
| 930 |
expandByDefault ? "true" : "false" |
| 931 |
); |
| 932 |
div.setAttribute("aria-controls", targetId); |
| 933 |
div.style.cursor = "pointer"; |
| 934 |
|
| 935 |
if (boldTag && content && content.length >= 3) { |
| 936 |
const tag = content.substring(0, 3); |
| 937 |
const rest = content.substring(3); |
| 938 |
div.innerHTML = `<i class="fa fa-chevron-down me-1"></i> <span class="segment-tag">${escapeHtml(tag)}</span>${escapeHtml(rest)}`; |
| 939 |
} else { |
| 940 |
div.innerHTML = `<i class="fa fa-chevron-down me-1"></i> ${escapeHtml(content)}`; |
| 941 |
} |
| 942 |
} else if (boldTag && content && content.length >= 3) { |
| 943 |
const tag = content.substring(0, 3); |
| 944 |
const rest = content.substring(3); |
| 945 |
div.innerHTML = `<span class="segment-tag">${escapeHtml(tag)}</span>${escapeHtml(rest)}`; |
| 946 |
} else { |
| 947 |
div.textContent = content; |
| 948 |
} |
| 949 |
|
| 950 |
return div; |
| 951 |
}; |
| 952 |
|
| 953 |
const groupSegmentsByLineId = segments => { |
| 954 |
const groups = []; |
| 955 |
let currentGroup = null; |
| 956 |
|
| 957 |
segments.forEach(segment => { |
| 958 |
if (segment.tag === "LIN") { |
| 959 |
if (currentGroup) groups.push(currentGroup); |
| 960 |
currentGroup = { |
| 961 |
isLineGroup: true, |
| 962 |
lineId: segment.line_id, |
| 963 |
segments: [segment], |
| 964 |
}; |
| 965 |
} else if ( |
| 966 |
currentGroup && |
| 967 |
segment.line_id === currentGroup.lineId |
| 968 |
) { |
| 969 |
currentGroup.segments.push(segment); |
| 970 |
} else { |
| 971 |
if (currentGroup) { |
| 972 |
groups.push(currentGroup); |
| 973 |
currentGroup = null; |
| 974 |
} |
| 975 |
groups.push({ isLineGroup: false, segments: [segment] }); |
| 976 |
} |
| 977 |
}); |
| 978 |
|
| 979 |
if (currentGroup) groups.push(currentGroup); |
| 980 |
return groups; |
| 981 |
}; |
| 982 |
|
| 983 |
const initializeModal = settings => { |
| 984 |
const modal = $(settings.modalId); |
| 985 |
|
| 986 |
modal.on("hidden.bs.modal", function () { |
| 987 |
const modalContent = $(this).find(".modal-content"); |
| 988 |
const modalBody = $(this).find(settings.modalBodySelector); |
| 989 |
|
| 990 |
// Remove focus highlighting and navbar |
| 991 |
modalContent |
| 992 |
.find(".edi-focused-message") |
| 993 |
.removeClass("edi-focused-message"); |
| 994 |
modalContent.find(".edi-main-navbar").remove(); |
| 995 |
|
| 996 |
// Remove scrolling class and reset modal body content |
| 997 |
modalBody.removeClass("edi-content"); |
| 998 |
modalBody.html(` |
| 999 |
<div class="edi-loading"> |
| 1000 |
<img src="/intranet-tmpl/${window.theme || "prog"}/img/spinner-small.gif" alt="" /> |
| 1001 |
Loading |
| 1002 |
</div> |
| 1003 |
`); |
| 1004 |
}); |
| 1005 |
}; |
| 1006 |
|
| 1007 |
const escapeHtml = str => { |
| 1008 |
if (!str) return ""; |
| 1009 |
const escapeMap = { |
| 1010 |
"&": "&", |
| 1011 |
"<": "<", |
| 1012 |
">": ">", |
| 1013 |
'"': """, |
| 1014 |
"'": "'", |
| 1015 |
}; |
| 1016 |
return str.replace(/[&<>"']/g, match => escapeMap[match]); |
| 1017 |
}; |
| 1018 |
|
| 1019 |
return { |
| 1020 |
init, |
| 1021 |
showMessage, |
| 1022 |
showMessageFromUrl, |
| 1023 |
buildEdiTree, |
| 1024 |
}; |
| 1025 |
})(); |
| 1026 |
|
| 1027 |
if (typeof $ !== "undefined") { |
| 1028 |
$(document).ready(() => { |
| 1029 |
// Individual pages handle initialization |
| 1030 |
}); |
| 1031 |
} |