| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 
 |  
 
class VirtualScroller
{
	//---------//
	//Variables//
	//---------//
	private var			_sizeContent:Number; //size content
	private var			_sizeScroller:Number; //size scroller
	private var			_sizeMask:Number; //size mask
	private var			_sizePlaceScroller:Number; //size place scroller	
	private var			_realSizeContent:Number; //calculated size
	private var			_realSizeScroller:Number; //calculated size
	private var			_currentPlaceContent:Number; //current place content
	private var			_currentPlaceScroller:Number; //current place scroller
	private var			_ratioContent:Number; //ratio of the content
 
	/**
	 * Called when a content is scrolled.
	 * <p>Prototype of the function :<br>
	 * <code>onScroll = function(posContent:Number, posScroller:Number):Void { ... }</code></p>
	 */
	public var onScroll:Function;
 
	//-----------//
	//Constructor//
	//-----------//
 
	/**
	 * Create a new VirtualScroller.
	 * 
	 * @param		sizeContent			Size of the content that must be scrolled.
	 * @param		sizeMask			Size of the mask of the content.
	 * @param		sizeScroller		Size of the scroller.
	 * @param		sizePlaceScroller	Size of the place of the scroller (by default {@code sizeMask}).
	 * @throws		Error				If {@code sizeContent} is {@code null} or less than 0.
	 * @throws		Error				If {@code sizeMask} is {@code null} or less than 0.
	 * @throws		Error				If {@code sizeScroller} is {@code null} or less than 0.
	 */
	public function VirtualScroller(sizeContent:Number, sizeMask:Number,
									sizeScroller:Number, sizePlaceScroller:Number)
	{
		if (sizeContent == null)
		{
			throw new Error(this+".<init> : sizeContent is undefined");
		}
 
		if (sizeContent < 0)
		{
			throw new Error(this+".<init> : sizeContent is invalid ("+sizeContent+")");
		}
 
		if (sizeMask == null)
		{
			throw new Error(this+".<init> : sizeMask is undefined");
		}
 
		if (sizeMask < 0)
		{
			throw new Error(this+".<init> : sizeMask is invalid ("+sizeMask+")");
		}
 
		if (sizeScroller == null)
		{
			throw new Error(this+".<init> : sizeScroller is undefined");
		}
 
		if (sizeScroller < 0)
		{
			throw new Error(this+".<init> : sizeScroller is invalid ("+sizeScroller+")");
		}
 
		if (sizePlaceScroller == null || sizePlaceScroller < 0)
		{
			sizePlaceScroller = sizeMask;
		}
 
		//init
		_sizeScroller = sizeScroller;
		_sizeMask = sizeMask;
		_sizePlaceScroller = sizePlaceScroller;
		_sizeContent = sizeContent;
		_realSizeContent = sizeContent-sizeMask;
		_realSizeScroller = sizePlaceScroller-sizeScroller;
		_currentPlaceContent = 0;
		_currentPlaceScroller = 0;
		_ratioContent = 1;
		onScroll = null;
	}
 
	//--------------//
	//Public methods//
	//--------------//
 
	/**
	 * Get the size of the content.
	 * 
	 * @return	The size of the content.
	 */
	public function getContentSize(Void):Number
	{
		return _sizeContent;
	}
 
	/**
	 * Get the size of the scroller.
	 * 
	 * @return	The size of the scroller.
	 */
	public function getScrollerSize(Void):Number
	{
		return _sizeScroller;
	}
 
	/**
	 * Get the size of the mask.
	 * 
	 * @return	The size of the mask.
	 */
	public function getMaskSize(Void):Number
	{
		return _sizeMask;
	}
 
	/**
	 * Get the size of the place of the scroller.
	 * 
	 * @return	The size of the place of the scroller.
	 */
	public function getPlaceScrollerSize(Void):Number
	{
		return _sizePlaceScroller;
	}
 
	/**
	 * Get the virtually place of the scroller.
	 * 
	 * @return	The current place of the scroller.
	 */
	public function getCurrentPlaceScroller(Void):Number
	{
		return _currentPlaceScroller;
	}
 
	/**
	 * Get the virtually place of the content.
	 * 
	 * @param	The current place of the content.
	 */
	public function getCurrentPlaceContent(Void):Number
	{
		return _currentPlaceContent;
	}
 
	/**
	 * Get if the {@code VirtualScroller} can be scrolled or not.
	 * <p>A non scrollable {@code VirtualScroller} means that the mask is
	 * bigger thant the content (so the content is already totaly visible) or
	 * that the size of the scroller is bigger than the place it has for scrolling.</p>
	 * 
	 * @return	{@code true} if the {@code VirtualScroller} is scrollable.
	 */
	public function isScrollable(Void):Boolean
	{
		return (_sizeMask < _sizeContent && _sizeScroller < _sizePlaceScroller);
	}
 
