<commit_before>import { Directive, ng } from "../ng-start";
import { INavigationGuard, InputGuard, navigationGuardService, AngularJSRouteChangeListener, DOMRouteChangeListener, ObjectGuard, ManualChangeListener, TemplateRouteChangeListener } from "../navigationGuard";
import { Element } from "../workspace/model";


export const guardRootDirective: Directive = ng.directive('guardRoot', () => {
    return {
        restrict: 'A',
        controller: ["$scope", "$attrs", function ($scope, $attrs) {

            let rootID: string;
            if ($attrs.guardRoot != null && $attrs.guardRoot != "")
                rootID = $attrs.guardRoot;
            else
                rootID = navigationGuardService.generateID();

            let angularRCL = AngularJSRouteChangeListener.getInstance($scope.$root);
            navigationGuardService.registerListener(angularRCL);
            navigationGuardService.registerListener(DOMRouteChangeListener.getInstance());
            navigationGuardService.registerListener(TemplateRouteChangeListener.getInstance());

            $scope.$on("$destroy", function () {
                navigationGuardService.unregisterListener(angularRCL);
                navigationGuardService.unregisterListener(DOMRouteChangeListener.getInstance());
                navigationGuardService.unregisterListener(TemplateRouteChangeListener.getInstance());
                navigationGuardService.unregisterRoot(rootID);
            });

            this.registerGuard = function (guard: INavigationGuard) {
                navigationGuardService.registerGuard(rootID, guard);
            };

            this.unregisterGuard = function (guard: INavigationGuard) {
                navigationGuardService.unregisterGuard(rootID, guard);
            };

            this.reset = function () {
                navigationGuardService.reset(rootID);
            };
        }],
    }
});

function generateGuardDirective(directiveName: string, guardFactory: (scope, element, attrs, ngModel) => INavigationGuard): Directive {
    return ng.directive(directiveName, () => {
        return {
            require: ['?ngModel', '?^^guardRoot'],
            restrict: 'A',
            link: function (scope, element, attrs, requires) {
                let ngModel = requires[0];
                let root = requires[1];
                const guard = guardFactory(scope, element, attrs, ngModel);
                let count = 0;
                if (guard != null) {
                    if (ngModel) {
                        ngModel.$formatters.push((value) => {
                            if (count == 0) {
                                setTimeout(() => guard.reset());
                                count++;
                            }
                            return value;
                        });
                    } else {
                        setTimeout(() => guard.reset());
                    }
                    if (root != null) {
                        root.registerGuard(guard);
                        scope.$on("$destroy", function () {
                            root.unregisterGuard(guard);
                        });
                    }
                    else {
                        let id: string = navigationGuardService.registerIndependantGuard(guard);
                        scope.$on("$destroy", function () {
                            navigationGuardService.unregisterIndependantGuard(id);
                        });
                    }
                }
            },
        };
    });
};

