<commit_before><%- content_for :scripts_bottom do -%>
  <script>
    // Warn users about leaving the page with a dirty form
    $(document).on("ornament:refresh", function(){
      var formsDirty = false;

      // Changing any form on the page will mark forms as dirty
      $("form").on("change", function(){
        formsDirty = true;
      });

      // Clicking on designated cleaner buttons with the attribute of 
      // data-form-cleaner will mark the forms as clean again
      $("[data-form-cleaner]").on("click", function(){
        formsDirty = false;
      });

      // Set a beforeunload function to warn the user if forms are dirty
      // and they try to leave the page. 
      $(window).bind('beforeunload', function(){
        if(formsDirty) {
          return 'Warning: There are unsaved changes.';
        }
      });
    });
  </script>
<%- end -%><commit_msg>Restructured dirty form checker in to Ornament Component API and added dirty state checking for CKEditor<commit_after><%- content_for :scripts_bottom do -%>
  <script>
    $(document).on("ornament:refresh", function(){

      // Warn users about leaving the page with a dirty form
      var DirtyForms = Ornament.C.DirtyForms = {
        isDirty: false,
        bindingsToWatch: "change, keyup",
        forceClean: false,

        // Changing any form on the page will mark forms as dirty
        _bindDirtyFormChecker: function(){
          DirtyForms.$forms.on(DirtyForms.bindingsToWatch, function(){
            DirtyForms.isDirty = true;
          });
        },

        // Clicking on designated cleaner buttons with the attribute of 
        // data-form-cleaner will mark the forms as clean again
        _bindFormCleaners: function(){
          DirtyForms.$cleaners.on("click", DirtyForms.cleanForms);
        },

        // Check if any CKEditors are dirty
        anyCKEditorsDirty: function(){
          for (var i in CKEDITOR.instances) {
            if(CKEDITOR.instances[i].checkDirty()) {
              return true;
            }
          }
        },

        // Force disabling of dirty checker
        // Used when leaving the page via a save method
        cleanForms: function() {
          DirtyForms.forceClean = true;
        },

        // Check if CKEditor or our internal checker are dirty
        anyDirtyForms: function(){
          return DirtyForms.anyCKEditorsDirty() || DirtyForms.isDirty;
        },

        init: function(){
          // Get our elements
          DirtyForms.$forms = $("form");
          DirtyForms.$cleaners = $("[data-form-cleaner]");
          // Bind those elements
          DirtyForms._bindDirtyFormChecker();
          DirtyForms._bindFormCleaners();
          // Set a beforeunload function to warn the user if forms are dirty
          // and they try to leave the page. 
          $(window).bind('beforeunload', function(){
            if(!DirtyForms.forceClean && DirtyForms.anyDirtyForms()) {
              return 'Warning: There are unsaved changes.';
            }
          });
        }
      }

      DirtyForms.init();

    });
  </script>
<%- end -%> 
{'fraction_non_alphanumeric': 0.11061093247588424, 'fraction_numerical': 0.0, 'mean_word_length': 2.0470127326150833, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4620110', 'n_tokens_mistral': 859, 'n_tokens_neox': 835, 'n_words': 260}