<commit_before>angular.module('conditions', ['core'])

.factory('ConditionsService', ['$resource', '$timeout', '$window', '$q', '$filter', function($resource, $timeout, $window, $q, $filter) {

    /* get the base url */

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

    /* configure resources */

    var resources = {
        conditions: $resource(baseurl + 'api/conditions/conditions/:list_route/:id/'),
        attributes: $resource(baseurl + 'api/conditions/attributes/:id/'),
        optionsets: $resource(baseurl + 'api/conditions/optionsets/:id/'),
        relations: $resource(baseurl + 'api/conditions/relations/:id/')
    };

    /* configure factories */

    var factories = {
        conditions: function(parent) {
            return {
                source: null,
                relation: null,
                target_option: null
            };
        }
    };

    /* create the conditions service */

    var service = {};

    service.init = function(options) {
        service.attributes = resources.attributes.query();
        service.optionsets = resources.optionsets.query();
        service.relations = resources.relations.query();

        service.initView().then(function () {
            var current_scroll_pos = sessionStorage.getItem('current_scroll_pos');
            if (current_scroll_pos) {
                $timeout(function() {
                    $window.scrollTo(0, current_scroll_pos);
                });
            }
        });

        $window.addEventListener('beforeunload', function() {
            sessionStorage.setItem('current_scroll_pos', $window.scrollY);
        });
    };

    service.initView = function(options) {
        return resources.conditions.query({list_route: 'index'}, function(response) {
            service.conditions = response;
        }).$promise;
    };

    service.openFormModal = function(resource, obj, create) {
        service.errors = {};
        service.values = {};

        if (angular.isDefined(create) && create) {
            service.values = factories[resource](obj);
        } else {
            service.values = resources[resource].get({id: obj.id});
        }

        $q.when(service.values.$promise).then(function() {
            $('#' + resource + '-form-modal').modal('show');
        });
    };

    service.submitFormModal = function(resource) {
        service.storeValues(resource).then(function() {
            $('#' + resource + '-form-modal').modal('hide');
            service.initView();
        }, function(result) {
            service.errors = result.data;
        });
    };

    service.openDeleteModal = function(resource, obj) {
        service.values = obj;
        $('#' + resource + '-delete-modal').modal('show');
    };

    service.submitDeleteModal = function(resource) {
        resources[resource].delete({id: service.values.id}, function() {
            $('#' + resource + '-delete-modal').modal('hide');
            service.initView();
        });
    };

    service.storeValues = function(resource, values) {
        if (angular.isUndefined(values)) {
            values = service.values;
        }

        if (angular.isDefined(values.removed) && values.removed) {
            if (angular.isDefined(values.id)) {
                return resources[resource].delete({id: values.id}).$promise;
            }
        } else {
            if (angular.isDefined(values.id)) {
                return resources[resource].update({id: values.id}, values).$promise;
            } else {
                return resources[resource].save(values).$promise;
            }
        }
    };

    service.getOptions = function() {
        if (angular.isDefined(service.values) && angular.isDefined(service.values.source)) {
            attribute = $filter('filter')(service.attributes, {id: service.values.source})[0];
            return attribute.options;
        }
    };

    return service;

}])
<commit_msg>fix wrong filtering in conditions angular app<commit_after>angular.module('conditions', ['core'])

.factory('ConditionsService', ['$resource', '$timeout', '$window', '$q', '$filter', function($resource, $timeout, $window, $q, $filter) {

    /* get the base url */

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

    /* configure resources */

    var resources = {
        conditions: $resource(baseurl + 'api/conditions/conditions/:list_route/:id/'),
        attributes: $resource(baseurl + 'api/conditions/attributes/:id/'),
        optionsets: $resource(baseurl + 'api/conditions/optionsets/:id/'),
        relations: $resource(baseurl + 'api/conditions/relations/:id/')
    };

    /* configure factories */

    var factories = {
        conditions: function(parent) {
            return {
                source: null,
                relation: null,
                target_option: null
            };
        }
    };

    /* create the conditions service */

    var service = {};

    service.init = function(options) {
        service.attributes = resources.attributes.query();
        service.optionsets = resources.optionsets.query();
        service.relations = resources.relations.query();

        service.initView().then(function () {
            var current_scroll_pos = sessionStorage.getItem('current_scroll_pos');
            if (current_scroll_pos) {
                $timeout(function() {
                    $window.scrollTo(0, current_scroll_pos);
                });
            }
        });

        $window.addEventListener('beforeunload', function() {
            sessionStorage.setItem('current_scroll_pos', $window.scrollY);
        });
    };

    service.initView = function(options) {
        return resources.conditions.query({list_route: 'index'}, function(response) {
            service.conditions = response;
        }).$promise;
    };

    service.openFormModal = function(resource, obj, create) {
        service.errors = {};
        service.values = {};

        if (angular.isDefined(create) && create) {
            service.values = factories[resource](obj);
        } else {
            service.values = resources[resource].get({id: obj.id});
        }

        $q.when(service.values.$promise).then(function() {
            $('#' + resource + '-form-modal').modal('show');
        });
    };

    service.submitFormModal = function(resource) {
        service.storeValues(resource).then(function() {
            $('#' + resource + '-form-modal').modal('hide');
            service.initView();
        }, function(result) {
            service.errors = result.data;
        });
    };

    service.openDeleteModal = function(resource, obj) {
        service.values = obj;
        $('#' + resource + '-delete-modal').modal('show');
    };

    service.submitDeleteModal = function(resource) {
        resources[resource].delete({id: service.values.id}, function() {
            $('#' + resource + '-delete-modal').modal('hide');
            service.initView();
        });
    };

    service.storeValues = function(resource, values) {
        if (angular.isUndefined(values)) {
            values = service.values;
        }

        if (angular.isDefined(values.removed) && values.removed) {
            if (angular.isDefined(values.id)) {
                return resources[resource].delete({id: values.id}).$promise;
            }
        } else {
            if (angular.isDefined(values.id)) {
                return resources[resource].update({id: values.id}, values).$promise;
            } else {
                return resources[resource].save(values).$promise;
            }
        }
    };

    service.getOptions = function() {
        if (angular.isDefined(service.values) && angular.isDefined(service.values.source)) {
            var attributes = $filter('filter')(service.attributes, {id: service.values.source});
            for (var i = 0; i < attributes.length; i++) {
                if (attributes[i].id == service.values.source) {
                    return attributes[i].options;
                }
            }
        }
    };

    return service;

}])

{'fraction_non_alphanumeric': 0.1525360977415772, 'fraction_numerical': 0.0004936443292607676, 'mean_word_length': 2.144741948001552, 'pattern_counts': {'":': 0, '<': 4, '<?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': '4036930', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2123, 'n_words': 347}