@kubik-cms/interface_elements 0.1.18 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15392,6 +15392,256 @@ chart_controller_default.values = {
15392
15392
  config: Object
15393
15393
  };
15394
15394
  chart_controller_default.targets = ["canvas", "legend"];
15395
+ class OrderedRecordListController extends Controller {
15396
+ constructor() {
15397
+ super(...arguments);
15398
+ this.fetchAbort = null;
15399
+ this.debounceTimer = null;
15400
+ this.draggingRow = null;
15401
+ }
15402
+ connect() {
15403
+ this.selectedIds = new Set(this.itemsValue.map((item) => String(item.id)));
15404
+ this.renderRows();
15405
+ this.onDocMouseUp = this.handleDocMouseUp.bind(this);
15406
+ document.addEventListener("mouseup", this.onDocMouseUp);
15407
+ }
15408
+ disconnect() {
15409
+ document.removeEventListener("mouseup", this.onDocMouseUp);
15410
+ this.clearDebounce();
15411
+ this.abortFetch();
15412
+ }
15413
+ onSearchInput() {
15414
+ this.clearDebounce();
15415
+ this.debounceTimer = window.setTimeout(() => this.refreshResults(), 200);
15416
+ }
15417
+ onSearchFocus() {
15418
+ if (this.searchInputTarget.value.trim().length >= 1) {
15419
+ this.refreshResults();
15420
+ }
15421
+ }
15422
+ onSearchBlur() {
15423
+ window.setTimeout(() => {
15424
+ if (!this.element.contains(document.activeElement)) {
15425
+ this.closeResults();
15426
+ }
15427
+ }, 150);
15428
+ }
15429
+ onSearchKeydown(event) {
15430
+ if (event.key === "Escape") {
15431
+ this.closeResults();
15432
+ }
15433
+ }
15434
+ refreshResults() {
15435
+ const query = this.searchInputTarget.value.trim();
15436
+ if (query.length < 1) {
15437
+ this.closeResults();
15438
+ return;
15439
+ }
15440
+ this.abortFetch();
15441
+ this.fetchAbort = new AbortController();
15442
+ const url = new URL(this.searchUrlValue, window.location.origin);
15443
+ url.searchParams.set("q", query);
15444
+ fetch(url.toString(), { headers: { Accept: "application/json" }, signal: this.fetchAbort.signal }).then((response) => response.json()).then((payload) => {
15445
+ const rows = (Array.isArray(payload) ? payload : []).map((entry) => this.normalizeSearchEntry(entry)).filter((entry) => entry != null && !this.selectedIds.has(String(entry.id)));
15446
+ this.renderResults(rows);
15447
+ }).catch(() => this.renderResults([]));
15448
+ }
15449
+ normalizeSearchEntry(entry) {
15450
+ var _a, _b, _c;
15451
+ if (!entry || typeof entry !== "object")
15452
+ return null;
15453
+ const record = entry;
15454
+ const returnObject = record.return_object;
15455
+ const id = (_a = record.id) != null ? _a : returnObject == null ? void 0 : returnObject.id;
15456
+ if (id == null)
15457
+ return null;
15458
+ const pick = returnObject || record;
15459
+ const statusInfo = pick.status_info;
15460
+ const published = (_c = (_b = statusInfo == null ? void 0 : statusInfo.published) != null ? _b : pick.published) != null ? _c : true;
15461
+ return {
15462
+ id,
15463
+ display_name: String(pick.display_name || pick.label || `Record #${id}`),
15464
+ thumb: pick.thumb,
15465
+ additional_info: pick.additional_info,
15466
+ published: Boolean(published)
15467
+ };
15468
+ }
15469
+ renderResults(options) {
15470
+ this.resultsTarget.innerHTML = "";
15471
+ if (options.length === 0) {
15472
+ const empty = document.createElement("li");
15473
+ empty.className = "kubik-interface-ordered-record-list__empty";
15474
+ empty.setAttribute("role", "presentation");
15475
+ empty.textContent = "No matches \u2014 try another name, or every result is already in the list.";
15476
+ this.resultsTarget.appendChild(empty);
15477
+ this.openResults();
15478
+ return;
15479
+ }
15480
+ options.forEach((option) => {
15481
+ const item = document.createElement("li");
15482
+ item.className = "kubik-interface-typeahead__option kubik-interface-ordered-record-list__result";
15483
+ item.setAttribute("role", "option");
15484
+ item.textContent = option.display_name;
15485
+ item.addEventListener("mousedown", (event) => {
15486
+ event.preventDefault();
15487
+ this.addItem(option);
15488
+ this.searchInputTarget.value = "";
15489
+ this.closeResults();
15490
+ });
15491
+ this.resultsTarget.appendChild(item);
15492
+ });
15493
+ this.openResults();
15494
+ }
15495
+ openResults() {
15496
+ this.resultsTarget.hidden = false;
15497
+ if (this.hasAddTarget) {
15498
+ this.addTarget.classList.add("kubik-interface-typeahead--open");
15499
+ }
15500
+ }
15501
+ closeResults() {
15502
+ this.resultsTarget.innerHTML = "";
15503
+ this.resultsTarget.hidden = true;
15504
+ if (this.hasAddTarget) {
15505
+ this.addTarget.classList.remove("kubik-interface-typeahead--open");
15506
+ }
15507
+ }
15508
+ addItem(item) {
15509
+ this.itemsValue = [...this.itemsValue, item];
15510
+ this.selectedIds.add(String(item.id));
15511
+ this.renderRows();
15512
+ this.syncHiddenFields();
15513
+ }
15514
+ removeRow(event) {
15515
+ const currentTarget = event.currentTarget;
15516
+ const row = currentTarget.closest('[data-kubik-ordered-record-list-target="row"]');
15517
+ if (!row)
15518
+ return;
15519
+ const id = row.dataset.recordId;
15520
+ if (!id)
15521
+ return;
15522
+ this.itemsValue = this.itemsValue.filter((item) => String(item.id) !== id);
15523
+ this.selectedIds.delete(id);
15524
+ this.renderRows();
15525
+ this.syncHiddenFields();
15526
+ }
15527
+ renderRows() {
15528
+ this.listTarget.innerHTML = "";
15529
+ const template = this.rowTemplateTarget.content;
15530
+ this.itemsValue.forEach((item) => {
15531
+ var _a;
15532
+ const row = (_a = template.firstElementChild) == null ? void 0 : _a.cloneNode(true);
15533
+ if (!row)
15534
+ return;
15535
+ row.dataset.recordId = String(item.id);
15536
+ if (this.dimUnpublishedValue && item.published === false) {
15537
+ row.classList.add("kubik-interface-ordered-record-list__row--unpublished");
15538
+ }
15539
+ const nameEl = row.querySelector('[data-role="name"]');
15540
+ if (nameEl)
15541
+ nameEl.textContent = item.display_name;
15542
+ const meta = row.querySelector('[data-role="meta"]');
15543
+ if (meta) {
15544
+ if (item.additional_info) {
15545
+ meta.textContent = item.additional_info;
15546
+ } else {
15547
+ meta.remove();
15548
+ }
15549
+ }
15550
+ const visual = row.querySelector('[data-role="visual"]');
15551
+ if (visual)
15552
+ visual.innerHTML = this.visualMarkup(item);
15553
+ row.addEventListener("dragstart", (e) => this.onDragStart(e, row));
15554
+ row.addEventListener("dragover", (e) => this.onDragOver(e, row));
15555
+ row.addEventListener("drop", (e) => this.onDrop(e));
15556
+ row.addEventListener("dragend", () => row.classList.remove("kubik-interface-ordered-record-list__row--dragging"));
15557
+ this.listTarget.appendChild(row);
15558
+ });
15559
+ }
15560
+ visualMarkup(item) {
15561
+ if (this.previewValue === "thumb" && item.thumb) {
15562
+ return `<img src="${this.escapeAttr(item.thumb)}" alt="" class="kubik-interface-ordered-record-list__thumb" />`;
15563
+ }
15564
+ return `<span class="material-symbols-outlined kubik-interface-ordered-record-list__icon" aria-hidden="true">place</span>`;
15565
+ }
15566
+ escapeAttr(value) {
15567
+ return String(value).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
15568
+ }
15569
+ syncHiddenFields() {
15570
+ const fieldName = this.hiddenContainerTarget.dataset.fieldName;
15571
+ if (!fieldName)
15572
+ return;
15573
+ this.hiddenContainerTarget.innerHTML = "";
15574
+ this.itemsValue.forEach((item) => {
15575
+ const input = document.createElement("input");
15576
+ input.type = "hidden";
15577
+ input.name = `${fieldName}[]`;
15578
+ input.value = String(item.id);
15579
+ this.hiddenContainerTarget.appendChild(input);
15580
+ });
15581
+ }
15582
+ onDragStart(event, row) {
15583
+ this.draggingRow = row;
15584
+ row.classList.add("kubik-interface-ordered-record-list__row--dragging");
15585
+ if (event.dataTransfer) {
15586
+ event.dataTransfer.effectAllowed = "move";
15587
+ event.dataTransfer.setData("text/plain", row.dataset.recordId || "");
15588
+ }
15589
+ }
15590
+ onDragOver(event, row) {
15591
+ if (!this.draggingRow || this.draggingRow === row)
15592
+ return;
15593
+ event.preventDefault();
15594
+ const rect = row.getBoundingClientRect();
15595
+ const before = event.clientY < rect.top + rect.height / 2;
15596
+ this.listTarget.insertBefore(this.draggingRow, before ? row : row.nextSibling);
15597
+ }
15598
+ onDrop(event) {
15599
+ event.preventDefault();
15600
+ this.persistOrderFromDom();
15601
+ }
15602
+ handleDocMouseUp() {
15603
+ if (this.draggingRow) {
15604
+ this.persistOrderFromDom();
15605
+ this.draggingRow = null;
15606
+ }
15607
+ }
15608
+ persistOrderFromDom() {
15609
+ const order = Array.from(this.listTarget.querySelectorAll("[data-record-id]")).map(
15610
+ (row) => row.dataset.recordId
15611
+ );
15612
+ const byId = {};
15613
+ this.itemsValue.forEach((item) => {
15614
+ byId[String(item.id)] = item;
15615
+ });
15616
+ this.itemsValue = order.map((id) => id ? byId[id] : void 0).filter(Boolean);
15617
+ this.syncHiddenFields();
15618
+ }
15619
+ onDragHandle(event) {
15620
+ const currentTarget = event.currentTarget;
15621
+ const row = currentTarget.closest('[data-kubik-ordered-record-list-target="row"]');
15622
+ if (row)
15623
+ row.draggable = true;
15624
+ }
15625
+ clearDebounce() {
15626
+ if (this.debounceTimer) {
15627
+ clearTimeout(this.debounceTimer);
15628
+ this.debounceTimer = null;
15629
+ }
15630
+ }
15631
+ abortFetch() {
15632
+ if (this.fetchAbort) {
15633
+ this.fetchAbort.abort();
15634
+ this.fetchAbort = null;
15635
+ }
15636
+ }
15637
+ }
15638
+ OrderedRecordListController.targets = ["list", "hiddenContainer", "searchInput", "results", "rowTemplate", "row", "add"];
15639
+ OrderedRecordListController.values = {
15640
+ items: { type: Array, default: [] },
15641
+ searchUrl: String,
15642
+ preview: { type: String, default: "thumb" },
15643
+ dimUnpublished: { type: Boolean, default: true }
15644
+ };
15395
15645
  const KUBIK_INTERFACE_STIMULUS_MANIFEST = [
15396
15646
  "kubik-modal",
15397
15647
  "kubik-offcanvas",
@@ -15400,7 +15650,8 @@ const KUBIK_INTERFACE_STIMULUS_MANIFEST = [
15400
15650
  "kubik-debounced-form-submit",
15401
15651
  "kubik-filter-tags-section",
15402
15652
  "kubik-interface-tabbed",
15403
- "kubik-chart"
15653
+ "kubik-chart",
15654
+ "kubik-ordered-record-list"
15404
15655
  ];
15405
15656
  function registerKubikInterfaceStimulusControllers(application) {
15406
15657
  application.register("kubik-modal", modal_controller_default);
@@ -15411,6 +15662,7 @@ function registerKubikInterfaceStimulusControllers(application) {
15411
15662
  application.register("kubik-filter-tags-section", filter_tags_section_controller_default);
15412
15663
  application.register("kubik-interface-tabbed", tabbed_panels_controller_default);
15413
15664
  application.register("kubik-chart", chart_controller_default);
15665
+ application.register("kubik-ordered-record-list", OrderedRecordListController);
15414
15666
  }
15415
15667
  const registerInterfaceElementControllers = function() {
15416
15668
  const existing = document.documentElement.getAttribute("data-controller") || "";