PageRenderTime 899ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/bit101/components/List.as

http://quickb2.googlecode.com/
ActionScript | 484 lines | 309 code | 48 blank | 127 comment | 24 complexity | 33cb40045d98de318558f6f4e017ba34 MD5 | raw file
  1. /**
  2. * List.as
  3. * Keith Peters
  4. * version 0.9.9
  5. *
  6. * A scrolling list of selectable items.
  7. *
  8. * Copyright (c) 2011 Keith Peters
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. package com.bit101.components
  29. {
  30. import flash.display.DisplayObjectContainer;
  31. import flash.display.Sprite;
  32. import flash.events.Event;
  33. import flash.events.MouseEvent;
  34. [Event(name="select", type="flash.events.Event")]
  35. public class List extends Component
  36. {
  37. protected var _items:Array;
  38. protected var _itemHolder:Sprite;
  39. protected var _panel:Panel;
  40. protected var _listItemHeight:Number = 20;
  41. protected var _listItemClass:Class =ListItem;
  42. protected var _scrollbar:VScrollBar;
  43. protected var _selectedIndex:int = -1;
  44. protected var _defaultColor:uint = Style.LIST_DEFAULT;
  45. protected var _alternateColor:uint = Style.LIST_ALTERNATE;
  46. protected var _selectedColor:uint = Style.LIST_SELECTED;
  47. protected var _rolloverColor:uint = Style.LIST_ROLLOVER;
  48. protected var _alternateRows:Boolean = false;
  49. /**
  50. * Constructor
  51. * @param parent The parent DisplayObjectContainer on which to add this List.
  52. * @param xpos The x position to place this component.
  53. * @param ypos The y position to place this component.
  54. * @param items An array of items to display in the list. Either strings or objects with label property.
  55. */
  56. public function List(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, items:Array=null)
  57. {
  58. if(items != null)
  59. {
  60. _items = items;
  61. }
  62. else
  63. {
  64. _items = new Array();
  65. }
  66. super(parent, xpos, ypos);
  67. }
  68. /**
  69. * Initilizes the component.
  70. */
  71. protected override function init() : void
  72. {
  73. super.init();
  74. setSize(100, 100);
  75. addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
  76. addEventListener(Event.RESIZE, onResize);
  77. makeListItems();
  78. fillItems();
  79. }
  80. /**
  81. * Creates and adds the child display objects of this component.
  82. */
  83. protected override function addChildren() : void
  84. {
  85. super.addChildren();
  86. _panel = new Panel(this, 0, 0);
  87. _panel.color = _defaultColor;
  88. _itemHolder = new Sprite();
  89. _panel.content.addChild(_itemHolder);
  90. _scrollbar = new VScrollBar(this, 0, 0, onScroll);
  91. _scrollbar.setSliderParams(0, 0, 0);
  92. }
  93. /**
  94. * Creates all the list items based on data.
  95. */
  96. protected function makeListItems():void
  97. {
  98. while(_itemHolder.numChildren > 0) _itemHolder.removeChildAt(0);
  99. var numItems:int = Math.ceil(_height / _listItemHeight);
  100. for(var i:int = 0; i < numItems; i++)
  101. {
  102. var item:ListItem = new _listItemClass(_itemHolder, 0, i * _listItemHeight);
  103. item.setSize(width, _listItemHeight);
  104. item.defaultColor = _defaultColor;
  105. item.selectedColor = _selectedColor;
  106. item.rolloverColor = _rolloverColor;
  107. item.addEventListener(MouseEvent.CLICK, onSelect);
  108. }
  109. }
  110. protected function fillItems():void
  111. {
  112. var offset:int = _scrollbar.value;
  113. var numItems:int = Math.ceil(_height / _listItemHeight);
  114. for(var i:int = 0; i < numItems; i++)
  115. {
  116. var item:ListItem = _itemHolder.getChildAt(i) as ListItem;
  117. if(offset + i < _items.length)
  118. {
  119. item.data = _items[offset + i];
  120. }
  121. else
  122. {
  123. item.data = "";
  124. }
  125. if(_alternateRows)
  126. {
  127. item.defaultColor = ((offset + i) % 2 == 0) ? _defaultColor : _alternateColor;
  128. }
  129. else
  130. {
  131. item.defaultColor = _defaultColor;
  132. }
  133. if(offset + i == _selectedIndex)
  134. {
  135. item.selected = true;
  136. }
  137. else
  138. {
  139. item.selected = false;
  140. }
  141. }
  142. }
  143. /**
  144. * If the selected item is not in view, scrolls the list to make the selected item appear in the view.
  145. */
  146. protected function scrollToSelection():void
  147. {
  148. var numItems:int = Math.ceil(_height / _listItemHeight);
  149. if(_selectedIndex != -1)
  150. {
  151. if(_scrollbar.value > _selectedIndex)
  152. {
  153. // _scrollbar.value = _selectedIndex;
  154. }
  155. else if(_scrollbar.value + numItems < _selectedIndex)
  156. {
  157. _scrollbar.value = _selectedIndex - numItems + 1;
  158. }
  159. }
  160. else
  161. {
  162. _scrollbar.value = 0;
  163. }
  164. fillItems();
  165. }
  166. ///////////////////////////////////
  167. // public methods
  168. ///////////////////////////////////
  169. /**
  170. * Draws the visual ui of the component.
  171. */
  172. public override function draw() : void
  173. {
  174. super.draw();
  175. _selectedIndex = Math.min(_selectedIndex, _items.length - 1);
  176. // panel
  177. _panel.setSize(_width, _height);
  178. _panel.color = _defaultColor;
  179. _panel.draw();
  180. // scrollbar
  181. _scrollbar.x = _width - 10;
  182. var contentHeight:Number = _items.length * _listItemHeight;
  183. _scrollbar.setThumbPercent(_height / contentHeight);
  184. var pageSize:Number = Math.floor(_height / _listItemHeight);
  185. _scrollbar.maximum = Math.max(0, _items.length - pageSize);
  186. _scrollbar.pageSize = pageSize;
  187. _scrollbar.height = _height;
  188. _scrollbar.draw();
  189. scrollToSelection();
  190. }
  191. /**
  192. * Adds an item to the list.
  193. * @param item The item to add. Can be a string or an object containing a string property named label.
  194. */
  195. public function addItem(item:Object):void
  196. {
  197. _items.push(item);
  198. invalidate();
  199. fillItems();
  200. }
  201. /**
  202. * Adds an item to the list at the specified index.
  203. * @param item The item to add. Can be a string or an object containing a string property named label.
  204. * @param index The index at which to add the item.
  205. */
  206. public function addItemAt(item:Object, index:int):void
  207. {
  208. index = Math.max(0, index);
  209. index = Math.min(_items.length, index);
  210. _items.splice(index, 0, item);
  211. invalidate();
  212. fillItems();
  213. }
  214. /**
  215. * Removes the referenced item from the list.
  216. * @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.
  217. */
  218. public function removeItem(item:Object):void
  219. {
  220. var index:int = _items.indexOf(item);
  221. removeItemAt(index);
  222. }
  223. /**
  224. * Removes the item from the list at the specified index
  225. * @param index The index of the item to remove.
  226. */
  227. public function removeItemAt(index:int):void
  228. {
  229. if(index < 0 || index >= _items.length) return;
  230. _items.splice(index, 1);
  231. invalidate();
  232. fillItems();
  233. }
  234. /**
  235. * Removes all items from the list.
  236. */
  237. public function removeAll():void
  238. {
  239. _items.length = 0;
  240. invalidate();
  241. fillItems();
  242. }
  243. ///////////////////////////////////
  244. // event handlers
  245. ///////////////////////////////////
  246. /**
  247. * Called when a user selects an item in the list.
  248. */
  249. protected function onSelect(event:Event):void
  250. {
  251. if(! (event.target is ListItem)) return;
  252. var offset:int = _scrollbar.value;
  253. for(var i:int = 0; i < _itemHolder.numChildren; i++)
  254. {
  255. if(_itemHolder.getChildAt(i) == event.target) _selectedIndex = i + offset;
  256. ListItem(_itemHolder.getChildAt(i)).selected = false;
  257. }
  258. ListItem(event.target).selected = true;
  259. dispatchEvent(new Event(Event.SELECT));
  260. }
  261. /**
  262. * Called when the user scrolls the scroll bar.
  263. */
  264. protected function onScroll(event:Event):void
  265. {
  266. fillItems();
  267. }
  268. /**
  269. * Called when the mouse wheel is scrolled over the component.
  270. */
  271. protected function onMouseWheel(event:MouseEvent):void
  272. {
  273. _scrollbar.value -= event.delta;
  274. fillItems();
  275. }
  276. protected function onResize(event:Event):void
  277. {
  278. makeListItems();
  279. fillItems();
  280. }
  281. ///////////////////////////////////
  282. // getter/setters
  283. ///////////////////////////////////
  284. /**
  285. * Sets / gets the index of the selected list item.
  286. */
  287. public function set selectedIndex(value:int):void
  288. {
  289. if(value >= 0 && value < _items.length)
  290. {
  291. _selectedIndex = value;
  292. // _scrollbar.value = _selectedIndex;
  293. }
  294. else
  295. {
  296. _selectedIndex = -1;
  297. }
  298. invalidate();
  299. dispatchEvent(new Event(Event.SELECT));
  300. }
  301. public function get selectedIndex():int
  302. {
  303. return _selectedIndex;
  304. }
  305. /**
  306. * Sets / gets the item in the list, if it exists.
  307. */
  308. public function set selectedItem(item:Object):void
  309. {
  310. var index:int = _items.indexOf(item);
  311. // if(index != -1)
  312. // {
  313. selectedIndex = index;
  314. invalidate();
  315. dispatchEvent(new Event(Event.SELECT));
  316. // }
  317. }
  318. public function get selectedItem():Object
  319. {
  320. if(_selectedIndex >= 0 && _selectedIndex < _items.length)
  321. {
  322. return _items[_selectedIndex];
  323. }
  324. return null;
  325. }
  326. /**
  327. * Sets/gets the default background color of list items.
  328. */
  329. public function set defaultColor(value:uint):void
  330. {
  331. _defaultColor = value;
  332. invalidate();
  333. }
  334. public function get defaultColor():uint
  335. {
  336. return _defaultColor;
  337. }
  338. /**
  339. * Sets/gets the selected background color of list items.
  340. */
  341. public function set selectedColor(value:uint):void
  342. {
  343. _selectedColor = value;
  344. invalidate();
  345. }
  346. public function get selectedColor():uint
  347. {
  348. return _selectedColor;
  349. }
  350. /**
  351. * Sets/gets the rollover background color of list items.
  352. */
  353. public function set rolloverColor(value:uint):void
  354. {
  355. _rolloverColor = value;
  356. invalidate();
  357. }
  358. public function get rolloverColor():uint
  359. {
  360. return _rolloverColor;
  361. }
  362. /**
  363. * Sets the height of each list item.
  364. */
  365. public function set listItemHeight(value:Number):void
  366. {
  367. _listItemHeight = value;
  368. makeListItems();
  369. invalidate();
  370. }
  371. public function get listItemHeight():Number
  372. {
  373. return _listItemHeight;
  374. }
  375. /**
  376. * Sets / gets the list of items to be shown.
  377. */
  378. public function set items(value:Array):void
  379. {
  380. _items = value;
  381. invalidate();
  382. }
  383. public function get items():Array
  384. {
  385. return _items;
  386. }
  387. /**
  388. * Sets / gets the class used to render list items. Must extend ListItem.
  389. */
  390. public function set listItemClass(value:Class):void
  391. {
  392. _listItemClass = value;
  393. makeListItems();
  394. invalidate();
  395. }
  396. public function get listItemClass():Class
  397. {
  398. return _listItemClass;
  399. }
  400. /**
  401. * Sets / gets the color for alternate rows if alternateRows is set to true.
  402. */
  403. public function set alternateColor(value:uint):void
  404. {
  405. _alternateColor = value;
  406. invalidate();
  407. }
  408. public function get alternateColor():uint
  409. {
  410. return _alternateColor;
  411. }
  412. /**
  413. * Sets / gets whether or not every other row will be colored with the alternate color.
  414. */
  415. public function set alternateRows(value:Boolean):void
  416. {
  417. _alternateRows = value;
  418. invalidate();
  419. }
  420. public function get alternateRows():Boolean
  421. {
  422. return _alternateRows;
  423. }
  424. /**
  425. * Sets / gets whether the scrollbar will auto hide when there is nothing to scroll.
  426. */
  427. public function set autoHideScrollBar(value:Boolean):void
  428. {
  429. _scrollbar.autoHide = value;
  430. }
  431. public function get autoHideScrollBar():Boolean
  432. {
  433. return _scrollbar.autoHide;
  434. }
  435. }
  436. }