forked from guardian/grid
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgr-image-usage.js
More file actions
135 lines (114 loc) · 4.02 KB
/
Copy pathgr-image-usage.js
File metadata and controls
135 lines (114 loc) · 4.02 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
import angular from 'angular';
import moment from 'moment';
import '../../util/rx';
import template from './gr-image-usage.html';
import usageTemplate from './gr-image-usage-list.html';
import './gr-image-usage.css';
import '../../services/image/usages';
import {deleteUsages} from '../gr-delete-usages/gr-delete-usages';
import '../gr-image-usage-photosales/gr-image-usage-photosales';
import {sendToCaptureUsagePanelTxt} from "../../util/constants/sendToCapture-config";
import {displayLinkedImagesMessage, linkedImagesMessage} from "../../util/linkedImages";
export const module = angular.module('gr.imageUsage', [
'gr.image-usages.service',
'gr.imageUsagePhotoSales',
'util.rx',
deleteUsages.name
]);
module.controller('grImageUsageCtrl', [
'$scope',
'$state',
'inject$',
'$window',
'imageUsagesService',
function ($scope, $state, inject$, $window, imageUsagesService) {
const ctrl = this;
ctrl.$onInit = () => {
ctrl.showSendToPhotoSales = $window._clientConfig.showSendToPhotoSales;
const org = $window._clientConfig.staffPhotographerOrganisation;
ctrl.displayLinkedImagesMessage = displayLinkedImagesMessage(ctrl.image, org);
ctrl.linkedImagesMessage = linkedImagesMessage(ctrl.image, org);
const usages = imageUsagesService.getUsages(ctrl.image);
const usages$ = usages.groupedByState$.map((u) => u.toJS());
const usagesCount$ = usages.count$;
// TODO match on `platform` rather than `type` as `platform` includes more detail
ctrl.usageTypeToName = (usageType) => {
switch (usageType) {
case 'removed':
return 'Taken down';
case 'pending':
return 'Pending publication';
case 'published':
return 'Published';
case 'downloaded':
return 'Downloads';
case 'unknown':
return 'Front'; // currently only fronts have an `unknown` type, see TODO above
default:
return usageType;
}
};
ctrl.photoSalesUsages = () => {
const processedUsages = [];
ctrl.image.data.usages.data.forEach( (usage) => {
if (usage.data.platform === "syndication" && usage.data.syndicationUsageMetadata.partnerName === "Capture") {
processedUsages.push({
title: usage.data.syndicationUsageMetadata.syndicatedBy,
usageName: sendToCaptureUsagePanelTxt,
usageType: "sendToPhotoSales",
dateAdded: usage.data.dateAdded
});
}
});
return processedUsages;
};
ctrl.onUsagesDeleted = () => {
// a bit nasty - but it updates the state of the page better than trying to do that in
// the client.
$state.go('image', {imageId: ctrl.image.data.id, crop: undefined}, {reload: true});
};
const hasSyndicationUsages$ =
imageUsagesService.getUsages(ctrl.image).hasSyndicationUsages$;
inject$($scope, usages$, ctrl, 'usages');
inject$($scope, usagesCount$, ctrl, 'usagesCount');
inject$($scope, hasSyndicationUsages$, ctrl, 'hasSyndicationUsages');
};
}]);
module.directive('grImageUsage', [function() {
return {
restrict: 'E',
template: template,
controller: 'grImageUsageCtrl',
controllerAs: 'ctrl',
bindToController: true,
scope: {
image: '=grImage'
}
};
}]);
module.controller('grImageUsageListCtrl', [
'imageUsagesService',
function (imageUsagesService) {
const ctrl = this;
ctrl.formatTimestamp = (timestamp) => {
return moment(timestamp).fromNow();
};
ctrl.isRecent = (timestamp) => {
const nowtime = new Date();
return moment(timestamp)
.isAfter(moment(nowtime).subtract(imageUsagesService.recentTime, 'days'));
};
}]);
module.directive('grImageUsageList', [function () {
return {
restrict: 'E',
template: usageTemplate,
controller: 'grImageUsageListCtrl',
controllerAs: 'ctrl',
bindToController: true,
scope: {
type: '=',
usages: '='
}
};
}]);