-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathgr-archiver.js
More file actions
165 lines (146 loc) · 5.25 KB
/
Copy pathgr-archiver.js
File metadata and controls
165 lines (146 loc) · 5.25 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import angular from 'angular';
import {Set} from 'immutable';
import './gr-archiver.css';
import template from './gr-archiver.html';
import '../../services/archive';
import '../../services/image-accessor';
import '../../util/string';
import {getDeletedState} from "../../util/get-deleted-state";
export const module = angular.module('gr.archiver', [
'kahuna.services.archive',
'kahuna.services.image-accessor',
'kahuna.services.image-logic',
'util.string',
getDeletedState.name
]);
module.controller('grArchiverCtrl', [
'$scope',
'$window',
'archiveService',
'imageAccessor',
'imageLogic',
'humanJoin',
'mediaApi',
'getDeletedState',
'apiPoll',
function ($scope,
$window,
archiveService,
imageAccessor,
imageLogic,
humanJoin,
mediaApi,
getDeletedState,
apiPoll) {
const ctrl = this;
ctrl.archivedState = 'unarchived';
ctrl.archiving = false;
ctrl.canArchive = false;
ctrl.canUndelete = false;
ctrl.isDeleted = false;
ctrl.undeleting = false;
ctrl.undelete = undelete;
ctrl.$onInit = () => {
getDeletedState(ctrl);
};
mediaApi.canUserArchive().then(canArchive => {
ctrl.canArchive = canArchive;
});
$scope.$watchCollection(getImageArray, (images) => {
const noneCanBeArchived = ! images.some(imageLogic.canBeArchived);
const allPersisted = images.every(imageAccessor.isPersisted);
ctrl.archivedState = noneCanBeArchived ? 'kept' :
allPersisted ? 'archived' : 'unarchived';
const explTokens = listAllPersistenceExplanations(images);
let explanation, reason;
if (images.length === 1) {
reason = humanJoin(explTokens, 'and');
explanation = `Kept in Library because the image has been ${reason}.`;
} else {
reason = humanJoin(explTokens, 'or');
explanation = `Kept in Library because the images have been ${reason}.`;
}
ctrl.archivedExplanation = explanation;
});
ctrl.archive = () => {
ctrl.archiving = true;
archiveService.batchArchive(getImageArray())
.catch(() => {
$window.alert('Failed to add to Library, please try again.');
})
.finally(() => {
ctrl.archiving = false;
});
};
ctrl.unarchive = () => {
ctrl.archiving = true;
archiveService.batchUnarchive(getImageArray())
.catch(() => {
$window.alert('Failed to remove from Library, please try again.');
})
.finally(() => {
ctrl.archiving = false;
});
};
function getImageArray() {
if (ctrl.images && ! ctrl.image) {
// Convert from Immutable Set to JS Array
return Array.from(ctrl.images);
} else if (! ctrl.images && ctrl.image) {
return [ctrl.image];
} else {
throw new Error('Either gr:images or gr:image should be provided ' +
'to gr-archiver directive');
}
}
function listAllPersistenceExplanations(images) {
return images.
map(imageLogic.getPersistenceExplanation).
map(items => new Set(items)).
reduce((all, items) => all.union(items)).
toArray();
}
function pollUndeleted(imageId) {
const findImage = () => mediaApi.find(imageId).then(
(i) => i.data?.softDeletedMetadata === undefined && i.data.userMetadata?.data?.archived
? Promise.resolve()
: Promise.reject()
);
return apiPoll(findImage);
}
function undelete() {
ctrl.undeleting = true;
const imageId = ctrl.image.data.id;
mediaApi.undelete(imageId)
.then(() => pollUndeleted(imageId))
.then(() => {
ctrl.canUndelete = false;
ctrl.isDeleted = false;
ctrl.image.softDeletedMetadata = undefined;
if (ctrl.image.userMetadata?.data !== undefined) {
ctrl.image.userMetadata.data.archived = true;
}
$scope.$emit('images-updated', [ctrl.image]);
}).catch((e) => {
$window.alert('Failed to undelete image!, please try again.');
console.error('Image undeletion failed', e);
}).finally(() => {
ctrl.undeleting = false;
});
}
}
]);
module.directive('grArchiver', [function () {
return {
restrict: 'E',
template: template,
scope: {
// Either is allowed, not both
image: '=grImage',
images: '=grImages'
},
controller: 'grArchiverCtrl',
controllerAs: 'ctrl',
bindToController: true
};
}]);