Unverified Commit e1d17fe9 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #1851 from hypothesis/remove-window-scroll-directive

Remove non-functional `window-scroll` Angular directive
parents eb8d8687 015016c0
......@@ -62,8 +62,6 @@ function StreamContentController(
// Sort the stream so that the newest annotations are at the top
store.setSortKey('Newest');
this.loadMore = fetch;
}
export default {
......
import angular from 'angular';
const inject = angular.mock.inject;
import windowScroll from '../window-scroll';
describe('windowScroll', function() {
let directive = null;
let doc = null;
let html = null;
let view = null;
let scope = null;
let elem = null;
let attr = null;
beforeEach(inject(function($injector) {
directive = $injector.invoke(windowScroll);
html = {};
view = {
addEventListener: sinon.spy(),
removeEventListener: sinon.spy(),
};
doc = { documentElement: html, defaultView: view };
scope = { $apply: sinon.stub().yields(), $on: sinon.stub() };
elem = {
prop: sinon
.stub()
.withArgs('ownerDocument')
.returns(doc),
};
attr = { windowScroll: sinon.stub() };
directive.link(scope, elem, attr);
}));
it('installs a scroll handler on the window', function() {
assert.calledOnce(view.addEventListener);
assert.calledWith(view.addEventListener, 'scroll', sinon.match.func);
assert.calledOnce(scope.$on);
assert.calledWith(scope.$on, '$destroy', sinon.match.func);
scope.$on.firstCall.args[1]();
const handler = view.addEventListener.firstCall.args[1];
assert.calledOnce(view.removeEventListener);
assert.calledWith(view.removeEventListener, 'scroll', handler);
});
describe('attribute argument', function() {
let callback = null;
let handler = null;
beforeEach(function() {
callback = attr.windowScroll;
handler = view.addEventListener.firstCall.args[1];
html.clientHeight = 100;
html.scrollHeight = 1000;
view.scrollY = 0;
});
it('is not called when scrolling near the top of the view', function() {
handler();
assert.notCalled(callback);
});
it('is not called when scrolling near the middle of the view', function() {
handler();
view.scrollY = 500;
handler();
assert.notCalled(callback);
});
it('is called when one screen remains', function() {
handler();
view.scrollY = 800;
handler();
assert.calledOnce(callback);
});
describe('throttle', function() {
it('prevents extraneous calls', function() {
view.scrollY = 800;
handler();
handler();
assert.calledOnce(callback);
});
it('allows calls after the view grows', function() {
view.scrollY = 800;
handler();
assert.calledOnce(callback);
html.scrollHeight = 2000;
handler();
assert.calledOnce(callback);
view.scrollY = 1800;
handler();
assert.calledTwice(callback);
});
});
});
});
export default function windowScrollDirective() {
return {
link: function(scope, elem, attr) {
let active = true;
const html = elem.prop('ownerDocument').documentElement;
const view = elem.prop('ownerDocument').defaultView;
function onScroll() {
const clientHeight = html.clientHeight;
const scrollHeight = html.scrollHeight;
if (view.scrollY + clientHeight >= scrollHeight - clientHeight) {
if (active) {
active = false;
scope.$apply(attr.windowScroll);
}
} else {
active = true;
}
}
view.addEventListener('scroll', onScroll, false);
scope.$on('$destroy', function() {
view.removeEventListener('scroll', onScroll);
});
},
};
}
......@@ -160,10 +160,6 @@ import sidebarContent from './components/sidebar-content';
import streamContent from './components/stream-content';
import threadList from './components/thread-list';
// Angular directives.
import windowScrollDirective from './directive/window-scroll';
// Services.
import bridgeService from '../shared/bridge';
......@@ -293,7 +289,6 @@ function startAngularApp(config) {
.component('tagList', wrapComponent(TagList))
.component('threadList', threadList)
.component('topBar', wrapComponent(TopBar))
.directive('windowScroll', windowScrollDirective)
// Register services, the store and utilities with Angular, so that
// Angular components can use them.
......
<span window-scroll="vm.loadMore(20)">
<thread-list
<thread-list
on-change-collapsed="vm.setCollapsed(id, collapsed)"
show-document-info="true"
thread="vm.rootThread()">
</thread-list>
</span>
</thread-list>
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