PageRenderTime 187ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/v01/src/com/CGFinal/Controls/LinkSplitPage.as

http://my-as3-lib.googlecode.com/
ActionScript | 193 lines | 143 code | 26 blank | 24 comment | 27 complexity | cca30d58ad2dc2cc18c60b69b62ad82f MD5 | raw file
  1. package com.CGFinal.Controls {
  2. import com.CGFinal.BaseUI;
  3. import com.CGFinal.LiteComponent;
  4. import flash.events.Event;
  5. import flash.events.TextEvent;
  6. import flash.text.StyleSheet;
  7. import flash.text.TextField;
  8. import flash.text.TextFormat;
  9. import xBei.Helper.StringHelper;
  10. /**
  11. * ...
  12. * @author ...
  13. */
  14. public class LinkSplitPage extends BaseUI {
  15. private var _txt:TextField;
  16. private var _format:String = '<a href="[FIRST]">??</a> <a href="[PRE]">???</a> [PAGELIST] <a href="[NEXT]">???</a> <a href="[LAST]">??</a>';
  17. public function set Format(v:String):void{
  18. _format = v;
  19. }
  20. private var _recordCount:uint = 0;
  21. /**
  22. * ????
  23. */
  24. public function get RecordCount():uint {
  25. return _recordCount;
  26. }
  27. public function set RecordCount(v:uint):void {
  28. _recordCount = v;
  29. _pageCount = 0;
  30. }
  31. private var _pageSize:uint = 10;
  32. /**
  33. * ????
  34. */
  35. public function get PageSize():uint {
  36. return _pageSize;
  37. }
  38. public function set PageSize(v:uint):void {
  39. if (v < 0) {
  40. _pageSize = 1;
  41. }
  42. _pageSize = v;
  43. _pageCount = 0;
  44. }
  45. private var _oldPageIndex:uint = 1;
  46. public function get OldPageIndex():uint {
  47. return _oldPageIndex;
  48. }
  49. private var _pageIndex:uint = 1;
  50. /**
  51. * ????
  52. */
  53. public function get PageIndex():uint {
  54. return _pageIndex;
  55. }
  56. public function set PageIndex(v:uint):void {
  57. if (v == _pageIndex) {
  58. return;
  59. }
  60. if (_pageCount != _oldPageIndex) {
  61. _oldPageIndex = _pageIndex;
  62. }
  63. if (v > this.PageCount) {
  64. _pageIndex = this.PageCount;
  65. } else {
  66. _pageIndex = v;
  67. }
  68. if (_pageIndex < 1) {
  69. _pageIndex = 1;
  70. }
  71. }
  72. private var _pageCount:uint = 0;
  73. /**
  74. * ????????
  75. */
  76. public function get PageCount():uint {
  77. if (this.RecordCount < 1) {
  78. _pageCount = 0;
  79. return 0;
  80. } else if (_pageCount == 0) {
  81. _pageCount = Math.round(RecordCount / PageSize + 0.499999999999);
  82. }
  83. return _pageCount;
  84. }
  85. public function LinkSplitPage() {
  86. throw new Error('??????xBei.UI.LinkSplitePager');
  87. }
  88. private var _tf:TextFormat;
  89. override protected function createChildren():void {
  90. super.createChildren();
  91. _txt = super.createLabel('');
  92. _tf = _txt.getTextFormat();
  93. _tf.bold = true;
  94. _tf.font = 'Arial';
  95. _tf.color = 0x366989;
  96. _txt.defaultTextFormat = _tf;
  97. _txt.addEventListener(TextEvent.LINK, DPE_ChangePageIndex);
  98. var css:StyleSheet = new StyleSheet();
  99. css.parseCSS("body{font-size:12;font-family:Arial;font-weight:bold;color:#366989;}a:link{text-decoration:none;color:#366989;}a:hover{text-decoration:none;color:#0E84CE;}.s{font-size:20;}");
  100. this._txt.styleSheet = css;
  101. addChild(_txt);
  102. }
  103. public function RenderPager():void {
  104. if (this.RecordCount == 0 || this.PageCount == 0) {
  105. this._txt.text = '??????';
  106. return;
  107. }
  108. var si:int = this.PageIndex - 5;
  109. var ei:int = this.PageIndex + 4;
  110. if (ei > this.PageCount) {
  111. si -= ei - this.PageCount;
  112. ei = this.PageCount;
  113. }
  114. if (si <= 0) {
  115. ei -= si;
  116. ei++;
  117. if (ei > this.PageCount) {
  118. ei = this.PageCount;
  119. }
  120. si = 1;
  121. }
  122. var pagelist:String = '';
  123. if(this.PageIndex > 6){
  124. pagelist += '<a href="event:gotoPage:1">1</a>...';
  125. }
  126. for (var i:int = si; i <= ei; i++) {
  127. var tmp:String;
  128. if(this.PageIndex == i){
  129. tmp = StringHelper.Format('<a href="event:gotoPage:{0}" class="s">{0}</a> ',i);
  130. }else {
  131. tmp = StringHelper.Format('<a href="event:gotoPage:{0}">{0}</a> ', i);
  132. }
  133. //tmp = StringHelper.Format('<a href="event:gotoPage:{0}">{0}</a>', i);
  134. pagelist += tmp;
  135. }
  136. if(this.PageIndex + 5 <= this.PageCount){
  137. pagelist += StringHelper.Format('...<a href="event:gotoPage:{0}">{0}</a>', this.PageCount);
  138. }
  139. //trace("pageindex=",this.PageIndex,this.PageIndex > 1 ? this.PageIndex - 1 : 1);
  140. var html:String = this._format.replace("[FIRST]", "event:gotoPage:1").
  141. replace("[PRE]", StringHelper.Format("event:gotoPage:{0}", this.PageIndex > 1 ? this.PageIndex - 1 : 1)).
  142. replace("[NEXT]", StringHelper.Format("event:gotoPage:{0}", this.PageIndex < this.PageCount ? this.PageIndex + 1 : this.PageCount))
  143. .replace("[LAST]", StringHelper.Format("event:gotoPage:{0}", this.PageCount)).replace("[PAGELIST]", pagelist)
  144. this._txt.htmlText = html;
  145. //this._txt.setTextFormat(_tf);
  146. }
  147. //Do Events
  148. protected function OnPageIndexChanged(newIndex:int):void {
  149. if(this.PageIndex != newIndex){
  150. this.PageIndex = newIndex;
  151. //trace("old:",this.OldPageIndex,"new:",newIndex);
  152. trace("????", newIndex);
  153. this.dispatchEvent(new Event(Event.CHANGE));
  154. }
  155. }
  156. //Events
  157. private function DPE_ChangePageIndex(e:TextEvent):void {
  158. //trace(e.text);
  159. var reg:RegExp = /^gotoPage:(\d+)$/ig;
  160. var m:Object = reg.exec(e.text);
  161. if (m) {
  162. //trace("goto:",m[1]);
  163. this.OnPageIndexChanged(int(m[1]));
  164. }
  165. }
  166. }
  167. }