scrollview-base-debug.js revision 50fe55a3f078d5dd70124b45a75271e75896be42
f69d245bb21be88752420e834a6b6be37e9b525fTrippYUI.add('scrollview-base', function(Y) {
f69d245bb21be88752420e834a6b6be37e9b525fTripp
f69d245bb21be88752420e834a6b6be37e9b525fTripp/**
f69d245bb21be88752420e834a6b6be37e9b525fTripp * The scrollview-base module provides a basic ScrollView Widget, without scrollbar indicators
f69d245bb21be88752420e834a6b6be37e9b525fTripp *
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @module scrollview-base
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTrippvar getClassName = Y.ClassNameManager.getClassName,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp SCROLLVIEW = 'scrollview',
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp CLASS_NAMES = {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp vertical: getClassName(SCROLLVIEW, 'vert'),
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp horizontal: getClassName(SCROLLVIEW, 'horiz')
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp EV_SCROLL_END = 'scrollEnd',
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp EV_SCROLL_FLICK = 'flick',
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp FLICK = EV_SCROLL_FLICK,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp UI = 'ui',
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp SCROLL_Y = "scrollY",
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp SCROLL_X = "scrollX",
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp BOUNCE = "bounce",
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp DIM_X = "x",
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp DIM_Y = "y",
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp BOUNDING_BOX = "boundingBox",
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp CONTENT_BOX = "contentBox",
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp NATIVE_TRANSITIONS = Y.Transition.useNative;
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTrippY.Node.DOM_EVENTS.DOMSubtreeModified = true;
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp/**
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * ScrollView provides a scrollable widget, supporting flick gestures, across both touch and mouse based devices.
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp *
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @class ScrollView
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @namespace
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @param config {Object} Object literal with initial attribute values
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @extends Widget
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @constructor
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp */
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTrippfunction ScrollView() {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp ScrollView.superclass.constructor.apply(this, arguments);
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp}
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTrippY.ScrollView = Y.extend(ScrollView, Y.Widget, {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp // Y.ScrollView prototype
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp /**
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * Designated initializer
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp *
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @method initializer
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp */
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp initializer: function() {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp this._createEvents();
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp // Cache - they're write once, and not going to change
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this._cb = this.get(CONTENT_BOX);
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this._bb = this.get(BOUNDING_BOX);
422668e1d4513bb870b8b576fd9d828c8872f074Tripp },
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp /**
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * Publishes events which occur during the scroll lifecycle
422668e1d4513bb870b8b576fd9d828c8872f074Tripp *
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @method _createEvents
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @private
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
422668e1d4513bb870b8b576fd9d828c8872f074Tripp _createEvents: function() {
422668e1d4513bb870b8b576fd9d828c8872f074Tripp /**
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * Notification event fired at the end of a scroll transition
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp *
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @event scrollEnd
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @param e {EventFacade} The default event facade.
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this.publish(EV_SCROLL_END);
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp /**
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * Notification event fired at the end of a flick gesture (the flick animation may still be in progress)
422668e1d4513bb870b8b576fd9d828c8872f074Tripp *
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @event flick
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param e {EventFacade} The default event facade.
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp */
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp this.publish(EV_SCROLL_FLICK);
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp },
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp /**
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * Override the contentBox sizing method, since the contentBox height
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * should not be that of the boundingBox.
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp *
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @method _uiSizeCB
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @protected
422668e1d4513bb870b8b576fd9d828c8872f074Tripp */
422668e1d4513bb870b8b576fd9d828c8872f074Tripp _uiSizeCB: function() {},
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp /**
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * TranstionEnd event handler
422668e1d4513bb870b8b576fd9d828c8872f074Tripp *
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @method _transitionEnded
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @private
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
422668e1d4513bb870b8b576fd9d828c8872f074Tripp _transitionEnded: function() {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this.fire(EV_SCROLL_END);
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp },
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * bindUI implementation
422668e1d4513bb870b8b576fd9d828c8872f074Tripp *
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * Hooks up events for the widget
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @method bindUI
422668e1d4513bb870b8b576fd9d828c8872f074Tripp */
422668e1d4513bb870b8b576fd9d828c8872f074Tripp bindUI: function() {
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp var cb = this._cb,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp flick = this.get(FLICK);
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this._bb.on('gesturemovestart', Y.bind(this._onGestureMoveStart, this));
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp cb.on('transition:end', Y.bind(this._transitionEnded, this), false);
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp // TODO: Fires way to often when using non-native transitions, due to property change
422668e1d4513bb870b8b576fd9d828c8872f074Tripp if (NATIVE_TRANSITIONS) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp cb.on('DOMSubtreeModified', Y.bind(this._uiDimensionsChange, this));
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (flick) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp cb.on("flick", Y.bind(this._flick, this), flick);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this.after({
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp 'scrollYChange' : this._afterScrollYChange,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp 'scrollXChange' : this._afterScrollXChange,
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp 'heightChange' : this._afterHeightChange,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp 'widthChange' : this._afterWidthChange,
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp 'renderedChange': function() { Y.later(0, this, '_uiDimensionsChange'); }
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp });
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp },
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp /**
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * syncUI implementation
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp *
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * Update the scroll position, based on the current value of scrollY
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @method bindUI
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp syncUI: function() {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this.scrollTo(this.get(SCROLL_X), this.get(SCROLL_Y));
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * Scroll the element to a given y coordinate
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp *
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @method scrollTo
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @param x {Number} The x-position to scroll to
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @param y {Number} The y-position to scroll to
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param duration {Number} Duration, in ms, of the scroll animation (default is 0)
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param easing {String} An easing equation if duration is set
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp scrollTo: function(x, y, duration, easing) {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp var cb = this._cb,
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp xSet = (x !== null),
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp ySet = (y !== null),
422668e1d4513bb870b8b576fd9d828c8872f074Tripp xMove = (xSet) ? x * -1 : 0,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp yMove = (ySet) ? y * -1 : 0,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp transition;
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp duration = duration || 0;
422668e1d4513bb870b8b576fd9d828c8872f074Tripp easing = easing || ScrollView.EASING;
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp if (xSet) {
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this.set(SCROLL_X, x, { src: UI });
422668e1d4513bb870b8b576fd9d828c8872f074Tripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp if (ySet) {
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this.set(SCROLL_Y, y, { src: UI });
422668e1d4513bb870b8b576fd9d828c8872f074Tripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp transition = {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp easing : easing,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp duration : duration/1000
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp };
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp Y.log("Transition: duration, easing:" + transition.duration, transition.easing, "scrollview");
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (NATIVE_TRANSITIONS) {
422668e1d4513bb870b8b576fd9d828c8872f074Tripp transition.transform = 'translate('+ xMove +'px,'+ yMove +'px)';
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp } else {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (xSet) { transition.left = xMove + "px"; }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (ySet) { transition.top = yMove + "px"; }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp cb.transition(transition);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * gesturemovestart event handler
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp *
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @method _onGestureMoveStart
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @param e {Event.Facade} The gesturemovestart event facade
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @private
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp _onGestureMoveStart: function(e) {
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp e.preventDefault();
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp this._killTimer();
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp var bb = this._bb;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this._moveEvt = bb.on('gesturemove', Y.bind(this._onGestureMove, this));
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveEndEvt = bb.on('gesturemoveend', Y.bind(this._onGestureMoveEnd, this));
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveStartY = e.clientY + this.get(SCROLL_Y);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveStartX = e.clientX + this.get(SCROLL_X);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveStartTime = (new Date()).getTime();
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp this._moveStartClientY = e.clientY;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveStartClientX = e.clientX;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._isDragging = false;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._snapToEdge = false;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * gesturemove event handler
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp *
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @method _onGestureMove
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param e {Event.Facade} The gesturemove event facade
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @private
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp _onGestureMove: function(e) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp e.preventDefault();
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp this._isDragging = true;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveEndClientY = e.clientY;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveEndClientX = e.clientX;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._lastMoved = (new Date()).getTime();
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(this._scrollsVertical) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this.set(SCROLL_Y, -(e.clientY - this._moveStartY));
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp }
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(this._scrollsHorizontal) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this.set(SCROLL_X, -(e.clientX - this._moveStartX));
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp },
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * gestureend event handler
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp *
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @method _onGestureMoveEnd
cc21b565833307c2b0b06deb4e3ab22c2a94be3eTripp * @param e {Event.Facade} The gesturemoveend event facade
422668e1d4513bb870b8b576fd9d828c8872f074Tripp * @private
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp */
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp _onGestureMoveEnd: function(e) {
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp e.preventDefault();
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp var minY = this._minScrollY,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp maxY = this._maxScrollY,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp minX = this._minScrollX,
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp maxX = this._maxScrollX,
f69d245bb21be88752420e834a6b6be37e9b525fTripp x,
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp y,
422668e1d4513bb870b8b576fd9d828c8872f074Tripp startPoint = this._scrollsVertical ? this._moveStartClientY : this._moveStartClientX,
422668e1d4513bb870b8b576fd9d828c8872f074Tripp endPoint = this._scrollsVertical ? this._moveEndClientY : this._moveEndClientX,
422668e1d4513bb870b8b576fd9d828c8872f074Tripp distance = startPoint - endPoint;
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
f69d245bb21be88752420e834a6b6be37e9b525fTripp this._moveEvt.detach();
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._moveEndEvt.detach();
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._scrolledHalfway = false;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._snapToEdge = false;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._isDragging = false;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(this._scrollsHorizontal && Math.abs(distance) > (this.get('width')/2)) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._scrolledHalfway = true;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._scrolledForward = distance > 0;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(this._scrollsVertical && Math.abs(distance) > (this.get('height')/2)) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._scrolledHalfway = true;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._scrolledForward = distance > 0;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp // Check for minY/maxY
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (this._scrollsVertical) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp y = this.get(SCROLL_Y);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(y < minY) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp y = minY;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._snapToEdge = true;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
422668e1d4513bb870b8b576fd9d828c8872f074Tripp
422668e1d4513bb870b8b576fd9d828c8872f074Tripp if (y > maxY) {
422668e1d4513bb870b8b576fd9d828c8872f074Tripp y = maxY;
422668e1d4513bb870b8b576fd9d828c8872f074Tripp this._snapToEdge = true;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (this._snapToEdge) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this.set(SCROLL_Y, y);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp // Check for minX/maxX
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (this._scrollsHorizontal) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp x = this.get(SCROLL_X);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(x < minX) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp x = minX;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._snapToEdge = true;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (x > maxX) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp x = maxX;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this._snapToEdge = true;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if (this._snapToEdge) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this.set(SCROLL_X, x);
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(this._snapToEdge) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp return;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp // Check for staleness
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp if(+(new Date()) - this._moveStartTime > 100) {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp this.fire(EV_SCROLL_END, {
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp staleScroll: true
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp });
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp return;
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * after listener for changes to the scrollY attr
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp *
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @method _afterScrollYChange
f69d245bb21be88752420e834a6b6be37e9b525fTripp * @param e {Event.Facade} The event
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp * @protected
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp */
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp _afterScrollYChange : function(e) {
8209f3939e32e0e5bde64192267fdaf9db6f4fbcTripp if(e.src !== UI) {
f69d245bb21be88752420e834a6b6be37e9b525fTripp this._uiScrollY(e.newVal, e.duration, e.easing);
f69d245bb21be88752420e834a6b6be37e9b525fTripp }
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp },
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp /**
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * Update the UI when the scrollY attr changes
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp *
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @method _uiScrollY
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param val {Number} The scrollY value
e393eced613f9b4a5fb6bdd461d0e0bf5064d5ecTripp * @param duration {Number} The length (in ms) of the scroll animation
* @param easing {String} An easing equation, if duration is defined
* @protected
*/
_uiScrollY : function(val, duration, easing) {
duration = duration || this._snapToEdge ? 400 : 0;
easing = easing || this._snapToEdge ? ScrollView.SNAP_EASING : null;
this.scrollTo(null, val, duration, easing);
},
/**
* after listener for changes to the scrollX attr
*
* @method _afterScrollXChange
* @param e {Event.Facade} The event
* @protected
*/
_afterScrollXChange : function(e) {
if(e.src !== UI) {
this._uiScrollX(e.newVal, e.duration, e.easing);
}
},
/**
* Update the UI when the scrollX attr changes
*
* @method _uiScrollX
* @param val {Number} The scrollX value
* @param duration {Number} The length (in ms) of the scroll animation
* @param easing {String} An easing equation, if duration is defined
* @protected
*/
_uiScrollX : function(val, duration, easing) {
duration = duration || this._snapToEdge ? 400 : 0;
easing = easing || this._snapToEdge ? ScrollView.SNAP_EASING : null;
this.scrollTo(val, null, duration, easing);
},
/**
* after listener for the height attribute
*
* @method _afterHeightChange
* @param e {Event.Facade} The event
* @protected
*/
_afterHeightChange: function() {
this._uiDimensionsChange();
},
/**
* after listener for the width attribute
*
* @method _afterHeightChange
* @param e {Event.Facade} The event
* @protected
*/
_afterWidthChange: function() {
this._uiDimensionsChange();
},
/**
* This method gets invoked whenever the height or width attrs change,
* allowing us to determine which scrolling axes need to be enabled.
*
* @method _uiDimensionsChange
* @protected
*/
_uiDimensionsChange: function() {
var bb = this._bb,
height = this.get('height'),
width = this.get('width'),
// Use bb instead of cb. cb doesn't gives us the right results
// in FF (due to overflow:hidden)
scrollHeight = bb.get('scrollHeight'),
scrollWidth = bb.get('scrollWidth');
if(height && scrollHeight > height) {
this._scrollsVertical = true;
this._maxScrollY = scrollHeight - height;
this._minScrollY = 0;
this._scrollHeight = scrollHeight;
bb.addClass(ScrollView.CLASS_NAMES.vertical);
}
if(width && scrollWidth > width) {
this._scrollsHorizontal = true;
this._maxScrollX = scrollWidth - width;
this._minScrollX = 0;
this._scrollWidth = scrollWidth;
bb.addClass(ScrollView.CLASS_NAMES.horizontal);
}
},
/**
* Execute a flick at the end of a scroll action
*
* @method _flick
* @param distance {Number} The distance (in px) the user scrolled before the flick
* @param time {Number} The number of ms the scroll event lasted before the flick
* @protected
*/
_flick: function(e) {
var flick = e.flick;
this._currentVelocity = flick.velocity;
this._flicking = true;
this._decelCached = this.get('deceleration');
this._bounceCached = this.get('bounce');
this._flickFrame();
this.fire(EV_SCROLL_FLICK);
},
/**
* Execute a single frame in the flick animation
*
* @method _flickFrame
* @protected
*/
_flickFrame: function() {
var newY,
maxY,
minY,
newX,
maxX,
minX,
scrollsVertical = this._scrollsVertical,
scrollsHorizontal = this._scrollsHorizontal,
deceleration = this._decelCached,
bounce = this._bounceCached,
step = ScrollView.FRAME_STEP;
if(scrollsVertical) {
maxY = this._maxScrollY;
minY = this._minScrollY;
newY = this.get(SCROLL_Y) - (this._currentVelocity * step);
}
if(scrollsHorizontal) {
maxX = this._maxScrollX;
minX = this._minScrollX;
newX = this.get(SCROLL_X) - (this._currentVelocity * step);
}
this._currentVelocity = (this._currentVelocity * deceleration);
if(Math.abs(this._currentVelocity).toFixed(4) <= 0.015) {
this._flicking = false;
this._killTimer(!(this._exceededYBoundary || this._exceededXBoundary));
if(scrollsVertical) {
if(newY < minY) {
this._snapToEdge = true;
this.set(SCROLL_Y, minY);
} else if(newY > maxY) {
this._snapToEdge = true;
this.set(SCROLL_Y, maxY);
}
}
if(scrollsHorizontal) {
if(newX < minX) {
this._snapToEdge = true;
this.set(SCROLL_X, minX);
} else if(newX > maxX) {
this._snapToEdge = true;
this.set(SCROLL_X, maxX);
}
}
return;
}
if (scrollsVertical) {
if (newY < minY || newY > maxY) {
this._exceededYBoundary = true;
this._currentVelocity *= bounce;
}
this.set(SCROLL_Y, newY);
}
if (scrollsHorizontal) {
if (newX < minX || newX > maxX) {
this._exceededXBoundary = true;
this._currentVelocity *= bounce;
}
this.set(SCROLL_X, newX);
}
if (!this._flickTimer) {
this._flickTimer = Y.later(step, this, '_flickFrame', null, true);
}
},
/**
* Stop the animation timer
*
* @method _killTimer
* @param fireEvent {Boolean} If true, fire the scrollEnd event
* @private
*/
_killTimer: function(fireEvent) {
if(this._flickTimer) {
this._flickTimer.cancel();
this._flickTimer = null;
}
if(fireEvent) {
this.fire(EV_SCROLL_END);
}
},
/**
* @method _setScroll
* @private
* @param {Number} val
* @param {String} dim
*/
_setScroll : function(val, dim) {
var bouncing = this._cachedBounce || this.get(BOUNCE),
range = ScrollView.BOUNCE_RANGE,
maxScroll = (dim == DIM_X) ? this._maxScrollX : this._maxScrollY,
min = bouncing ? -range : 0,
max = bouncing ? maxScroll + range : maxScroll;
if(!bouncing || !this._isDragging) {
if(val < min) {
val = min;
} else if(val > max) {
val = max;
}
}
return val;
},
/**
* Setter for the scrollX ATTR
*
* @method _setScrollX
* @param val {Number} The new scrollX value
* @protected
*/
_setScrollX: function(val) {
return this._setScroll(val, DIM_X);
},
/**
* Setter for the scrollY ATTR
*
* @method _setScrollY
* @param val {Number} The new scrollY value
* @protected
*/
_setScrollY: function(val) {
return this._setScroll(val, DIM_Y);
}
}, {
// Y.ScrollView static properties
/**
* The identity of the widget.
*
* @property ScrollView.NAME
* @type String
* @default 'scrollview'
* @readOnly
* @protected
* @static
*/
NAME: 'scrollview',
/**
* Static property used to define the default attribute configuration of
* the Widget.
*
* @property ScrollView.ATTRS
* @type {Object}
* @protected
* @static
*/
ATTRS: {
/**
* The scroll position in the y-axis
*
* @attribute scrollY
* @type Number
* @default 0
*/
scrollY: {
value: 0,
setter: '_setScrollY'
},
/**
* The scroll position in the x-axis
*
* @attribute scrollX
* @type Number
* @default 0
*/
scrollX: {
value: 0,
setter: '_setScrollX'
},
/**
* Drag coefficent for inertial scrolling. The closer to 1 this
* value is, the less friction during scrolling.
*
* @attribute deceleration
* @default 0.93
*/
deceleration: {
value: 0.93
},
/**
* Drag coefficient for intertial scrolling at the upper
* and lower boundaries of the scrollview. Set to 0 to
* disable "rubber-banding".
*
* @attribute bounce
* @type Number
* @default 0.1
*/
bounce: {
value: 0.1
},
/**
* The minimum distance and/or velocity which define a flick
*
* @attribute flick
* @type Object
* @default Object with properties minDistance = 10, minVelocity = 0.3.
*/
flick: {
value: {
minDistance: 10,
minVelocity: 0.3
}
}
},
/**
* List of class names used in the scrollview's DOM
*
* @property ScrollView.CLASS_NAMES
* @type Object
* @static
*/
CLASS_NAMES: CLASS_NAMES,
/**
* Flag used to source property changes initiated from the DOM
*
* @property ScrollView.UI_SRC
* @type String
* @static
* @default "ui"
*/
UI_SRC: UI,
/**
* The default bounce distance in pixels
*
* @property ScrollView.BOUNCE_RANGE
* @type Number
* @static
* @default 150
*/
BOUNCE_RANGE : 150,
/**
* The step amount used when animating the flick
*
* @property ScrollView.FRAME_STEP
* @type Number
* @static
* @default 30
*/
FRAME_STEP : 30,
/**
* The default easing used when animating the flick
*
* @property ScrollView.EASING
* @type String
* @static
* @default 'cubic-bezier(0, 0.1, 0, 1.0)'
*/
EASING : 'cubic-bezier(0, 0.1, 0, 1.0)',
/**
* The default easing to use when animatiing the bounce snap back.
*
* @property ScrollView.SNAP_EASING
* @type String
* @static
* @default 'ease-out'
*/
SNAP_EASING : 'ease-out'
});
}, '@VERSION@' ,{skinnable:true, requires:['widget', 'event-gestures', 'transition']});