Skip to content

Commit

Permalink
only use page[XY]Offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 25, 2018
1 parent 836cc36 commit a5cc451
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
16 changes: 11 additions & 5 deletions src/generators/nodes/Window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const associatedEvents = {
scrollY: 'scroll',
};

const properties = {
scrollX: 'pageXOffset',
scrollY: 'pageYOffset'
};

const readonly = new Set([
'innerWidth',
'innerHeight',
Expand Down Expand Up @@ -93,15 +98,16 @@ export default class Window extends Node {
if (attribute.name === 'online') return;

const associatedEvent = associatedEvents[attribute.name];
const property = properties[attribute.name] || attribute.name;

if (!events[associatedEvent]) events[associatedEvent] = [];
events[associatedEvent].push(
`${attribute.value.name}: this.${attribute.name}`
`${attribute.value.name}: this.${property}`
);

// add initial value
generator.metaBindings.push(
`this._state.${attribute.value.name} = window.${attribute.name};`
`this._state.${attribute.value.name} = window.${property};`
);
}
});
Expand Down Expand Up @@ -158,10 +164,10 @@ export default class Window extends Node {
clearTimeout(${timeout});
var x = ${bindings.scrollX
? `#component.get("${bindings.scrollX}")`
: `window.pageXOffset || window.scrollX`};
: `window.pageXOffset`};
var y = ${bindings.scrollY
? `#component.get("${bindings.scrollY}")`
: `window.pageYOffset || window.scrollY`};
: `window.pageYOffset`};
window.scrollTo(x, y);
${timeout} = setTimeout(${clear}, 100);
}
Expand All @@ -182,7 +188,7 @@ export default class Window extends Node {
#component.observe("${bindings.scrollX || bindings.scrollY}", function(${isX ? 'x' : 'y'}) {
${lock} = true;
clearTimeout(${timeout});
window.scrollTo(${isX ? 'x, window.pageYOffset || window.scrollY' : 'window.pageXOffset || window.scrollX, y'});
window.scrollTo(${isX ? 'x, window.pageYOffset' : 'window.pageXOffset, y'});
${timeout} = setTimeout(${clear}, 100);
});
`);
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/window-binding-scroll/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function create_main_fragment(state, component) {
window_updating = true;

component.set({
y: this.pageYOffset || this.scrollY
y: this.pageYOffset
});
window_updating = false;
}
Expand All @@ -207,7 +207,7 @@ function create_main_fragment(state, component) {
component.observe("y", function(y) {
window_updating = true;
clearTimeout(window_updating_timeout);
window.scrollTo(window.pageXOffset || window.scrollX, y);
window.scrollTo(window.pageXOffset, y);
window_updating_timeout = setTimeout(clear_window_updating, 100);
});

Expand Down Expand Up @@ -243,7 +243,7 @@ function create_main_fragment(state, component) {
function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);
this._state.y = window.pageYOffset || window.scrollY;
this._state.y = window.pageYOffset;

this._fragment = create_main_fragment(this._state, this);

Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/window-binding-scroll/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function create_main_fragment(state, component) {
window_updating = true;

component.set({
y: this.pageYOffset || this.scrollY
y: this.pageYOffset
});
window_updating = false;
}
Expand All @@ -18,7 +18,7 @@ function create_main_fragment(state, component) {
component.observe("y", function(y) {
window_updating = true;
clearTimeout(window_updating_timeout);
window.scrollTo(window.pageXOffset || window.scrollX, y);
window.scrollTo(window.pageXOffset, y);
window_updating_timeout = setTimeout(clear_window_updating, 100);
});

Expand Down Expand Up @@ -54,7 +54,7 @@ function create_main_fragment(state, component) {
function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);
this._state.y = window.pageYOffset || window.scrollY;
this._state.y = window.pageYOffset;

this._fragment = create_main_fragment(this._state, this);

Expand Down
4 changes: 2 additions & 2 deletions test/runtime/samples/window-bind-scroll-update/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ export default {
skip: true, // JSDOM

test ( assert, component, target, window ) {
assert.equal( window.pageYOffset || window.scrollY, 0 );
assert.equal( window.pageYOffset, 0 );

component.set({ scrollY: 100 });
assert.equal( window.pageYOffset || window.scrollY, 100 );
assert.equal( window.pageYOffset, 100 );

component.destroy();
}
Expand Down

0 comments on commit a5cc451

Please sign in to comment.