Sha256: 4e158a6023e3c4221edde6eee70afb0c97940d688136ed53001c0543b3da8826

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

var buttons = document.querySelectorAll("[data-batch-action-option='button']");
var checkboxes = document.querySelectorAll("[data-batch-action-option='checkbox']");
var selectAllCheckboxes = document.querySelector("[data-batch-action-option='select_all']");

if (selectAllCheckboxes && checkboxes && buttons) {

  window.onpageshow = function(event) {
    if (selectedItemIds()) {
      checkboxes.forEach(function(checkbox) {
        checkbox.checked = false;
      });

      selectAllCheckboxes.checked = false;
    }
  };

  selectAllCheckboxes.addEventListener('click', function(){
    checkboxes.forEach(function(checkbox) {
      checkbox.checked = selectAllCheckboxes.checked;
    });

    checkAndToggleActionButtons();
  });

  buttons.forEach(function(button){
    button.addEventListener('click', function(event){
      button.href += '?' + selectedItemIds()
    });
  });

  checkboxes.forEach(function(checkbox){
    checkbox.closest('td').addEventListener('click', function(event){
      event.stopImmediatePropagation();
    })

    checkbox.addEventListener('click', function(event) {
      event.stopImmediatePropagation();

      checkAndToggleActionButtons();
    })
  })
}

function selectedItemIds() {
  var ids = Array.prototype.filter.call(checkboxes, function(checkbox) {
        if (checkbox.checked) { return checkbox }
      }).map(function(checkbox) {
        return 'batch_action_ids[]=' + checkbox.value
      }).join('&');
  return ids;
}

function checkAndToggleActionButtons() {
  if (selectedItemIds()) {
    buttons.forEach(function(button){
      button.classList.remove('disabled');
      button.removeAttribute('disabled');
    });
  } else {
    buttons.forEach(function(button){
      button.classList.add('disabled');
      button.setAttribute('disabled', 'disabled');
    });
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
administrate_batch_actions-0.3.2 app/assets/javascripts/administrate_batch_actions/script.js