BigW Consortium Gitlab

preview_markdown.js 5.19 KB
Newer Older
1 2
/* eslint-disable func-names, space-before-function-paren, no-var, one-var, one-var-declaration-per-line, wrap-iife, no-else-return, consistent-return, object-shorthand, comma-dangle, no-param-reassign, padded-blocks, camelcase, prefer-arrow-callback, max-len */

3 4 5 6 7
// MarkdownPreview
//
// Handles toggling the "Write" and "Preview" tab clicks, rendering the preview,
// and showing a warning when more than `x` users are referenced.
//
Fatih Acet committed
8 9 10
(function() {
  var lastTextareaPreviewed, markdownPreview, previewButtonSelector, writeButtonSelector;

11
  window.MarkdownPreview = (function() {
Fatih Acet committed
12 13
    function MarkdownPreview() {}

14
    // Minimum number of users referenced before triggering a warning
Fatih Acet committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    MarkdownPreview.prototype.referenceThreshold = 10;

    MarkdownPreview.prototype.ajaxCache = {};

    MarkdownPreview.prototype.showPreview = function(form) {
      var mdText, preview;
      preview = form.find('.js-md-preview');
      mdText = form.find('textarea.markdown-area').val();
      if (mdText.trim().length === 0) {
        preview.text('Nothing to preview.');
        return this.hideReferencedUsers(form);
      } else {
        preview.text('Loading...');
        return this.renderMarkdown(mdText, (function(_this) {
          return function(response) {
            preview.html(response.body);
31
            preview.renderGFM();
Fatih Acet committed
32 33 34 35 36 37 38
            return _this.renderReferencedUsers(response.references.users, form);
          };
        })(this));
      }
    };

    MarkdownPreview.prototype.renderMarkdown = function(text, success) {
39
      if (!window.preview_markdown_path) {
Fatih Acet committed
40 41 42 43 44 45 46
        return;
      }
      if (text === this.ajaxCache.text) {
        return success(this.ajaxCache.response);
      }
      return $.ajax({
        type: 'POST',
47
        url: window.preview_markdown_path,
Fatih Acet committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        data: {
          text: text
        },
        dataType: 'json',
        success: (function(_this) {
          return function(response) {
            _this.ajaxCache = {
              text: text,
              response: response
            };
            return success(response);
          };
        })(this)
      });
    };

    MarkdownPreview.prototype.hideReferencedUsers = function(form) {
      var referencedUsers;
      referencedUsers = form.find('.referenced-users');
      return referencedUsers.hide();
    };

    MarkdownPreview.prototype.renderReferencedUsers = function(users, form) {
      var referencedUsers;
      referencedUsers = form.find('.referenced-users');
      if (referencedUsers.length) {
        if (users.length >= this.referenceThreshold) {
          referencedUsers.show();
          return referencedUsers.find('.js-referenced-users-count').text(users.length);
        } else {
          return referencedUsers.hide();
        }
      }
    };

    return MarkdownPreview;

  })();

87
  markdownPreview = new window.MarkdownPreview();
Fatih Acet committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  previewButtonSelector = '.js-md-preview-button';

  writeButtonSelector = '.js-md-write-button';

  lastTextareaPreviewed = null;

  $.fn.setupMarkdownPreview = function() {
    var $form, form_textarea;
    $form = $(this);
    form_textarea = $form.find('textarea.markdown-area');
    form_textarea.on('input', function() {
      return markdownPreview.hideReferencedUsers($form);
    });
    return form_textarea.on('blur', function() {
      return markdownPreview.showPreview($form);
    });
  };

  $(document).on('markdown-preview:show', function(e, $form) {
    if (!$form) {
      return;
    }
    lastTextareaPreviewed = $form.find('textarea.markdown-area');
112
    // toggle tabs
Fatih Acet committed
113 114
    $form.find(writeButtonSelector).parent().removeClass('active');
    $form.find(previewButtonSelector).parent().addClass('active');
115
    // toggle content
Fatih Acet committed
116 117 118 119 120 121 122 123 124 125
    $form.find('.md-write-holder').hide();
    $form.find('.md-preview-holder').show();
    return markdownPreview.showPreview($form);
  });

  $(document).on('markdown-preview:hide', function(e, $form) {
    if (!$form) {
      return;
    }
    lastTextareaPreviewed = null;
126
    // toggle tabs
Fatih Acet committed
127 128
    $form.find(writeButtonSelector).parent().addClass('active');
    $form.find(previewButtonSelector).parent().removeClass('active');
129
    // toggle content
Fatih Acet committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    $form.find('.md-write-holder').show();
    $form.find('textarea.markdown-area').focus();
    return $form.find('.md-preview-holder').hide();
  });

  $(document).on('markdown-preview:toggle', function(e, keyboardEvent) {
    var $target;
    $target = $(keyboardEvent.target);
    if ($target.is('textarea.markdown-area')) {
      $(document).triggerHandler('markdown-preview:show', [$target.closest('form')]);
      return keyboardEvent.preventDefault();
    } else if (lastTextareaPreviewed) {
      $target = lastTextareaPreviewed;
      $(document).triggerHandler('markdown-preview:hide', [$target.closest('form')]);
      return keyboardEvent.preventDefault();
    }
  });

  $(document).on('click', previewButtonSelector, function(e) {
    var $form;
    e.preventDefault();
    $form = $(this).closest('form');
    return $(document).triggerHandler('markdown-preview:show', [$form]);
  });

  $(document).on('click', writeButtonSelector, function(e) {
    var $form;
    e.preventDefault();
    $form = $(this).closest('form');
    return $(document).triggerHandler('markdown-preview:hide', [$form]);
  });

}).call(this);