Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Multiple adjust should keep points #53

Merged
merged 3 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/align/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,24 @@ function doAlign(el, tgtRegion, align, isTgtRegionVisible) {
// 检查反下后的位置是否可以放下了,如果仍然放不下:
// 1. 复原修改过的定位参数
if (isStillFailX || isStillFailY) {
points = align.points;
let newPoints = points;

// 重置对应部分的翻转逻辑
if (isStillFailX) {
newPoints = flip(points, /[lr]/gi, {
l: 'r',
r: 'l',
});
}
if (isStillFailY) {
newPoints = flip(points, /[tb]/gi, {
t: 'b',
b: 't',
});
}

points = newPoints;

offset = align.offset || [0, 0];
targetOffset = align.targetOffset || [0, 0];
}
Expand Down
54 changes: 52 additions & 2 deletions tests/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ describe('dom-align', () => {
points: ['bl', 'tl'],
});
// |------|
// | _____|________
// |______|_______
// | | |
// | | |
// | | |
Expand Down Expand Up @@ -548,7 +548,7 @@ describe('dom-align', () => {
expect(target.offset().top - containerOffset.top).within(-5, 5);
});

it('should consider offset when caculate realXRegion and realYRegion', () => {
it('should consider offset when calculate realXRegion and realYRegion', () => {
if (navigator.userAgent.toLowerCase().indexOf('phantomjs') !== -1) {
return;
}
Expand All @@ -574,6 +574,56 @@ describe('dom-align', () => {
expect(source.offset().top + 9 - target.offset().top).within(-5, 5);
expect(source.offset().left + 9 - target.offset().left).within(-5, 5);
});

it('should have correct points', () => {
if (navigator.userAgent.toLowerCase().indexOf('phantomjs') !== -1) {
return;
}

// 让 src 放不下,偏移到右侧
// |-----|
// | | TOP |
// | |-----|
// | src | tgt |
// | |-----|
// | |
// |-----|

// To

// TOP |-----|
// |-----| |
// | tgt | |
// |-----| src |
// | |
// | |
// |-----|

const node = $(`
<div style="position: absolute; width: 100px; height: 100px;">
<div style="position: absolute; top: 10px; left:0; width: 50px; height: 50px;">
</div>
<div style="position: absolute; width: 50px; height: 100px"></div>
</div>
`).appendTo('body');
const source = node.children().eq(1);
const target = node.children().eq(0);

const result = domAlign(source[0], target[0], {
points: ['cr', 'cl'],
offset: [0, 0],
overflow: {
adjustX: true,
adjustY: true,
},
});

expect(result.points).to.eql(['cl', 'cr']);
expect(source.offset().top).within(-5, 5);
expect(source.offset().left - target.width()).within(-5, 5);

node.remove();
});
});
});
}
Expand Down