diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 307f6c5a2c..5aa738cbe6 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -26,28 +26,27 @@ const NodeEvent = require('../CCNode').EventType; -var NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED = 5; -var OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05; -var EPSILON = 1e-4; -var MOVEMENT_FACTOR = 0.7; +const NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED = 5; +const OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05; +const EPSILON = 1e-4; +const MOVEMENT_FACTOR = 0.7; -var quintEaseOut = function(time) { +let quintEaseOut = function(time) { time -= 1; return (time * time * time * time * time + 1); }; -var getTimeInMilliseconds = function() { - var currentTime = new Date(); +let getTimeInMilliseconds = function() { + let currentTime = new Date(); return currentTime.getMilliseconds(); }; - /** * !#en Enum for ScrollView event type. * !#zh 滚动视图事件类型 * @enum ScrollView.EventType */ -var EventType = cc.Enum({ +const EventType = cc.Enum({ /** * !#en The event emmitted when ScrollView scroll to the top boundary of inner container * !#zh 滚动视图滚动到顶部边界事件 @@ -128,7 +127,7 @@ var EventType = cc.Enum({ SCROLL_BEGAN: 12 }); -var eventMap = { +const eventMap = { 'scroll-to-top' : EventType.SCROLL_TO_TOP, 'scroll-to-bottom': EventType.SCROLL_TO_BOTTOM, 'scroll-to-left' : EventType.SCROLL_TO_LEFT, @@ -154,7 +153,7 @@ var eventMap = { * @class ScrollView * @extends Component */ -var ScrollView = cc.Class({ +let ScrollView = cc.Class({ name: 'cc.ScrollView', extends: require('./CCViewGroup'), @@ -164,7 +163,7 @@ var ScrollView = cc.Class({ executeInEditMode: false, }, - ctor: function() { + ctor () { this._topBoundary = 0; this._bottomBoundary = 0; this._leftBoundary = 0; @@ -286,7 +285,7 @@ var ScrollView = cc.Class({ default: undefined, type: cc.Scrollbar, tooltip: CC_DEV && 'i18n:COMPONENT.scrollview.horizontal_bar', - notify: function() { + notify () { if (this.horizontalScrollBar) { this.horizontalScrollBar.setTargetScrollView(this); this._updateScrollBar(0); @@ -304,7 +303,7 @@ var ScrollView = cc.Class({ default: undefined, type: cc.Scrollbar, tooltip: CC_DEV && 'i18n:COMPONENT.scrollview.vertical_bar', - notify: function() { + notify () { if (this.verticalScrollBar) { this.verticalScrollBar.setTargetScrollView(this); this._updateScrollBar(0); @@ -353,8 +352,8 @@ var ScrollView = cc.Class({ * // Scroll to the bottom of the view. * scrollView.scrollToBottom(0.1); */ - scrollToBottom: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToBottom (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(0, 0), applyToHorizontal: false, applyToVertical: true, @@ -378,8 +377,8 @@ var ScrollView = cc.Class({ * // Scroll to the top of the view. * scrollView.scrollToTop(0.1); */ - scrollToTop: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToTop (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(0, 1), applyToHorizontal: false, applyToVertical: true, @@ -403,8 +402,8 @@ var ScrollView = cc.Class({ * // Scroll to the left of the view. * scrollView.scrollToLeft(0.1); */ - scrollToLeft: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToLeft (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(0, 0), applyToHorizontal: true, applyToVertical: false, @@ -428,8 +427,8 @@ var ScrollView = cc.Class({ * // Scroll to the right of the view. * scrollView.scrollToRight(0.1); */ - scrollToRight: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToRight (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(1, 0), applyToHorizontal: true, applyToVertical: false, @@ -453,8 +452,8 @@ var ScrollView = cc.Class({ * // Scroll to the upper left corner of the view. * scrollView.scrollToTopLeft(0.1); */ - scrollToTopLeft: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToTopLeft (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(0, 1), applyToHorizontal: true, applyToVertical: true, @@ -478,8 +477,8 @@ var ScrollView = cc.Class({ * // Scroll to the top right corner of the view. * scrollView.scrollToTopRight(0.1); */ - scrollToTopRight: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToTopRight (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(1, 1), applyToHorizontal: true, applyToVertical: true, @@ -503,8 +502,8 @@ var ScrollView = cc.Class({ * // Scroll to the lower left corner of the view. * scrollView.scrollToBottomLeft(0.1); */ - scrollToBottomLeft: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToBottomLeft (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(0, 0), applyToHorizontal: true, applyToVertical: true, @@ -528,8 +527,8 @@ var ScrollView = cc.Class({ * // Scroll to the lower right corner of the view. * scrollView.scrollToBottomRight(0.1); */ - scrollToBottomRight: function(timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToBottomRight (timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(1, 0), applyToHorizontal: true, applyToVertical: true, @@ -554,13 +553,13 @@ var ScrollView = cc.Class({ * @param {Boolean} [attenuated=true] - Whether the scroll acceleration attenuated, default is true. * @example * // Scroll to middle position in 0.1 second in x-axis - * var maxScrollOffset = this.getMaxScrollOffset(); + * let maxScrollOffset = this.getMaxScrollOffset(); * scrollView.scrollToOffset(cc.v2(maxScrollOffset.x / 2, 0), 0.1); */ - scrollToOffset: function(offset, timeInSecond, attenuated) { - var maxScrollOffset = this.getMaxScrollOffset(); + scrollToOffset (offset, timeInSecond, attenuated) { + let maxScrollOffset = this.getMaxScrollOffset(); - var anchor = cc.v2(0, 0); + let anchor = cc.v2(0, 0); //if maxScrollOffset is 0, then always align the content's top left origin to the top left corner of its parent if (maxScrollOffset.x === 0) { anchor.x = 0; @@ -583,9 +582,9 @@ var ScrollView = cc.Class({ * @method getScrollOffset * @return {Vec2} - A Vec2 value indicate the current scroll offset. */ - getScrollOffset: function() { - var topDelta = this._getContentTopBoundary() - this._topBoundary; - var leftDeta = this._getContentLeftBoundary() - this._leftBoundary; + getScrollOffset () { + let topDelta = this._getContentTopBoundary() - this._topBoundary; + let leftDeta = this._getContentLeftBoundary() - this._leftBoundary; return cc.v2(leftDeta, topDelta); }, @@ -596,11 +595,11 @@ var ScrollView = cc.Class({ * @method getMaxScrollOffset * @return {Vec2} - A Vec2 value indicate the maximize scroll offset in x and y axis. */ - getMaxScrollOffset: function() { - var scrollSize = this.node.getContentSize(); - var contentSize = this.content.getContentSize(); - var horizontalMaximizeOffset = contentSize.width - scrollSize.width; - var verticalMaximizeOffset = contentSize.height - scrollSize.height; + getMaxScrollOffset () { + let scrollSize = this.node.getContentSize(); + let contentSize = this.content.getContentSize(); + let horizontalMaximizeOffset = contentSize.width - scrollSize.width; + let verticalMaximizeOffset = contentSize.height - scrollSize.height; horizontalMaximizeOffset = horizontalMaximizeOffset >= 0 ? horizontalMaximizeOffset : 0; verticalMaximizeOffset = verticalMaximizeOffset >=0 ? verticalMaximizeOffset : 0; @@ -619,8 +618,8 @@ var ScrollView = cc.Class({ * // Scroll to middle position. * scrollView.scrollToBottomRight(0.5, 0.1); */ - scrollToPercentHorizontal: function(percent, timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToPercentHorizontal (percent, timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(percent, 0), applyToHorizontal: true, applyToVertical: false, @@ -648,8 +647,8 @@ var ScrollView = cc.Class({ * // Horizontal scroll to view right. * scrollView.scrollTo(cc.v2(1, 0), 0.1); */ - scrollTo: function(anchor, timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollTo (anchor, timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: anchor, applyToHorizontal: true, applyToVertical: true, @@ -673,8 +672,8 @@ var ScrollView = cc.Class({ * // Scroll to middle position. * scrollView.scrollToPercentVertical(0.5, 0.1); */ - scrollToPercentVertical: function(percent, timeInSecond, attenuated) { - var moveDelta = this._calculateMovePercentDelta({ + scrollToPercentVertical (percent, timeInSecond, attenuated) { + let moveDelta = this._calculateMovePercentDelta({ anchor: cc.v2(0, percent), applyToHorizontal: false, applyToVertical: true, @@ -692,7 +691,7 @@ var ScrollView = cc.Class({ * !#zh 停止自动滚动, 调用此 API 可以让 Scrollview 立即停止滚动 * @method stopAutoScroll */ - stopAutoScroll: function() { + stopAutoScroll () { this._autoScrolling = false; this._autoScrollAccumulatedTime = this._autoScrollTotalTime; }, @@ -703,7 +702,7 @@ var ScrollView = cc.Class({ * @method setContentPosition * @param {Vec2} position - The position in content's parent space. */ - setContentPosition: function(position) { + setContentPosition (position) { if (position.fuzzyEquals(this.getContentPosition(), EPSILON)) { return; } @@ -718,7 +717,7 @@ var ScrollView = cc.Class({ * @method getContentPosition * @returns {Position} - The content's position in its parent space. */ - getContentPosition: function () { + getContentPosition () { return this.content.getPosition(); }, @@ -728,7 +727,7 @@ var ScrollView = cc.Class({ * @method isScrolling * @returns {Boolean} - Whether the user is currently dragging the ScrollView to scroll it */ - isScrolling: function () { + isScrolling () { return this._scrolling; }, @@ -738,12 +737,12 @@ var ScrollView = cc.Class({ * @method isAutoScrolling * @returns {Boolean} - Whether the ScrollView is currently scrolling because of a bounceback or inertia slowdown. */ - isAutoScrolling: function () { + isAutoScrolling () { return this._autoScrolling; }, //private methods - _registerEvent: function () { + _registerEvent () { this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this, true); this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, true); this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, true); @@ -751,7 +750,7 @@ var ScrollView = cc.Class({ this.node.on(cc.Node.EventType.MOUSE_WHEEL, this._onMouseWheel, this, true); }, - _unregisterEvent: function () { + _unregisterEvent () { this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this, true); this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, true); this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, true); @@ -759,12 +758,12 @@ var ScrollView = cc.Class({ this.node.off(cc.Node.EventType.MOUSE_WHEEL, this._onMouseWheel, this, true); }, - _onMouseWheel: function(event, captureListeners) { + _onMouseWheel (event, captureListeners) { if (!this.enabledInHierarchy) return; if (this._hasNestedViewGroup(event, captureListeners)) return; - var deltaMove = cc.v2(0, 0); - var wheelPrecision = -0.1; + let deltaMove = cc.v2(0, 0); + let wheelPrecision = -0.1; if(CC_JSB) { wheelPrecision = -7; } @@ -787,9 +786,9 @@ var ScrollView = cc.Class({ this._stopPropagationIfTargetIsMe(event); }, - _checkMouseWheel: function(dt) { - var currentOutOfBoundary = this._getHowMuchOutOfBoundary(); - var maxElapsedTime = 0.1; + _checkMouseWheel (dt) { + let currentOutOfBoundary = this._getHowMuchOutOfBoundary(); + let maxElapsedTime = 0.1; if (!currentOutOfBoundary.fuzzyEquals(cc.v2(0, 0), EPSILON)) { this._processInertiaScroll(); @@ -806,27 +805,26 @@ var ScrollView = cc.Class({ this.unschedule(this._checkMouseWheel); this._stopMouseWheel = false; } - }, - _calculateMovePercentDelta: function(options) { - var anchor = options.anchor; - var applyToHorizontal = options.applyToHorizontal; - var applyToVertical = options.applyToVertical; + _calculateMovePercentDelta (options) { + let anchor = options.anchor; + let applyToHorizontal = options.applyToHorizontal; + let applyToVertical = options.applyToVertical; this._calculateBoundary(); anchor = anchor.clampf(cc.v2(0, 0), cc.v2(1, 1)); - var scrollSize = this.node.getContentSize(); - var contentSize = this.content.getContentSize(); - var bottomDeta = this._getContentBottomBoundary() - this._bottomBoundary; + let scrollSize = this.node.getContentSize(); + let contentSize = this.content.getContentSize(); + let bottomDeta = this._getContentBottomBoundary() - this._bottomBoundary; bottomDeta = -bottomDeta; - var leftDeta = this._getContentLeftBoundary() - this._leftBoundary; + let leftDeta = this._getContentLeftBoundary() - this._leftBoundary; leftDeta = -leftDeta; - var moveDelta = cc.v2(0, 0); - var totalScrollDelta = 0; + let moveDelta = cc.v2(0, 0); + let totalScrollDelta = 0; if (applyToHorizontal) { totalScrollDelta = contentSize.width - scrollSize.width; moveDelta.x = leftDeta - totalScrollDelta * anchor.x; @@ -840,15 +838,15 @@ var ScrollView = cc.Class({ return moveDelta; }, - _moveContentToTopLeft: function (scrollViewSize) { - var contentSize = this.content.getContentSize(); + _moveContentToTopLeft (scrollViewSize) { + let contentSize = this.content.getContentSize(); - var bottomDeta = this._getContentBottomBoundary() - this._bottomBoundary; + let bottomDeta = this._getContentBottomBoundary() - this._bottomBoundary; bottomDeta = -bottomDeta; - var moveDelta = cc.v2(0, 0); - var totalScrollDelta = 0; + let moveDelta = cc.v2(0, 0); + let totalScrollDelta = 0; - var leftDeta = this._getContentLeftBoundary() - this._leftBoundary; + let leftDeta = this._getContentLeftBoundary() - this._leftBoundary; leftDeta = -leftDeta; if (contentSize.height < scrollViewSize.height) { @@ -882,46 +880,46 @@ var ScrollView = cc.Class({ this._adjustContentOutOfBoundary(); }, - _calculateBoundary: function() { + _calculateBoundary () { if (this.content) { //refresh content size - var layout = this.content.getComponent(cc.Layout); + let layout = this.content.getComponent(cc.Layout); if(layout && layout.enabledInHierarchy) { layout.updateLayout(); } - var scrollViewSize = this.node.getContentSize(); + let scrollViewSize = this.node.getContentSize(); - var leftBottomPosition = this._convertToContentParentSpace(cc.v2(0, 0)); + let leftBottomPosition = this._convertToContentParentSpace(cc.v2(0, 0)); this._leftBoundary = leftBottomPosition.x; this._bottomBoundary = leftBottomPosition.y; - var topRightPosition = this._convertToContentParentSpace(cc.v2(scrollViewSize.width, scrollViewSize.height)); + let topRightPosition = this._convertToContentParentSpace(cc.v2(scrollViewSize.width, scrollViewSize.height)); this._rightBoundary = topRightPosition.x; this._topBoundary = topRightPosition.y; - if(!CC_EDITOR) { + if (!CC_EDITOR) { this._moveContentToTopLeft(scrollViewSize); } } }, - _convertToContentParentSpace: function(position) { - var scrollViewPositionInWorldSpace = this.node.convertToWorldSpace(position); - var contentParent = this.content.parent; + _convertToContentParentSpace (position) { + let scrollViewPositionInWorldSpace = this.node.convertToWorldSpace(position); + let contentParent = this.content.parent; return contentParent.convertToNodeSpaceAR(scrollViewPositionInWorldSpace); }, //this is for nested scrollview - _hasNestedViewGroup: function (event, captureListeners) { - if(event.eventPhase !== cc.Event.CAPTURING_PHASE) return; + _hasNestedViewGroup (event, captureListeners) { + if (event.eventPhase !== cc.Event.CAPTURING_PHASE) return; - if(captureListeners) { + if (captureListeners) { //captureListeners are arranged from child to parent - for(var i = 0; i < captureListeners.length; ++i){ - var item = captureListeners[i]; + for (let i = 0; i < captureListeners.length; ++i){ + let item = captureListeners[i]; - if(this.node === item) { - if(event.target.getComponent(cc.ViewGroup)) { + if (this.node === item) { + if (event.target.getComponent(cc.ViewGroup)) { return true; } return false; @@ -936,18 +934,18 @@ var ScrollView = cc.Class({ }, //This is for Scrollview as children of a Button - _stopPropagationIfTargetIsMe: function (event) { - if(event.eventPhase === cc.Event.AT_TARGET && event.target === this.node) { + _stopPropagationIfTargetIsMe (event) { + if (event.eventPhase === cc.Event.AT_TARGET && event.target === this.node) { event.stopPropagation(); } }, // touch event handler - _onTouchBegan: function(event, captureListeners) { + _onTouchBegan (event, captureListeners) { if (!this.enabledInHierarchy) return; if (this._hasNestedViewGroup(event, captureListeners)) return; - var touch = event.touch; + let touch = event.touch; if (this.content) { this._handlePressLogic(touch); } @@ -955,11 +953,11 @@ var ScrollView = cc.Class({ this._stopPropagationIfTargetIsMe(event); }, - _onTouchMoved: function(event, captureListeners) { + _onTouchMoved (event, captureListeners) { if (!this.enabledInHierarchy) return; if (this._hasNestedViewGroup(event, captureListeners)) return; - var touch = event.touch; + let touch = event.touch; if (this.content) { this._handleMoveLogic(touch); } @@ -968,12 +966,12 @@ var ScrollView = cc.Class({ return; } - var deltaMove = touch.getLocation().sub(touch.getStartLocation()); + let deltaMove = touch.getLocation().sub(touch.getStartLocation()); //FIXME: touch move delta should be calculated by DPI. if (deltaMove.mag() > 7) { if (!this._touchMoved && event.target !== this.node) { // Simulate touch cancel for target node - var cancelEvent = new cc.Event.EventTouch(event.getTouches(), event.bubbles); + let cancelEvent = new cc.Event.EventTouch(event.getTouches(), event.bubbles); cancelEvent.type = cc.Node.EventType.TOUCH_CANCEL; cancelEvent.touch = event.touch; cancelEvent.simulate = true; @@ -984,13 +982,13 @@ var ScrollView = cc.Class({ this._stopPropagationIfTargetIsMe(event); }, - _onTouchEnded: function(event, captureListeners) { + _onTouchEnded (event, captureListeners) { if (!this.enabledInHierarchy) return; if (this._hasNestedViewGroup(event, captureListeners)) return; this._dispatchEvent('touch-up'); - var touch = event.touch; + let touch = event.touch; if (this.content) { this._handleReleaseLogic(touch); } @@ -1001,13 +999,13 @@ var ScrollView = cc.Class({ } }, - _onTouchCancelled: function(event, captureListeners) { + _onTouchCancelled (event, captureListeners) { if (!this.enabledInHierarchy) return; if (this._hasNestedViewGroup(event, captureListeners)) return; // Filte touch cancel event send from self if (!event.simulate) { - var touch = event.touch; + let touch = event.touch; if(this.content){ this._handleReleaseLogic(touch); } @@ -1015,21 +1013,21 @@ var ScrollView = cc.Class({ this._stopPropagationIfTargetIsMe(event); }, - _processDeltaMove: function(deltaMove) { + _processDeltaMove (deltaMove) { this._scrollChildren(deltaMove); this._gatherTouchMove(deltaMove); }, - _handleMoveLogic: function(touch) { - var deltaMove = touch.getDelta(); + _handleMoveLogic (touch) { + let deltaMove = touch.getDelta(); this._processDeltaMove(deltaMove); }, - _scrollChildren: function(deltaMove) { + _scrollChildren (deltaMove) { deltaMove = this._clampDelta(deltaMove); - var realMove = deltaMove; - var outOfBoundary; + let realMove = deltaMove; + let outOfBoundary; if (this.elastic) { outOfBoundary = this._getHowMuchOutOfBoundary(); realMove.x *= (outOfBoundary.x === 0 ? 1 : 0.5); @@ -1041,30 +1039,30 @@ var ScrollView = cc.Class({ realMove = realMove.add(outOfBoundary); } - var scrollEventType = -1; + let scrollEventType = -1; if (realMove.y > 0) { //up - var icBottomPos = this.content.y - this.content.anchorY * this.content.height; + let icBottomPos = this.content.y - this.content.anchorY * this.content.height; if (icBottomPos + realMove.y > this._bottomBoundary) { scrollEventType = 'scroll-to-bottom'; } } else if (realMove.y < 0) { //down - var icTopPos = this.content.y - this.content.anchorY * this.content.height + this.content.height; + let icTopPos = this.content.y - this.content.anchorY * this.content.height + this.content.height; - if(icTopPos + realMove.y <= this._topBoundary) { + if (icTopPos + realMove.y <= this._topBoundary) { scrollEventType = 'scroll-to-top'; } } else if (realMove.x < 0) { //left - var icRightPos = this.content.x - this.content.anchorX * this.content.width + this.content.width; + let icRightPos = this.content.x - this.content.anchorX * this.content.width + this.content.width; if (icRightPos + realMove.x <= this._rightBoundary) { scrollEventType = 'scroll-to-right'; } } else if (realMove.x > 0) { //right - var icLeftPos = this.content.x - this.content.anchorX * this.content.width; + let icLeftPos = this.content.x - this.content.anchorX * this.content.width; if (icLeftPos + realMove.x >= this._leftBoundary) { scrollEventType = 'scroll-to-left'; } @@ -1072,8 +1070,7 @@ var ScrollView = cc.Class({ this._moveContent(realMove, false); - if(realMove.x !== 0 || realMove.y !== 0) - { + if (realMove.x !== 0 || realMove.y !== 0) { if (!this._scrolling) { this._scrolling = true; this._dispatchEvent('scroll-began'); @@ -1087,7 +1084,7 @@ var ScrollView = cc.Class({ }, - _handlePressLogic: function() { + _handlePressLogic () { if (this._autoScrolling) { this._dispatchEvent('scroll-ended'); } @@ -1101,9 +1098,9 @@ var ScrollView = cc.Class({ this._onScrollBarTouchBegan(); }, - _clampDelta: function(delta) { - var contentSize = this.content.getContentSize(); - var scrollViewSize = this.node.getContentSize(); + _clampDelta (delta) { + let contentSize = this.content.getContentSize(); + let scrollViewSize = this.node.getContentSize(); if (contentSize.width < scrollViewSize.width) { delta.x = 0; } @@ -1114,7 +1111,7 @@ var ScrollView = cc.Class({ return delta; }, - _gatherTouchMove: function(delta) { + _gatherTouchMove (delta) { delta = this._clampDelta(delta); while (this._touchMoveDisplacements.length >= NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED) { @@ -1124,24 +1121,24 @@ var ScrollView = cc.Class({ this._touchMoveDisplacements.push(delta); - var timeStamp = getTimeInMilliseconds(); + let timeStamp = getTimeInMilliseconds(); this._touchMoveTimeDeltas.push((timeStamp - this._touchMovePreviousTimestamp) / 1000); this._touchMovePreviousTimestamp = timeStamp; }, - _startBounceBackIfNeeded: function() { + _startBounceBackIfNeeded () { if (!this.elastic) { return false; } - var bounceBackAmount = this._getHowMuchOutOfBoundary(); + let bounceBackAmount = this._getHowMuchOutOfBoundary(); bounceBackAmount = this._clampDelta(bounceBackAmount); if (bounceBackAmount.fuzzyEquals(cc.v2(0, 0), EPSILON)) { return false; } - var bounceBackTime = Math.max(this.bounceDuration, 0); + let bounceBackTime = Math.max(this.bounceDuration, 0); this._startAutoScroll(bounceBackAmount, bounceBackTime, true); if (!this._isBouncing) { @@ -1155,10 +1152,10 @@ var ScrollView = cc.Class({ return true; }, - _processInertiaScroll: function () { - var bounceBackStarted = this._startBounceBackIfNeeded(); + _processInertiaScroll () { + let bounceBackStarted = this._startBounceBackIfNeeded(); if (!bounceBackStarted && this.inertia) { - var touchMoveVelocity = this._calculateTouchMoveVelocity(); + let touchMoveVelocity = this._calculateTouchMoveVelocity(); if (!touchMoveVelocity.fuzzyEquals(cc.v2(0, 0), EPSILON) && this.brake < 1) { this._startInertiaScroll(touchMoveVelocity); } @@ -1167,8 +1164,8 @@ var ScrollView = cc.Class({ this._onScrollBarTouchEnded(); }, - _handleReleaseLogic: function(touch) { - var delta = touch.getDelta(); + _handleReleaseLogic (touch) { + let delta = touch.getDelta(); this._gatherTouchMove(delta); this._processInertiaScroll(); @@ -1180,12 +1177,12 @@ var ScrollView = cc.Class({ } }, - _isOutOfBoundary: function() { - var outOfBoundary = this._getHowMuchOutOfBoundary(); + _isOutOfBoundary () { + let outOfBoundary = this._getHowMuchOutOfBoundary(); return !outOfBoundary.fuzzyEquals(cc.v2(0, 0), EPSILON); }, - _isNecessaryAutoScrollBrake: function() { + _isNecessaryAutoScrollBrake () { if (this._autoScrollBraking) { return true; } @@ -1205,39 +1202,38 @@ var ScrollView = cc.Class({ return false; }, - getScrollEndedEventTiming: function () { + getScrollEndedEventTiming () { return EPSILON; }, - - _processAutoScrolling: function(dt) { - var isAutoScrollBrake = this._isNecessaryAutoScrollBrake(); - var brakingFactor = isAutoScrollBrake ? OUT_OF_BOUNDARY_BREAKING_FACTOR : 1; + _processAutoScrolling (dt) { + let isAutoScrollBrake = this._isNecessaryAutoScrollBrake(); + let brakingFactor = isAutoScrollBrake ? OUT_OF_BOUNDARY_BREAKING_FACTOR : 1; this._autoScrollAccumulatedTime += dt * (1 / brakingFactor); - var percentage = Math.min(1, this._autoScrollAccumulatedTime / this._autoScrollTotalTime); + let percentage = Math.min(1, this._autoScrollAccumulatedTime / this._autoScrollTotalTime); if (this._autoScrollAttenuate) { percentage = quintEaseOut(percentage); } - var newPosition = this._autoScrollStartPosition.add(this._autoScrollTargetDelta.mul(percentage)); - var reachedEnd = Math.abs(percentage - 1) <= EPSILON; + let newPosition = this._autoScrollStartPosition.add(this._autoScrollTargetDelta.mul(percentage)); + let reachedEnd = Math.abs(percentage - 1) <= EPSILON; - var fireEvent = Math.abs(percentage - 1) <= this.getScrollEndedEventTiming(); - if(fireEvent && !this._isScrollEndedWithThresholdEventFired) { + let fireEvent = Math.abs(percentage - 1) <= this.getScrollEndedEventTiming(); + if (fireEvent && !this._isScrollEndedWithThresholdEventFired) { this._dispatchEvent('scroll-ended-with-threshold'); this._isScrollEndedWithThresholdEventFired = true; } if (this.elastic) { - var brakeOffsetPosition = newPosition.sub(this._autoScrollBrakingStartPosition); + let brakeOffsetPosition = newPosition.sub(this._autoScrollBrakingStartPosition); if (isAutoScrollBrake) { brakeOffsetPosition = brakeOffsetPosition.mul(brakingFactor); } newPosition = this._autoScrollBrakingStartPosition.add(brakeOffsetPosition); } else { - var moveDelta = newPosition.sub(this.getContentPosition()); - var outOfBoundary = this._getHowMuchOutOfBoundary(moveDelta); + let moveDelta = newPosition.sub(this.getContentPosition()); + let outOfBoundary = this._getHowMuchOutOfBoundary(moveDelta); if (!outOfBoundary.fuzzyEquals(cc.v2(0, 0), EPSILON)) { newPosition = newPosition.add(outOfBoundary); reachedEnd = true; @@ -1248,7 +1244,7 @@ var ScrollView = cc.Class({ this._autoScrolling = false; } - var contentPos = newPosition.sub(this.getContentPosition()); + let deltaMove = newPosition.sub(this.getContentPosition()); this._moveContent(this._clampDelta(deltaMove), reachedEnd); this._dispatchEvent('scrolling'); @@ -1258,65 +1254,63 @@ var ScrollView = cc.Class({ } }, - _startInertiaScroll: function(touchMoveVelocity) { - var inertiaTotalMovement = touchMoveVelocity.mul(MOVEMENT_FACTOR); + _startInertiaScroll (touchMoveVelocity) { + let inertiaTotalMovement = touchMoveVelocity.mul(MOVEMENT_FACTOR); this._startAttenuatingAutoScroll(inertiaTotalMovement, touchMoveVelocity); }, - _calculateAttenuatedFactor: function (distance) { - if(this.brake <= 0){ + _calculateAttenuatedFactor (distance) { + if (this.brake <= 0){ return (1 - this.brake); } //attenuate formula from: http://learnopengl.com/#!Lighting/Light-casters - var attenuatedFactor = (1 - this.brake) * (1 / (1 + distance * 0.000014 + distance * distance * 0.000000008) ); - return attenuatedFactor; + return (1 - this.brake) * (1 / (1 + distance * 0.000014 + distance * distance * 0.000000008)); }, - _startAttenuatingAutoScroll: function(deltaMove, initialVelocity) { - var time = this._calculateAutoScrollTimeByInitalSpeed(initialVelocity.mag()); + _startAttenuatingAutoScroll (deltaMove, initialVelocity) { + let time = this._calculateAutoScrollTimeByInitalSpeed(initialVelocity.mag()); - var targetDelta = deltaMove.normalize(); - var contentSize = this.content.getContentSize(); - var scrollviewSize = this.node.getContentSize(); + let targetDelta = deltaMove.normalize(); + let contentSize = this.content.getContentSize(); + let scrollviewSize = this.node.getContentSize(); - var totalMoveWidth = (contentSize.width - scrollviewSize.width); - var totalMoveHeight = (contentSize.height - scrollviewSize.height); + let totalMoveWidth = (contentSize.width - scrollviewSize.width); + let totalMoveHeight = (contentSize.height - scrollviewSize.height); - var attenuatedFactorX = this._calculateAttenuatedFactor(totalMoveWidth); - var attenuatedFactorY = this._calculateAttenuatedFactor(totalMoveHeight); + let attenuatedFactorX = this._calculateAttenuatedFactor(totalMoveWidth); + let attenuatedFactorY = this._calculateAttenuatedFactor(totalMoveHeight); targetDelta = cc.v2(targetDelta.x * totalMoveWidth * (1 - this.brake) * attenuatedFactorX, targetDelta.y * totalMoveHeight * attenuatedFactorY * (1 - this.brake)); - var originalMoveLength = deltaMove.mag(); - var factor = targetDelta.mag() / originalMoveLength; + let originalMoveLength = deltaMove.mag(); + let factor = targetDelta.mag() / originalMoveLength; targetDelta = targetDelta.add(deltaMove); - if(this.brake > 0 && factor > 7) { + if (this.brake > 0 && factor > 7) { factor = Math.sqrt(factor); targetDelta = deltaMove.mul(factor).add(deltaMove); } - if(this.brake > 0 && factor > 3) { + if (this.brake > 0 && factor > 3) { factor = 3; time = time * factor; } - if(this.brake === 0 && factor > 1) { + if (this.brake === 0 && factor > 1) { time = time * factor; } this._startAutoScroll(targetDelta, time, true); }, - _calculateAutoScrollTimeByInitalSpeed: function(initalSpeed) { - var time = Math.sqrt(Math.sqrt(initalSpeed / 5)); - return time; + _calculateAutoScrollTimeByInitalSpeed (initalSpeed) { + return Math.sqrt(Math.sqrt(initalSpeed / 5)); }, - _startAutoScroll: function(deltaMove, timeInSecond, attenuated) { - var adjustedDeltaMove = this._flattenVectorByDirection(deltaMove); + _startAutoScroll (deltaMove, timeInSecond, attenuated) { + let adjustedDeltaMove = this._flattenVectorByDirection(deltaMove); this._autoScrolling = true; this._autoScrollTargetDelta = adjustedDeltaMove; @@ -1328,10 +1322,10 @@ var ScrollView = cc.Class({ this._isScrollEndedWithThresholdEventFired = false; this._autoScrollBrakingStartPosition = cc.v2(0, 0); - var currentOutOfBoundary = this._getHowMuchOutOfBoundary(); + let currentOutOfBoundary = this._getHowMuchOutOfBoundary(); if (!currentOutOfBoundary.fuzzyEquals(cc.v2(0, 0), EPSILON)) { this._autoScrollCurrentlyOutOfBoundary = true; - var afterOutOfBoundary = this._getHowMuchOutOfBoundary(adjustedDeltaMove); + let afterOutOfBoundary = this._getHowMuchOutOfBoundary(adjustedDeltaMove); if (currentOutOfBoundary.x * afterOutOfBoundary.x > 0 || currentOutOfBoundary.y * afterOutOfBoundary.y > 0) { this._autoScrollBraking = true; @@ -1339,8 +1333,8 @@ var ScrollView = cc.Class({ } }, - _calculateTouchMoveVelocity: function() { - var totalTime = 0; + _calculateTouchMoveVelocity () { + let totalTime = 0; totalTime = this._touchMoveTimeDeltas.reduce(function(a, b) { return a + b; }, totalTime); @@ -1349,7 +1343,7 @@ var ScrollView = cc.Class({ return cc.v2(0, 0); } - var totalMovement = cc.v2(0, 0); + let totalMovement = cc.v2(0, 0); totalMovement = this._touchMoveDisplacements.reduce(function(a, b) { return a.add(b); }, totalMovement); @@ -1358,20 +1352,20 @@ var ScrollView = cc.Class({ totalMovement.y * (1 - this.brake) / totalTime); }, - _flattenVectorByDirection: function(vector) { - var result = vector; + _flattenVectorByDirection (vector) { + let result = vector; result.x = this.horizontal ? result.x : 0; result.y = this.vertical ? result.y : 0; return result; }, - _moveContent: function(deltaMove, canStartBounceBack) { - var adjustedMove = this._flattenVectorByDirection(deltaMove); - var newPosition = this.getContentPosition().add(adjustedMove); + _moveContent (deltaMove, canStartBounceBack) { + let adjustedMove = this._flattenVectorByDirection(deltaMove); + let newPosition = this.getContentPosition().add(adjustedMove); this.setContentPosition(newPosition); - var outOfBoundary = this._getHowMuchOutOfBoundary(); + let outOfBoundary = this._getHowMuchOutOfBoundary(); this._updateScrollBar(outOfBoundary); if (this.elastic && canStartBounceBack) { @@ -1379,35 +1373,33 @@ var ScrollView = cc.Class({ } }, - _getContentLeftBoundary: function() { - var contentPos = this.getContentPosition(); - var leftBoundary = contentPos.x - this.content.getAnchorPoint().x * this.content.getContentSize().width; - return leftBoundary; + _getContentLeftBoundary () { + let contentPos = this.getContentPosition(); + return contentPos.x - this.content.getAnchorPoint().x * this.content.getContentSize().width; }, - _getContentRightBoundary: function() { - var contentSize = this.content.getContentSize(); + _getContentRightBoundary () { + let contentSize = this.content.getContentSize(); return this._getContentLeftBoundary() + contentSize.width; }, - _getContentTopBoundary: function() { - var contentSize = this.content.getContentSize(); + _getContentTopBoundary () { + let contentSize = this.content.getContentSize(); return this._getContentBottomBoundary() + contentSize.height; }, - _getContentBottomBoundary: function() { - var contentPos = this.getContentPosition(); - var bottomBoundary = contentPos.y - this.content.getAnchorPoint().y * this.content.getContentSize().height; - return bottomBoundary; + _getContentBottomBoundary () { + let contentPos = this.getContentPosition(); + return contentPos.y - this.content.getAnchorPoint().y * this.content.getContentSize().height; }, - _getHowMuchOutOfBoundary: function(addition) { + _getHowMuchOutOfBoundary (addition) { addition = addition || cc.v2(0, 0); if (addition.fuzzyEquals(cc.v2(0, 0), EPSILON) && !this._outOfBoundaryAmountDirty) { return this._outOfBoundaryAmount; } - var outOfBoundaryAmount = cc.v2(0, 0); + let outOfBoundaryAmount = cc.v2(0, 0); if (this._getContentLeftBoundary() + addition.x > this._leftBoundary) { outOfBoundaryAmount.x = this._leftBoundary - (this._getContentLeftBoundary() + addition.x); } else if (this._getContentRightBoundary() + addition.x < this._rightBoundary) { @@ -1430,7 +1422,7 @@ var ScrollView = cc.Class({ return outOfBoundaryAmount; }, - _updateScrollBar: function(outOfBoundary) { + _updateScrollBar (outOfBoundary) { if (this.horizontalScrollBar) { this.horizontalScrollBar._onScroll(outOfBoundary); } @@ -1440,7 +1432,7 @@ var ScrollView = cc.Class({ } }, - _onScrollBarTouchBegan: function() { + _onScrollBarTouchBegan () { if (this.horizontalScrollBar) { this.horizontalScrollBar._onTouchBegan(); } @@ -1450,7 +1442,7 @@ var ScrollView = cc.Class({ } }, - _onScrollBarTouchEnded: function() { + _onScrollBarTouchEnded () { if (this.horizontalScrollBar) { this.horizontalScrollBar._onTouchEnded(); } @@ -1460,7 +1452,7 @@ var ScrollView = cc.Class({ } }, - _dispatchEvent: function(event) { + _dispatchEvent (event) { if (event === 'scroll-ended') { this._scrollEventEmitMask = 0; @@ -1469,7 +1461,7 @@ var ScrollView = cc.Class({ || event === 'scroll-to-left' || event === 'scroll-to-right') { - var flag = (1 << eventMap[event]); + let flag = (1 << eventMap[event]); if (this._scrollEventEmitMask & flag) { return; } else { @@ -1481,28 +1473,28 @@ var ScrollView = cc.Class({ this.node.emit(event, this); }, - _adjustContentOutOfBoundary: function () { + _adjustContentOutOfBoundary () { this._outOfBoundaryAmountDirty = true; - if(this._isOutOfBoundary()) { - var outOfBoundary = this._getHowMuchOutOfBoundary(cc.v2(0, 0)); - var newPosition = this.getContentPosition().add(outOfBoundary); - if(this.content) { + if (this._isOutOfBoundary()) { + let outOfBoundary = this._getHowMuchOutOfBoundary(cc.v2(0, 0)); + let newPosition = this.getContentPosition().add(outOfBoundary); + if (this.content) { this.content.setPosition(newPosition); this._updateScrollBar(0); } } }, - start: function() { + start () { this._calculateBoundary(); //Because widget component will adjust content position and scrollview position is correct after visit //So this event could make sure the content is on the correct position after loading. - if(this.content) { + if (this.content) { cc.director.once(cc.Director.EVENT_BEFORE_DRAW, this._adjustContentOutOfBoundary, this); } }, - _hideScrollbar: function () { + _hideScrollbar () { if (this.horizontalScrollBar) { this.horizontalScrollBar.hide(); } @@ -1512,7 +1504,7 @@ var ScrollView = cc.Class({ } }, - _showScrollbar: function () { + _showScrollbar () { if (this.horizontalScrollBar) { this.horizontalScrollBar.show(); } @@ -1522,12 +1514,12 @@ var ScrollView = cc.Class({ } }, - onDisable: function () { + onDisable () { if (!CC_EDITOR) { this._unregisterEvent(); this.node.off(NodeEvent.SIZE_CHANGED, this._calculateBoundary, this); this.node.off(NodeEvent.SCALE_CHANGED, this._calculateBoundary, this); - if(this.content) { + if (this.content) { this.content.off(NodeEvent.SIZE_CHANGED, this._calculateBoundary, this); this.content.off(NodeEvent.SCALE_CHANGED, this._calculateBoundary, this); } @@ -1536,12 +1528,12 @@ var ScrollView = cc.Class({ this.stopAutoScroll(); }, - onEnable: function () { + onEnable () { if (!CC_EDITOR) { this._registerEvent(); this.node.on(NodeEvent.SIZE_CHANGED, this._calculateBoundary, this); this.node.on(NodeEvent.SCALE_CHANGED, this._calculateBoundary, this); - if(this.content) { + if (this.content) { this.content.on(NodeEvent.SIZE_CHANGED, this._calculateBoundary, this); this.content.on(NodeEvent.SCALE_CHANGED, this._calculateBoundary, this); } @@ -1549,7 +1541,7 @@ var ScrollView = cc.Class({ this._showScrollbar(); }, - update: function(dt) { + update (dt) { if (this._autoScrolling) { this._processAutoScrolling(dt); }