-
Notifications
You must be signed in to change notification settings - Fork 194
/
sidebar-content.js
338 lines (288 loc) · 9.91 KB
/
sidebar-content.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
'use strict';
var SearchClient = require('../search-client');
var events = require('../events');
var memoize = require('../util/memoize');
var tabs = require('../tabs');
var uiConstants = require('../ui-constants');
function firstKey(object) {
for (var k in object) {
if (!object.hasOwnProperty(k)) {
continue;
}
return k;
}
return null;
}
/**
* Returns the group ID of the first annotation in `results` whose
* ID is a key in `selection`.
*/
function groupIDFromSelection(selection, results) {
var id = firstKey(selection);
var annot = results.find(function (annot) {
return annot.id === id;
});
if (!annot) {
return null;
}
return annot.group;
}
// @ngInject
function SidebarContentController(
$scope, analytics, annotationUI, annotationMapper, drafts, features, frameSync,
groups, rootThread, settings, streamer, streamFilter, store
) {
var self = this;
function thread() {
return rootThread.thread(annotationUI.getState());
}
var unsubscribeAnnotationUI = annotationUI.subscribe(function () {
var state = annotationUI.getState();
self.rootThread = thread();
self.selectedTab = state.selectedTab;
var separateOrphans = tabs.shouldSeparateOrphans(state);
var counts = tabs.counts(state.annotations, separateOrphans);
Object.assign(self, {
totalNotes: counts.notes,
totalAnnotations: counts.annotations,
totalOrphans: counts.orphans,
waitingToAnchorAnnotations: counts.anchoring > 0,
});
});
$scope.$on('$destroy', unsubscribeAnnotationUI);
function focusAnnotation(annotation) {
var highlights = [];
if (annotation) {
highlights = [annotation.$tag];
}
frameSync.focusAnnotations(highlights);
}
function scrollToAnnotation(annotation) {
if (!annotation) {
return;
}
frameSync.scrollToAnnotation(annotation.$tag);
}
/**
* Returns the Annotation object for the first annotation in the
* selected annotation set. Note that 'first' refers to the order
* of annotations passed to annotationUI when selecting annotations,
* not the order in which they appear in the document.
*/
function firstSelectedAnnotation() {
if (annotationUI.getState().selectedAnnotationMap) {
var id = Object.keys(annotationUI.getState().selectedAnnotationMap)[0];
return annotationUI.getState().annotations.find(function (annot) {
return annot.id === id;
});
} else {
return null;
}
}
var searchClients = [];
function _resetAnnotations() {
annotationMapper.unloadAnnotations(annotationUI.savedAnnotations());
}
function _loadAnnotationsFor(uris, group) {
var searchClient = new SearchClient(store.search, {
// If no group is specified, we are fetching annotations from
// all groups in order to find out which group contains the selected
// annotation, therefore we need to load all chunks before processing
// the results
incremental: !!group,
});
searchClients.push(searchClient);
searchClient.on('results', function (results) {
if (annotationUI.hasSelectedAnnotations()) {
// Focus the group containing the selected annotation and filter
// annotations to those from this group
var groupID = groupIDFromSelection(
annotationUI.getState().selectedAnnotationMap, results);
if (!groupID) {
// If the selected annotation is not available, fall back to
// loading annotations for the currently focused group
groupID = groups.focused().id;
}
results = results.filter(function (result) {
return result.group === groupID;
});
groups.focus(groupID);
}
if (results.length) {
annotationMapper.loadAnnotations(results);
}
});
searchClient.on('end', function () {
// Remove client from list of active search clients.
//
// $evalAsync is required here because search results are emitted
// asynchronously. A better solution would be that the loading state is
// tracked as part of the app state.
$scope.$evalAsync(function () {
searchClients.splice(searchClients.indexOf(searchClient), 1);
});
annotationUI.frames().forEach(function (frame) {
if (0 <= uris.indexOf(frame.uri)) {
annotationUI.updateFrameAnnotationFetchStatus(frame.uri, true);
}
});
});
searchClient.get({uri: uris, group: group});
}
function isLoading() {
if (!annotationUI.frames().some(function (frame) { return frame.uri; })) {
// The document's URL isn't known so the document must still be loading.
return true;
}
if (searchClients.length > 0) {
// We're still waiting for annotation search results from the API.
return true;
}
return false;
}
/**
* Load annotations for all URLs associated with `frames`.
*/
function loadAnnotations() {
_resetAnnotations();
searchClients.forEach(function (client) {
client.cancel();
});
// If there is no selection, load annotations only for the focused group.
//
// If there is a selection, we load annotations for all groups, find out
// which group the first selected annotation is in and then filter the
// results on the client by that group.
//
// In the common case where the total number of annotations on
// a page that are visible to the user is not greater than
// the batch size, this saves an extra roundtrip to the server
// to fetch the selected annotation in order to determine which group
// it is in before fetching the remaining annotations.
var group = annotationUI.hasSelectedAnnotations() ?
null : groups.focused().id;
var searchUris = annotationUI.searchUris();
if (searchUris.length > 0) {
_loadAnnotationsFor(searchUris, group);
streamFilter.resetFilter().addClause('/uri', 'one_of', searchUris);
streamer.setConfig('filter', {filter: streamFilter.getFilter()});
}
}
$scope.$on('sidebarOpened', function () {
analytics.track(analytics.events.SIDEBAR_OPENED);
streamer.connect();
});
// If the user is logged in, we connect nevertheless
if (this.auth.status === 'logged-in') {
streamer.connect();
}
$scope.$on(events.USER_CHANGED, function () {
streamer.reconnect();
});
$scope.$on(events.ANNOTATIONS_SYNCED, function (event, tags) {
// When a direct-linked annotation is successfully anchored in the page,
// focus and scroll to it
var selectedAnnot = firstSelectedAnnotation();
if (!selectedAnnot) {
return;
}
var matchesSelection = tags.some(function (tag) {
return tag === selectedAnnot.$tag;
});
if (!matchesSelection) {
return;
}
focusAnnotation(selectedAnnot);
scrollToAnnotation(selectedAnnot);
var separateOrphans = tabs.shouldSeparateOrphans(annotationUI.getState());
annotationUI.selectTab(tabs.tabForAnnotation(selectedAnnot, separateOrphans));
});
$scope.$on(events.GROUP_FOCUSED, function () {
// The focused group may be changed during loading annotations as a result
// of switching to the group containing a direct-linked annotation.
//
// In that case, we don't want to trigger reloading annotations again.
if (isLoading()) {
return;
}
annotationUI.clearSelectedAnnotations();
loadAnnotations();
});
// Watch anything that may require us to reload annotations.
$scope.$watch(function () {
return annotationUI.frames().map(function(frame) {
return frame.uri;
});
}, loadAnnotations, true);
this.setCollapsed = function (id, collapsed) {
annotationUI.setCollapsed(id, collapsed);
};
this.forceVisible = function (thread) {
annotationUI.setForceVisible(thread.id, true);
if (thread.parent) {
annotationUI.setCollapsed(thread.parent.id, false);
}
};
this.focus = focusAnnotation;
this.scrollTo = scrollToAnnotation;
this.selectedAnnotationCount = function () {
var selection = annotationUI.getState().selectedAnnotationMap;
if (!selection) {
return 0;
}
return Object.keys(selection).length;
};
this.selectedAnnotationUnavailable = function () {
var selectedID = firstKey(annotationUI.getState().selectedAnnotationMap);
return !isLoading() &&
!!selectedID &&
!annotationUI.annotationExists(selectedID);
};
this.shouldShowLoggedOutMessage = function () {
// If user is not logged out, don't show CTA.
if (self.auth.status !== 'logged-out') {
return false;
}
// If user has not landed on a direct linked annotation
// don't show the CTA.
if (!settings.annotations) {
return false;
}
// The user is logged out and has landed on a direct linked
// annotation. If there is an annotation selection and that
// selection is available to the user, show the CTA.
var selectedID = firstKey(annotationUI.getState().selectedAnnotationMap);
return !isLoading() &&
!!selectedID &&
annotationUI.annotationExists(selectedID);
};
this.isLoading = isLoading;
var visibleCount = memoize(function (thread) {
return thread.children.reduce(function (count, child) {
return count + visibleCount(child);
}, thread.visible ? 1 : 0);
});
this.visibleCount = function () {
return visibleCount(thread());
};
this.topLevelThreadCount = function () {
return thread().totalChildren;
};
this.clearSelection = function () {
var selectedTab = annotationUI.getState().selectedTab;
if (!annotationUI.getState().selectedTab || annotationUI.getState().selectedTab === uiConstants.TAB_ORPHANS) {
selectedTab = uiConstants.TAB_ANNOTATIONS;
}
annotationUI.clearSelectedAnnotations();
annotationUI.selectTab(selectedTab);
};
}
module.exports = {
controller: SidebarContentController,
controllerAs: 'vm',
bindings: {
auth: '<',
search: '<',
},
template: require('../templates/sidebar-content.html'),
};