Jump to content

Record Deletion and Update Alert JS


Recommended Posts

Copy and paste this JS to your result or details page footer: :)

<script>
if (typeof mainDataPageHandler == 'undefined') {
  const GetEventListenerEnabler = () => {
    Element.prototype._addEventListener = Element.prototype.addEventListener;
    Element.prototype._removeEventListener = Element.prototype.removeEventListener;

    Element.prototype.addEventListener = function(type, listener, useCapture = false) {
      this._addEventListener(type, listener, useCapture);

      if (!this.eventListenerList) this.eventListenerList = {};
      if (!this.eventListenerList[type]) this.eventListenerList[type] = [];

      this.eventListenerList[type].push({ type, listener, useCapture });
    };

    Element.prototype.removeEventListener = function(type, listener, useCapture = false) {
      this._removeEventListener(type, listener, useCapture);

      if (!this.eventListenerList) this.eventListenerList = {};
      if (!this.eventListenerList[type]) this.eventListenerList[type] = [];

      for (let i = 0; i < this.eventListenerList[type].length; i++) {
        if (
          this.eventListenerList[type][i].listener === listener &&
          this.eventListenerList[type][i].useCapture === useCapture
        ) {
          this.eventListenerList[type].splice(i, 1);
          break;
        }
      }

      if (this.eventListenerList[type].length == 0) delete this.eventListenerList[type];
    };

    Element.prototype.getEventListeners = function(type) {
      if (!this.eventListenerList) this.eventListenerList = {};
      if (type === undefined) return this.eventListenerList;
      return this.eventListenerList[type];
    };
  };

  if (Element.prototype.getEventListeners == undefined) {
    GetEventListenerEnabler();
  }

  const customDeleteAlertMessage = 'Are you sure you want to delete the record? Click "OK" if yes or "Cancel" if no.';
  const customUpdateAlertMessage = 'Are you sure you want to update the record? Click "OK" if yes or "Cancel" if no.';

  const addCustomDeleteBtn = () => {
    let deleteBtn = document.querySelector('.cbDeleteButton');
    if (document.querySelector('#customDeleteButton') != null || deleteBtn == null) {
      return;
    }

    deleteBtn.insertAdjacentHTML('afterend', `<input type="button" name="Mod0DeleteRecord" id="customDeleteButton" value="Delete" class="cbDeleteButton">`);
    deleteBtn.style = 'display: none!important';
  };

  const addCustomDeleteBtnListener = () => {
    let customDeleteBtn = document.querySelector('#customDeleteButton');
    if (customDeleteBtn == null || customDeleteBtn.getEventListeners('click') != null) {
      return;
    }

    customDeleteBtn.addEventListener('click', (e) => {
      if (confirm(customDeleteAlertMessage)) {
        document.querySelector('input[id*="Mod0DeleteRecord"]').click();
      }
    });

    console.log(customDeleteBtn.getEventListeners('click'));
  };

  const addCustomUpdateBtnListener = () => {
    let updateBtn = document.querySelector('.cbUpdateButton');
    if (updateBtn == null || updateBtn.getEventListeners('click') != null) {
      return;
    }

    updateBtn.addEventListener('click', (e) => {
      if (!confirm(customUpdateAlertMessage)) {
        e.preventDefault();
      }
    });

    console.log(updateBtn.getEventListeners('click'));
  };

  const mainDataPageHandler = () => {
    addCustomDeleteBtn();
    addCustomDeleteBtnListener();
    addCustomUpdateBtnListener();
  };

  document.addEventListener('DataPageReady', mainDataPageHandler);
}
</script>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...