Skip to content

Instantly share code, notes, and snippets.

@robertknight
Last active February 28, 2017 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertknight/43b20d83ba4c36c031c55a24afe0b0b3 to your computer and use it in GitHub Desktop.
Save robertknight/43b20d83ba4c36c031c55a24afe0b0b3 to your computer and use it in GitHub Desktop.
Computing visible annotation tags and notifying host page
diff --git a/src/sidebar/frame-sync.js b/src/sidebar/frame-sync.js
index 8a80563..6a505d7 100644
--- a/src/sidebar/frame-sync.js
+++ b/src/sidebar/frame-sync.js
@@ -2,6 +2,7 @@
var events = require('./events');
var bridgeEvents = require('../shared/bridge-events');
+var memoize = require('./util/memoize');
var metadata = require('./annotation-metadata');
var uiConstants = require('./ui-constants');
@@ -34,12 +35,27 @@ function formatAnnot(ann) {
}
/**
+ * Return the tags of annotations displayed in a list of threads.
+ *
+ * @param {Thread[]} threads - A list of visible threads
+ * @return {string[]} List of annotation tags
+ */
+var annotationTags = memoize(function (threads) {
+ return threads.reduce(function (tags, thread) {
+ if (thread.annotation) {
+ tags.push(thread.annotation.$tag);
+ }
+ return tags;
+ }, []);
+});
+
+/**
* This service runs in the sidebar and is responsible for keeping the set of
* annotations displayed in connected frames in sync with the set shown in the
* sidebar.
*/
// @ngInject
-function FrameSync($rootScope, $window, Discovery, annotationUI, bridge) {
+function FrameSync($rootScope, $window, Discovery, annotationUI, bridge, rootThread) {
// Set of tags of annotations that are currently loaded into the frame
var inFrame = new Set();
@@ -51,13 +67,18 @@ function FrameSync($rootScope, $window, Discovery, annotationUI, bridge) {
function setupSyncToFrame() {
// List of loaded annotations in previous state
var prevAnnotations = [];
+ var prevFilterQuery = null;
var prevFrames = [];
var prevPublicAnns = 0;
+ var prevSelectedTab = null;
+ var prevVisibleAnns = [];
annotationUI.subscribe(function () {
var state = annotationUI.getState();
if (state.annotations === prevAnnotations &&
- state.frames === prevFrames) {
+ state.frames === prevFrames &&
+ state.filterQuery === prevFilterQuery &&
+ state.selectedTab === prevSelectedTab) {
return;
}
@@ -109,6 +130,21 @@ function FrameSync($rootScope, $window, Discovery, annotationUI, bridge) {
}
}
}
+
+ // Notify the page which annotations are currently visible in the sidebar
+ prevFilterQuery = state.filterQuery;
+ prevSelectedTab = state.selectedTab;
+ var visibleAnns = annotationTags(rootThread.thread(state).children);
+ if (prevVisibleAnns !== visibleAnns) {
+
+ // TODO - Trigger hiding/showing of anns in the page depending on
+ // their visibility.
+
+ prevVisibleAnns = visibleAnns;
+ }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment