2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2024-11-13 07:05:32 +01:00
|
|
|
const {zrequire} = require("./lib/namespace.cjs");
|
|
|
|
const {run_test} = require("./lib/test.cjs");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const scroll_util = zrequire("scroll_util");
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("scroll_delta", () => {
|
2018-04-24 14:09:16 +02:00
|
|
|
// If we are entirely on-screen, don't scroll
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
0,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: 1,
|
|
|
|
elem_bottom: 9,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
0,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: -5,
|
|
|
|
elem_bottom: 15,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
|
|
|
// The top is offscreen.
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
-3,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: -3,
|
|
|
|
elem_bottom: 5,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
-3,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: -3,
|
|
|
|
elem_bottom: -1,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
-11,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: -150,
|
|
|
|
elem_bottom: -1,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
|
|
|
// The bottom is offscreen.
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
3,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: 7,
|
|
|
|
elem_bottom: 13,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
3,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: 11,
|
|
|
|
elem_bottom: 13,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-04-24 14:09:16 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
11,
|
|
|
|
scroll_util.scroll_delta({
|
|
|
|
elem_top: 11,
|
|
|
|
elem_bottom: 99,
|
|
|
|
container_height: 10,
|
|
|
|
}),
|
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-24 14:28:24 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("scroll_element_into_container", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = (function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
let top = 3;
|
2018-04-24 14:28:24 +02:00
|
|
|
return {
|
|
|
|
height: () => 100,
|
|
|
|
scrollTop: (arg) => {
|
|
|
|
if (arg === undefined) {
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
top = arg;
|
2020-09-24 07:50:36 +02:00
|
|
|
return this;
|
2018-04-24 14:28:24 +02:00
|
|
|
},
|
2023-04-25 18:01:02 +02:00
|
|
|
__zjquery: true,
|
2018-04-24 14:28:24 +02:00
|
|
|
};
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-04-24 14:28:24 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem1 = {
|
2018-07-22 06:03:57 +02:00
|
|
|
innerHeight: () => 25,
|
2020-07-02 01:41:40 +02:00
|
|
|
position: () => ({
|
|
|
|
top: 0,
|
|
|
|
}),
|
2018-04-24 14:28:24 +02:00
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
scroll_util.scroll_element_into_container($elem1, $container);
|
|
|
|
assert.equal($container.scrollTop(), 3);
|
2018-04-24 14:28:24 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem2 = {
|
2018-07-22 06:03:57 +02:00
|
|
|
innerHeight: () => 15,
|
2020-07-02 01:41:40 +02:00
|
|
|
position: () => ({
|
|
|
|
top: 250,
|
|
|
|
}),
|
2018-04-24 14:28:24 +02:00
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
scroll_util.scroll_element_into_container($elem2, $container);
|
|
|
|
assert.equal($container.scrollTop(), 250 - 100 + 3 + 15);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|