File size: 2,922 Bytes
7f88cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(function() {

'use strict';

VersioningController.$inject = ['$scope', 'authoring', 'api', 'notify', 'lock', 'desks', 'archiveService'];
function VersioningController($scope, authoring, api, notify, lock, desks, archiveService) {

    $scope.last = null;
    $scope.versions = null;
    $scope.selected = null;
    $scope.users = null;
    $scope.canRevert = false;
    $scope.desks = null;
    $scope.stages = null;

    function fetchVersions() {
        desks.initialize()
            .then(function() {
                $scope.desks = desks.desks;
                $scope.stages = desks.deskStages;
                $scope.users = desks.users;

                archiveService.getVersionHistory($scope.item, desks, 'versions').then(function(versions) {
                    $scope.versions = versions;
                    $scope.last = archiveService.lastVersion($scope.item, $scope.versions);

                    if (archiveService.isLegal($scope.item)) {
                        $scope.canRevert =  false;
                        $scope.openVersion($scope.last);
                    } else {
                        $scope.canRevert =  authoring.isEditable($scope.item) && !authoring.isPublished($scope.item);

                        if ($scope.item._autosave) {
                            $scope.selected = $scope.item._autosave;
                        } else {
                            $scope.openVersion($scope.last);
                        }
                    }
                });
            });
    }

    /**
     * Opens the given version for preview if the story is not editable
     * Or if editable but not dirty.
     * Then the last version can be edited only when editable and not dirty.
     */
    $scope.openVersion = function (version) {
        if (!$scope.item._editable) {
            $scope.preview(version);
        } else if ($scope.item._editable && !$scope.dirty) {
            $scope.selected = version;
            if (version === $scope.last && !$scope.item._autosave) {
                if (!$scope._editable) {
                    $scope.closePreview();
                }
            } else {
                $scope.preview(version);
            }
        }
    };

    /**
     * Revert to given version
     *
     * If the version is the last one and there is an autosave - drop autosave
     */
    $scope.revert = function(version) {
        $scope.$parent.revert(version).then(fetchVersions);
    };

    $scope.$watchGroup(['item._id', 'item._latest_version'], fetchVersions);
}

versioningVersionDirective.$inject = [];
function versioningVersionDirective() {
    return {
        templateUrl: 'scripts/superdesk-authoring/versioning/versions/views/versions.html'
    };
}

angular.module('superdesk.authoring.versioning.versions', [])
    .directive('sdVersioningVersion', versioningVersionDirective)
    .controller('VersioningWidgetCtrl', VersioningController);
})();