export const resetGuardDirective: Directive = ng.directive('resetGuard', () => {
    return {
        require: '?^guardRoot',
        restrict: 'A',
        link: function (scope, element, attrs, root) {
            let resetID = attrs.resetGuardId;
            const submit = async () => {
                const promise: Promise<any> = scope.$eval(attrs.resetGuard)
                if (promise instanceof Promise) {
                    await promise;//reset if promise return success
                } else {
                    console.warn("[resetGuard] result is not instance of Promise");
                }
                if (resetID != null && resetID != "") {
                    navigationGuardService.reset(resetID);
                }
                else if (root != null) {
                    root.reset();
                }
                else {
                    console.warn("[resetGuard] A reset directive has no root, resetting all guards...");
                    navigationGuardService.resetAll();
                }
            }
            const bind = () => {
                const tagname = (element.prop("tagName") as string || "").toLowerCase();
                if (tagname == "form") {
                    element.on("submit", submit)
                    return () => element.off("submit", submit)
                } else {
                    element.on("click", submit)
                    return () => element.off("click", submit)
                }
            }
            const unbind = bind();
            scope.$on("$destroy", function () {
                unbind();<commit_msg>feat(navigationGuard): Add a custom event option to reset guard<commit_after>import { Directive, ng } from "../ng-start";
import { INavigationGuard, InputGuard, navigationGuardService, AngularJSRouteChangeListener, DOMRouteChangeListener, ObjectGuard, ManualChangeListener, TemplateRouteChangeListener } from "../navigationGuard";
import { Element } from "../workspace/model";


export const guardRootDirective: Directive = ng.directive('guardRoot', () => {
    return {
        restrict: 'A',
        controller: ["$scope", "$attrs", function ($scope, $attrs) {

            let rootID: string;
            if ($attrs.guardRoot != null && $attrs.guardRoot != "")
                rootID = $attrs.guardRoot;
            else
                rootID = navigationGuardService.generateID();

            let angularRCL = AngularJSRouteChangeListener.getInstance($scope.$root);
            navigationGuardService.registerListener(angularRCL);
            navigationGuardService.registerListener(DOMRouteChangeListener.getInstance());
            navigationGuardService.registerListener(TemplateRouteChangeListener.getInstance());

            $scope.$on("$destroy", function () {
                navigationGuardService.unregisterListener(angularRCL);
                navigationGuardService.unregisterListener(DOMRouteChangeListener.getInstance());
                navigationGuardService.unregisterListener(TemplateRouteChangeListener.getInstance());
                navigationGuardService.unregisterRoot(rootID);
            });

            this.registerGuard = function (guard: INavigationGuard) {
                navigationGuardService.registerGuard(rootID, guard);
            };

            this.unregisterGuard = function (guard: INavigationGuard) {
                navigationGuardService.unregisterGuard(rootID, guard);
            };

            this.reset = function () {
                navigationGuardService.reset(rootID);
            };
        }],
    }
});

function generateGuardDirective(directiveName: string, guardFactory: (scope, element, attrs, ngModel) => INavigationGuard): Directive {
    return ng.directive(directiveName, () => {
        return {
            require: ['?ngModel', '?^^guardRoot'],
            restrict: 'A',
            link: function (scope, element, attrs, requires) {
                let ngModel = requires[0];
                let root = requires[1];
                const guard = guardFactory(scope, element, attrs, ngModel);
                let count = 0;
                if (guard != null) {
                    if (ngModel) {
                        ngModel.$formatters.push((value) => {
                            if (count == 0) {
                                setTimeout(() => guard.reset());
                                count++;
                            }
                            return value;
                        });
                    } else {
                        setTimeout(() => guard.reset());
                    }
                    if (root != null) {
                        root.registerGuard(guard);
                        scope.$on("$destroy", function () {
                            root.unregisterGuard(guard);
                        });
                    }
                    else {
                        let id: string = navigationGuardService.registerIndependantGuard(guard);
                        scope.$on("$destroy", function () {
                            navigationGuardService.unregisterIndependantGuard(id);
                        });
                    }
                }
            },
        };
    });
};

export const resetGuardDirective: Directive = ng.directive('resetGuard', () => {
    return {
        require: '?^guardRoot',
        restrict: 'A',
        link: function (scope, element, attrs, root) {
            let resetID = attrs.resetGuardId;
            const submit = async () => {
                const promise: Promise<any> = scope.$eval(attrs.resetGuard)
                if (promise instanceof Promise) {
                    await promise;//reset if promise return success
                } else {
                    console.warn("[resetGuard] result is not instance of Promise");
                }
                if (resetID != null && resetID != "") {
                    navigationGuardService.reset(resetID);
                }
                else if (root != null) {
                    root.reset();
                }
                else {
                    console.warn("[resetGuard] A reset directive has no root, resetting all guards...");
                    navigationGuardService.resetAll();
                }
            }
            const bind = () => {
                let event: string = attrs.resetGuardEvent;
                if(event != null && event != "")
                {
                    const tagname = (element.prop("tagName") as string || "").toLowerCase();
                    if (tagname == "form") {
                        event = "submit";
                    } else {
                        event = "click";
                    }
                }

                element.on(event, submit);
                return () => element.off(event, submit);
            }
            const unbind = bind();
            scope.$on("$destroy", function () {
                unbind();
{'fraction_non_alphanumeric': 0.10951747720364742, 'fraction_numerical': 0.0007598784194528875, 'mean_word_length': 1.4762464722483537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '6184909', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2306, 'n_words': 489}