<commit_before>var myApp = angular.module('unsavedNew', [])

.directive('unsavedWarningGroup', ['$rootScope',
    function($rootScope) {
        return {
            scope: true,
            priority: 2000,
            controller: function() {

                // Controller scopped variables
                var message = "screeny!";
                var allForms = [];
                var areAllFormsClean = true;
                var removeFunction = function() {};

                // messages. Change here is you need 
                var messages = {
                    navigate: "You will lose unsaved changes if you leave this page",
                    reload: "You will lose unsaved changes if you reload this page"
                };

                // Checks all forms, if any one is dirty will return true

                function allFormsClean() {
                    angular.forEach(allForms, function(item, idx) {
                        if (item.$dirty) {
                            //console.log('FOUND AT LEAST 1 DIRTY FORM');
                            areAllFormsClean = false;
                        }
                    });
                    return areAllFormsClean; // no dirty forms were found
                }


                this.doSomethingScreeny = function() {
                    alert(message);
                };

                this.changeText = function() {
                    message = 'matt';
                };

                // pass form controller and adds it to the array
                this.init = function(form) {
                    //console.log('Adding to all forms');
                    allForms.push(form);
                };

                this.removePrompt = function() {
                    //console.log('CHOOSING TO REMOVE THIS FUNCTION');
                    allForms = []; // reset forms array
                    removeFunction();
                    window.onbeforeunload = null;
                };

                // Function called when user tries to close the window
                this.confirmExit = function() {

                    //console.log('REFRESH / CLOSE detected');

                    // @todo this could be written a lot cleaner! 
                    if (!allFormsClean()) {
                        return messages.reload;
                    } else {
                        removeFunction();
                        window.onbeforeunload = null;
                    }
                };

                // bind to window close
                window.onbeforeunload = this.confirmExit;

                // calling this function later will unbind this, acting as $off()
                removeFunction = $rootScope.$on('$locationChangeStart', function(event, next, current) {

                    //console.log('ROUTE CHANGE detected');

                    // @todo this could be written a lot cleaner! 
                    if (!allFormsClean()) {
                        if (!confirm(messages.navigate)) {
                            event.preventDefault(); // user clicks cancel, wants to stay on page 
                        } else {
                            removeFunction(); // unbind our `locationChangeStart` listener
                            window.onbeforeunload = null; // clear our the `refresh page` listener
                        }
                    } else {
                        removeFunction(); // unbind our `locationChangeStart` listener
                        window.onbeforeunload = null; // clear our the `refresh page` listener
                    }

                });

                /**
                * Used to call unsaved warning on tab change. 
                *
                */
                $rootScope.$on('$tabChangeStart', function(event, dataObj) {

                    var form = dataObj.form;
                    var callback = 
                        dataObj.callback && typeof dataObj.callback === 'function' ? 
                        dataObj.callback : 
                        angular.noop();

                    // if form is invalid, message user about unsaved changed
                    if(form && (form.$invalid || form.$dirty)) {
                        if (!allFormsClean()) {
                            if (!confirm(messages.navigate)) {
                                return false; 
                            } else {
                                callback();
                            }
                        } else {
                            callback();
                        }
                    // do callback right away
                    } else {
                        callback();
                    }

                });

            }
        };
    }
])<commit_msg>Fix issue where warning was not being removed.<commit_after>var myApp = angular.module('unsavedNew', [])

.directive('unsavedWarningGroup', ['$rootScope',
    function($rootScope) {
        return {
            scope: true,
            priority: 2000,
            controller: function() {

                // Controller scopped variables
                var message = "screeny!";
                var allForms = [];
                var areAllFormsClean = true;
                var removeFunction = function() {};

                // messages. Change here is you need 
                var messages = {
                    navigate: "You will lose unsaved changes if you leave this page",
                    reload: "You will lose unsaved changes if you reload this page"
                };

                // Checks all forms, if any one is dirty will return true

                function allFormsClean() {
                    angular.forEach(allForms, function(item, idx) {
                        if (item.$dirty) {
                            //console.log('FOUND AT LEAST 1 DIRTY FORM');
                            areAllFormsClean = false;
                        }
                    });
                    return areAllFormsClean; // no dirty forms were found
                }


                this.doSomethingScreeny = function() {
                    alert(message);
                };

                this.changeText = function() {
                    message = 'matt';
                };

                // pass form controller and adds it to the array
                this.init = function(form) {
                    //console.log('Adding to all forms');
                    allForms.push(form);
                };

                this.removePrompt = function() {
                    //console.log('CHOOSING TO REMOVE THIS FUNCTION');
                    allForms = []; // reset forms array
                    removeFunction();
                    window.onbeforeunload = null;
                };

                // Function called when user tries to close the window
                this.confirmExit = function() {

                    //console.log('REFRESH / CLOSE detected');

                    // @todo this could be written a lot cleaner! 
                    if (!allFormsClean()) {
                        return messages.reload;
                    } else {
                        removeFunction();
                        window.onbeforeunload = null;
                    }
                };

                // bind to window close
                window.onbeforeunload = this.confirmExit;

                // calling this function later will unbind this, acting as $off()
                removeFunction = $rootScope.$on('$locationChangeStart', function(event, next, current) {

                    //console.log('ROUTE CHANGE detected');

                    // @todo this could be written a lot cleaner! 
                    if (!allFormsClean()) {
                        if (!confirm(messages.navigate)) {
                            event.preventDefault(); // user clicks cancel, wants to stay on page 
                        } else {
                            removeFunction(); // unbind our `locationChangeStart` listener
                            window.onbeforeunload = null; // clear our the `refresh page` listener
                        }
                    } else {
                        removeFunction(); // unbind our `locationChangeStart` listener
                        window.onbeforeunload = null; // clear our the `refresh page` listener
                    }

                });

                /**
                * Used to call unsaved warning on tab change. 
                *
                */
                $rootScope.$on('$tabChangeStart', function(event, dataObj) {

                    var form = dataObj.form;
                    var callback = 
                        dataObj.callback && typeof dataObj.callback === 'function' ? 
                        dataObj.callback : 
                        angular.noop();

                    console.log(form);

                    // callback if no form right away
                    if(!form) callback();

                    // if form is invalid, message user about unsaved changed
                    if(form.$invalid || form.$dirty) {
                        if (!allFormsClean()) {
                            if (!confirm(messages.navigate)) {
                                return false; 
                            } else {
                                callback();
                                window.onbeforeunload = null;
                            }
                        } else {
                            callback();
                            window.onbeforeunload = null;
                        }
                    // do callback right away
                    } else {
                        callback();
                        window.onbeforeunload = null;
                    }

                });

            }
        };
    }
])
{'fraction_non_alphanumeric': 0.08412730589904724, 'fraction_numerical': 0.0010135819987837015, 'mean_word_length': 0.9476904855902092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '813663', 'n_tokens_mistral': 2198, 'n_tokens_neox': 1938, 'n_words': 581}