My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
Copyright (c) 2010 Tink Ltd - http://www.tink.ws

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package ws.tink.spark.controls
{

import flash.events.KeyboardEvent;

import mx.core.mx_internal;
import mx.events.FlexEvent;

import spark.components.List;
import spark.core.NavigationUnit;
import spark.events.IndexChangeEvent;

use namespace mx_internal;

/**
* The ExcludeSelectionList control inherits all the functionality
* of the List control and adds the ability to exclude items from being
* selectable.
*
* <p><b>Note: </b>The ExcludeSelectionList control does not support the BasicLayout class
* as the value of the <code>layout</code> property.
* Do not use BasicLayout with the Spark list-based controls.</p>
*
* <p>The ExcludeSelectionList control has the following default characteristics:</p>
* <table class="innertable">
* <tr><th>Characteristic</th><th>Description</th></tr>
* <tr><td>Default size</td><td>112 pixels wide by 112 pixels high</td></tr>
* <tr><td>Minimum size</td><td>112 pixels wide by 112 pixels high</td></tr>
* <tr><td>Maximum size</td><td>10000 pixels wide and 10000 pixels high</td></tr>
* <tr><td>Default skin class</td><td>spark.skins.spark.BorderContainerSkin</td></tr>
* </table>
*
* @mxml <p>The <code>&lt;s:List&gt;</code> tag inherits all of the tag
* attributes of its superclass and adds the following tag attributes:</p>
*
* <pre>
* &lt;s:List
* <strong>Properties</strong>
* excludeType="null"
* excludeTypes="null"
* excludeIndex="null"
* excludeIndices="null"
* excludeFunction="null"
* /&gt;
* </pre>
*
* @see spark.skins.spark.ListSkin
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public class ExcludeSelectionList extends List
{

/**
* @private
* Flag to indicate whether any of the exclusion details
* have changed.
*/
private var _excludeChanged:Boolean;

/**
* Constructor.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function ExcludeSelectionList()
{
super();
}


//----------------------------------
// excludeType
//----------------------------------

/**
* @private
* Storage for the excludeType property.
*/
private var _excludeType : Class;

/**
* A Class representing a datatype that cannot be selected.
*
* @default null
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function get excludeType():Class
{
return _excludeType;
}

/**
* @private
*/
public function set excludeType( value:Class ):void
{
if( _excludeType == value ) return;

_excludeTypes = null;
_excludeType = value;
_excludeChanged = true;
invalidateProperties();
}


//----------------------------------
// excludeTypes
//----------------------------------

/**
* @private
* Storage for the excludeTypes property.
*/
private var _excludeTypes : Vector.<Class>;

/**
* A Vector of Classes representing the datatypes that cannot be selected.
*
* @default null
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function get excludeTypes():Vector.<Class>
{
return _excludeTypes;
}

/**
* @private
*/
public function set excludeTypes( value:Vector.<Class> ):void
{
if( _excludeTypes == value ) return;

_excludeType = null;
_excludeTypes = value;
_excludeChanged = true;
invalidateProperties();
}


//----------------------------------
// excludeIndex
//----------------------------------

/**
* @private
* Storage for the excludeIndex property.
*/
private var _excludeIndex : int = -1;

/**
* An int representing an index that cannot be selected.
*
* @default -1
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function get excludeIndex():int
{
return _excludeIndex;
}

/**
* @private
*/
public function set excludeIndex( value:int ):void
{
if( _excludeIndex == value ) return;

_excludeIndices = null;
_excludeIndex = value;
_excludeChanged = true;
invalidateProperties();
}


//----------------------------------
// excludeIndices
//----------------------------------

/**
* @private
* Storage for the excludeIndices property.
*/
private var _excludeIndices : Vector.<int>;

/**
* A Vector of ints representing the indices that cannot be selected.
*
* @default null
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function get excludeIndices():Vector.<int>
{
return _excludeIndices;
}

/**
* @private
*/
public function set excludeIndices( value:Vector.<int> ):void
{
if( _excludeIndices == value ) return;

_excludeIndex = NaN;
_excludeIndices = value;
_excludeChanged = true;
invalidateProperties();
}


//----------------------------------
// excludeFunction
//----------------------------------

/**
* @private
* Storage for the excludeFunction property.
*/
private var _excludeFunction : Function;

/**
* Function that returns a Boolean to indicate whether a
* specific item or index is selectable. You should define
* an exclude function similar to this sample function:
*
* <pre>function myExcludeFunction( index:int, item:Object ):Boolean</pre>
*
* @default null
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function get excludeFunction():Function
{
return _excludeFunction;
}

/**
* @private
*/
public function set excludeFunction( value:Function ):void
{
if( _excludeFunction == value ) return;

_excludeFunction = value;
_excludeChanged = true;
invalidateProperties();
}


/**
* Returns whether an item is selectable
*/
public function itemSelectable( value:Object ):Boolean
{
if( !dataProvider ) return true;

return indexSelectable( dataProvider.getItemIndex( value ) );
}

/**
* Returns whether an index is selectable
*/
public function indexSelectable( value:int ):Boolean
{
if( !dataProvider ) return true;

if( value > -1 && value < dataProvider.length )
{
// Check for excluded indices
if( _excludeIndex )
{
if( value == _excludeIndex ) return false;
}
else if( _excludeIndices )
{
for each( var i:int in _excludeIndices )
{
if( value == i ) return false;
}
}

// Check for excluded data types
if( _excludeType )
{
if( dataProvider.getItemAt( value ) is _excludeType ) return false;
}
else if( _excludeTypes )
{
var item:Object = dataProvider.getItemAt( value );
for each( var c:Class in _excludeTypes )
{
if( item is c ) return false;
}
}

if( _excludeFunction != null ) return !_excludeFunction( value, dataProvider.getItemAt( value ) );
}

return true;
}


/**
* @private
*/
override protected function commitProperties():void
{
super.commitProperties();

if( _excludeChanged )
{
_excludeChanged = false;

if( selectedIndices )
{
var excluded:Boolean;
var newValue:Vector.<int> = new Vector.<int>();
for each( var i:int in selectedIndices )
{
if( indexSelectable( i ) )
{
newValue.push( i );
}
else
{
excluded = true;
}
}

if( excluded ) selectedIndices = newValue;
}
}
}


/**
* @inherit
*/
override mx_internal function setSelectedIndex( value:int, dispatchChangeEvent:Boolean = false ):void
{
if( value == selectedIndex || !indexSelectable( value ) ) return;

super.setSelectedIndex( value, dispatchChangeEvent );
}


/**
* @inherit
*/
override mx_internal function setSelectedIndices( value:Vector.<int>, dispatchChangeEvent:Boolean = false ):void
{
if( value == selectedIndices ) return;

var newValue:Vector.<int> = new Vector.<int>();
for each( var i:int in value )
{
if( indexSelectable( i ) ) newValue.push( i );
}

super.setSelectedIndices( newValue, dispatchChangeEvent );
}


/**
* @inherit
*/
override protected function commitSelection( dispatchChangedEvents:Boolean = true ):Boolean
{
if( requireSelection ) _proposedSelectedIndex = getNextSelectableIndex( _proposedSelectedIndex );

return super.commitSelection( dispatchChangedEvents );
}


/**
* @inherit
*/
override protected function adjustSelection(index:int, add:Boolean=false):void
{
var i:int;
var curr:Number;
var newInterval:Vector.<int> = new Vector.<int>();
var e:IndexChangeEvent;

if( selectedIndex == NO_SELECTION || doingWholesaleChanges )
{
// The case where one item has been newly added and it needs to be
// selected and careted because requireSelection is true.
if( dataProvider && dataProvider.length == 1 && requireSelection && indexSelectable( 0 ) )
{
newInterval.push( 0 );
selectedIndices = newInterval;
selectedIndex = 0;
itemShowingCaret( 0, true );
// If the selection properties have been adjusted to account for items that
// have been added or removed, send out a "valueCommit" event and
// "caretChange" event so any bindings to them are updated correctly.

dispatchEvent( new FlexEvent( FlexEvent.VALUE_COMMIT ) );

e = new IndexChangeEvent( IndexChangeEvent.CARET_CHANGE );
e.oldIndex = -1;
e.newIndex = _caretIndex;
dispatchEvent( e );
}
return;
}

// Ensure multiple and single selection are in-sync before adjusting
// selection. Sometimes if selection has been changed before adding/removing
// an item, we may not have handled selection via invalidation, so in those
// cases, force a call to commitSelection() to validate and commit the selection.
if( ( !selectedIndices && selectedIndex > NO_SELECTION ) ||
( selectedIndex > NO_SELECTION && selectedIndices.indexOf( selectedIndex ) == -1 ) )
{
commitSelection();
}

// Handle the add or remove and adjust selection accordingly.
if( add )
{
for( i = 0; i < selectedIndices.length; i++ )
{
curr = selectedIndices[ i ];

// Adding an item above one of the selected items,
// bump the selected item up.
if( curr >= index )
{
newInterval.push( curr + 1 );
}
else
{
newInterval.push( curr );
}
}
}
else
{
// Quick check to see if we're removing the only selected item
// in which case we need to honor requireSelection.
if( !isEmpty( selectedIndices ) && selectedIndices.length == 1
&& index == selectedIndex && requireSelection )
{
//Removing the last item
if( dataProvider.length == 0 )
{
newInterval = new Vector.<int>();
}
else if( index == 0 )
{
// We can't just set selectedIndex to 0 directly
// since the previous value was 0 and the new value is
// 0, so the setter will return early.
_proposedSelectedIndex = getNextSelectableIndex( 0 );
invalidateProperties();
return;
}
else
{
newInterval.push( getNextSelectableIndex( 0 ) );
}
}
else
{
for( i = 0; i < selectedIndices.length; i++ )
{
curr = selectedIndices[ i ];
// Removing an item above one of the selected items,
// bump the selected item down.
if( curr > index )
{
newInterval.push( curr - 1 );
}
else if( curr < index )
{
newInterval.push( curr );
}
}
}
}

if (caretIndex == selectedIndex)
{
// caretIndex is not changing, so we just need to dispatch
// an "caretChange" event to update any bindings and update the
// caretIndex backing variable.
var oldIndex:Number = caretIndex;
_caretIndex = getFirstItemValue( newInterval );
e = new IndexChangeEvent(IndexChangeEvent.CARET_CHANGE);
e.oldIndex = oldIndex;
e.newIndex = caretIndex;
dispatchEvent(e);
}
else
{
// De-caret the previous caretIndex renderer and set the
// caretIndexAdjusted flag to true. This will mean in
// commitProperties, the caretIndex will be adjusted to
// match the selectedIndex;

// TODO (dsubrama): We should revisit the synchronous nature of the
// de-careting/re-careting behavior.
itemShowingCaret( caretIndex, false );
caretIndexAdjusted = true;
invalidateProperties();
}

selectedIndices = newInterval;
selectedIndex = getFirstItemValue( newInterval );
}


/**
* @inherit
*/
override protected function adjustSelectionAndCaretUponNavigation( event:KeyboardEvent ):void
{
// Some unrecognized key stroke was entered, return.
var navigationUnit:uint = event.keyCode;
if( !NavigationUnit.isNavigationUnit( event.keyCode ) ) return;

// Delegate to the layout to tell us what the next item is we should select or focus into.
// TODO (dsubrama): At some point we should refactor this so we don't depend on layout
// for keyboard handling. If layout doesn't exist, then use some other keyboard handler
var proposedNewIndex:int = layout.getNavigationDestinationIndex( caretIndex, navigationUnit, arrowKeysWrapFocus );

if( !indexSelectable( proposedNewIndex ) )
{
if( caretIndex < proposedNewIndex )
{
proposedNewIndex = getNextSelectableIndex( proposedNewIndex );
}
else
{
proposedNewIndex = getPrevSelectableIndex( proposedNewIndex );
}
}

// Note that the KeyboardEvent is canceled even if the current selected or in focus index
// doesn't change because we don't want another component to start handling these
// events when the index reaches a limit.
if( proposedNewIndex == -1 ) return;

event.preventDefault();

// Contiguous multi-selection action. Create the new selection
// interval.
if( allowMultipleSelection && event.shiftKey && selectedIndices )
{
var startIndex:Number = getLastSelectedIndex();
var newInterval:Vector.<int> = new Vector.<int>();
var i:int;
if( startIndex <= proposedNewIndex )
{
for( i = startIndex; i <= proposedNewIndex; i++ )
{
newInterval.splice( 0, 0, i );
}
}
else
{
for( i = startIndex; i >= proposedNewIndex; i-- )
{
newInterval.splice( 0, 0, i );
}
}
setSelectedIndices( newInterval, true );
ensureIndexIsVisible( proposedNewIndex );
}
// Entering the caret state with the Ctrl key down
else if( event.ctrlKey )
{
var oldCaretIndex:Number = caretIndex;
setCurrentCaretIndex( proposedNewIndex );
var e:IndexChangeEvent = new IndexChangeEvent( IndexChangeEvent.CARET_CHANGE );
e.oldIndex = oldCaretIndex;
e.newIndex = caretIndex;
dispatchEvent( e );
ensureIndexIsVisible( proposedNewIndex );
}
// Its just a new selection action, select the new index.
else
{
setSelectedIndex( proposedNewIndex, true );
ensureIndexIsVisible( proposedNewIndex );
}
}


/**
* Returns the index of the next selectable item.
*/
protected function getNextSelectableIndex( value:int ):int
{
if( !dataProvider ) return -1;

for( var i:int = value; i < dataProvider.length; i++ )
{
if( indexSelectable( i ) ) return i;
}

return -1;
}


/**
* Returns the index of the previous selectable item.
*/
protected function getPrevSelectableIndex( value:int ):int
{
if( !dataProvider ) return -1;

for( var i:int = value; i > -1; i-- )
{
if( indexSelectable( i ) ) return i;
}

return -1;
}


/**
* @private
* Returns the index of the last selected item. In single
* selection, this is just selectedIndex. In multiple
* selection, this is the index of the first selected item.
*/
private function getLastSelectedIndex():int
{
if ( selectedIndices && selectedIndices.length > 0 )
{
return selectedIndices[ selectedIndices.length - 1 ];
}
else
{
return 0;
}
}


/**
* @private
* Returns true if v is null or an empty Vector.
*/
private function isEmpty( v:Vector.<int> ):Boolean
{
return v == null || v.length == 0;
}


/**
* @private
* Given a Vector, returns the value of the first item,
* or -1 if there are no items in the Vector;
*/
private function getFirstItemValue(v:Vector.<int>):int
{
if( v && v.length > 0 )
{
return v[ 0 ];
}
else
{
return -1;
}
}
}


}

Change log

r375 by sdowns on Apr 25, 2011   Diff
4.5 repos
Go to: 
Project members, sign in to write a code review

Older revisions

r373 by sdowns on Apr 25, 2011   Diff
Removed import of
SemiCarouselLayoutType
r260 by sdowns on Oct 16, 2010   Diff
spark library in new setup
All revisions of this file

File info

Size: 18319 bytes, 715 lines
Powered by Google Project Hosting