	/**
	 * Get the maximum value for the {@link #moveContentTo(Number)} method.
	 * <p>Note that the minimal value is 0.</p>
	 * 
	 * @return	The maximum value for the content move.
	 */
	public function getMaxMoveContent(Void):Number
	{
		return _realSizeContent;
	}
 
	/**
	 * Get the maximum value for the {@link #moveScrollerTo(Number)} method.
	 * <p>Note that the minimal value is 0.</p>
	 * 
	 * @return	The maximum value for the scroller move.
	 */
	public function getMaxMoveScroller(Void):Number
	{
		return _realSizeScroller;
	}
 
	/**
	 * Move the content (relative).
	 * <p>This method simply call the {@link moveContentTo(Number)} method,
	 * taking the current place and adding {@code append}.</p>
	 * <p>Note that if the current place of the content adding {@code append}
	 * is bigger than {@link @getMaxMoveContent()}, no {@code Error} is thrown,
	 * it will go to the max position.</p>
	 * 
	 * @param	append	The new relative place.
	 */
	public function appendContent(append:Number):Void
	{
		var np:Number = _currentPlaceContent+append;
 
		if (np > getMaxMoveContent())
		{
			np = getMaxMoveContent();
		}
		else if (np < 0)
		{
			np = 0;
		}
 
		moveContentTo(np);
	}
 
	/**
	 * Move the scroller (relative).
	 * <p>This method simply call the {@link moveScrollerTo(Number)} method,
	 * taking the current place and adding {@code append}.</p>
	 * <p>Note that if the current place of the scroller adding {@code append}
	 * is bigger than {@link @getMaxMoveScroller()}, no {@code Error} is thrown,
	 * it will go to the max position.</p>
	 * 
	 * @param	append	The new relative place.
	 */
	public function appendScroller(append:Number):Void
	{
		var np:Number = _currentPlaceScroller+append;
 
		if (np > getMaxMoveScroller())
		{
			np = getMaxMoveScroller();
		}
		else if (np < 0)
		{
			np = 0;
		}
 
		moveScrollerTo(np);
	}
 
	/**
	 * Move the content (absolute).
	 * <p>This method call the {@link onScroll} function with the associated
	 * parameters and adjust the scroller position.</p>
	 * 
	 * @param	target		The new place of the content.
	 * @throws	Error		If {@code target} is less than 0 or bigger
	 * 						than {@link getMaxMoveContent()}.
	 */
	public function moveContentTo(target:Number):Void
	{
		//check
		if (target < 0 || target > getMaxMoveContent())
		{
			throw new Error(this+".moveContentTo : invalid target ("+target+")");
		}
 
		var ratio:Number = target / getMaxMoveContent();
		_currentPlaceContent = target;
		_currentPlaceScroller = getMaxMoveScroller()*ratio;
 
		//scroll event
		onScroll(_currentPlaceContent*_ratioContent, _currentPlaceScroller);
	}
 
	/**
	 * Move the scroller (absolute).
	 * <p>This method call the {@link onScroll} function with the associated
	 * parameters and adjust the content position.</p>
	 * 
	 * @param	target		The new place of the scroller.
	 * @throws	Error		If {@code target} is less than 0 or bigger
	 * 						than {@link getMaxMoveScroller()}.
	 */
	public function moveScrollerTo(target:Number):Void
	{
		//check
		if (target < 0 || target > getMaxMoveScroller())
		{
			throw new Error(this+".moveScrollerTo : invalid target ("+target+")");
		}
 
		var ratio:Number = target / getMaxMoveScroller();
		_currentPlaceContent = getMaxMoveContent()*ratio;
		_currentPlaceScroller = target;
 
		//scroll event
		onScroll(_currentPlaceContent*_ratioContent, _currentPlaceScroller);
	}
 
	/**
	 * Get the ratio.
	 * 
	 * @return	The ratio.
	 */
	public function getRatio(Void):Number
	{
		return _ratioContent;
	}
 
	/**
	 * Set the ratio.
	 * <p>The ratio is used when dispatching the {@link onScroll(Number,Number)}
	 * event for the new content position. It is very useful to have directly
	 * the inverted position (ratio = -1).</p>
	 * <p>By default, the ratio is set to 1.</p>
	 * 
	 * @param	ratio	The new ratio.
	 */
	public function setRatio(ratio:Number):Void
	{
		_ratioContent = ratio;
	}
 
	/**
	 * Represent the current instance into a String.
	 *
	 * @return	A String representing the VirtualScroller instance.
	 */
	public function toString(Void):String
	{
		return "VirtualScroller";
	}
} | 
Partager