Sha256: 9f3bdbd117ef2a25d5575744f289010ad4bb595b97a9ce7e73317c7ac03c27da

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

(function() {

  NodeList.prototype.dismissible = function(options) {
    this.forEach(function(el) {
      el.dismissible(options);
    });
  }

  HTMLElement.prototype.dismissible = function(options) {
    this.querySelector('[data-dismissible-hide]').addEventListener('click', function(event) {
      event.preventDefault();
      this.dismiss(options);
    });
  }

  HTMLElement.prototype.dismiss = function(options) {
    var block_name = this.getAttribute('data-dismissible-hide');
    var block = document.querySelector('[data-dismissible=' + block_name + ']');
    var csrf = document.querySelector('meta[name=csrf-token]');
    var token = csrf != null ? csrf.getAttribute('content') : null;
    var xhr = new XMLHttpRequest();

    xhr.open('PUT', '/dismissible_blocks.json');
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.onload = function() {
      if (xhr.status === 200) {
        if (options !== undefined && options.dismiss !== undefined) {
          return options.dismiss(block);
        } else {
          return block.remove();
        }
      }
    };
    xhr.send(JSON.stringify({
      block: block_name,
      authenticity_token: token
    }));
  }

})();

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dismissible_blocks-2.0.0 app/assets/javascripts/dismissible_blocks.js