Skip to content

Commit

Permalink
Merge pull request vitmalina#1069 from data-experts/master
Browse files Browse the repository at this point in the history
Improvement of toolbar scrolling
  • Loading branch information
vitmalina committed Oct 25, 2015
2 parents adb2788 + 87e64de commit 84b7531
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
15 changes: 12 additions & 3 deletions src/less/src/toolbar.less
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@
.opacity(0.3)
}

&.overflowed {
.w2ui-toolbar-scroll-left,
&.overflowed-left {
.w2ui-toolbar-scroll-left {
display: block;
}

.w2ui-toolbar-scroll-wrapper > table {
padding-left: 18px;
}
}

&.overflowed-right {
.w2ui-toolbar-scroll-right {
display: block;
}

.w2ui-toolbar-scroll-wrapper > table {
padding: 0 18px;
padding-right: 18px;
}
}

Expand Down
73 changes: 64 additions & 9 deletions src/w2toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* - added button types: menu-check, menu-radio - will save into item.selected
* - item.text and item.html - can be functions now (or string), where this keyword is the item
* - item.style - style for caption in the button
* - item.check
* - item.check
*
************************************************************************/

Expand Down Expand Up @@ -427,15 +427,59 @@
},

scroll: function (direction) {
var box = $(this.box).children('.w2ui-toolbar-scroll-wrapper');
var left = box.scrollLeft();
var box = $(this.box);
var scrollBox = box.children('.w2ui-toolbar-scroll-wrapper');
var scrollLeft = scrollBox.scrollLeft();
var item, shift, tmp;

switch (direction) {
case 'left':
box.scrollLeft(left - 10);
box.addClass('overflowed-right');
tmp = parseInt(box.find('table').first().css('padding-right'));
shift = -scrollLeft;

for (var i = this.items.length - 1; i >= 0; i--) {
// search last invisible break or spacer from left side
if (['break', 'spacer'].indexOf(this.items[i].type) >= 0) {
item = $('#tb_' + this.name + '_item_' + this.items[i].id);
if (item.position().left > 0) continue;
shift = item.position().left - tmp + item.width();
break;
}
}

// cut scroll shift to max page size
tmp = box.width() - tmp - parseInt(box.find('table').first().css('padding-left'));
shift = Math.abs(shift) > tmp ? -tmp : shift;

scrollBox.scrollLeft(scrollLeft + shift);
if (scrollBox.scrollLeft() <= 0) {
box.removeClass('overflowed-left');
}
break;
case 'right':
box.scrollLeft(left + 10);
box.addClass('overflowed-left');
tmp = parseInt(box.find('table').first().css('padding-left'));
shift = scrollBox.width();

for (var i = 0; i < this.items.length; i++) {
// search nearest visible break or spacer from left side
if (['break', 'spacer'].indexOf(this.items[i].type) >= 0) {
item = $('#tb_' + this.name + '_item_' + this.items[i].id);
if (item.position().left - tmp < 0) continue;
shift = item.position().left - tmp + item.width();
break;
}
}

// cut scroll shift to max page size
tmp = box.width() - tmp - parseInt(box.find('table').first().css('padding-right'));
shift = Math.abs(shift) > tmp ? tmp : shift;

scrollBox.scrollLeft(scrollLeft + shift);
if (scrollBox.scrollLeft() >= scrollBox[0].scrollWidth - scrollBox.width()) {
box.removeClass('overflowed-right')
}
break;
}
},
Expand Down Expand Up @@ -543,11 +587,22 @@
if (eventData.isCancelled === true) return;

var box = $(this.box);
if (box.find('table').width() > box.width()) {
var scrollBox = box.children('.w2ui-toolbar-scroll-wrapper');
if (box.find('table').first().outerWidth() > box.width()) {
// we have overflowed content
box.addClass('overflowed');
} else if (box.hasClass('overflowed')) {
box.removeClass('overflowed');
if (scrollBox.scrollLeft() > 0) {
box.addClass('overflowed-left');
} else {
box.removeClass('overflowed-left');
}

if (scrollBox.scrollLeft() < scrollBox[0].scrollWidth - scrollBox.width()) {
box.addClass('overflowed-right');
} else {
box.removeClass('overflowed-right');
}
} else {
box.removeClass('overflowed-right overflowed-left');
}

// event after
Expand Down

0 comments on commit 84b7531

Please sign in to comment.