<commit_before>define([
    'angular'
], function(angular) {
    'use strict';

    AuthoringController.$inject = ['$scope', 'api', '$location', 'workqueue', 'notify', 'gettext', 'modal', '$window'];
    function AuthoringController($scope, api, $location, workqueue, notify, gettext, modal, $window) {

        $window.onbeforeunload = function() {
            if ($scope.dirty) {
                return gettext('There are unsaved changes. If you navigate away, your changes will be lost.');
            }
        };

        var _item;
        $scope.item = null;
        $scope.dirty = null;
        $scope.workqueue = workqueue.all();
        setupNewItem();

        function setupNewItem() {
            var _id = $location.search()._id;
            if (_id) {
                _item = workqueue.find({_id: _id}) || workqueue.active;
                $scope.item = _.create(_item);
                workqueue.setActive(_item);
            }
        }

        $scope.$watch('item', function(item, oldItem) {
            if (item === oldItem) {
                $scope.dirty = false;
                return;
            }

            $scope.dirty = item && oldItem && item._id === oldItem._id;
        }, true);

        $scope.create = function() {
            var temp = {type: 'text'};
            api.archive.save(temp, {}).then(function(newItem) {
                workqueue.add(newItem);
                $scope.switchArticle(newItem);
            }, function(response) {
                notify.error(gettext('Error. Item not created.'));
            });
        };

        $scope.switchArticle = function(article) {
            workqueue.update($scope.item);
            workqueue.setActive(article);
            $location.search({_id: article._id});
        };

    	$scope.save = function() {
    		api.archive.save(_item, $scope.item).then(function(res) {
                workqueue.update($scope.item);
                $scope.dirty = false;
                notify.success(gettext('Item updated.'));
    		}, function(response) {
    			notify.error(gettext('Error. Item not updated.'));
    		});
    	};

        $scope.close = function() {
            if ($scope.dirty) {
                return confirmDirty().then(_close);
            } else {
                _close();
            }
        };

        var confirmed = false;
        $scope.$on('$locationChangeStart', function(e, next) {
            if ($scope.dirty && !confirmed) {
                e.preventDefault();
                confirmDirty().then(function() {
                    confirmed = true;
                    $location.url(next.split('#')[1]);
                });
            }
        });

        $scope.$on('$routeUpdate', function() {
            confirmed = false;
            setupNewItem();
        });

        var _close = function() {
            workqueue.remove(_item);
            $location.search('_id', workqueue.getActive());
        };

        function confirmDirty() {
            return modal.confirm(gettext('There are unsaved changes. Please confirm you want to close the article without saving.'));
        }
    }

    VersioningController.$inject = ['$scope', 'api', '$location', 'notify'];
    function VersioningController($scope, api, $location, notify) {
        $scope.item = null;
        $scope.versions = null;
        $scope.selected = null;
        $scope.users = {};

        $scope.$watch(function() {
            return $location.search()._id;
        }, function(_id) {
            $scope.item = null;
            $scope.versions = null;

            if (_id) {
                fetchItem(_id);
            }
        });

        var fetchUser = function(id) {
            api.users.getById(id)
            .then(function(result) {
                $scope.users[id] = result;
            });
        };

        var fetchItem = function(id) {
            id = id || $scope.item._id;
            api.archive.getById(id)
            .then(function(result) {
                $scope.item = result;
                fetchVersions();
            });
        };

        var fetchVersions = function() {
            $scope.users = {};
            api.archive.getByUrl($scope.item._links.self.href + '?version=all&embedded={"user":1}')
            .then(function(result) {
                _.each(result._items, function(version) {
                    var creator = version.creator || version.original_creator;
                    if (creator && !$scope.users[creator]) {
                        fetchUser(creator);
                    }
                });
                $scope.versions = result;
                $scope.selected = _.find($scope.versions._items, {_version: $scope.item._latest_version});
            });
        };

        $scope.revert = function(version) {
            api.archive.replace($scope.item._links.self.href, {
                type: 'text',
                last_version: $scope.item._version,
                old_version: version
            })
            .then(function(result) {
                fetchItem();
                notify.success(gettext('Item reverted.'));
            }, function(result) {
                notify.error(gettext('Error. Item not reverted.'));
            });
        };
    }

    WorkqueueService.$inject = ['storage'];
    function WorkqueueService(storage) {
        /**
         * Set items for further work, in next step of the workflow.
         */

        var queue = storage.getItem('workqueue:items') || [];
        this.length = 0;
        this.active = null;

        /**
         * Add an item into queue
         *
         * it checks if item is in queue already and if yes it will move it to the very end
         *
         * @param {Object} item
         */
        this.add = function(item) {
            _.remove(queue, {_id: item._id});
            queue.unshift(item);<commit_msg>fix(authoring): Updating main view after version revert fixed, close button fixed.<commit_after>define([
    'angular'
], function(angular) {
    'use strict';

    AuthoringController.$inject = ['$scope', 'api', '$location', 'workqueue', 'notify', 'gettext', 'modal', '$window'];
    function AuthoringController($scope, api, $location, workqueue, notify, gettext, modal, $window) {

        $window.onbeforeunload = function() {
            if ($scope.dirty) {
                return gettext('There are unsaved changes. If you navigate away, your changes will be lost.');
            }
        };

        var _item;
        $scope.item = null;
        $scope.dirty = null;
        $scope.workqueue = workqueue.all();
        setupNewItem();

        function setupNewItem() {
            var _id = $location.search()._id;
            if (_id) {
                _item = workqueue.find({_id: _id}) || workqueue.active;
                $scope.item = _.create(_item);
                workqueue.setActive(_item);
            }
        }

        $scope.$watch('item', function(item, oldItem) {
            if (item === oldItem) {
                $scope.dirty = false;
                return;
            }

            $scope.dirty = item && oldItem && item._id === oldItem._id;
        }, true);

        $scope.create = function() {
            var temp = {type: 'text'};
            api.archive.save(temp, {}).then(function(newItem) {
                workqueue.add(newItem);
                $scope.switchArticle(newItem);
            }, function(response) {
                notify.error(gettext('Error. Item not created.'));
            });
        };

        $scope.switchArticle = function(article) {
            workqueue.update($scope.item);
            workqueue.setActive(article);
            $location.search({_id: article._id});
        };

    	$scope.save = function() {
    		api.archive.save(_item, $scope.item).then(function(res) {
                workqueue.update($scope.item);
                $scope.dirty = false;
                notify.success(gettext('Item updated.'));
    		}, function(response) {
    			notify.error(gettext('Error. Item not updated.'));
    		});
    	};

        $scope.close = function() {
            if ($scope.dirty) {
                return confirmDirty().then(_close);
            } else {
                _close();
            }
        };

        var confirmed = false;
        $scope.$on('$locationChangeStart', function(e, next) {
            if ($scope.dirty && !confirmed) {
                e.preventDefault();
                confirmDirty().then(function() {
                    confirmed = true;
                    $location.url(next.split('#')[1]);
                });
            }
        });

        $scope.$on('$routeUpdate', function() {
            confirmed = false;
            setupNewItem();
        });

        var _close = function() {
            workqueue.remove(_item);
            var active = workqueue.getActive();
            $location.search('_id', active);
            if (!active) {
                $scope.item = null;
                $scope.dirty = null;
            }
        };

        function confirmDirty() {
            return modal.confirm(gettext('There are unsaved changes. Please confirm you want to close the article without saving.'));
        }
    }

    VersioningController.$inject = ['$scope', 'api', '$location', 'notify', 'workqueue'];
    function VersioningController($scope, api, $location, notify, workqueue) {
        $scope.item = null;
        $scope.versions = null;
        $scope.selected = null;
        $scope.users = {};

        $scope.$watch(function() {
            return $location.search()._id;
        }, function(_id) {
            $scope.item = null;
            $scope.versions = null;

            if (_id) {
                fetchItem(_id);
            }
        });

        var fetchUser = function(id) {
            api.users.getById(id)
            .then(function(result) {
                $scope.users[id] = result;
            });
        };

        var fetchItem = function(id) {
            id = id || $scope.item._id;
            return api.archive.getById(id)
            .then(function(result) {
                $scope.item = result;
                return fetchVersions();
            });
        };

        var fetchVersions = function() {
            $scope.users = {};
            return api.archive.getByUrl($scope.item._links.self.href + '?version=all&embedded={"user":1}')
            .then(function(result) {
                _.each(result._items, function(version) {
                    var creator = version.creator || version.original_creator;
                    if (creator && !$scope.users[creator]) {
                        fetchUser(creator);
                    }
                });
                $scope.versions = result;
                $scope.selected = _.find($scope.versions._items, {_version: $scope.item._latest_version});
            });
        };

        $scope.revert = function(version) {
            api.archive.replace($scope.item._links.self.href, {
                type: 'text',
                last_version: $scope.item._version,
                old_version: version
            })
            .then(function(result) {
                notify.success(gettext('Item reverted.'));
                fetchItem()
                .then(function() {
                    workqueue.update($scope.item);
                });
            }, function(result) {
                notify.error(gettext('Error. Item not reverted.'));
            });
        };
    }

    WorkqueueService.$inject = ['storage'];
    function WorkqueueService(storage) {
        /**
         * Set items for further work, in next step of the workflow.
         */

        var queue = storage.getItem('workqueue:items') || [];
        this.length = 0;
        this.active = null;

        /**
         * Add an item into queue
         *
         * it checks if item is in queue already and if yes it will move it to the very end
         *
         * @param {Object} item
         */
        this.add = function(item) {
            _.remove(queue, {_id: item._id});
            queue.unshift(item);
{'fraction_non_alphanumeric': 0.137444279346211, 'fraction_numerical': 0.0004952947003467063, 'mean_word_length': 1.5134854771784232, 'pattern_counts': {'":': 2, '<': 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': '239113', 'n_tokens_mistral': 3288, 'n_tokens_neox': 3151, 'n_words': 637}