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

Cleanup #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 19 additions & 6 deletions dist/ng-scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
replace: true,
transclude: true,
link: function (scope, element, attrs) {
var mainElm, transculdedContainer, tools, thumb, thumbLine, track;
var mainElm, transculdedContainer, thumb, thumbContainer, track;
var flags = { bottom: attrs.hasOwnProperty('bottom') };
var win = angular.element($window);
// Elements
var dragger = { top: 0 }, page = { top: 0 };
// Styles
var scrollboxStyle, draggerStyle, draggerLineStyle, pageStyle;
var calcStyles = function () {
scrollboxStyle = {
Expand Down Expand Up @@ -53,6 +55,7 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
};
var wheelHandler = function (event) {
var wheelDivider = 20;
// so it can be changed easily
var deltaY = event.wheelDeltaY !== undefined ? event.wheelDeltaY / wheelDivider : event.wheelDelta !== undefined ? event.wheelDelta / wheelDivider : -event.detail * (wheelDivider / 10);
dragger.top = Math.max(0, Math.min(parseInt(page.height, 10) - parseInt(dragger.height, 10), parseInt(dragger.top, 10) - deltaY));
redraw();
Expand All @@ -70,33 +73,40 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
var dragHandler = function (event) {
var newOffsetY = event.pageY - thumb[0].scrollTop - lastOffsetY;
var newOffsetX = 0;
// TBD
thumbDrag(event, newOffsetX, newOffsetY);
redraw();
};
var buildScrollbar = function (rollToBottom) {
// Getting top position of a parent element to place scroll correctly
var parentOffsetTop = element[0].parentElement.offsetTop;
var wheelEvent = win[0].onmousewheel !== undefined ? 'mousewheel' : 'DOMMouseScroll';
rollToBottom = flags.bottom || rollToBottom;
mainElm = angular.element(element.children()[0]);
transculdedContainer = angular.element(mainElm.children()[0]);
tools = angular.element(mainElm.children()[1]);
thumb = angular.element(angular.element(tools.children()[0]).children()[0]);
thumbLine = angular.element(thumb.children()[0]);
track = angular.element(angular.element(tools.children()[0]).children()[1]);
thumb = angular.element(element[0].querySelector('.ngsb-thumb-pos'));
thumbContainer = angular.element(element[0].querySelector('.ngsb-thumb'));
track = angular.element(element[0].querySelector('.ngsb-track'));
// Check if scroll bar is needed
page.height = element[0].offsetHeight - parentOffsetTop;
page.scrollHeight = transculdedContainer[0].scrollHeight;
if (page.height < page.scrollHeight) {
scope.showYScrollbar = true;
// Calculate the dragger height
dragger.height = Math.round(page.height / page.scrollHeight * page.height);
dragger.trackHeight = page.height;
// update the transcluded content style and clear the parent's
calcStyles();
element.css({ overflow: 'hidden' });
mainElm.css(scrollboxStyle);
transculdedContainer.css(pageStyle);
thumb.css(draggerStyle);
thumbLine.css(draggerLineStyle);
thumbContainer.css(draggerLineStyle);
// Bind scroll bar events
track.bind('click', trackClick);
// Handle mousewheel
transculdedContainer[0].addEventListener(wheelEvent, wheelHandler, false);
// Drag the scroller
thumb.on('mousedown', function (event) {
lastOffsetY = event.pageY - thumb[0].offsetTop;
win.on('mouseup', function () {
Expand All @@ -118,14 +128,17 @@ angular.module('ngScrollbar', []).directive('ngScrollbar', [
thumb.off('mousedown');
transculdedContainer[0].removeEventListener(wheelEvent, wheelHandler, false);
transculdedContainer.attr('style', 'position:relative;top:0');
// little hack to remove other inline styles
mainElm.css({ height: '100%' });
}
};
var rebuildTimer;
var rebuild = function (e, data) {
/* jshint -W116 */
if (rebuildTimer != null) {
clearTimeout(rebuildTimer);
}
/* jshint +W116 */
var rollToBottom = !!data && !!data.rollToBottom;
rebuildTimer = setTimeout(function () {
page.height = null;
Expand Down
4 changes: 2 additions & 2 deletions dist/ng-scrollbar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions src/ng-scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ angular.module('ngScrollbar', []).
transclude: true,
link: function(scope, element, attrs) {

var mainElm, transculdedContainer, tools, thumb, thumbLine, track;
var mainElm, transculdedContainer, thumb, thumbContainer, track;

var flags = {
bottom: attrs.hasOwnProperty('bottom')
Expand Down Expand Up @@ -120,10 +120,9 @@ angular.module('ngScrollbar', []).
rollToBottom = flags.bottom || rollToBottom;
mainElm = angular.element(element.children()[0]);
transculdedContainer = angular.element(mainElm.children()[0]);
tools = angular.element(mainElm.children()[1]);
thumb = angular.element(angular.element(tools.children()[0]).children()[0]);
thumbLine = angular.element(thumb.children()[0]);
track = angular.element(angular.element(tools.children()[0]).children()[1]);
thumb = angular.element(element[0].querySelector('.ngsb-thumb-pos'));
thumbContainer = angular.element(element[0].querySelector('.ngsb-thumb'));
track = angular.element(element[0].querySelector('.ngsb-track'));

// Check if scroll bar is needed
page.height = element[0].offsetHeight - parentOffsetTop;
Expand All @@ -142,12 +141,12 @@ angular.module('ngScrollbar', []).
mainElm.css(scrollboxStyle);
transculdedContainer.css(pageStyle);
thumb.css(draggerStyle);
thumbLine.css(draggerLineStyle);
thumbContainer.css(draggerLineStyle);

// Bind scroll bar events
track.bind('click', trackClick);

// Handl mousewheel
// Handle mousewheel
transculdedContainer[0].addEventListener(wheelEvent, wheelHandler, false);

// Drag the scroller
Expand Down