<commit_before>/*jshint browser: true */
(function () {
    "use strict";
    
    var root = this,
        $ = root.jQuery;
    
    if (typeof GOVUK === 'undefined') { root.GOVUK = {}; }

    var AutoSave = function(options) {

        this.form = $(options.form);
        this.statusElement = $(options.statusElement);
        this.url = options.url;
        this.saved = true;

        this.addEventHandlers();

        this.form.find('button[type="submit"]').hide();
        
    };
    
    var NONE =  {label:'', state:''};
    var SAVING =  {label:'Saving...', state:'saving'};
    var SAVED =  {label:'Saved', state:'saved'};
    var NOTSAVED =  {label:'Not saved', state:'notsaved'};
    
    AutoSave.prototype.addEventHandlers = function () {
        this.blurHandler = this.getBlurHandler();
        this.submitHandler = this.getSubmitHandler();
        this.keyPressHandler = this.getKeyPressHandler();
        this.pasteHandler = this.getPasteHandler();
        
        this.form.on('submit', this.submitHandler);
        this.form.find('input,textarea')
            .on('blur', this.blurHandler)
            .on('keypress', this.keyPressHandler)
            .on('paste', this.pasteHandler);
    };
    
    AutoSave.prototype.getBlurHandler = function () {
        return function (e) {
            e.preventDefault();
            if (this.saved === false) {
                this.save();
            }
            return true;
        }.bind(this);
    };
    AutoSave.prototype.getSubmitHandler = function () {
        return function (e) {
            e.preventDefault();
            if (this.saved === false) {
                this.save();
            }
            // redirect to desired location
            return false;
        }.bind(this);
    };
    AutoSave.prototype.getKeyPressHandler = function () {
        return function (event) {
            var char;
            if (event.which === null) {
                char = event.keyCode;    // old IE
            } else if (event.which !== 0) {
                char = event.which;	  // All others
            } else {
                return;
            }

            if (char >= 48 && char <= 57 || char === 190 || char === 188) {
                this.saved = false;
                this.displayStatus(NONE);
                this.clearErrorsOnField($(event.target));
                this.startSaveTimer();
            }
            
        }.bind(this);
    };
    AutoSave.prototype.getPasteHandler = function () {
        return function () {
           this.saved = false;
           this.displayStatus(NONE);<commit_msg>Handle changes made to textareas that need to invalidate the save state to cause it to trigger a new save.<commit_after>/*jshint browser: true */
(function () {
    "use strict";
    
    var root = this,
        $ = root.jQuery;
    
    if (typeof GOVUK === 'undefined') { root.GOVUK = {}; }

    var AutoSave = function(options) {

        this.form = $(options.form);
        this.statusElement = $(options.statusElement);
        this.url = options.url;
        this.saved = true;

        this.addEventHandlers();

        this.form.find('button[type="submit"]').hide();
        
    };
    
    var NONE =  {label:'', state:''};
    var SAVING =  {label:'Saving...', state:'saving'};
    var SAVED =  {label:'Saved', state:'saved'};
    var NOTSAVED =  {label:'Not saved', state:'notsaved'};

    var ignoreCodes = [93, 13, 9, 35, 36, 45,34, 33, 37, 38, 39, 40, 27, 44, 145,19, 125, 124,126];
    
    AutoSave.prototype.addEventHandlers = function () {
        this.blurHandler = this.getBlurHandler();
        this.submitHandler = this.getSubmitHandler();
        this.keyPressHandler = this.getKeyPressHandler();
        this.keyDownHandler = this.getKeyDownHandler();
        this.pasteHandler = this.getPasteHandler();
        
        this.form.on('submit', this.submitHandler);
        this.form.find('input,textarea')
            .on('blur', this.blurHandler)
            .on('keypress', this.keyPressHandler)
            .on('keydown', this.keyDownHandler)
            .on('paste', this.pasteHandler);
    };
    
    AutoSave.prototype.getBlurHandler = function () {
        return function (e) {
            e.preventDefault();
            if (this.saved === false) {
                this.save();
            }
            return true;
        }.bind(this);
    };
    AutoSave.prototype.getSubmitHandler = function () {
        return function (e) {
            e.preventDefault();
            if (this.saved === false) {
                this.save();
            }
            // redirect to desired location
            return false;
        }.bind(this);
    };
    AutoSave.prototype.getKeyDownHandler = function () {
      return function (event) {
          if (event.keyCode === 8) {
              this.keyPressHandler(event);
          }
      }.bind(this);
    };
    AutoSave.prototype.getKeyPressHandler = function () {
        return function (event) {
            var char;
            if (event.which === null) {
                char = event.keyCode;    // old IE
            } else if (event.which !== 0) {
                char = event.which;	  // All others
            } else {
                return;
            }
            
            // If the user entered a key that affects the field value then mark things as changed
            // otherwise ignore it, for things like tab or arrow keys
            if ($.inArray(char, ignoreCodes) === -1 || (event.target.tagName === 'TEXTAREA' && char === 13)) {
                this.saved = false;
                this.displayStatus(NONE);
                this.clearErrorsOnField($(event.target));
                this.startSaveTimer();
            }
            
        }.bind(this);
    };
    AutoSave.prototype.getPasteHandler = function () {
        return function () {
           this.saved = false;
           this.displayStatus(NONE);
{'fraction_non_alphanumeric': 0.13695395513577333, 'fraction_numerical': 0.009613762860516108, 'mean_word_length': 1.7003642987249545, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2887463', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1630, 'n_words': 355}