Skip to content

Commit

Permalink
add support for more than two elements - fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancahill committed Oct 6, 2015
1 parent d55f020 commit b36b5b1
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions split.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ Split = function (ids, options) {

this.dragging = true;

// Calculate the pairs width, and percentage of the parent width

this.width = this.left.clientWidth + this.right.clientWidth + options.gutterWidth;
this.percentage = Math.min(this.width / this.parent.clientWidth * 100, 100);

this.x = this.left.getBoundingClientRect().left;

this.left.addEventListener('selectstart', preventSelection);
Expand Down Expand Up @@ -75,10 +79,24 @@ Split = function (ids, options) {
offsetX = this.width - this.rightMin;
}

// For first and last pairs, left and right gutter width is half.

var leftGutterWidth = options.gutterWidth;
var rightGutterWidth = options.gutterWidth;

if (this.isFirst) {
leftGutterWidth = options.gutterWidth / 2;
}

if (this.isLast) {
rightGutterWidth = options.gutterWidth / 2;
}

// Left width is the same as offset. Right width is total width - left width.
// Both widths are calculated from the initial parent percentage.

this.left.style.width = 'calc(' + (offsetX / this.width * 100) + '% - ' + options.gutterWidth / 2 + 'px)';
this.right.style.width = 'calc(' + (100 - (offsetX / this.width * 100)) + '% - ' + options.gutterWidth / 2 + 'px)';
this.left.style.width = 'calc(' + (offsetX / this.width * this.percentage) + '% - ' + leftGutterWidth + 'px)';
this.right.style.width = 'calc(' + (this.percentage - (offsetX / this.width * this.percentage)) + '% - ' + rightGutterWidth + 'px)';

if (options.onDrag) {
options.onDrag();
Expand Down Expand Up @@ -116,7 +134,10 @@ Split = function (ids, options) {
}

for (var i = 0; i < ids.length; i++) {
var el = document.getElementById(ids[i]);
var el = document.getElementById(ids[i]),
isFirst = (i == 1),
isLast = (i == ids.length - 1),
gutterWidth = options.gutterWidth;

if (i > 0) {
var pair = {
Expand All @@ -125,7 +146,9 @@ Split = function (ids, options) {
leftMin: options.minWidth[i - 1],
rightMin: options.minWidth[i],
dragging: false,
parent: parent
parent: parent,
isFirst: isFirst,
isLast: isLast
},
gutter = document.createElement('div');

Expand All @@ -143,6 +166,10 @@ Split = function (ids, options) {
pair.gutter = gutter;
}

el.style.width = 'calc(' + options.widths[i] + '% - ' + options.gutterWidth / 2 + 'px)';
if (i == 0 || i == ids.length - 1) {
gutterWidth = options.gutterWidth / 2;
}

el.style.width = 'calc(' + options.widths[i] + '% - ' + gutterWidth + 'px)';
}
};

6 comments on commit b36b5b1

@RodMontgomery
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 138: Should isFirst be (i==0) rather than (i==1)?

@RodMontgomery
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried several cycles of dragging both gutters in the three-pane example left and right?

When I do that, the right edge of the right pane creeps gradually to the left.

Don't know whether this is due to the isFirst i==0 vs i==1 issue or something else.

@RodMontgomery
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More specifically, in the three-pane example:

Call the two gutters GL and GR, for left and right.

From initial position, drag GR to the left. The right edge of the right pane move left. Holding the mouse button down, moving the mouse back and forth, the right edge does not move more, but releasing the mouse button, re-grabbing and dragging left again makes the right edge move left again.

@nathancahill
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i == 1 is because the pairs are only created once i > 0. Will look into the moving problem. My guess is that clientWidth returns whole integers, which we then use to generate float percentages, so over time things will wobble.

Parsing the CSS calc() like you were doing might be better.

@RodMontgomery
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The right edge of the right pane migrates left in the two-pane example, too.

@nathancahill
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a new issue for this here: #3

Please sign in to comment.