Flex で Google 風のページナビゲーション

Google のページを切り替えるリンク。ページネーションとかページャっていうのかな。あれを Flex で作ってみる。
出来上がりはこんな感じ。
ASDoc は面倒なので作ってませんが、linkColor という独自のスタイルを追加しています。linkColor でリンク文字の色を指定します。現在のページの色は よく使われる color で指定。あとは適当にテキスト系のスタイル等をコピーしています。
リンクをクリックすると flash.events.Event.CHANGE が飛びます。
ソースは以下。
ベースにするようなものが特にないので UIComponent を拡張して作ります。
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 | package jp.playwell.controls { import flash.display.DisplayObject; import flash.events.Event; import flash.events.MouseEvent; import mx.controls.LinkButton; import mx.core.UIComponent; import mx.styles.CSSStyleDeclaration; import mx.styles.StyleManager; //-------------------------------------- // Styles //-------------------------------------- include "../styles/metadata/FocusStyles.as" include "../styles/metadata/PaddingStyles.as" include "../styles/metadata/LeadingStyle.as" include "../styles/metadata/TextStyles.as" /** * リンクの色です. * * @default 0x003399 */ [Style(name="linkColor", type="uint", format="Color", inherit="yes")] //-------------------------------------- // Events //-------------------------------------- [Event(name="change", type="flash.events.Event")] /** * */ public class PageNavigator extends UIComponent { /** * デフォルトスタイルを設定 * @private */ private static var classConstructed:Boolean = classConstruct(); /** * @private */ private static function classConstruct():Boolean { var styleDeclaration:CSSStyleDeclaration = new CSSStyleDeclaration(); styleDeclaration.setStyle("linkColor", 0x003399); StyleManager.setStyleDeclaration("PageNavigator", styleDeclaration, true); return true; } /** * コンストラクタです. */ public function PageNavigator() { super(); } //--------------------------------------- // private properties //--------------------------------------- private var valueChanged:Boolean; //--------------------------------------- // prevLabel //--------------------------------------- private var _prevLabel:String = "<"; [Bindable] /** * prevLabel プロパティは前のページに戻るリンクのテキストです. * @default < */ public function get prevLabel():String { return _prevLabel; } public function set prevLabel(value:String):void { _prevLabel = value; valueChanged = true; invalidateProperties(); invalidateSize(); invalidateDisplayList(); } //--------------------------------------- // nextLabel //--------------------------------------- private var _nextLabel:String = ">"; [Bindable] /** * nextLabel プロパティは次のページに進むリンクのテキストです. * @default > */ public function get nextLabel():String { return _nextLabel; } public function set nextLabel(value:String):void { _nextLabel = value; valueChanged = true; invalidateProperties(); invalidateSize(); invalidateDisplayList(); } //--------------------------------------- // sidePages //--------------------------------------- private var _showPages:Number = 11; /** * showPages プロパティは最大何ページ分のリンクを表示するかを指定します. * @default 11 (現在のページ + 10ページ分) */ public function get showPages():Number { return _showPages; } public function set showPages(value:Number):void { _showPages = value; valueChanged = true; invalidateProperties(); invalidateSize(); invalidateDisplayList(); } //--------------------------------------- // maximum //--------------------------------------- private var _maximum:uint = 1; [Bindable] /** * maximum プロパティは最大ページ数を指定します. * @default 1 */ public function get maximum():uint { return _maximum; } public function set maximum(value:uint):void { _maximum = value; valueChanged = true; invalidateProperties(); invalidateSize(); invalidateDisplayList(); } //--------------------------------------- // value //--------------------------------------- private var _page:uint = 1; [Bindable] /** * page プロパティは現在のページを指定します. * @default 1 */ public function get page():uint { return _page; } public function set page(value:uint):void { _page = value; valueChanged = true; invalidateProperties(); invalidateSize(); invalidateDisplayList(); } //--------------------------------------- // override protected methods //--------------------------------------- override protected function commitProperties():void { super.commitProperties(); if (valueChanged) { valueChanged = false; while (numChildren > 0) removeEventListeners(removeChildAt(0)); _maximum = isNaN(maximum) ? 1 : uint(maximum); _page = isNaN(page) ? 1 : Math.min(maximum, uint(page)); var leftPages:uint = Math.floor((showPages - 1) / 2); var rightPages:uint = Math.ceil((showPages - 1) / 2); var min:uint = Math.max(1, page - leftPages); var max:uint = min + showPages - 1; if (max > maximum) { min = Math.max(1, min - max + maximum); max = maximum; } if (maximum > 1) { if (page > 1) addLink(prevLabel); var link:LinkButton; for (var i:uint = min; i <= max; i++) { link = addLink(i.toString()); if (i == page) { link.mouseEnabled = false; link.setStyle("textDecoration", "none"); link.setStyle("color", getStyle("color")); } } if (page < maximum) addLink(nextLabel); } } } override protected function measure():void { super.measure(); var w:uint = 0; var h:uint = 0; var n:uint = numChildren; var child:DisplayObject; for (var i:uint = 0; i < n; i++) { child = getChildAt(i); if (child is UIComponent) { w += UIComponent(child).measuredWidth; h = Math.max(h, UIComponent(child).measuredHeight); } } measuredWidth = measuredMinWidth = w; measuredHeight = measuredMinHeight = h; } override protected function updateDisplayList(w, h):void { super.updateDisplayList(w, h); var left:uint = 0; var n:uint = numChildren; var child:DisplayObject; for (var i:uint = 0; i < n; i++) { child = getChildAt(i); if (child is UIComponent) { UIComponent(child).setActualSize( UIComponent(child).measuredWidth, UIComponent(child).measuredHeight); UIComponent(child).move(left, (unscaledHeight - UIComponent(child).measuredHeight) / 2); left = UIComponent(child).x + UIComponent(child).measuredWidth; } } } override public function setStyle(styleProp:String, newValue:*):void { super.setStyle(styleProp, newValue); switch (styleProp) { case "color": case "linkColor": var n:int = numChildren; var child:DisplayObject; var label:String; for (var i:int = 0; i < n; i++) { child = getChildAt(i); if (!(child is LinkButton)) continue; label = LinkButton(child).label; if (label.match(/^\d+$/)) { if (parseInt(label, 10) == page) { LinkButton(child).setStyle("color", getStyle("color")); continue; } } LinkButton(child).setStyle("color", getStyle("linkColor")); } valueChanged = true; invalidateProperties(); break; } } //--------------------------------------- // private methods //--------------------------------------- /** * @private */ private function addLink(label:String):LinkButton { var linkButton:LinkButton = new LinkButton(); linkButton.styleName = this; linkButton.setStyle("paddingTop", 2); linkButton.setStyle("paddingBottom", 2); linkButton.setStyle("paddingLeft", 3); linkButton.setStyle("paddingRight", 3); linkButton.setStyle("textDecoration", "underline"); linkButton.setStyle("color", getStyle("linkColor")); linkButton.label = label; linkButton.addEventListener(MouseEvent.CLICK, link_clickHandler); addChild(linkButton); return linkButton; } private function removeEventListeners(obj:DisplayObject):void { obj.removeEventListener(MouseEvent.CLICK, link_clickHandler); } private function link_clickHandler(event:MouseEvent):void { var label:String = LinkButton(event.currentTarget).label; switch (label) { case prevLabel: page = page - 1; break; case nextLabel: page = page + 1; break; default: if (label.match(/^\d+$/)) page = parseInt(label, 10); else return; } dispatchEvent(new Event(Event.CHANGE)); } } } |
結構長くなりました。見にくいなあ。
今回は途中で色を変更した場合に setStyle メソッド内で色を変更してますが、ほんとは commitProperty あたりでやるべきかな。
もっと汎用性を高めるといいんだけど、今回はここまで。
Categories: Flex 3

