[Event(name="purchase", type="lib_flex_store_samples_flexstore_ProductThumbEvent")]
[Event(name="compare", type="lib_flex_store_samples_flexstore_ProductThumbEvent")]
[Event(name="details", type="lib_flex_store_samples_flexstore_ProductThumbEvent")]
0)
{
fadeOut.targets = targets;
fadeOut.play();
fadeOut.addEventListener(EffectEvent.EFFECT_END,
function(event:EffectEvent):void
{
layoutCatalog();
});
}
else if (!live)
{
layoutCatalog();
}
}
private function layoutCatalog():Effect
{
var tileWidth:Number;
var tileHeight:Number;
var numCols:int;
if (filterCount > 9 || currentState == "compare")
{
numCols = 4;
tileWidth = lib_flex_store_productsView_ProductCatalogThumbnail.COL_WIDTH_4;
tileWidth = currentState == "compare"
? lib_flex_store_productsView_ProductCatalogThumbnail.COMPARE_WIDTH
: lib_flex_store_productsView_ProductCatalogThumbnail.COL_WIDTH_4
tileHeight = currentState == "compare"
? height - 4
: lib_flex_store_productsView_ProductCatalogThumbnail.COL_HEIGHT_4;
}
else if (filterCount > 4)
{
numCols = 3;
tileWidth = lib_flex_store_productsView_ProductCatalogThumbnail.COL_WIDTH_3;
tileHeight = lib_flex_store_productsView_ProductCatalogThumbnail.COL_HEIGHT_3;
}
else if (filterCount <= 9)
{
numCols = 2;
tileWidth = lib_flex_store_productsView_ProductCatalogThumbnail.COL_WIDTH_2;
tileHeight = lib_flex_store_productsView_ProductCatalogThumbnail.COL_HEIGHT_2;
}
else
{
}
var row:int = 0;
var col:int = -1;
var move:Move = null;
var n:int = catalog.length;
for (var i:int = 0 ; i < n ; i++)
{
var product:lib_flex_store_samples_flexstore_Product = catalog.getItemAt(i) as lib_flex_store_samples_flexstore_Product;
var thumb:lib_flex_store_productsView_ProductCatalogThumbnail = thumbnails[i];
if (accepted[product])
{
thumb.currentState = "" + numCols + "cols";
col++;
if (col > numCols - 1)
{
row++;
col = 0;
}
var xTo:Number = col * (tileWidth + lib_flex_store_productsView_ProductCatalogThumbnail.HORIZONTAL_GAP);
var yTo:Number = row * (tileHeight + lib_flex_store_productsView_ProductCatalogThumbnail.VERTICAL_GAP);
// If the thumbnail is already visible
// animate it to its new position.
if (thumb.visible)
{
// Animate only if the position is different
// from its current position.
if (thumb.x != xTo || thumb.y != yTo)
{
move = new Move(thumb);
move.xTo = xTo;
move.yTo = yTo;
move.play();
}
}
// If the thumbnail was not previously visible, sets its
// x and y coordinates. We'll make it reappear after all
// the visible thumbnails have reached their new position.
else
{
thumb.x = xTo;
thumb.y = yTo;
thumb.includeInLayout = true;
}
}
else
{
thumb.visible = false;
thumb.includeInLayout = false;
}
}
if (!move)
{
// No visible thumbnails were animated to a new position;
// fade in newly selected thumbnails right away.
fadeInThumbnails();
}
else
{
//since movement is happening get the scrollbar back to the top
thumbContent.verticalScrollPosition = 0;
// Fade in newly selected thumbnails after the last
// visible thumbnail has moved to its new position.
move.addEventListener(EffectEvent.EFFECT_END,
function(event:EffectEvent):void
{
fadeInThumbnails();
});
}
//return the last move to watch
return move;
}
//return the last effect so we could add effectEnd handler if desired
private function fadeInThumbnails():void
{
var n:int = thumbnails.length;
var effect:Fade = new Fade();
effect.alphaTo = 1;
var targets:Array = [];
for (var i:int = 0; i < n ; i++)
{
var thumb:lib_flex_store_productsView_ProductCatalogThumbnail = thumbnails[i];
if (accepted[thumb.product] && !thumb.visible)
{
thumb.alpha = 0;
thumb.visible = true;
targets.push(thumb);
}
}
if (targets.length > 0)
{
effect.targets = targets;
effect.play();
}
}
private function showDetails(product:lib_flex_store_samples_flexstore_Product):void
{
if (currentState == "details")
{
details.product = product;
return;
}
var row:int = -1;
//should be computed using border metrics instead of hard-coding the 20, but...
var xTo:Number = thumbContent.width - lib_flex_store_productsView_ProductCatalogThumbnail.COL_WIDTH_4 - 20;
var yTo:Number;
var move:Move;
var first:Boolean = true;
var selectedThumb:lib_flex_store_productsView_ProductCatalogThumbnail;
var n:int = thumbnails.length;
for (var i:int = 0; i < n; i++)
{
var thumb:lib_flex_store_productsView_ProductCatalogThumbnail = thumbnails[i];
if (thumb.visible)
{
row++;
yTo = row * (lib_flex_store_productsView_ProductCatalogThumbnail.COL_HEIGHT_4 + lib_flex_store_productsView_ProductCatalogThumbnail.VERTICAL_GAP);
thumb.currentState = "4cols";
if (thumb.x != xTo || thumb.y != yTo)
{
move = new Move(thumb);
if (first)
{
move.addEventListener(EffectEvent.EFFECT_END,
function(event:EffectEvent):void
{
details.product = product;
currentState = "details";
});
first = false;
}
move.xTo = xTo;
move.yTo = yTo;
move.play();
}
if (thumb.product == product)
{
selectedThumb = thumb;
}
}
}
if (selectedThumb != null)
{
//make sure that the selected thumb is visible in the list on the right
move.addEventListener(EffectEvent.EFFECT_END,
function(e:EffectEvent):void
{
var curpos:int = thumbContent.verticalScrollPosition;
if (selectedThumb.y < curpos)
{
thumbContent.verticalScrollPosition = y;
}
else if (selectedThumb.y + lib_flex_store_productsView_ProductCatalogThumbnail.COL_HEIGHT_4 > curpos + thumbContent.height)
{
//this logic doesn't seem to be perfect but it will do
var diff:int = selectedThumb.y - (curpos + thumbContent.height)
thumbContent.verticalScrollPosition += diff + lib_flex_store_productsView_ProductCatalogThumbnail.COL_HEIGHT_4 + lib_flex_store_productsView_ProductCatalogThumbnail.VERTICAL_GAP;
}
});
}
}
private function productThumbEventHandler(event:lib_flex_store_samples_flexstore_ProductThumbEvent):void
{
if (event.type == lib_flex_store_samples_flexstore_ProductThumbEvent.DETAILS)
{
showDetails(event.product);
}
else if (event.type == lib_flex_store_samples_flexstore_ProductThumbEvent.BROWSE)
{
if (thumbnailState == "browse")
{
currentState = "browse";
layoutCatalog();
}
else
{
compare();
}
}
else
{
dispatchEvent(event);
}
}
public function compare(toCompare:Array=null):void
{
currentState = "compare";
thumbnailState = "compare";
if (toCompare != null)
{
var n:int = thumbnails.length;
for (var i:int = 0; i < n; i++)
{
accepted[thumbnails[i].product] = false;
}
for (i=0; i < toCompare.length; i++)
{
accepted[toCompare[i]] = true;
}
}
var lastEffect:Effect = layoutCatalog();
if (lastEffect != null)
{
lastEffect.addEventListener(EffectEvent.EFFECT_END,
function (event:EffectEvent):void
{
setCompareState();
});
}
else
{
setCompareState();
}
}
private function setCompareState():void
{
//avoid an issue if the user clicks quickly where we move into
//compare state even though we're no longer in compare
if (currentState == "compare")
{
var n:int = thumbnails.length;
for (var i:int = 0; i < n; i++)
{
var thumb:lib_flex_store_productsView_ProductCatalogThumbnail = thumbnails[i];
if (accepted[thumb.product])
{
thumb.currentState = "compare";
}
}
}
}
]]>