<commit_before>/* global Scientilla */

(function () {

    angular
        .module('documents')
        .component('scientillaDocumentAffiliations', {
            templateUrl: 'partials/scientilla-document-affiliations.html',
            controller: ScientillaDocumentAffiliations,
            controllerAs: 'vm',
            bindings: {
                document: "<",
                onFailure: "&",
                onSubmit: "&"
            }
        });


    ScientillaDocumentAffiliations.$inject = [
        '$scope',
        'Restangular'
    ];

    function ScientillaDocumentAffiliations($scope, Restangular) {
        var vm = this;
        vm.getInstitutesFilter = getInstitutesFilter;
        vm.getInstitutesQuery = getInstitutesQuery;
        vm.submit = submit;
        vm.cancel = cancel;


        vm.$onInit = () => {
            $scope.$watch('vm.position', userSelectedChanged);
            $scope.$watch('vm.authorship.affiliations', resetInstitutes, true);
        };

        function getInstitutesQuery(searchText) {
            var qs = {where: {name: {contains: searchText}}};
            var model = 'institutes';
            return {model: model, qs: qs};
        }

        function resetInstitutes() {
            vm.document.institutes = _.uniqBy(_.flatMap(vm.document.authorships, 'affiliations'), 'id');
        }

        function userSelectedChanged() {
            if(_.isUndefined(vm.position))
                return;
            vm.author = vm.document.getAuthors()[vm.position];
            vm.authorship = vm.document.authorships.find(a => a.position === vm.position);
            if (!vm.authorship) {
                const newAuthorship = {
                    document: vm.document.id,
                    position: vm.position,
                    affiliations: []
                };
                vm.document.authorships.push(newAuthorship);
                vm.authorship = newAuthorship;
            }

            getAuthorInstitutes()
                .then((institutes) => vm.authorship.affiliations = institutes);
        }

        function getAuthorInstitutes() {
            if (_.isNil(vm.position))
                return Promise.resolve([]);
            var qs = {
                where: {document: vm.document.id, position: vm.position},
                populate: 'affiliations'
            };
            return Restangular.all('authorships').getList(qs).then(function (authorships) {
                if (_.isEmpty(authorships))
                    return [];
                var authorship = authorships[0];
                return authorship.affiliations;
            });
        }

        function getInstitutesFilter() {
            return vm.authorship.affiliations;
        }

        function cancel() {
            executeOnSubmit(0);
        }

        function submit() {
            return save()
                .then(function (user) {
                    executeOnSubmit(1);
                })
                .catch(function () {
                    executeOnFailure();
                });
        }

        function save() {
            return vm.document.customPUT(vm.document.authorships, 'authorships');
        }

        function executeOnSubmit(i) {
            if (_.isFunction(vm.onSubmit()))
                vm.onSubmit()(i);
        }

        function executeOnFailure() {
            if (_.isFunction(vm.onFailure()))
                vm.onFailure()();
        }

    }
})
();<commit_msg>Adedd update of "editedAfterImport" field on affiliations editing<commit_after>/* global Scientilla */

(function () {
    "use strict";

    angular
        .module('documents')
        .component('scientillaDocumentAffiliations', {
            templateUrl: 'partials/scientilla-document-affiliations.html',
            controller: ScientillaDocumentAffiliations,
            controllerAs: 'vm',
            bindings: {
                document: "<",
                onFailure: "&",
                onSubmit: "&"
            }
        });


    ScientillaDocumentAffiliations.$inject = [
        '$scope',
        'Restangular'
    ];

    function ScientillaDocumentAffiliations($scope, Restangular) {
        var vm = this;
        vm.getInstitutesFilter = getInstitutesFilter;
        vm.getInstitutesQuery = getInstitutesQuery;
        vm.submit = submit;
        vm.cancel = cancel;

        let oldAuthorships;

        vm.$onInit = () => {
            $scope.$watch('vm.position', userSelectedChanged);
            $scope.$watch('vm.authorship.affiliations', resetInstitutes, true);
            oldAuthorships = getSimpleAuthorships(vm.document.authorships);
        };

        function getInstitutesQuery(searchText) {
            var qs = {where: {name: {contains: searchText}}};
            var model = 'institutes';
            return {model: model, qs: qs};
        }

        function resetInstitutes() {
            vm.document.institutes = _.uniqBy(_.flatMap(vm.document.authorships, 'affiliations'), 'id');
        }

        function userSelectedChanged() {
            if (_.isUndefined(vm.position))
                return;
            vm.author = vm.document.getAuthors()[vm.position];
            vm.authorship = vm.document.authorships.find(a => a.position === vm.position);
            if (!vm.authorship) {
                const newAuthorship = {
                    document: vm.document.id,
                    position: vm.position,
                    affiliations: []
                };
                vm.document.authorships.push(newAuthorship);
                vm.authorship = newAuthorship;
            }

            getAuthorInstitutes()
                .then((institutes) => vm.authorship.affiliations = institutes);
        }

        function getAuthorInstitutes() {
            if (_.isNil(vm.position))
                return Promise.resolve([]);
            var qs = {
                where: {document: vm.document.id, position: vm.position},
                populate: 'affiliations'
            };
            return Restangular.all('authorships').getList(qs).then(function (authorships) {
                if (_.isEmpty(authorships))
                    return [];
                var authorship = authorships[0];
                return authorship.affiliations;
            });
        }

        function getInstitutesFilter() {
            return vm.authorship.affiliations;
        }

        function cancel() {
            executeOnSubmit(0);
        }

        function submit() {
            return save()
                .then(function (user) {
                    executeOnSubmit(1);
                })
                .catch(function () {
                    executeOnFailure();
                });
        }

        function save() {
            return vm.document.customPUT(vm.document.authorships, 'authorships')
                .then(() => {
                    const newAuthorships = getSimpleAuthorships(vm.document.authorships);
                    if (_.isEqual(oldAuthorships, newAuthorships))
                        return;
                    
                    vm.document.editedAfterImport = true;
                    return vm.document.save();

                });
        }

        function executeOnSubmit(i) {
            if (_.isFunction(vm.onSubmit()))
                vm.onSubmit()(i);
        }

        function executeOnFailure() {
            if (_.isFunction(vm.onFailure()))
                vm.onFailure()();
        }

        function getSimpleAuthorships(authorships) {
            const simplyfiedAuthorships = [];
            for (let a of authorships)
                simplyfiedAuthorships.push({
                    researchEntity: a.researchEntity,
                    affiliations: a.affiliations,
                    corresponding: a.corresponding,
                    position: a.position,
                    public: a.public
                });

            return simplyfiedAuthorships;
        }

    }
})
();
{'fraction_non_alphanumeric': 0.11044327573253193, 'fraction_numerical': 0.0007513148009015778, 'mean_word_length': 1.4943785134291068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '4670909', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1958, 'n_words': 338}