~xdavidwu/xdavidwu.link

55b6896627751bf5fcba9699f918de60af5abb9e — Michael Rose 6 years ago c820734
Add support for Staticman v2 endpoint and reCAPTCHA

Ref: #803
2 files changed, 114 insertions(+), 0 deletions(-)

A _includes/comments-providers/staticman_v2.html
M _includes/comments.html
A _includes/comments-providers/staticman_v2.html => _includes/comments-providers/staticman_v2.html +42 -0
@@ 0,0 1,42 @@
{% if site.repository and site.staticman.branch %}
  <script>
    (function ($) {
    var $comments = $('.js-comments');

    $('#new_comment').submit(function () {
      var form = this;

      $(form).addClass('disabled');
      $('#comment-form-submit').html('<i class="fa fa-spinner fa-spin fa-fw"></i> {{ site.data.ui-text[site.locale].loading_label | default: "Loading..." }}');

      $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serialize(),
        contentType: 'application/x-www-form-urlencoded',
        success: function (data) {
          $('#comment-form-submit').html('{{ site.data.ui-text[site.locale].comment_btn_submitted | default: "Submitted" }}');
          $('.page__comments-form .js-notice').removeClass('notice--danger');
          $('.page__comments-form .js-notice').addClass('notice--success');
          showAlert('{{ site.data.ui-text[site.locale].comment_success_msg | default: "Thanks for your comment! It will show on the site once it has been approved." }}');
        },
        error: function (err) {
          console.log(err);
          $('#comment-form-submit').html('{{ site.data.ui-text[site.locale].comment_btn_submit  | default: "Submit Comment" }}');
          $('.page__comments-form .js-notice').removeClass('notice--success');
          $('.page__comments-form .js-notice').addClass('notice--danger');
          showAlert('{{ site.data.ui-text[site.locale].comment_error_msg | default: "Sorry, there was an error with your submission. Please make sure all required fields have been completed and try again." }}');
          $(form).removeClass('disabled');
        }
      });

      return false;
    });

    function showAlert(message) {
      $('.page__comments-form .js-notice').removeClass('hidden');
      $('.page__comments-form .js-notice-text').html(message);
    }
  })(jQuery);
  </script>
{% endif %}
\ No newline at end of file

M _includes/comments.html => _includes/comments.html +72 -0
@@ 91,6 91,78 @@
            <!-- End new comment form -->
          {% endif %}
        </section>
        {% when "staticman_v2" %}
        <section id="static-comments">
          {% if site.repository and site.staticman.branch %}
            <!-- Start static comments -->
            <div class="js-comments">
              {% if site.data.comments[page.slug] %}
                <h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_title | default: "Comments" }}</h4>
                {% assign comments = site.data.comments[page.slug] | sort %}

                {% for comment in comments %}
                  {% assign email = comment[1].email %}
                  {% assign name = comment[1].name %}
                  {% assign url = comment[1].url %}
                  {% assign date = comment[1].date %}
                  {% assign message = comment[1].message %}
                  {% include comment.html index=forloop.index email=email name=name url=url date=date message=message %}
                {% endfor %}
              {% endif %}
            </div>
            <!-- End static comments -->

            <!-- Start new comment form -->
            <div class="page__comments-form">
              <h4 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_label | default: "Leave a Comment" }}</h4>
              <p class="small">{{ site.data.ui-text[site.locale].comment_form_info | default: "Your email address will not be published. Required fields are marked" }} <span class="required">*</span></p>
              <form id="new_comment" class="page__comments-form js-form form" method="post" action="https://api.staticman.net/v2/entry/{{ site.repository }}/{{ site.staticman.branch }}/comments">
                <div class="form__spinner">
                  <i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
                  <span class="sr-only">{{ site.data.ui-text[site.locale].loading_label | default: "Loading..." }}</span>
                </div>

                <fieldset>
                  <label for="comment-form-message">{{ site.data.ui-text[site.locale].comment_form_comment_label | default: "Comment" }} <small class="required">*</small></label>
                  <textarea type="text" rows="3" id="comment-form-message" name="fields[message]" tabindex="1"></textarea>
                  <div class="small help-block"><a href="https://daringfireball.net/projects/markdown/">{{ site.data.ui-text[site.locale].comment_form_md_info | default: "Markdown is supported." }}</a></div>
                </fieldset>
                <fieldset>
                  <label for="comment-form-name">{{ site.data.ui-text[site.locale].comment_form_name_label | default: "Name" }} <small class="required">*</small></label>
                  <input type="text" id="comment-form-name" name="fields[name]" tabindex="2" />
                </fieldset>
                <fieldset>
                  <label for="comment-form-email">{{ site.data.ui-text[site.locale].comment_form_email_label | default: "Email address" }} <small class="required">*</small></label>
                  <input type="email" id="comment-form-email" name="fields[email]" tabindex="3" />
                </fieldset>
                <fieldset>
                  <label for="comment-form-url">{{ site.data.ui-text[site.locale].comment_form_website_label | default: "Website (optional)" }}</label>
                  <input type="url" id="comment-form-url" name="fields[url]" tabindex="4"/>
                </fieldset>
                <fieldset class="hidden" style="display: none;">
                  <input type="hidden" name="options[slug]" value="{{ page.slug }}">
                  <label for="comment-form-location">Not used. Leave blank if you are a human.</label>
                  <input type="text" id="comment-form-location" name="fields[hidden]" autocomplete="off"/>
                  <input type="hidden" name="options[reCaptcha][siteKey]" value="{{ site.reCaptcha.siteKey }}">
                  <input type="hidden" name="options[reCaptcha][secret]" value="{{ site.reCaptcha.secret }}">
                </fieldset>
                <!-- Start comment form alert messaging -->
                <p class="hidden js-notice">
                  <strong class="js-notice-text"></strong>
                </p>
                <!-- End comment form alert messaging -->
                <fieldset>
                  <div class="g-recaptcha" data-sitekey="{{ site.reCaptcha.siteKey }}"></div>
                </fieldset>
                <fieldset>
                  <button type="submit" id="comment-form-submit" tabindex="5" class="btn btn--primary btn--large">{{ site.data.ui-text[site.locale].comment_btn_submit | default: "Submit Comment" }}</button>
                </fieldset>
              </form>
            </div>
            <!-- End new comment form -->
            {% if site.reCaptcha.siteKey %}<script async src="https://www.google.com/recaptcha/api.js"></script>{% endif %}
          {% endif %}
        </section>
      {% when "custom" %}
        <section id="custom-comments"></section>
  {% endcase %}