Sha256: 8e802bc843bbc9c4c516f4b93b2330bfc66d553fcbc59469e06f0d1803afa145

Contents?: true

Size: 1.76 KB

Versions: 15

Compression:

Stored size: 1.76 KB

Contents

//= require thredded/core/serialize_form

(() => {
  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;
    }
  }

  window.ThreddedPreviewArea = ThreddedPreviewArea;
})();

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
threddedDANIEL-0.14.5 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.14.4 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.14.3 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.14.2 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.14.1 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.14.0 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.8 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.7 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.6 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.5 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.4 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.3 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.2 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.1 app/assets/javascripts/thredded/components/preview_area.es6
thredded-0.13.0 app/assets/javascripts/thredded/components/preview_area.es6