Flexi.as
上传用户:szxykj
上传日期:2009-10-05
资源大小:322k
文件大小:6k
源码类别:

FlashMX/Flex源码

开发平台:

Flex

  1. /*
  2.  * Flexi
  3.  * v1.2
  4.  * Arcadio Carballares Martin, 2009
  5.  * http://www.arcadiocarballares.com
  6.  */
  7. package com.arcadiocarballares {
  8. import flash.external.ExternalInterface;
  9. import flash.geom.Point;
  10. import mx.containers.Box;
  11. import mx.core.Application;
  12. import mx.core.Container;
  13. import mx.events.FlexEvent;
  14. import mx.events.MoveEvent;
  15. import mx.events.ResizeEvent;
  16. import mx.events.ScrollEvent;
  17.             
  18. public class Flexi extends Box {
  19.     
  20.     [Bindable]
  21.     public var url:String="http://www.carballares.es/arcadio";
  22.     [Bindable]
  23.     public var border:int=0;
  24.     private var contX:Boolean=false;
  25.     private var contY:Boolean=false;
  26.     
  27. public function Flexi() {
  28. super();
  29. }
  30. public function hideIFrame():void {
  31. var action:String="function () {" + 
  32.             "document.getElementById('FlexIframe"+name+"').style.display='none';"+
  33. "}";
  34. ExternalInterface.call(action);
  35. }
  36. public function showIFrame():void {
  37. var action:String="function () {" + 
  38.             "document.getElementById('FlexIframe"+name+"').style.display='block';"+
  39. "}";
  40. ExternalInterface.call(action);
  41. }
  42. override protected function measure():void {
  43. super.measure();
  44. // Script to create the IFRAME which contain HTML 'url'
  45.         var action:String="function () {" + 
  46.             "var FlexIframe=document.createElement('iframe');" + 
  47.             "FlexIframe.id='FlexIframe"+name+"';" +
  48.             "FlexIframe.style.position='absolute';" + 
  49.             "FlexIframe.frameBorder='"+border+"';" + 
  50.             "FlexIframe.src='"+url+"';" +
  51.             "document.getElementsByTagName('body')[0].appendChild(FlexIframe);" +
  52. "}";
  53.         // Control scroll events
  54.         Application.application.addEventListener(ScrollEvent.SCROLL, updateDimensions);
  55.         Container(owner).addEventListener(FlexEvent.CREATION_COMPLETE, updateDimensions);
  56.         Container(owner).addEventListener(ResizeEvent.RESIZE, updateDimensions);
  57.         Container(owner).addEventListener(MoveEvent.MOVE, updateDimensions);
  58.         
  59.         ExternalInterface.call(action);
  60. //getContainers(this);
  61. getContainers(Container(owner));
  62. }
  63. private function updateDimensions(event:Event=null):void {
  64. if (this==null) {return};
  65. var dimensions:Object=getDimensions();
  66.         var pos:Point=Container(owner).localToGlobal(new Point(0,0));
  67.         var left:int=int(pos.x);
  68.         var top:int=int(pos.y);
  69.         var myWidth:int=int(dimensions.myWidth);
  70.         var myHeight:int=int(dimensions.myHeight);
  71. var action:String="function () {" + 
  72.         "document.getElementById('FlexIframe"+name+"').style.left="+left+"+'px';"+
  73.         "document.getElementById('FlexIframe"+name+"').style.top="+top+"+'px';"+
  74.         "document.getElementById('FlexIframe"+name+"').style.width="+myWidth+";"+ 
  75.         "document.getElementById('FlexIframe"+name+"').style.height="+myHeight+";"+
  76. "}";
  77.     ExternalInterface.call(action);
  78. }
  79. private function getContainers(cont:Container):void {
  80.     /*  
  81.      Check 'parent' with fixed dimensions and containers with 100% dimensions
  82.         because they cause 'move' and 'resize' events and attach them a listener.
  83.         Function is recursive until it adds all listeners or reaches superior level.
  84.     */
  85.     if (cont is Container && contX==false && contY==false) {
  86.         if (isNaN(cont.percentWidth) && contX==false && esContenedorValido(cont)) {
  87.             cont.parent.addEventListener(MoveEvent.MOVE, updateDimensions);
  88.             contX=true;
  89.         }
  90.         if (isNaN(cont.percentHeight) && contY==false && esContenedorValido(cont)) {
  91.             cont.addEventListener(MoveEvent.MOVE, updateDimensions);
  92.             contY=true;
  93.         }
  94.         getContainers(cont.parent as Container);
  95.     }
  96.      
  97. private function esContenedorValido(cont:Object):Boolean {
  98.     var isValid:Boolean=true;
  99.     // Containers than doesn't dispatch 'move' or 'resize' events
  100.     switch (Container(cont).className) {
  101.         case "GridItem":
  102.             isValid=false;
  103.             break;
  104.         case "GridRow":
  105.             isValid=false;
  106.             break;
  107.     }
  108.     return isValid;
  109. }
  110. private function getDimensions():Object {
  111.     var dimensions:Object=new Object();
  112.     dimensions.myWidth=100;
  113.     dimensions.myHeight=100;
  114.     if (width) {
  115.      dimensions.myWidth = width;
  116.     } else {
  117.     if (isNaN(percentWidth)) {
  118.         dimensions.myWidth = Container(owner).width;
  119.     } else {
  120.         dimensions.myWidth = Container(owner).width
  121.             -int(Container(owner).getStyle('paddingLeft'))
  122.             -int(Container(owner).getStyle('paddingRight'));
  123.             if (Container(owner).getStyle('borderStyle')=='solid') {
  124.                 dimensions.myWidth=dimensions.myWidth
  125.                     -int(Container(owner).getStyle('borderThickness'))
  126.                     -int(Container(owner).getStyle('borderThickness'));
  127.             }
  128.     }
  129.     }
  130.     if (height) {
  131.      dimensions.myHeight = height;
  132.     } else {
  133.     if (isNaN(percentHeight)) {
  134.         dimensions.myHeight = Container(owner).height;
  135.     } else {
  136.         dimensions.myHeight = Container(owner).height
  137.             -int(Container(owner).getStyle('paddingTop'))
  138.             -int(Container(owner).getStyle('paddingBottom'));
  139.             if (Container(owner).getStyle('borderStyle')=='solid') {
  140.                 dimensions.myHeight=dimensions.myHeight
  141.                     -int(Container(owner).getStyle('borderThickness'))
  142.                     -int(Container(owner).getStyle('borderThickness'));
  143.             }
  144.     }
  145.     }
  146.     return dimensions;
  147. }
  148. }
  149. }