Sha256: facdf7502e7dfdd8adcfe4d88bcaf4f16e2adaf6d0069ac55aed0d85b47a1a41

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 KB

Contents

//= require thredded/core/serialize_form
//= require thredded/components/spoilers

(() => {
  const Thredded = window.Thredded;
  const PREVIEW_AREA_SELECTOR = '[data-thredded-preview-area]';
  const PREVIEW_AREA_POST_SELECTOR = '[data-thredded-preview-area-post]';

  class ThreddedPreviewArea {

    constructor(form, textarea) {
      const preview = form.querySelector(PREVIEW_AREA_SELECTOR);
      if (!preview || !textarea) return;
      this.form = form;
      this.preview = preview;
      this.previewPost = form.querySelector(PREVIEW_AREA_POST_SELECTOR);
      this.previewUrl = this.preview.getAttribute('data-thredded-preview-url');

      let prevValue = null;
      const onChange = Thredded.debounce(() => {
        if (prevValue !== textarea.value) {
          this.updatePreview();
          prevValue = textarea.value;
        }
      }, 200, false);

      textarea.addEventListener('input', onChange, false);

      this.requestId = 0;
    }

    updatePreview() {
      this.requestId++;
      const requestId = this.requestId;
      const request = new XMLHttpRequest();
      request.open(this.form.method, this.previewUrl, /* async */ true);
      request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
      request.onload = () => {
        if (
          // Ignore server errors
          request.status >= 200 && request.status < 400 &&
          // Ignore older responses received out-of-order
          requestId === this.requestId) {
          this.onPreviewResponse(request.responseText);
        }
      };
      request.send(Thredded.serializeForm(this.form));
    }

    onPreviewResponse(data) {
      this.preview.style.display = 'block';
      this.previewPost.innerHTML = data;
      Thredded.spoilers.init(this.previewPost);
    }
  }

  window.ThreddedPreviewArea = ThreddedPreviewArea;
})();

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
thredded-0.16.13 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.12 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.11 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.10 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.9 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.8 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.7 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.6 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.5 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.4 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.3 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.1 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.16.0 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.15.5 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.15.4 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.15.3 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.15.2 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.15.1 app/assets/javascripts/thredded/components/preview_area.es6