/sitios/com/ui/SiteAlert.as

https://bitbucket.org/simkinjerkin/simkinfw · ActionScript · 172 lines · 146 code · 24 blank · 2 comment · 14 complexity · b341412255d2e2be3c4ca4559e30b3b9 MD5 · raw file

  1. package com.ui {
  2. import com.ui.theme.SimpleButtonFactory;
  3. import flash.display.DisplayObject;
  4. import flash.display.MovieClip;
  5. import flash.display.SimpleButton;
  6. import flash.display.Sprite;
  7. import flash.events.MouseEvent;
  8. import flash.text.TextField;
  9. public class SiteAlert extends Sprite implements ISiteAlert {
  10. protected var _content:DisplayObject;
  11. protected var _buttonsList:Array;
  12. protected var _buttonsLabelsList: Array;
  13. protected var _closeButton: SimpleButton;
  14. protected var _initPositionY: Number;
  15. protected var _container: Sprite;
  16. protected var _text: TextField;
  17. protected var _background: DisplayObject;
  18. public function set message($message: String): void {
  19. _content["_txt"]["htmlText"] = $message;
  20. updateTextPosition()
  21. }
  22. protected function get bottomMarginButton(): Number{
  23. return 5;
  24. }
  25. public function get message():String {
  26. return _content["_txt"]["text"];
  27. }
  28. public function get container(): Sprite{
  29. return _container as Sprite;
  30. }
  31. public function get content():Sprite {
  32. return _content as Sprite;
  33. }
  34. override public function set width($value:Number):void {
  35. _background.width = $value;
  36. arrangeElements();
  37. }
  38. override public function set height($value:Number):void {
  39. _background.height = $value;
  40. arrangeElements();
  41. }
  42. protected function arrangeElements():void {
  43. _text.x = _background.x + ((_background.width - _text.width)/2);
  44. updateTextPosition();
  45. arrangeCloseButton();
  46. arrangeButtons();
  47. }
  48. protected function arrangeCloseButton():void {
  49. //_closeButton.x = _background.x + _background.width - 26;
  50. //_closeButton.y = _background.y + 5.3;
  51. }
  52. protected function arrangeButtons():void {
  53. var buttonsWidth:Number = 0;
  54. for(var j:uint = 0; j < _buttonsList.length; j++) {
  55. var btn:SimpleButton = _buttonsList[j] as SimpleButton;
  56. buttonsWidth += btn.width + 10;
  57. }
  58. var currentPos:Number = (_background.width - buttonsWidth) / 2;
  59. for (var i: int = 0; i<_buttonsList.length; i++) {
  60. var button : SimpleButton = _buttonsList[i] as SimpleButton;
  61. button.x = currentPos;
  62. button.y = _content.height - button.height - 5;
  63. currentPos += button.width + 10;
  64. }
  65. }
  66. public function SiteAlert($message: String = "", $buttonsLabelsList: Array = null) {
  67. initialize($message, $buttonsLabelsList != null ? $buttonsLabelsList : new Array());
  68. }
  69. public function destruct():void {
  70. _SiteAlert();
  71. }
  72. protected function initialize($message: String, $buttonsLabelsList: Array ): void {
  73. _content = new Alert_mc();
  74. _container = new Sprite();
  75. _buttonsList = new Array();
  76. _background = (_content as MovieClip)._background;
  77. _text = _content["_txt"];
  78. _closeButton = (_content as MovieClip).close_btn;
  79. _initPositionY = (_content as MovieClip)._txt.y - 5;
  80. _closeButton.addEventListener(MouseEvent.CLICK, onPressButtonClose);
  81. addChild(_content);
  82. addChild(_container);
  83. addButtons($buttonsLabelsList);
  84. arrangeElements();
  85. message = $message;
  86. }
  87. protected function updateTextPosition(): void{
  88. var content: MovieClip = _content as MovieClip;
  89. var textHeight: Number = (content._txt as TextField).textHeight;
  90. var height: Number = (content._txt as TextField).height;
  91. if(textHeight < height) {
  92. (content._txt as TextField).y = _initPositionY + ((height - textHeight) / 2);
  93. } else {
  94. (content._txt as TextField).y = _initPositionY;
  95. }
  96. }
  97. protected function addButtons($buttonsLabelsList : Array): void {
  98. for (var i: int=0; i<$buttonsLabelsList.length; i++) {
  99. var button: SimpleButton = getButton($buttonsLabelsList[i]);
  100. _buttonsList.push(button);
  101. addChild(button);
  102. button.addEventListener(MouseEvent.CLICK, onPressButton);
  103. }
  104. }
  105. protected function getButton($label: String):SimpleButton {
  106. return SimpleButtonFactory.getButton($label);
  107. }
  108. protected function onPressButton($event : MouseEvent): void {
  109. dispatchEvent(new AlertEvent(AlertEvent.ON_PRESS_ALERT_BUTTON, getID($event.currentTarget.name)));
  110. _SiteAlert();
  111. }
  112. protected function onPressButtonClose($event : MouseEvent):void {
  113. dispatchEvent(new AlertEvent(AlertEvent.ON_PRESS_CLOSE_BUTTON, -1));
  114. _SiteAlert();
  115. }
  116. protected function _SiteAlert():void {
  117. var i: int;
  118. if(_buttonsList) {
  119. for (i=0; i<_buttonsList.length; i++) {
  120. var button : SimpleButton = _buttonsList[i] as SimpleButton;
  121. button.removeEventListener(MouseEvent.CLICK, onPressButton);
  122. removeChild(button);
  123. }
  124. for (i=0; i<_buttonsList.length; i++) {
  125. _buttonsList[i]= null;
  126. }
  127. _buttonsList = null;
  128. }
  129. var lenght: int = _container.numChildren;
  130. for(var index: uint = 0 ; index<lenght ; index++){
  131. _container.removeChild(_container.getChildAt(0));
  132. }
  133. if(_content) {
  134. removeChild(_content);
  135. }
  136. _content = null;
  137. }
  138. protected function getID($currentName : String):uint {
  139. var i: int;
  140. for (i=0; i<_buttonsList.length; i++) {
  141. if (String (_buttonsList[i]["name"]) == $currentName) {
  142. break;
  143. }
  144. }
  145. return i;
  146. }
  147. }
  148. }