scrollview-base.js revision 1173c2328981243ad8284f9fceb86f4ea1773fd8
/**
* The scrollview-base module provides a basic ScrollView Widget, without scrollbar indicators
*
* @module scrollview-base
*/
SCROLLVIEW = 'scrollview',
CLASS_NAMES = {
},
EV_SCROLL_END = 'scrollEnd',
EV_SCROLL_FLICK = 'flick',
UI = 'ui',
LEFT = "left",
TOP = "top",
PX = "px",
SCROLL_Y = "scrollY",
SCROLL_X = "scrollX",
BOUNCE = "bounce",
DIM_X = "x",
DIM_Y = "y",
BOUNDING_BOX = "boundingBox",
CONTENT_BOX = "contentBox",
EMPTY = "",
ZERO = "0s",
/**
* ScrollView provides a scrollable widget, supporting flick gestures, across both touch and mouse based devices.
*
* @class ScrollView
* @namespace
* @param config {Object} Object literal with initial attribute values
* @extends Widget
* @constructor
*/
function ScrollView() {
}
// Y.ScrollView prototype
/**
* Designated initializer
*
* @method initializer
*/
initializer: function() {
this._createEvents();
// Cache - they're write once, and not going to change
},
/**
* Publishes events which occur during the scroll lifecycle
*
* @method _createEvents
* @private
*/
_createEvents: function() {
/**
* Notification event fired at the end of a scroll transition
*
* @event scrollEnd
* @param e {EventFacade} The default event facade.
*/
this.publish(EV_SCROLL_END);
/**
* Notification event fired at the end of a flick gesture (the flick animation may still be in progress)
*
* @event flick
* @param e {EventFacade} The default event facade.
*/
this.publish(EV_SCROLL_FLICK);
},
/**
* Override the contentBox sizing method, since the contentBox height
* should not be that of the boundingBox.
*
* @method _uiSizeCB
* @protected
*/
_uiSizeCB: function() {},
/**
* Content box transition callback
*
* @method _transitionEnded
* @param {Event.Facade} e The event facade
* @private
*/
_transitionEnded: function(e) {
this.fire(EV_SCROLL_END);
},
/**
* bindUI implementation
*
* Hooks up events for the widget
* @method bindUI
*/
bindUI: function() {
// TODO: Fires way to often when using non-native transitions, due to property change
if (NATIVE_TRANSITIONS) {
}
if (flick) {
}
this.after({
'scrollYChange' : this._afterScrollYChange,
'scrollXChange' : this._afterScrollXChange,
'heightChange' : this._afterHeightChange,
'widthChange' : this._afterWidthChange,
});
},
/**
* syncUI implementation
*
* Update the scroll position, based on the current value of scrollY
* @method bindUI
*/
syncUI: function() {
},
/**
* Scroll the element to a given y coordinate
*
* @method scrollTo
* @param x {Number} The x-position to scroll to
* @param y {Number} The y-position to scroll to
* @param duration {Number} Duration, in ms, of the scroll animation (default is 0)
* @param easing {String} An easing equation if duration is set
*/
xSet = (x !== null),
ySet = (y !== null),
callback = this._transEndCallback;
if (xSet) {
}
if (ySet) {
}
if (NATIVE_TRANSITIONS) {
// ANDROID WORKAROUND - try and stop existing transition, before kicking off new one.
// cb.setStyle("WebkitTransform", cb.getComputedStyle("WebkitTransform"));
}
if (duration !== 0) {
transition = {
};
if (NATIVE_TRANSITIONS) {
} else {
}
if (!callback) {
}
} else {
if (NATIVE_TRANSITIONS) {
} else {
}
}
},
/**
* gesturemovestart event handler
*
* @method _onGestureMoveStart
* @param e {Event.Facade} The gesturemovestart event facade
* @private
*/
_onGestureMoveStart: function(e) {
e.preventDefault();
this._killTimer();
this._moveStartTime = (new Date()).getTime();
this._moveStartClientY = e.clientY;
this._moveStartClientX = e.clientX;
/**
* Internal state, defines whether or not the scrollview is currently being dragged
*
* @property _isDragging
* @type boolean
* @protected
*/
this._isDragging = false;
/**
* Internal state, defines whether or not the scrollview is currently animating a flick
*
* @property _flicking
* @type boolean
* @protected
*/
this._flicking = false;
/**
* Internal state, defines whether or not the scrollview needs to snap to a boundary edge
*
* @property _snapToEdge
* @type boolean
* @protected
*/
this._snapToEdge = false;
},
/**
* gesturemove event handler
*
* @method _onGestureMove
* @param e {Event.Facade} The gesturemove event facade
* @private
*/
_onGestureMove: function(e) {
e.preventDefault();
this._isDragging = true;
this._moveEndClientY = e.clientY;
this._moveEndClientX = e.clientX;
this._lastMoved = (new Date()).getTime();
if(this._scrollsVertical) {
}
if(this._scrollsHorizontal) {
}
},
/**
* gestureend event handler
*
* @method _onGestureMoveEnd
* @param e {Event.Facade} The gesturemoveend event facade
* @private
*/
_onGestureMoveEnd: function(e) {
e.preventDefault();
var minY = this._minScrollY,
maxY = this._maxScrollY,
minX = this._minScrollX,
maxX = this._maxScrollX,
this._moveEndEvt.detach();
/**
*
* @property _scrolledHalfway
* @type boolean
* @protected
*/
this._scrolledHalfway = false;
this._snapToEdge = false;
this._isDragging = false;
this._scrolledHalfway = true;
/**
* Internal state, defines whether or not the scrollview has been scrolled in the forward (distance > 0), or backward (distance < 0) direction
*
* @property _scrolledForward
* @type boolean
* @protected
*/
}
this._scrolledHalfway = true;
}
// Check for minY
this._snapToEdge = true;
}
// Check for minX
this._snapToEdge = true;
}
// Check for maxY
this._snapToEdge = true;
}
// Check for maxX
this._snapToEdge = true;
}
if(this._snapToEdge) {
return;
}
this.fire(EV_SCROLL_END, {
onGestureMoveEnd: true
});
return;
},
/**
* After listener for changes to the scrollY attribute
*
* @method _afterScrollYChange
* @param e {Event.Facade} The event facade
* @protected
*/
_afterScrollYChange : function(e) {
}
},
/**
* Update the UI when the scrollY attribute changes
*
* @method _uiScrollY
* @param val {Number} The scrollY value
* @param duration {Number} The length (in ms) of the scroll animation
* @param easing {String} An easing equation, if duration is defined
* @protected
*/
},
/**
* After listener for changes to the scrollX attribute
*
* @method _afterScrollXChange
* @param e {Event.Facade} The event facade
* @protected
*/
_afterScrollXChange : function(e) {
}
},
/**
* Update the UI when the scrollX attribute 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
*/
},
/**
* After listener for the height attribute
*
* @method _afterHeightChange
* @param e {Event.Facade} The event facade
* @protected
*/
_afterHeightChange: function() {
this._uiDimensionsChange();
},
/**
* After listener for the width attribute
*
* @method _afterWidthChange
* @param e {Event.Facade} The event facade
* @protected
*/
_afterWidthChange: function() {
this._uiDimensionsChange();
},
/**
* This method gets invoked whenever the height or width attributes change,
* allowing us to determine which scrolling axes need to be enabled.
*
* @method _uiDimensionsChange
* @protected
*/
_uiDimensionsChange: function() {
// Use bb instead of cb. cb doesn't gives us the right results
// in FF (due to overflow:hidden)
this._scrollsVertical = true;
this._minScrollY = 0;
this._scrollHeight = scrollHeight;
}
this._scrollsHorizontal = true;
this._minScrollX = 0;
this._scrollWidth = scrollWidth;
}
/**
* Internal state, defines whether or not the scrollview can scroll vertically
*
* @property _scrollsVertical
* @type boolean
* @protected
*/
/**
* Internal state, defines the maximum amount that the scrollview can be scrolled along the Y axis
*
* @property _maxScrollY
* @type number
* @protected
*/
/**
* Internal state, defines the minimum amount that the scrollview can be scrolled along the Y axis
*
* @property _minScrollY
* @type number
* @protected
*/
/**
* Internal state, cached scrollHeight, for performance
*
* @property _scrollHeight
* @type number
* @protected
*/
/**
* Internal state, defines whether or not the scrollview can scroll horizontally
*
* @property _scrollsHorizontal
* @type boolean
* @protected
*/
/**
* Internal state, defines the maximum amount that the scrollview can be scrolled along the X axis
*
* @property _maxScrollX
* @type number
* @protected
*/
/**
* Internal state, defines the minimum amount that the scrollview can be scrolled along the X axis
*
* @property _minScrollX
* @type number
* @protected
*/
/**
* Internal state, cached scrollWidth, for performance
*
* @property _scrollWidth
* @type number
* @protected
*/
},
/**
* 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) {
/**
* Internal state, currently calculated velocity from the flick
*
* @property _currentVelocity
* @type number
* @protected
*/
this._flicking = true;
this._pastYEdge = false;
this._pastXEdge = false;
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,
if(scrollsVertical) {
maxY = this._maxScrollY;
minY = this._minScrollY;
}
if(scrollsHorizontal) {
maxX = this._maxScrollX;
minX = this._minScrollX;
}
this._flicking = false;
if(scrollsVertical) {
this._snapToEdge = true;
this._snapToEdge = true;
}
}
if(scrollsHorizontal) {
this._snapToEdge = true;
this._snapToEdge = true;
}
}
return;
}
if (scrollsVertical) {
this._pastYEdge = true;
this._currentVelocity *= bounce;
}
}
if (scrollsHorizontal) {
this._pastXEdge = true;
this._currentVelocity *= bounce;
}
}
if (!this._flickTimer) {
}
},
/**
* Stop the animation timer
*
* @method _killTimer
* @param fireEvent {Boolean} If true, fire the scrollEnd event
* @protected
*/
_killTimer: function(fireEvent) {
if(this._flickTimer) {
this._flickTimer.cancel();
this._flickTimer = null;
}
if(fireEvent) {
this.fire(EV_SCROLL_END);
}
},
/**
* The scrollX, scrollY setter implementation
*
* @method _setScroll
* @private
* @param {Number} val
* @param {String} dim
*
*/
if(!bouncing || !this._isDragging) {
}
}
return val;
},
/**
* Setter for the scrollX attribute
*
* @method _setScrollX
* @param val {Number} The new scrollX value
* @return {Number} The normalized value
* @protected
*/
_setScrollX: function(val) {
},
/**
* Setter for the scrollY ATTR
*
* @method _setScrollY
* @param val {Number} The new scrollY value
* @return {Number} The normalized value
* @protected
*/
_setScrollY: function(val) {
}
}, {
// 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
},
/**
*
* @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
*/
/**
* Flag used to source property changes initiated from the DOM
*
* @property ScrollView.UI_SRC
* @type String
* @static
* @default "ui"
*/
/**
* The default bounce distance in pixels
*
* @property ScrollView.BOUNCE_RANGE
* @type Number
* @static
* @default 150
*/
BOUNCE_RANGE : 150,
/**
* The interval 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',
/**
* Style property name to use to set transition duration. Currently Webkit specific (WebkitTransitionDuration)
*
* @property ScrollView._TRANSITION_DURATION
* @private
*/
_TRANSITION_DURATION : "WebkitTransitionDuration",
/**
* Style property name to use to set transition property. Currently, Webkit specific (WebkitTransitionProperty)
*
* @property ScrollView._TRANSITION_PROPERTY
* @private
*/
_TRANSITION_PROPERTY : "WebkitTransitionProperty"
});