Skip to content
wiki.fftac.org

Manuscript Workstation - Source Excerpt 04

Back to Manuscript Workstation

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/ns12-manuscript/assets/js/manuscript-workstation.js so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/ns12-manuscript/assets/js/manuscript-workstation.js

if (source) {
        records = parseJsonOr(source.textContent || '[]', [], { context: 'workstation-records' });
      }

      if (!Array.isArray(records) || !records.length) {
        records = this.folios.map((folio) => ({
          slug: folio.slug,
          title: folio.title,
          sequence: folio.sequence,
          image_url: folio.previewUrl || (folio.image ? folio.image.currentSrc || folio.image.src : ''),
          preview_url: folio.previewUrl || '',
          standard_url: folio.standardUrl || '',
          full_resolution_url: folio.fullResolutionUrl || '',
          prefetch_url: folio.standardUrl || '',
          crop: folio.crop,
          overlay_data: folio.overlayData || {},
        }));
      }

      return records
        .map((record) => this.normalizeFolioRecord(record))
        .filter(Boolean)
        .sort((left, right) => left.sequence - right.sequence);
    }

    getCurrentFolio() {
      return this.folios.find((folio) => folio.isCurrent) || this.folios[0];
    }

    getResponsiveViewMode(requestedMode = 'spread') {
      const normalized = requestedMode === 'single' ? 'single' : 'spread';
      if (normalized !== 'spread') {
        return normalized;
      }

      if (this.folios.length < 2) {
        return 'single';
      }

      const canUseSpread =
        typeof window.matchMedia === 'function'
          ? window.matchMedia('(min-width: 1080px)').matches
          : window.innerWidth >= 1080;

      return canUseSpread ? 'spread' : 'single';
    }

    getVisibleFolios() {
      if (this.state.viewMode === 'single') {
        return this.folios.filter((folio) => folio.isCurrent);
      }

      return this.folios.filter((folio) => {
        if (!folio.element) {
          return false;
        }

        return folio.element.getClientRects().length > 0;
      });
    }

    getFolioSource(folio, level = 'standard') {
      if (!folio) {
        return '';
      }

      const previewSource = `${folio.previewUrl || folio.thumbnailUrl || folio.imageUrl || ''}`.trim();
      const standardSource = `${folio.standardUrl || folio.prefetchUrl || folio.imageUrl || previewSource}`.trim();
      const fullSource = `${folio.fullResolutionUrl || standardSource || previewSource}`.trim();

      if (level === 'full') {
        return fullSource || standardSource || previewSource;
      }

      if (level === 'standard') {
        return standardSource || previewSource || fullSource;
      }

      return previewSource || standardSource || fullSource;
    }

    applyFolioImageSource(folio, level = 'standard', options = {}) {
      if (!folio || !folio.image) {
        return Promise.resolve('');
      }

      const cacheBust = options.cacheBust === true;
      const force = options.force === true;
      const targetSource = this.getFolioSource(folio, level);
      if (!targetSource) {
        return Promise.resolve('');
      }

      const nextSource = cacheBust ? addCacheBuster(targetSource) : targetSource;
      const currentLevel = `${folio.image.dataset.currentLevel || ''}`.trim() || 'preview';
      const currentSource = `${folio.image.dataset.loadedSrc || folio.image.currentSrc || folio.image.getAttribute('src') || ''}`.trim();

      if (!force && currentLevel === level && currentSource === targetSource) {
        folio.image.classList.toggle('is-preview-source', level === 'preview');
        return Promise.resolve(nextSource);
      }

      if (level === 'preview') {
        if (folio.image.getAttribute('src') !== nextSource) {
          folio.image.setAttribute('src', nextSource);
        }

        folio.image.dataset.currentLevel = 'preview';
        folio.image.dataset.loadedSrc = targetSource;
        delete folio.image.dataset.pendingSrc;
        delete folio.image.dataset.pendingLevel;
        folio.image.classList.add('is-preview-source');
        folio.image.classList.remove('is-image-upgrading');
        return Promise.resolve(nextSource);
      }

      folio.image.dataset.pendingSrc = nextSource;
      folio.image.dataset.pendingLevel = level;
      folio.image.classList.add('is-image-upgrading');

      return preloadImageAsset(nextSource).then((loadedSource) => {
        if (!loadedSource) {
          if (folio.image.dataset.pendingSrc === nextSource) {
            delete folio.image.dataset.pendingSrc;
            delete folio.image.dataset.pendingLevel;
            folio.image.classList.remove('is-image-upgrading');
          }

          return '';
        }

        if (folio.image.dataset.pendingSrc !== nextSource) {
          return loadedSource;
        }

        if (folio.image.getAttribute('src') !== nextSource) {
          folio.image.setAttribute('src', nextSource);
        }

        folio.image.dataset.currentLevel = level;
        folio.image.dataset.loadedSrc = targetSource;
        delete folio.image.dataset.pendingSrc;
        delete folio.image.dataset.pendingLevel;
        folio.image.classList.toggle('is-preview-source', level === 'preview');
        folio.image.classList.remove('is-image-upgrading');

        return loadedSource;
      });
    }

    getDefaultViewMode() {
      return this.getResponsiveViewMode(`${this.root.dataset.initialViewMode || 'spread'}`.trim() || 'spread');
    }

    getSearchIdleCopy() {
      return 'Search the full manuscript index, not just the visible thumbnails.';
    }

    getResetFitMode() {
      return this.baseFitMode === 'height' || this.baseFitMode === 'actual' ? this.baseFitMode : 'width';
    }

    getLinkBaseHref(link) {
      if (!(link instanceof HTMLElement)) {
        return '';
      }

      if (!link.dataset.baseHref) {
        link.dataset.baseHref = `${link.getAttribute('href') || ''}`.trim();
      }

      return `${link.dataset.baseHref || ''}`.trim();
    }

    getLinkBaseSpreadHref(link) {
      if (!(link instanceof HTMLElement)) {
        return '';
      }

      if (!link.dataset.baseSpreadHref) {
        link.dataset.baseSpreadHref = `${link.dataset.spreadHref || ''}`.trim();
      }

      return `${link.dataset.baseSpreadHref || ''}`.trim();
    }

    getOptionBaseValue(option) {
      if (!(option instanceof HTMLOptionElement)) {
        return '';
      }

      if (!option.dataset.baseValue) {
        option.dataset.baseValue = `${option.value || ''}`.trim();
      }

      return `${option.dataset.baseValue || ''}`.trim();
    }

    readViewerStateFromUrl() {
      const url = createUrlOr(window.location.href, null, { context: 'workstation-viewer-state-url' });
      if (!url) {
        return {};
      }