Change the way to handle event class name.

Use factory pattern to handle it, instead of using + operator of
string.
This commit is contained in:
Hyunje Alex Jun 2014-10-28 16:41:36 +00:00
parent 967c30a5e4
commit f2342f2678

View File

@ -39,14 +39,18 @@
includePadding: false includePadding: false
}; };
var getEventClassName = (function () {
var incrementingId = 0; var incrementingId = 0;
return function () { var eventClassFactory = function () {
var id = incrementingId; var id = incrementingId++;
incrementingId += 1; return function (eventName) {
return '.perfect-scrollbar-' + id; var className = '.perfect-scrollbar-' + id;
if (typeof eventName === 'undefined') {
return className;
} else {
return eventName + className;
}
};
}; };
})();
$.fn.perfectScrollbar = function (suppliedSettings, option) { $.fn.perfectScrollbar = function (suppliedSettings, option) {
@ -92,7 +96,7 @@
var contentHeight; var contentHeight;
var isRtl = $this.css('direction') === "rtl"; var isRtl = $this.css('direction') === "rtl";
var eventClassName = getEventClassName(); var eventClass = eventClassFactory();
var ownerDocument = this.ownerDocument || document; var ownerDocument = this.ownerDocument || document;
var $scrollbarXRail = $("<div class='ps-scrollbar-x-rail'>").appendTo($this); var $scrollbarXRail = $("<div class='ps-scrollbar-x-rail'>").appendTo($this);
@ -258,7 +262,7 @@
var currentLeft; var currentLeft;
var currentPageX; var currentPageX;
$scrollbarX.bind('mousedown' + eventClassName, function (e) { $scrollbarX.bind(eventClass('mousedown'), function (e) {
currentPageX = e.pageX; currentPageX = e.pageX;
currentLeft = $scrollbarX.position().left; currentLeft = $scrollbarX.position().left;
$scrollbarXRail.addClass('in-scrolling'); $scrollbarXRail.addClass('in-scrolling');
@ -266,7 +270,7 @@
e.preventDefault(); e.preventDefault();
}); });
$(ownerDocument).bind('mousemove' + eventClassName, function (e) { $(ownerDocument).bind(eventClass('mousemove'), function (e) {
if ($scrollbarXRail.hasClass('in-scrolling')) { if ($scrollbarXRail.hasClass('in-scrolling')) {
updateScrollLeft(currentLeft, e.pageX - currentPageX); updateScrollLeft(currentLeft, e.pageX - currentPageX);
updateGeometry(); updateGeometry();
@ -275,7 +279,7 @@
} }
}); });
$(ownerDocument).bind('mouseup' + eventClassName, function (e) { $(ownerDocument).bind(eventClass('mouseup'), function (e) {
if ($scrollbarXRail.hasClass('in-scrolling')) { if ($scrollbarXRail.hasClass('in-scrolling')) {
$scrollbarXRail.removeClass('in-scrolling'); $scrollbarXRail.removeClass('in-scrolling');
} }
@ -289,7 +293,7 @@
var currentTop; var currentTop;
var currentPageY; var currentPageY;
$scrollbarY.bind('mousedown' + eventClassName, function (e) { $scrollbarY.bind(eventClass('mousedown'), function (e) {
currentPageY = e.pageY; currentPageY = e.pageY;
currentTop = $scrollbarY.position().top; currentTop = $scrollbarY.position().top;
$scrollbarYRail.addClass('in-scrolling'); $scrollbarYRail.addClass('in-scrolling');
@ -297,7 +301,7 @@
e.preventDefault(); e.preventDefault();
}); });
$(ownerDocument).bind('mousemove' + eventClassName, function (e) { $(ownerDocument).bind(eventClass('mousemove'), function (e) {
if ($scrollbarYRail.hasClass('in-scrolling')) { if ($scrollbarYRail.hasClass('in-scrolling')) {
updateScrollTop(currentTop, e.pageY - currentPageY); updateScrollTop(currentTop, e.pageY - currentPageY);
updateGeometry(); updateGeometry();
@ -306,7 +310,7 @@
} }
}); });
$(ownerDocument).bind('mouseup' + eventClassName, function (e) { $(ownerDocument).bind(eventClass('mouseup'), function (e) {
if ($scrollbarYRail.hasClass('in-scrolling')) { if ($scrollbarYRail.hasClass('in-scrolling')) {
$scrollbarYRail.removeClass('in-scrolling'); $scrollbarYRail.removeClass('in-scrolling');
} }
@ -410,23 +414,23 @@
} }
if (typeof window.onwheel !== "undefined") { if (typeof window.onwheel !== "undefined") {
$this.bind('wheel' + eventClassName, mousewheelHandler); $this.bind(eventClass('wheel'), mousewheelHandler);
} else if (typeof window.onmousewheel !== "undefined") { } else if (typeof window.onmousewheel !== "undefined") {
$this.bind('mousewheel' + eventClassName, mousewheelHandler); $this.bind(eventClass('mousewheel'), mousewheelHandler);
} }
} }
function bindKeyboardHandler() { function bindKeyboardHandler() {
var hovered = false; var hovered = false;
$this.bind('mouseenter' + eventClassName, function (e) { $this.bind(eventClass('mouseenter'), function (e) {
hovered = true; hovered = true;
}); });
$this.bind('mouseleave' + eventClassName, function (e) { $this.bind(eventClass('mouseleave'), function (e) {
hovered = false; hovered = false;
}); });
var shouldPrevent = false; var shouldPrevent = false;
$(ownerDocument).bind('keydown' + eventClassName, function (e) { $(ownerDocument).bind(eventClass('keydown'), function (e) {
if (e.isDefaultPrevented && e.isDefaultPrevented()) { if (e.isDefaultPrevented && e.isDefaultPrevented()) {
return; return;
} }
@ -498,8 +502,8 @@
function bindRailClickHandler() { function bindRailClickHandler() {
function stopPropagation(e) { e.stopPropagation(); } function stopPropagation(e) { e.stopPropagation(); }
$scrollbarY.bind('click' + eventClassName, stopPropagation); $scrollbarY.bind(eventClass('click'), stopPropagation);
$scrollbarYRail.bind('click' + eventClassName, function (e) { $scrollbarYRail.bind(eventClass('click'), function (e) {
var halfOfScrollbarLength = int(scrollbarYHeight / 2); var halfOfScrollbarLength = int(scrollbarYHeight / 2);
var positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength; var positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength;
var maxPositionTop = containerHeight - scrollbarYHeight; var maxPositionTop = containerHeight - scrollbarYHeight;
@ -514,8 +518,8 @@
$this.scrollTop((contentHeight - containerHeight) * positionRatio); $this.scrollTop((contentHeight - containerHeight) * positionRatio);
}); });
$scrollbarX.bind('click' + eventClassName, stopPropagation); $scrollbarX.bind(eventClass('click'), stopPropagation);
$scrollbarXRail.bind('click' + eventClassName, function (e) { $scrollbarXRail.bind(eventClass('click'), function (e) {
var halfOfScrollbarLength = int(scrollbarXWidth / 2); var halfOfScrollbarLength = int(scrollbarXWidth / 2);
var positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength; var positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength;
var maxPositionLeft = containerWidth - scrollbarXWidth; var maxPositionLeft = containerWidth - scrollbarXWidth;
@ -634,40 +638,40 @@
} }
if (supportsTouch) { if (supportsTouch) {
$(window).bind("touchstart" + eventClassName, globalTouchStart); $(window).bind(eventClass("touchstart"), globalTouchStart);
$(window).bind("touchend" + eventClassName, globalTouchEnd); $(window).bind(eventClass("touchend"), globalTouchEnd);
$this.bind("touchstart" + eventClassName, touchStart); $this.bind(eventClass("touchstart"), touchStart);
$this.bind("touchmove" + eventClassName, touchMove); $this.bind(eventClass("touchmove"), touchMove);
$this.bind("touchend" + eventClassName, touchEnd); $this.bind(eventClass("touchend"), touchEnd);
} }
if (supportsIePointer) { if (supportsIePointer) {
if (window.PointerEvent) { if (window.PointerEvent) {
$(window).bind("pointerdown" + eventClassName, globalTouchStart); $(window).bind(eventClass("pointerdown"), globalTouchStart);
$(window).bind("pointerup" + eventClassName, globalTouchEnd); $(window).bind(eventClass("pointerup"), globalTouchEnd);
$this.bind("pointerdown" + eventClassName, touchStart); $this.bind(eventClass("pointerdown"), touchStart);
$this.bind("pointermove" + eventClassName, touchMove); $this.bind(eventClass("pointermove"), touchMove);
$this.bind("pointerup" + eventClassName, touchEnd); $this.bind(eventClass("pointerup"), touchEnd);
} else if (window.MSPointerEvent) { } else if (window.MSPointerEvent) {
$(window).bind("MSPointerDown" + eventClassName, globalTouchStart); $(window).bind(eventClass("MSPointerDown"), globalTouchStart);
$(window).bind("MSPointerUp" + eventClassName, globalTouchEnd); $(window).bind(eventClass("MSPointerUp"), globalTouchEnd);
$this.bind("MSPointerDown" + eventClassName, touchStart); $this.bind(eventClass("MSPointerDown"), touchStart);
$this.bind("MSPointerMove" + eventClassName, touchMove); $this.bind(eventClass("MSPointerMove"), touchMove);
$this.bind("MSPointerUp" + eventClassName, touchEnd); $this.bind(eventClass("MSPointerUp"), touchEnd);
} }
} }
} }
function bindScrollHandler() { function bindScrollHandler() {
$this.bind('scroll' + eventClassName, function (e) { $this.bind(eventClass('scroll'), function (e) {
updateGeometry(); updateGeometry();
}); });
} }
function destroy() { function destroy() {
$this.unbind(eventClassName); $this.unbind(eventClass());
$(window).unbind(eventClassName); $(window).unbind(eventClass());
$(ownerDocument).unbind(eventClassName); $(ownerDocument).unbind(eventClass());
$this.data('perfect-scrollbar', null); $this.data('perfect-scrollbar', null);
$this.data('perfect-scrollbar-update', null); $this.data('perfect-scrollbar-update', null);
$this.data('perfect-scrollbar-destroy', null); $this.data('perfect-scrollbar-destroy', null);
@ -698,7 +702,7 @@
isScrollbarYUsingRight = isScrollbarYUsingRight =
scrollbarYLeft = scrollbarYLeft =
isRtl = isRtl =
eventClassName = null; eventClass = null;
} }
var supportsTouch = (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch); var supportsTouch = (('ontouchstart' in window) || window.DocumentTouch && document instanceof window.DocumentTouch);