Commit c81fea3c authored by Robert Knight's avatar Robert Knight

Avoid emitting GROUP_FOCUSED event if the group has not changed

widget-controller.js triggers a reload of annotations whenever a
GROUP_FOCUSED event is emitted. Avoid reloading annotations
unnecessarily if group.focus() is called with the currently focused
group.
parent 23256279
......@@ -25,7 +25,7 @@ function groups(localStorage, session, settings, $rootScope, $http) {
function all() {
return session.state.groups || [];
};
}
// Return the full object for the group with the given id.
function get(id) {
......@@ -35,7 +35,7 @@ function groups(localStorage, session, settings, $rootScope, $http) {
return gs[i];
}
}
};
}
/** Leave the group with the given ID.
* Returns a promise which resolves when the action completes.
......@@ -51,7 +51,7 @@ function groups(localStorage, session, settings, $rootScope, $http) {
// by optimistically updating the session state
return response;
};
}
/** Return the currently focused group. If no group is explicitly focused we
......@@ -72,13 +72,16 @@ function groups(localStorage, session, settings, $rootScope, $http) {
/** Set the group with the passed id as the currently focused group. */
function focus(id) {
var prevFocused = focused();
var g = get(id);
if (g) {
focusedGroup = g;
localStorage.setItem(STORAGE_KEY, g.id);
if (prevFocused.id !== g.id) {
$rootScope.$broadcast(events.GROUP_FOCUSED, g.id);
}
}
}
// reset the focused group if the user leaves it
$rootScope.$on(events.GROUPS_CHANGED, function () {
......
......@@ -45,7 +45,7 @@ describe('groups', function() {
}
}
};
fakeHttp = sandbox.stub()
fakeHttp = sandbox.stub();
});
afterEach(function () {
......@@ -134,7 +134,7 @@ describe('groups', function() {
});
});
describe('.focus() method', function() {
describe('.focus()', function() {
it('sets the focused group to the named group', function() {
var s = service();
s.focus('id2');
......@@ -155,6 +155,20 @@ describe('groups', function() {
assert.calledWithMatch(fakeLocalStorage.setItem, sinon.match.any, 'id3');
});
it('emits the GROUP_FOCUSED event if the focused group changed', function () {
var s = service();
s.focus('id3');
assert.calledWith(fakeRootScope.$broadcast, events.GROUP_FOCUSED, 'id3');
});
it('does not emit GROUP_FOCUSED if the focused group did not change', function () {
var s = service();
s.focus('id3');
fakeRootScope.$broadcast = sinon.stub();
s.focus('id3');
assert.notCalled(fakeRootScope.$broadcast);
});
});
describe('.leave()', function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment