In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

XKCD browser

/**
 * Copyright makc3d ( http://wonderfl.net/user/makc3d )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6w9l
 */

// forked from wh0's Using proxies without crossdomain.xml
package {
	import com.bit101.components.TextArea;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
	import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.system.Security;
    import flash.text.TextField;
    public class FlashTest extends Sprite {
        
        // Wrap url in proxy parameter.
        private function proxy(url:String):String {
            return 'http://www.gmodules.com/ig/proxy?url=' + encodeURIComponent(url);
        }
        
        public function FlashTest () {
			// for local testing
/*			if (loaderInfo.url.charAt (0) != "h") {
				init (null); return;
			}*/
            // Only the top level movie starts off with stage populated.
            if (stage) {
                // The top level movie is likely included from wonderfl.net and therefore unproxied.
                // All we do in this case is load the proxied version.
                // Allow the proxy domain so wee can access stage on lines 45-46.
                Security.allowDomain('www.gmodules.com');
                var l:Loader = new Loader();
                l.load(new URLRequest(proxy(loaderInfo.url)));
                addChild(l);
                return;
            } else {
                // Allow Wonderfl to take a screen cap.
                // doesnt work ??
                // I get Error: SecurityError: Error #2122
                Security.allowDomain('swf.wonderfl.net');
                addEventListener(Event.ADDED_TO_STAGE, init);
            }
        }
        
        private function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            // Now you write your own code.
            // Since the inner proxied movie is in the proxy's domain, we can request anything.
            var ul:URLLoader = new URLLoader(new URLRequest(proxy('http://xkcd.com/info.0.json')));
            ul.addEventListener(Event.COMPLETE, complete);
        }
        
        private function complete(e:Event):void {
			// here we only need to get last xkcd comic: ... "num": 908, ...
			// we could use com.adobe.serialization.json.JSON but I dont feel like
			var re:RegExp = /num":\s+(\d+),/;
			var num:int = parseInt (String (e.target.data).match (re) [1]);
			var items:Array = [];
			for (var i:int = 0; i <= num - 2; i++) {
				if (i < 403) {
					items [i] = { label: (i + 1).toString () };
				} else {
					items [i] = { label: (i + 2).toString () };
				}
			}
			list = new List (this, 0, 0, items);
			list.width /= 2; list.height = 465;
			list.addEventListener (Event.SELECT, onSelect);
			stage.addEventListener (MouseEvent.MOUSE_MOVE, scrollTheComic);
			altText = new TextArea (this, list.width, 465 - 50, "<< select the comic from the list");
			altText.width = 465 - list.width;
			altText.height = 50;
        }

		private var list:List, altText:TextArea;
		private function onSelect (e:Event):void {
			var jurl:String = 'http://xkcd.com/' + list.selectedItem.label + '/info.0.json';

			list.enabled = false;
			var ul:URLLoader = new URLLoader(new URLRequest(proxy(jurl)));
            ul.addEventListener(Event.COMPLETE, onImageAddressRetrieved);
		}

		private var image:Loader;
		private function onImageAddressRetrieved (e:Event):void {
			// here we only need to get xkcd comic url: {"img": "http://imgs.xkcd.com/comics/the_cloud.png", ...
			// again, I don't feel like using com.adobe.serialization.json.JSON
			var re:RegExp = /img":\s+"([^"]+)"/;
			var img:String = String (e.target.data).match (re) [1];
			// ok, maybe alt text, too
			re = /alt":\s+"([^"]+)"/;
			var alt:String = String (e.target.data).match (re) [1];

			// we do not really need to proxy comic image
			// but if we don't, wonderfl will not be able to capture it
			if (image != null) removeChild (image);
			image = new Loader;
			image.x = list.width;
			// edit: actually, fuck proxy, it does not work
			// any way for some reason (see above)
			//image.load (new URLRequest (proxy (img)));
			image.load (new URLRequest (img));
			addChildAt (image, 0);

			list.enabled = true;

			altText.text = alt;
		}

		private function scrollTheComic (e:MouseEvent):void {
			if (image) {
				try {
					var w:Number = image.width;
					var h:Number = image.height;
					if (w > 465 - list.width) {
						// map mouseX = list.width to list.width and 465 to 465 - w
						var t:Number = (mouseX - list.width) / (465 - list.width);
						t = Math.max (0, Math.min (1, t));
						image.x = list.width * (1 - t) + (465 - w) * t;
					}
					if (h > 415) {
						// map mouseY = 0 to 0 and 415 to 415 - h
						t = mouseY / 415;
						t = Math.max (0, Math.min (1, t));
						image.y = (415 - h) * t;
					}
				} catch (e:*) {
					// insufficiently loaded or whatever
				}
			}
		}
    }
}

/**
 * List.as
 * Keith Peters
 * version 0.9.10
 * 
 * Wonderfl lib version has the bug,
 * list reports wrong selected item when scrolled.
 */

import com.bit101.components.*
/*package com.bit101.components
{*/
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[Event(name="select", type="flash.events.Event")]
	/*public*/ class List extends Component
	{
		protected var _items:Array;
		protected var _itemHolder:Sprite;
		protected var _panel:Panel;
		protected var _listItemHeight:Number = 20;
		protected var _listItemClass:Class =ListItem;
		protected var _scrollbar:VScrollBar;
		protected var _selectedIndex:int = -1;
		protected var _defaultColor:uint = 0xffffff//Style.LIST_DEFAULT;
		protected var _alternateColor:uint = 0xf3f3f3//Style.LIST_ALTERNATE;
		protected var _selectedColor:uint = 0xcccccc//Style.LIST_SELECTED;
		protected var _rolloverColor:uint = 0xdddddd//Style.LIST_ROLLOVER;
		protected var _alternateRows:Boolean = false;
		
		/**
		 * Constructor
		 * @param parent The parent DisplayObjectContainer on which to add this List.
		 * @param xpos The x position to place this component.
		 * @param ypos The y position to place this component.
		 * @param items An array of items to display in the list. Either strings or objects with label property.
		 */
		public function List(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, items:Array=null)
		{
			if(items != null)
			{
				_items = items;
			}
			else
			{
				_items = new Array();
			}
			super(parent, xpos, ypos);
		}
		
		/**
		 * Initilizes the component.
		 */
		protected override function init() : void
		{
			super.init();
			setSize(100, 100);
			addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
            addEventListener(Event.RESIZE, onResize);
            makeListItems();
            fillItems();
		}
		
		/**
		 * Creates and adds the child display objects of this component.
		 */
		protected override function addChildren() : void
		{
			super.addChildren();
			_panel = new Panel(this, 0, 0);
			_panel.color = _defaultColor;
			_itemHolder = new Sprite();
			_panel.content.addChild(_itemHolder);
			_scrollbar = new VScrollBar(this, 0, 0, onScroll);
            _scrollbar.setSliderParams(0, 0, 0);
		}
		
		/**
		 * Creates all the list items based on data.
		 */
		protected function makeListItems():void
		{
			var item:ListItem;
			while(_itemHolder.numChildren > 0)
			{
				item = ListItem(_itemHolder.getChildAt(0));
				item.removeEventListener(MouseEvent.CLICK, onSelect);
				_itemHolder.removeChildAt(0);
			}

            var numItems:int = Math.ceil(_height / _listItemHeight);
			numItems = Math.min(numItems, _items.length);
            numItems = Math.max(numItems, 1);
			for(var i:int = 0; i < numItems; i++)
			{

				item = new _listItemClass(_itemHolder, 0, i * _listItemHeight);
				item.setSize(width, _listItemHeight);
				item.defaultColor = _defaultColor;

				item.selectedColor = _selectedColor;
				item.rolloverColor = _rolloverColor;
				item.addEventListener(MouseEvent.CLICK, onSelect);
			}
		}

        protected function fillItems():void
        {
            var offset:int = _scrollbar.value;
            var numItems:int = Math.ceil(_height / _listItemHeight);
			numItems = Math.min(numItems, _items.length);
            for(var i:int = 0; i < numItems; i++)
            {
                var item:ListItem = _itemHolder.getChildAt(i) as ListItem;
				if(offset + i < _items.length)
				{
	                item.data = _items[offset + i];
				}
				else
				{
					item.data = "";
				}
				if(_alternateRows)
				{
					item.defaultColor = ((offset + i) % 2 == 0) ? _defaultColor : _alternateColor;
				}
				else
				{
					item.defaultColor = _defaultColor;
				}
                if(offset + i == _selectedIndex)
                {
                    item.selected = true;
                }
                else
                {
                    item.selected = false;
                }
            }
        }
		
		/**
		 * If the selected item is not in view, scrolls the list to make the selected item appear in the view.
		 */
		protected function scrollToSelection():void
		{
            var numItems:int = Math.ceil(_height / _listItemHeight);
			if(_selectedIndex != -1)
			{
				if(_scrollbar.value > _selectedIndex)
				{
//                    _scrollbar.value = _selectedIndex;
				}
				else if(_scrollbar.value + numItems < _selectedIndex)
				{
                    _scrollbar.value = _selectedIndex - numItems + 1;
				}
			}
			else
			{
				_scrollbar.value = 0;
			}
            fillItems();
		}
		
		
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		/**
		 * Draws the visual ui of the component.
		 */
		public override function draw() : void
		{
			super.draw();
			
			_selectedIndex = Math.min(_selectedIndex, _items.length - 1);


			// panel
			_panel.setSize(_width, _height);
			_panel.color = _defaultColor;
			_panel.draw();
			
			// scrollbar
			_scrollbar.x = _width - 10;
			var contentHeight:Number = _items.length * _listItemHeight;
			_scrollbar.setThumbPercent(_height / contentHeight); 
			var pageSize:Number = Math.floor(_height / _listItemHeight);
            _scrollbar.maximum = Math.max(0, _items.length - pageSize);
			_scrollbar.pageSize = pageSize;
			_scrollbar.height = _height;
			_scrollbar.draw();
            scrollToSelection();
		}
		
		/**
		 * Adds an item to the list.
		 * @param item The item to add. Can be a string or an object containing a string property named label.
		 */
		public function addItem(item:Object):void
		{
			_items.push(item);
			invalidate();
			makeListItems();
            fillItems();
		}
		
		/**
		 * Adds an item to the list at the specified index.
		 * @param item The item to add. Can be a string or an object containing a string property named label.
		 * @param index The index at which to add the item.
		 */
		public function addItemAt(item:Object, index:int):void
		{
			index = Math.max(0, index);
			index = Math.min(_items.length, index);
			_items.splice(index, 0, item);
			invalidate();
            fillItems();
		}
		
		/**
		 * Removes the referenced item from the list.
		 * @param item The item to remove. If a string, must match the item containing that string. If an object, must be a reference to the exact same object.
		 */
		public function removeItem(item:Object):void
		{
			var index:int = _items.indexOf(item);
			removeItemAt(index);
		}
		
		/**
		 * Removes the item from the list at the specified index
		 * @param index The index of the item to remove.
		 */
		public function removeItemAt(index:int):void
		{
			if(index < 0 || index >= _items.length) return;
			_items.splice(index, 1);
			invalidate();
            fillItems();
		}
		
		/**
		 * Removes all items from the list.
		 */
		public function removeAll():void
		{
			_items.length = 0;
			invalidate();
            fillItems();
		}
		
		
		
		
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		/**
		 * Called when a user selects an item in the list.
		 */
		protected function onSelect(event:Event):void
		{
			if(! (event.target is ListItem)) return;
			
			var offset:int = _scrollbar.value;
			
			for(var i:int = 0; i < _itemHolder.numChildren; i++)
			{
				if(_itemHolder.getChildAt(i) == event.target) _selectedIndex = i + offset;
				ListItem(_itemHolder.getChildAt(i)).selected = false;
			}
			ListItem(event.target).selected = true;
			dispatchEvent(new Event(Event.SELECT));
		}
		
		/**
		 * Called when the user scrolls the scroll bar.
		 */
		protected function onScroll(event:Event):void
		{
            fillItems();
		}
		
		/**
		 * Called when the mouse wheel is scrolled over the component.
		 */
		protected function onMouseWheel(event:MouseEvent):void
		{
			_scrollbar.value -= event.delta;
            fillItems();
		}

        protected function onResize(event:Event):void
        {
            makeListItems();
            fillItems();
        }
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		/**
		 * Sets / gets the index of the selected list item.
		 */
		public function set selectedIndex(value:int):void
		{
			if(value >= 0 && value < _items.length)
			{
				_selectedIndex = value;
//				_scrollbar.value = _selectedIndex;
			}
			else
			{
				_selectedIndex = -1;
			}
			invalidate();
			dispatchEvent(new Event(Event.SELECT));
		}
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}
		
		/**
		 * Sets / gets the item in the list, if it exists.
		 */
		public function set selectedItem(item:Object):void
		{
			var index:int = _items.indexOf(item);
//			if(index != -1)
//			{
				selectedIndex = index;
				invalidate();
				dispatchEvent(new Event(Event.SELECT));
//			}
		}
		public function get selectedItem():Object
		{
			if(_selectedIndex >= 0 && _selectedIndex < _items.length)
			{
				return _items[_selectedIndex];
			}
			return null;
		}

		/**
		 * Sets/gets the default background color of list items.
		 */
		public function set defaultColor(value:uint):void
		{
			_defaultColor = value;
			invalidate();
		}
		public function get defaultColor():uint
		{
			return _defaultColor;
		}

		/**
		 * Sets/gets the selected background color of list items.
		 */
		public function set selectedColor(value:uint):void
		{
			_selectedColor = value;
			invalidate();
		}
		public function get selectedColor():uint
		{
			return _selectedColor;
		}

		/**
		 * Sets/gets the rollover background color of list items.
		 */
		public function set rolloverColor(value:uint):void
		{
			_rolloverColor = value;
			invalidate();
		}
		public function get rolloverColor():uint
		{
			return _rolloverColor;
		}

		/**
		 * Sets the height of each list item.
		 */
		public function set listItemHeight(value:Number):void
		{
			_listItemHeight = value;
            makeListItems();
			invalidate();
		}
		public function get listItemHeight():Number
		{
			return _listItemHeight;
		}

		/**
		 * Sets / gets the list of items to be shown.
		 */
		public function set items(value:Array):void
		{
			_items = value;
			invalidate();
		}
		public function get items():Array
		{
			return _items;
		}

		/**
		 * Sets / gets the class used to render list items. Must extend ListItem.
		 */
		public function set listItemClass(value:Class):void
		{
			_listItemClass = value;
			makeListItems();
			invalidate();
		}
		public function get listItemClass():Class
		{
			return _listItemClass;
		}

		/**
		 * Sets / gets the color for alternate rows if alternateRows is set to true.
		 */
		public function set alternateColor(value:uint):void
		{
			_alternateColor = value;
			invalidate();
		}
		public function get alternateColor():uint
		{
			return _alternateColor;
		}
		
		/**
		 * Sets / gets whether or not every other row will be colored with the alternate color.
		 */
		public function set alternateRows(value:Boolean):void
		{
			_alternateRows = value;
			invalidate();
		}
		public function get alternateRows():Boolean
		{
			return _alternateRows;
		}

        /**
         * Sets / gets whether the scrollbar will auto hide when there is nothing to scroll.
         */
        public function set autoHideScrollBar(value:Boolean):void
        {
            _scrollbar.autoHide = value;
        }
        public function get autoHideScrollBar():Boolean
        {
            return _scrollbar.autoHide;
        }

	}
/*}*/