Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

5.1 KB
Raw Blame
/*
 * -- copyright
 * OpenProject is an open source project management software.
 * Copyright (C) the OpenProject GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 3.
 *
 * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
 * Copyright (C) 2006-2013 Jean-Philippe Lang
 * Copyright (C) 2010-2013 the ChiliProject Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * See COPYRIGHT and LICENSE files for more details.
 * ++
 */

import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
import type { TurboBeforeStreamRenderEvent } from 'core-typings/turbo';
import { useIntersection } from 'stimulus-use';
import BaseController from './base.controller';
import { DomHelpers } from './services/dom_helpers';

export default class LazyPageController extends BaseController {
  static values = {
    url: String,
    insertTargetId: String,
    page: { type: Number, default: 1 },
    isLoaded: { type: Boolean, default: false },
    loadDelayMs: { type: Number, default: 75 },
  };

  declare urlValue:string;
  declare insertTargetIdValue:string;
  declare pageValue:number;
  declare isLoadedValue:boolean;
  declare loadDelayMsValue:number;

  private turboRequests:TurboRequestsService;
  private abortController = new AbortController();
  private pageStreamHandler?:(_event:TurboBeforeStreamRenderEvent) => void;
  private stopObserving?:() => void;
  private loadTimeout?:number;

  connect() {
    if (this.isLoadedValue) return;

    super.connect();
    void this.initializeTurboRequestService();
    this.startObserving();
    this.setupScrollPreservation();
  }

  disconnect() {
    super.disconnect();
    this.tearDownScrollPreservation();
    this.cancelPendingLoad();
    this.stopObserving?.();
  }

  appear() {
    if (this.isLoadedValue) return;

    // Delay loading to allow rapid scrolling without triggering loads
    this.loadTimeout = window.setTimeout(() => {
      void this.fetchPageStream()
        .catch((error) => {
          console.error('Error fetching next page:', error);
        }).finally(() => {
          this.isLoadedValue = true;
        });
    }, this.loadDelayMsValue);
  }

  disappear() {
    this.cancelPendingLoad(); // Cancel load if element leaves viewport before delay expires
  }

  private setupScrollPreservation() {
    if (this.pageStreamHandler) return;

    const { signal } = this.abortController;
    const scrollContainer = this.scrollableContainer;

    this.pageStreamHandler = (event:TurboBeforeStreamRenderEvent) => {
      event.preventDefault();

      const stream = event.detail.newStream;
      const insertTargetId = this.insertTargetIdValue;

      if (scrollContainer && insertTargetId && stream.target.includes(insertTargetId)) {
        const isPrepending = this.indexOutlet.sortingAscending; // Newest at the bottom sorting order
        void DomHelpers.keepScroll(scrollContainer, isPrepending, () => {
          event.detail.render(stream);
          return Promise.resolve();
        });
      } else {
        event.detail.render(stream);
      }
    };

    document.addEventListener('turbo:before-stream-render', this.pageStreamHandler as EventListener, { signal });
  }

  private startObserving(root = this.scrollableContainer) {
    if (!root) return;

    const [_observe, unobserve] = useIntersection(this, {
      root,
      threshold: 0.25,
      dispatchEvent: false
    });

    this.stopObserving = unobserve;
  }

  private tearDownScrollPreservation() {
    this.abortController.abort();
    if (this.pageStreamHandler) this.pageStreamHandler = undefined;
  }

  private fetchPageStream():Promise<{ html:string, headers:Headers }> {
    const url = this.preparePageStreamsUrl();
    return this.turboRequests.requestStream(url);
  }

  private preparePageStreamsUrl():string {
    const baseUrl = window.location.origin;
    const url = new URL(this.urlValue, baseUrl);

    url.searchParams.set('page', this.pageValue.toString());
    url.searchParams.set('filter', this.indexOutlet.filterValue);

    return url.toString();
  }

  private async initializeTurboRequestService() {
    const context = await window.OpenProject.getPluginContext();
    this.turboRequests = context.services.turboRequests;
  }

  private cancelPendingLoad() {
    if (this.loadTimeout) {
      window.clearTimeout(this.loadTimeout);
      this.loadTimeout = undefined;
    }
  }
}