<commit_before>/* global Scientilla */

(function () {
    angular
            .module('references')
            .directive('scientillaDocumentForm', scientillaDocumentForm);

    function scientillaDocumentForm() {
        return {
            restrict: 'E',
            templateUrl: 'partials/scientillaDocumentForm.html',
            controller: scientillaDocumentFormController,
            controllerAs: 'vm',
            scope: {},
            bindToController: {
                document: "=",
                researchEntity: "=",
                onClose: "&",
                onSubmit: "&"
            }
        };
    }

    scientillaDocumentFormController.$inject = [
        'FormForConfiguration',
        'Notification',
        'researchEntityService',
        '$scope',
        '$rootScope',
        '$timeout'
    ];

    function scientillaDocumentFormController(FormForConfiguration, Notification, researchEntityService, $scope, $rootScope, $timeout) {
        var vm = this;
        vm.submit = submit;
        vm.status = createStatus();
        vm.cancel = cancel;
        vm.formVisible = true;
        vm.saveVerify = saveVerify;
        activate();
        
        function createStatus() {
            var isSavedVar = true;
            return {
                isSaved: function() {
                    return isSavedVar;
                },
                setSaved: function(isSaved) {
                    isSavedVar = isSaved;
                }
            };
        };


        function activate() {
            FormForConfiguration.enableAutoLabels();

            loadDocumentFields();
            watchDocument();
        }

        function watchDocument() {
            $scope.$watch('vm.document.sourceType', loadDocumentFields, true);
            var fieldsToWatch = _.keys(vm.validationAndViewRules);
            _.forEach(fieldsToWatch, function (f) {
                $scope.$watch('vm.document.' + f, markModified, true);
                $scope.$watch('vm.document.' + f, prepareSave, true);
            });
        }

        function loadDocumentFields() {
            var types = Scientilla.reference.getDocumentTypes()
                    .map(function(t) {
                        return {
                            value: t.key,
                            label: t.label
                        };
                    });
            vm.validationAndViewRules = _.merge({
                sourceType: {
                    inputType: 'select',
                    label: 'Source type',
                    allowBlank: true,
                    preventDefaultOption: true,
                    required: true,
                    values: [
                        {value: 'journal', label: 'Journal'},
                        {value: 'book', label: 'Book'},
                        {value: 'conference', label: 'Conference'}
                    ]
                },
                type: {
                    inputType: 'select',
                    label: 'Type',
                    allowBlank: true,
                    preventDefaultOption: true,
                    required: true,
                    values: types
                }
            }, Scientilla.reference.getDocumentsFields(vm.document.sourceType));
            refreshForm();
        }

        function refreshForm() {
            vm.formVisible = false;
            $timeout(function () {
                vm.formVisible = true;
            }, 0);
        }

        function markModified(newValue, oldValue) {
            if (newValue === oldValue || _.isUndefined(oldValue))
                return;
            vm.status.setSaved(false);
        }

        function prepareSave(newValue, oldValue) {
            if (newValue === oldValue || _.isUndefined(oldValue))
                return;
            if (vm.status.isSaved())
                return;
            _.debounce(saveDocument, 3000)();
        }

        function saveDocument() {
            if (vm.document.id)
                return vm.document.save().then(function () {
                    vm.status.setSaved(true);
                });
            else
                return vm.researchEntity.all('drafts')
                        .post(vm.document)
                        .then(function (draft) {
                            vm.document = draft;
                            vm.status.setSaved(true);
                            $rootScope.$broadcast('draft.updated', {});
                        });
        }
        
        function executeOnSubmit() {
            if (_.isFunction(vm.onSubmit()))
                vm.onSubmit()(vm.document);
            
        }

        function submit() {
            saveDocument()
                    .then(function () {
                        Notification.success("Document saved");
                        executeOnSubmit();

                    })
                    .catch(function () {
                        Notification.warning("Failed to save document");
                    });

        }

        function cancel() {
            if (_.isFunction(vm.onClose()))
                vm.onClose()();
        }
        
        function saveVerify() {
            saveDocument()
                    .then(function(){
                        return researchEntityService.verifyDraft(vm.researchEntity, vm.document);
                    })
                    .then(function (document) {
                        if (document.draft) {
                            Notification.warning("Draft is not valid and cannot be verified");
                        }
                        else {
                            executeOnSubmit();
                            Notification.success("Draft verified");<commit_msg>Changed messages inside the document-form component to use 'draft' instead of 'document'.<commit_after>/* global Scientilla */

(function () {
    angular
            .module('references')
            .directive('scientillaDocumentForm', scientillaDocumentForm);

    function scientillaDocumentForm() {
        return {
            restrict: 'E',
            templateUrl: 'partials/scientillaDocumentForm.html',
            controller: scientillaDocumentFormController,
            controllerAs: 'vm',
            scope: {},
            bindToController: {
                document: "=",
                researchEntity: "=",
                onClose: "&",
                onSubmit: "&"
            }
        };
    }

    scientillaDocumentFormController.$inject = [
        'FormForConfiguration',
        'Notification',
        'researchEntityService',
        '$scope',
        '$rootScope',
        '$timeout'
    ];

    function scientillaDocumentFormController(FormForConfiguration, Notification, researchEntityService, $scope, $rootScope, $timeout) {
        var vm = this;
        vm.submit = submit;
        vm.status = createStatus();
        vm.cancel = cancel;
        vm.formVisible = true;
        vm.saveVerify = saveVerify;
        activate();
        
        function createStatus() {
            var isSavedVar = true;
            return {
                isSaved: function() {
                    return isSavedVar;
                },
                setSaved: function(isSaved) {
                    isSavedVar = isSaved;
                }
            };
        };


        function activate() {
            FormForConfiguration.enableAutoLabels();

            loadDocumentFields();
            watchDocument();
        }

        function watchDocument() {
            $scope.$watch('vm.document.sourceType', loadDocumentFields, true);
            var fieldsToWatch = _.keys(vm.validationAndViewRules);
            _.forEach(fieldsToWatch, function (f) {
                $scope.$watch('vm.document.' + f, markModified, true);
                $scope.$watch('vm.document.' + f, prepareSave, true);
            });
        }

        function loadDocumentFields() {
            var types = Scientilla.reference.getDocumentTypes()
                    .map(function(t) {
                        return {
                            value: t.key,
                            label: t.label
                        };
                    });
            vm.validationAndViewRules = _.merge({
                sourceType: {
                    inputType: 'select',
                    label: 'Source type',
                    allowBlank: true,
                    preventDefaultOption: true,
                    required: true,
                    values: [
                        {value: 'journal', label: 'Journal'},
                        {value: 'book', label: 'Book'},
                        {value: 'conference', label: 'Conference'}
                    ]
                },
                type: {
                    inputType: 'select',
                    label: 'Type',
                    allowBlank: true,
                    preventDefaultOption: true,
                    required: true,
                    values: types
                }
            }, Scientilla.reference.getDocumentsFields(vm.document.sourceType));
            refreshForm();
        }

        function refreshForm() {
            vm.formVisible = false;
            $timeout(function () {
                vm.formVisible = true;
            }, 0);
        }

        function markModified(newValue, oldValue) {
            if (newValue === oldValue || _.isUndefined(oldValue))
                return;
            vm.status.setSaved(false);
        }

        function prepareSave(newValue, oldValue) {
            if (newValue === oldValue || _.isUndefined(oldValue))
                return;
            if (vm.status.isSaved())
                return;
            _.debounce(saveDocument, 3000)();
        }

        function saveDocument() {
            if (vm.document.id)
                return vm.document.save().then(function () {
                    vm.status.setSaved(true);
                });
            else
                return vm.researchEntity.all('drafts')
                        .post(vm.document)
                        .then(function (draft) {
                            vm.document = draft;
                            vm.status.setSaved(true);
                            $rootScope.$broadcast('draft.updated', {});
                        });
        }
        
        function executeOnSubmit() {
            if (_.isFunction(vm.onSubmit()))
                vm.onSubmit()(vm.document);
            
        }

        function submit() {
            saveDocument()
                    .then(function () {
                        Notification.success("Draft saved");
                        executeOnSubmit();

                    })
                    .catch(function () {
                        Notification.warning("Failed to save draft");
                    });

        }

        function cancel() {
            if (_.isFunction(vm.onClose()))
                vm.onClose()();
        }
        
        function saveVerify() {
            saveDocument()
                    .then(function(){
                        return researchEntityService.verifyDraft(vm.researchEntity, vm.document);
                    })
                    .then(function (document) {
                        if (document.draft) {
                            Notification.warning("Draft is not valid and cannot be verified");
                        }
                        else {
                            executeOnSubmit();
                            Notification.success("Draft verified");
{'fraction_non_alphanumeric': 0.10216076058772688, 'fraction_numerical': 0.000864304235090752, 'mean_word_length': 1.1396079881656804, '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': '1888381', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2540, 'n_words': 484}