Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/dds_content / js / node_edit_protection / node-edit-protection.js
Size: Mime:
/**
 * @file
 * Stops page from redirecting when user has unsaved data.
 */

(function($, Drupal, drupalSettings) {
  Drupal.node_edit_protection = {};
  let allow = true;
  let edit = false;
  Drupal.behaviors.nodeEditProtection = {
    attach : function(context) {
      // If user leaves an input field, we assume it has been changed
      $(".node-form :input").each(function() {
        $(this).blur(function() {
          edit = true;
        });
      });

      // Let all form submit buttons through
      $(".node-form input[type='submit'], .node-form button[type='submit']").each(function() {
        $(this).addClass('node-edit-protection-processed');
        $(this).click(function() {
          allow = true;
        });
      });

      // Loop through all links, and check link url when it has been clicked
      $("a").each(function() {
        $(this).click(function() {
          let url = $(this).attr("href");
          let url_last_segment = url.split('/').filter(Boolean).pop();
          if (url_last_segment == 'layout') {
            allow = false;
          } else {
            allow = true;
          }
        });
      });

      // Check if redirect is allowed when window is unloaded
      window.onbeforeunload = function() {
        if (edit && !allow) {
          allow = false;
          return (Drupal.t("You will lose all unsaved work."));
        } else {
          allow = true;
        }
      }
    }
  };
})(jQuery, Drupal, drupalSettings);