(function(){ var SCROLLBAR_CLASSNAMES = { VERTICAL: "dxTouchVScrollHandle", HORIZONTAL: "dxTouchHScrollHandle", SHOWN_VERTICAL: "dxTouchScrollHandleVisible", SHOWN_HORIZONTAL: "dxTouchScrollHandleVisible" } _aspxAttachEventToDocument("gesturestart", function() { ASPxClientTouchUI.isGesture = true; }); _aspxAttachEventToDocument("gestureend", function() { ASPxClientTouchUI.isGesture = false; }); ASPxClientTouchUI.MakeScrollable = function(element, options) { return new ASPxClientTouchUI.ScrollExtender(element, options); } ASPxClientTouchUI.ScrollExtender = function(element, options) { this.parseOptions(options ? options : {}); this.create(element); }; ASPxClientTouchUI.preventScrollOnEvent = function(evt){ evt.ASPxTouchUIScrollOff = true; }, ASPxClientTouchUI.ScrollExtender.prototype = { ChangeElement: function(element){ this.destroy(); this.create(element); }, acceptElement: function(element) { if(typeof(element) == "string") element = document.getElementById(element); this.element = element; if(__aspxAndroidMobilePlatform){ element.style["overflow-x"] = "hidden"; element.style["overflow-y"] = "hidden"; } return element; }, create: function(element) { this.acceptElement(element); if(this.options.nativeScrolling) { this.InitNativeScrolling(); } else { this.createScrollHandlers(); this.createEventHandlers(); this.updateInitData(); this.updateScrollHandles(); this.attachEventHandlers(); } }, destroy: function() { this.destroyScrollHandlers(); this.detachEventHandlers(); }, parseOptions: function(options) { this.options = {}; this.options.showHorizontalScrollbar = options.showHorizontalScrollbar !== false; this.options.showVerticalScrollbar = options.showVerticalScrollbar !== false; this.options.acceleration = options.acceleration || 0.8; this.options.timeStep = options.timeStep || 50; this.options.minScrollbarSize = options.minScrollbarSize || 20; this.options.vScrollClassName = options.vScrollClassName || SCROLLBAR_CLASSNAMES.VERTICAL; this.options.hScrollClassName = options.hScrollClassName || SCROLLBAR_CLASSNAMES.HORIZONTAL; this.options.vScrollClassNameShown = [ this.options.vScrollClassName, options.vScrollClassNameShown || SCROLLBAR_CLASSNAMES.SHOWN_VERTICAL ].join(" "); this.options.hScrollClassNameShown = [ this.options.hScrollClassName, options.hScrollClassNameShown || SCROLLBAR_CLASSNAMES.SHOWN_HORIZONTAL ].join(" "); this.options.forceCustomScroll = options.forceCustomScroll === true; var nativeScrollPossible = !options.acceleration && !options.timeStep && !options.minScrollbarSize && !options.vScrollClassName && !options.hScrollClassName && !options.forceCustomScroll; if(ASPxClientTouchUI.nativeScrollingSupported() && nativeScrollPossible && ASPxClientTouchUI.isNativeScrollingAllowed) this.options.nativeScrolling = true; }, InitNativeScrolling: function(){ if(this.options.showHorizontalScrollbar || this.options.showVerticalScrollbar){ this.element.style["overflow"] = "scroll"; this.element.style["overflow-x"] = this.options.showHorizontalScrollbar ? "scroll" : "hidden"; this.element.style["overflow-y"] = this.options.showVerticalScrollbar ? "scroll" : "hidden"; this.element.style["-webkit-overflow-scrolling"] = "touch"; } }, createEventHandlers: function() { var instance = this; this.onTouchStart = function(e) { if(!ASPxClientTouchUI.isGesture){ if(!ASPxClientTouchUI.ScrollExtender.activeScrolling) { ASPxClientTouchUI.ScrollExtender.activeScrolling = instance; instance.startScroll(e); } } } this.onTouchMove = function(e) { if(!ASPxClientTouchUI.isGesture && instance.ScrollingActive(e)) { instance.scroll(e); if(instance.scrollBarsShown) e.preventDefault(); } } this.onTouchEnd = function(e) { if(!ASPxClientTouchUI.isGesture) { instance.stopScroll(); instance.MouseEventEmulationProtectHelper.onTouchEnd(instance.initPageX, instance.initPageY, e); instance.ReleaseScrolling(); } } this.onScroll = function(e) { if(ASPxClientTouchUI.isGesture && instance.ScrollingActive(e)) { instance.showScrollBars() instance.updateScrollHandles(); } } this.onClick = function() { instance.MouseEventEmulationProtectHelper.onClick(); } }, createScrollHandlers: function() { if(this.options.showHorizontalScrollbar) { this.hScrollHandleElement = document.createElement("DIV"); this.hScrollHandleElement.className = this.options.hScrollClassName; this.element.appendChild(this.hScrollHandleElement); this.hEndMargin = this.options.showVerticalScrollbar ? _aspxPxToInt(_aspxGetCurrentStyle(this.hScrollHandleElement).marginRight) : 0; } if(this.options.showVerticalScrollbar) { this.vScrollHandleElement = document.createElement("DIV"); this.vScrollHandleElement.className = this.options.vScrollClassName; this.element.appendChild(this.vScrollHandleElement); this.vEndMargin = this.options.showHorizontalScrollbar ? _aspxPxToInt(_aspxGetCurrentStyle(this.vScrollHandleElement).marginBottom) : 0; } }, destroyScrollHandlers: function() { if(this.hScrollHandleElement && this.hScrollHandleElement.parentNode) this.hScrollHandleElement.parentNode.removeChild(this.hScrollHandleElement) if(this.vScrollHandleElement && this.vScrollHandleElement.parentNode) this.vScrollHandleElement.parentNode.removeChild(this.vScrollHandleElement) this.hScrollHandleElement = null; this.vScrollHandleElement = null; }, attachEventHandlers: function() { this.element.addEventListener("touchstart", this.onTouchStart , false); this.element.addEventListener("touchend" , this.onTouchEnd , false); this.element.addEventListener("touchmove" , this.onTouchMove , false); this.element.addEventListener("scroll" , this.onScroll , false); this.element.addEventListener("click" , this.onClick , false); }, detachEventHandlers: function() { _aspxDetachEventFromElement(this.element, "touchstart", this.onTouchStart); _aspxDetachEventFromElement(this.element, "touchend", this.onTouchEnd); _aspxDetachEventFromElement(this.element, "touchmove", this.onTouchMove); _aspxDetachEventFromElement(this.element, "scroll", this.onScroll); _aspxDetachEventFromElement(this.element, "click", this.onClick); }, updateInitData: function() { window.clearTimeout(this.inertialStopTimerId); this.initScrollLeft = this.element.scrollLeft; this.initScrollTop = this.element.scrollTop; this.initElementX = _aspxGetAbsoluteX(this.element); this.initElementY = _aspxGetAbsoluteY(this.element); this.scrollTime = new Date(); this.vx = 0; this.vy = 0; }, ScrollingActive: function(e){ return (!e || !e.ASPxTouchUIScrollOff) && ASPxClientTouchUI.ScrollExtender.activeScrolling == this; }, ReleaseScrolling: function(){ if(this.ScrollingActive()) ASPxClientTouchUI.ScrollExtender.activeScrolling = null; }, startScroll: function(e) { this.initPageX = ASPxClientTouchUI.getEventX(e); this.initPageY = ASPxClientTouchUI.getEventY(e); this.updateInitData(); this.updateScrollHandles(); this.showScrollBars(); }, scroll: function(e) { var newX = this.initScrollLeft + (this.initPageX - ASPxClientTouchUI.getEventX(e)); var newY = this.initScrollTop + (this.initPageY - ASPxClientTouchUI.getEventY(e)); var dt = (new Date() - this.scrollTime); var dx = newX - this.element.scrollLeft; var dy = newY - this.element.scrollTop; this.vx = dx / dt; this.vy = dy / dt; if(this.options.showHorizontalScrollbar) this.element.scrollLeft = newX; if(this.options.showVerticalScrollbar) this.element.scrollTop = newY; this.updateScrollHandles(); this.scrollTime = new Date(); }, stopScroll: function() { var instance = this; var element = this.element; var acceleration = this.options.acceleration; var timeStep = this.options.timeStep; this.inertialStopTimerId = window.setTimeout(function() { instance.vx *= acceleration; instance.vy *= acceleration; if(Math.abs(instance.vx) < 0.1) instance.vx = 0; if(Math.abs(instance.vy) < 0.1) instance.vy = 0; if(instance.vx == 0 && instance.vy == 0) { instance.hideScrollBars(); return; } var dx = instance.vx * timeStep; var dy = instance.vy * timeStep; if(instance.options.showHorizontalScrollbar) element.scrollLeft += dx; if(instance.options.showVerticalScrollbar) element.scrollTop += dy; if(element.scrollLeft + element.clientWidth >= element.scrollWidth || element.scrollLeft <= 0) instance.vx = 0; if(element.scrollTop + element.clientHeight>= element.scrollHeight || element.scrollTop <= 0) instance.vy = 0; instance.updateScrollHandles(); instance.stopScroll() }, timeStep); }, updateScrollHandles: function() { if(this.hScrollHandleElement) { var scrollHandler = this.calcScrollHandles(this.element.scrollWidth, this.element.clientWidth, this.options.minScrollbarSize, this.element.scrollLeft, this.hEndMargin); this.hScrollHandleElement.style.width = scrollHandler.size + "px"; _aspxSetAbsoluteX(this.hScrollHandleElement, this.initElementX + scrollHandler.pos); _aspxSetAbsoluteY(this.hScrollHandleElement, this.initElementY + this.element.clientHeight - this.hScrollHandleElement.offsetHeight); } if(this.vScrollHandleElement) { var scrollHandler = this.calcScrollHandles(this.element.scrollHeight, this.element.clientHeight, this.options.minScrollbarSize, this.element.scrollTop, this.vEndMargin); this.vScrollHandleElement.style.height = scrollHandler.size + "px"; _aspxSetAbsoluteX(this.vScrollHandleElement, this.initElementX + this.element.clientWidth - this.vScrollHandleElement.offsetWidth); _aspxSetAbsoluteY(this.vScrollHandleElement, this.initElementY + scrollHandler.pos); } }, calcScrollHandles: function(scrollSize, clientSize, scrollBarMinSize, scrollPos, endMargin) { var scrollBarSize = clientSize * clientSize / scrollSize; scrollBarSize = scrollBarSize > scrollBarMinSize ? scrollBarSize : scrollBarMinSize; var k = (scrollSize == clientSize) ? 0 : (clientSize - scrollBarSize - endMargin) / (scrollSize - clientSize); return {size:scrollBarSize, pos:scrollPos * k}; }, showScrollBars: function() { var needVScrollHandle = this.element.scrollHeight > this.element.clientHeight; var needHScrollHandle = this.element.scrollWidth > this.element.clientWidth; if(this.vScrollHandleElement && needVScrollHandle) this.vScrollHandleElement.className = this.options.vScrollClassNameShown; if(this.hScrollHandleElement && needHScrollHandle) this.hScrollHandleElement.className = this.options.hScrollClassNameShown; this.scrollBarsShown = needVScrollHandle || needHScrollHandle; }, hideScrollBars: function() { if(this.vScrollHandleElement) this.vScrollHandleElement.className = this.options.vScrollClassName; if(this.hScrollHandleElement) this.hScrollHandleElement.className = this.options.hScrollClassName; this.scrollBarsShown = false; }, MouseEventEmulationProtectHelper: { onTouchEnd: function(initPageX, initPageY, e) { var difX = initPageX - ASPxClientTouchUI.getEventX(e); var difY = initPageY - ASPxClientTouchUI.getEventY(e); if(Math.abs(difX) > ASPxClientTouchUI.clickSensetivity || Math.abs(difY) > ASPxClientTouchUI.clickSensetivity){ ASPxClientTouchUI.isMouseEventFromScrolling = true; window.setTimeout(function(){ ASPxClientTouchUI.isMouseEventFromScrolling = false; }, 100); } }, onClick: function(){ if(ASPxClientTouchUI.isMouseEventFromScrolling){ window.setTimeout(function(){ ASPxClientTouchUI.isMouseEventFromScrolling = false; }, 0); } } } }; })();