<commit_before>angular.module('project_questions')

.factory('QuestionsService', ['$resource', '$http', '$timeout', '$location', '$rootScope', '$filter', '$q', '$window', '$sce', function($resource, $http, $timeout, $location, $rootScope, $filter, $q, $window, $sce) {

    /* get the base url */

    var baseurl = angular.element('meta[name="baseurl"]').attr('content');

    /* configure resources */

    var resources = {
        projects: $resource(baseurl + 'api/v1/projects/projects/:id/:detail_action/'),
        values: $resource(baseurl + 'api/v1/projects/projects/:project/values/:id/'),
        questionsets: $resource(baseurl + 'api/v1/projects/projects/:project/questionsets/:list_action/:id/')
    };

    /* configure factories */

    var factories = {
        values: function(attribute_id) {
            return {
                text: '',
                option: null,
                file: null,
                selected: null,
                project: service.project.id,
                attribute: attribute_id
            };
        },
        valuesets: function() {
            return {
                values: {}
            };
        }
    };

    /* create a buffer for the future service content */

    var future = {};

    /* create a flag to be used when clicking the prev button */

    var back = false;

    /* create a flag to be used when the view is initializing */

    var initializing = false;

    /* create the questions service */

    var service = {
        values: null
    };

    service.init = function(project_id) {
        resources.projects.get({id: project_id, detail_action: 'overview'}, function(response) {
            service.project = response;

            // get the current questionset_id form the url
            var questionset_id = $location.path().replace(/\//g,'');

            // init the view
            service.initView(questionset_id).then(function() {
                // enable back/forward button of browser
                $rootScope.$on('$locationChangeSuccess', function (scope, next, current) {
                    var questionset_id = parseInt($location.path().replace(/\//g,''), 10);
                    if (questionset_id !== service.questionset.id) {
                        service.initView(questionset_id);
                    }
                });
            });
        });
    };

    service.initView = function(questionset_id) {

        if (initializing) return;

        if (questionset_id !== null) {

            // enable initializing flag
            initializing = true;

            return service.fetchQuestionSet(questionset_id)
            .then(function() {
                return service.checkConditions();
            })
            .then(function() {
                return service.initOptions();
            })
            .then(function() {
                return service.checkOptionSetConditions();
            })
            .then(function() {
                return service.fetchValues();
            })
            .then(function () {
                // initialize values
                service.initValues();

                // copy questionset
                service.questionset = angular.copy(future.questionset);

                // copy progress
                service.progress = angular.copy(future.progress);

                // copy valuesets
                service.valueset_list = angular.copy(future.valueset_list);
                service.valuesets = angular.copy(future.valuesets);

                // activate fist valueset
                if (service.valueset_list.length) {
                    service.activateValueSet(service.valueset_list[0])
                } else {
                    service.activateValueSet(null);
                }

                // focus the first field
                if (service.values && Object.keys(service.values).length) {
                    var first_question = service.questionset.questions[0];

                    if (first_question.widget_type != 'date') {
                        if (first_question.is_collection) {
                            service.focusField(first_question.attribute.id, 0);
                        } else {
                            service.focusField(first_question.attribute.id);
                        }
                    }
                }

                // disable initializing flag again
                initializing = false;

                // set browser location, scroll to top and set back flag
                $location.path('/' + service.questionset.id + '/');
                $window.scrollTo(0, 0);
                back = false;

            }, function (result) {
                if (result === false) {
                    // checkConditions returned $q.reject

                    // disable initializing flag again
                    initializing = false;

                    // navigate to another question questionset when checkConditions returned $q.reject
                    if (back) {
                        return service.initView(future.questionset.prev);
                    } else {
                        return service.initView(future.questionset.next);
                    }
                }
            });
        } else {
            service.questionset = {
                id: false,
                progress: 100,
                next: null,
                prev: service.questionset.id
            };

            // set browser location, scroll to top and set back flag
            // $location.path('/done/');
            $window.scrollTo(0, 0);
            back = false;
        }
    };

    service.fetchQuestionSet = function(questionset_id) {

        // fetch the current (or the first) question set from the server<commit_msg>Fix continuation to end of interview<commit_after>angular.module('project_questions')

.factory('QuestionsService', ['$resource', '$http', '$timeout', '$location', '$rootScope', '$filter', '$q', '$window', '$sce', function($resource, $http, $timeout, $location, $rootScope, $filter, $q, $window, $sce) {

    /* get the base url */

    var baseurl = angular.element('meta[name="baseurl"]').attr('content');

    /* configure resources */

    var resources = {
        projects: $resource(baseurl + 'api/v1/projects/projects/:id/:detail_action/'),
        values: $resource(baseurl + 'api/v1/projects/projects/:project/values/:id/'),
        questionsets: $resource(baseurl + 'api/v1/projects/projects/:project/questionsets/:list_action/:id/')
    };

    /* configure factories */

    var factories = {
        values: function(attribute_id) {
            return {
                text: '',
                option: null,
                file: null,
                selected: null,
                project: service.project.id,
                attribute: attribute_id
            };
        },
        valuesets: function() {
            return {
                values: {}
            };
        }
    };

    /* create a buffer for the future service content */

    var future = {};

    /* create a flag to be used when clicking the prev button */

    var back = false;

    /* create a flag to be used when the view is initializing */

    var initializing = false;

    /* create the questions service */

    var service = {
        values: null
    };

    service.init = function(project_id) {
        resources.projects.get({id: project_id, detail_action: 'overview'}, function(response) {
            service.project = response;

            // get the current questionset_id form the url
            var questionset_id = $location.path().replace(/\//g,'');

            // init the view
            service.initView(questionset_id).then(function() {
                // enable back/forward button of browser
                $rootScope.$on('$locationChangeSuccess', function (scope, next, current) {
                    var questionset_id = parseInt($location.path().replace(/\//g,''), 10);
                    if (questionset_id !== service.questionset.id) {
                        service.initView(questionset_id);
                    }
                });
            });
        });
    };

    service.initView = function(questionset_id) {

        if (initializing) return;

        if (questionset_id !== null) {

            // enable initializing flag
            initializing = true;

            return service.fetchQuestionSet(questionset_id)
            .then(function() {
                return service.checkConditions();
            })
            .then(function() {
                return service.initOptions();
            })
            .then(function() {
                return service.checkOptionSetConditions();
            })
            .then(function() {
                return service.fetchValues();
            })
            .then(function () {
                // initialize values
                service.initValues();

                // copy questionset
                service.questionset = angular.copy(future.questionset);

                // copy progress
                service.progress = angular.copy(future.progress);

                // copy valuesets
                service.valueset_list = angular.copy(future.valueset_list);
                service.valuesets = angular.copy(future.valuesets);

                // activate fist valueset
                if (service.valueset_list.length) {
                    service.activateValueSet(service.valueset_list[0])
                } else {
                    service.activateValueSet(null);
                }

                // focus the first field
                if (service.values && Object.keys(service.values).length) {
                    var first_question = service.questionset.questions[0];

                    if (first_question.widget_type != 'date') {
                        if (first_question.is_collection) {
                            service.focusField(first_question.attribute.id, 0);
                        } else {
                            service.focusField(first_question.attribute.id);
                        }
                    }
                }

                // disable initializing flag again
                initializing = false;

                // set browser location, scroll to top and set back flag
                $location.path('/' + service.questionset.id + '/');
                $window.scrollTo(0, 0);
                back = false;

            }, function (result) {
                if (result === false) {
                    // checkConditions returned $q.reject

                    // disable initializing flag again
                    initializing = false;

                    // navigate to another question questionset when checkConditions returned $q.reject
                    if (back) {
                        return service.initView(future.questionset.prev);
                    } else {
                        return service.initView(future.questionset.next);
                    }
                }
            });
        } else {
            service.questionset = {
                id: false,
                progress: 100,
                next: null,
                prev: service.questionset ? service.questionset.id : null
            };

            // set browser location, scroll to top and set back flag
            // $location.path('/done/');
            $window.scrollTo(0, 0);
            back = false;
        }
    };

    service.fetchQuestionSet = function(questionset_id) {

        // fetch the current (or the first) question set from the server
{'fraction_non_alphanumeric': 0.10953364309069007, 'fraction_numerical': 0.002581311306143521, 'mean_word_length': 1.4290491118077324, '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': '677144', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2684, 'n_words': 628}