Advertisement
Guest User

Flex HTML Class

a guest
Sep 2nd, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package mx.controls
  2. {
  3.    import mx.core.ScrollControlBase;
  4.    import mx.core.IDataRenderer;
  5.    import mx.controls.listClasses.IDropInListItemRenderer;
  6.    import mx.controls.listClasses.IListItemRenderer;
  7.    import mx.managers.IFocusManagerComponent;
  8.    import flash.html.HTMLLoader;
  9.    import flash.events.Event;
  10.    import mx.core.IFactory;
  11.    import flash.system.ApplicationDomain;
  12.    import flash.events.HTMLUncaughtScriptExceptionEvent;
  13.    import flash.html.HTMLHistoryItem;
  14.    import mx.controls.listClasses.BaseListData;
  15.    import flash.net.URLRequest;
  16.    import flash.events.MouseEvent;
  17.    import flash.html.HTMLHost;
  18.    import mx.events.FlexEvent;
  19.    import mx.core.EdgeMetrics;
  20.    import mx.core.ClassFactory;
  21.    import mx.core.FlexHTMLLoader;
  22.    import mx.core.mx_internal;
  23.    import mx.core.ScrollPolicy;
  24.    
  25.    public class HTML extends ScrollControlBase implements IDataRenderer, IDropInListItemRenderer, IListItemRenderer, IFocusManagerComponent
  26.    {
  27.      
  28.       private static const MAX_HTML_HEIGHT:Number = 2880;
  29.      
  30.       static const VERSION:String = "3.6.0.12937";
  31.      
  32.       private static const MAX_HTML_WIDTH:Number = 2880;
  33.        
  34.       private var _data:Object;
  35.      
  36.       private var _userAgent:String;
  37.      
  38.       private var _htmlLoaderFactory:IFactory;
  39.      
  40.       private var _location:String;
  41.      
  42.       private var _runtimeApplicationDomain:ApplicationDomain;
  43.      
  44.       private var htmlTextChanged:Boolean = false;
  45.      
  46.       public var htmlLoader:HTMLLoader;
  47.      
  48.       private var _paintsDefaultBackground:Boolean;
  49.      
  50.       private var locationChanged:Boolean = false;
  51.      
  52.       private var htmlHostChanged:Boolean = false;
  53.      
  54.       private var _listData:BaseListData;
  55.      
  56.       private var _htmlText:String;
  57.      
  58.       private var _htmlHost:HTMLHost;
  59.      
  60.       private var paintsDefaultBackgroundChanged:Boolean = false;
  61.      
  62.       private var userAgentChanged:Boolean = false;
  63.      
  64.       private var runtimeApplicationDomainChanged:Boolean = false;
  65.      
  66.       private var textSet:Boolean;
  67.      
  68.       public function HTML()
  69.       {
  70.          _htmlLoaderFactory = new ClassFactory(FlexHTMLLoader);
  71.          super();
  72.          mx_internal::_horizontalScrollPolicy = ScrollPolicy.AUTO;
  73.          mx_internal::_verticalScrollPolicy = ScrollPolicy.AUTO;
  74.          tabEnabled = false;
  75.          tabChildren = true;
  76.       }
  77.      
  78.       public static function get pdfCapability() : int
  79.       {
  80.          return HTMLLoader.pdfCapability;
  81.       }
  82.      
  83.       public function get contentHeight() : Number
  84.       {
  85.          if(!htmlLoader)
  86.          {
  87.             return 0;
  88.          }
  89.          return htmlLoader.contentHeight;
  90.       }
  91.      
  92.       public function get userAgent() : String
  93.       {
  94.          return _userAgent;
  95.       }
  96.      
  97.       public function set userAgent(value:String) : void
  98.       {
  99.          _userAgent = value;
  100.          userAgentChanged = true;
  101.          invalidateProperties();
  102.       }
  103.      
  104.       public function get loaded() : Boolean
  105.       {
  106.          if(!htmlLoader || locationChanged || htmlTextChanged)
  107.          {
  108.             return false;
  109.          }
  110.          return htmlLoader.loaded;
  111.       }
  112.      
  113.       public function cancelLoad() : void
  114.       {
  115.          if(htmlLoader)
  116.          {
  117.             htmlLoader.cancelLoad();
  118.          }
  119.       }
  120.      
  121.       private function htmlLoader_htmlRenderHandler(event:Event) : void
  122.       {
  123.          dispatchEvent(event);
  124.          adjustScrollBars();
  125.       }
  126.      
  127.       public function get htmlLoaderFactory() : IFactory
  128.       {
  129.          return _htmlLoaderFactory;
  130.       }
  131.      
  132.       public function historyForward() : void
  133.       {
  134.          if(htmlLoader)
  135.          {
  136.             htmlLoader.historyForward();
  137.          }
  138.       }
  139.      
  140.       public function set htmlLoaderFactory(value:IFactory) : void
  141.       {
  142.          _htmlLoaderFactory = value;
  143.          dispatchEvent(new Event("htmlLoaderFactoryChanged"));
  144.       }
  145.      
  146.       public function get historyLength() : int
  147.       {
  148.          if(!htmlLoader)
  149.          {
  150.             return 0;
  151.          }
  152.          return htmlLoader.historyLength;
  153.       }
  154.      
  155.       public function reload() : void
  156.       {
  157.          if(htmlLoader)
  158.          {
  159.             htmlLoader.reload();
  160.          }
  161.       }
  162.      
  163.       override protected function createChildren() : void
  164.       {
  165.          super.createChildren();
  166.          if(!htmlLoader)
  167.          {
  168.             htmlLoader = htmlLoaderFactory.newInstance();
  169.             htmlLoader.addEventListener(Event.HTML_DOM_INITIALIZE,htmlLoader_domInitialize);
  170.             htmlLoader.addEventListener(Event.COMPLETE,htmlLoader_completeHandler);
  171.             htmlLoader.addEventListener(Event.HTML_RENDER,htmlLoader_htmlRenderHandler);
  172.             htmlLoader.addEventListener(Event.LOCATION_CHANGE,htmlLoader_locationChangeHandler);
  173.             htmlLoader.addEventListener(Event.HTML_BOUNDS_CHANGE,htmlLoader_htmlBoundsChangeHandler);
  174.             htmlLoader.addEventListener(Event.SCROLL,htmlLoader_scrollHandler);
  175.             htmlLoader.addEventListener(HTMLUncaughtScriptExceptionEvent.UNCAUGHT_SCRIPT_EXCEPTION,htmlLoader_uncaughtScriptExceptionHandler);
  176.             addChild(htmlLoader);
  177.          }
  178.       }
  179.      
  180.       public function historyGo(steps:int) : void
  181.       {
  182.          if(htmlLoader)
  183.          {
  184.             htmlLoader.historyGo(steps);
  185.          }
  186.       }
  187.      
  188.       private function htmlLoader_domInitialize(event:Event) : void
  189.       {
  190.          dispatchEvent(event);
  191.       }
  192.      
  193.       private function adjustScrollBars() : void
  194.       {
  195.          setScrollBarProperties(htmlLoader.contentWidth,htmlLoader.width,htmlLoader.contentHeight,htmlLoader.height);
  196.          if(verticalScrollBar)
  197.          {
  198.             verticalScrollBar.lineScrollSize = 20;
  199.          }
  200.          if(horizontalScrollBar)
  201.          {
  202.             horizontalScrollBar.lineScrollSize = 20;
  203.          }
  204.       }
  205.      
  206.       public function get data() : Object
  207.       {
  208.          return _data;
  209.       }
  210.      
  211.       public function get location() : String
  212.       {
  213.          return _location;
  214.       }
  215.      
  216.       private function htmlLoader_completeHandler(event:Event) : void
  217.       {
  218.          invalidateSize();
  219.          dispatchEvent(event);
  220.       }
  221.      
  222.       public function set historyPosition(value:int) : void
  223.       {
  224.          if(htmlLoader)
  225.          {
  226.             htmlLoader.historyPosition = value;
  227.          }
  228.       }
  229.      
  230.       public function getHistoryAt(position:int) : HTMLHistoryItem
  231.       {
  232.          if(!htmlLoader)
  233.          {
  234.             return null;
  235.          }
  236.          return htmlLoader.getHistoryAt(position);
  237.       }
  238.      
  239.       public function get runtimeApplicationDomain() : ApplicationDomain
  240.       {
  241.          return _runtimeApplicationDomain;
  242.       }
  243.      
  244.       override protected function scrollHandler(event:Event) : void
  245.       {
  246.          super.scrollHandler(event);
  247.          htmlLoader.scrollH = horizontalScrollPosition;
  248.          htmlLoader.scrollV = verticalScrollPosition;
  249.       }
  250.      
  251.       public function historyBack() : void
  252.       {
  253.          if(htmlLoader)
  254.          {
  255.             htmlLoader.historyBack();
  256.          }
  257.       }
  258.      
  259.       override protected function commitProperties() : void
  260.       {
  261.          super.commitProperties();
  262.          if(htmlHostChanged)
  263.          {
  264.             htmlLoader.htmlHost = _htmlHost;
  265.             htmlHostChanged = false;
  266.          }
  267.          if(paintsDefaultBackgroundChanged)
  268.          {
  269.             htmlLoader.paintsDefaultBackground = _paintsDefaultBackground;
  270.             paintsDefaultBackgroundChanged = false;
  271.          }
  272.          if(runtimeApplicationDomainChanged)
  273.          {
  274.             htmlLoader.runtimeApplicationDomain = _runtimeApplicationDomain;
  275.             runtimeApplicationDomainChanged = false;
  276.          }
  277.          if(userAgentChanged)
  278.          {
  279.             htmlLoader.userAgent = _userAgent;
  280.             userAgentChanged = false;
  281.          }
  282.          if(locationChanged)
  283.          {
  284.             htmlLoader.load(new URLRequest(_location));
  285.             locationChanged = false;
  286.          }
  287.          if(htmlTextChanged)
  288.          {
  289.             htmlLoader.loadString("<html><body><h1>HALLOOOOOOOOO</h1></body></html>");
  290.             htmlTextChanged = false;
  291.          }
  292.       }
  293.      
  294.       public function get contentWidth() : Number
  295.       {
  296.          if(!htmlLoader)
  297.          {
  298.             return 0;
  299.          }
  300.          return htmlLoader.contentWidth;
  301.       }
  302.      
  303.       public function get domWindow() : Object
  304.       {
  305.          if(!htmlLoader)
  306.          {
  307.             return null;
  308.          }
  309.          return htmlLoader.window;
  310.       }
  311.      
  312.       override protected function mouseWheelHandler(event:MouseEvent) : void
  313.       {
  314.          if(event.target != this)
  315.          {
  316.             return;
  317.          }
  318.          event.delta = event.delta * 6;
  319.          super.mouseWheelHandler(event);
  320.       }
  321.      
  322.       override public function set verticalScrollPosition(value:Number) : void
  323.       {
  324.          var value:Number = Math.max(value,0);
  325.          if(htmlLoader && htmlLoader.contentHeight > htmlLoader.height)
  326.          {
  327.             value = Math.min(value,htmlLoader.contentHeight - htmlLoader.height);
  328.          }
  329.          super.verticalScrollPosition = value;
  330.          if(htmlLoader)
  331.          {
  332.             htmlLoader.scrollV = value;
  333.          }
  334.          else
  335.          {
  336.             invalidateProperties();
  337.          }
  338.       }
  339.      
  340.       public function set data(value:Object) : void
  341.       {
  342.          var newText:* = undefined;
  343.          _data = value;
  344.          if(_listData)
  345.          {
  346.             newText = _listData.label;
  347.          }
  348.          else if(_data != null)
  349.          {
  350.             if(_data is String)
  351.             {
  352.                newText = String(_data);
  353.             }
  354.             else
  355.             {
  356.                newText = _data.toString();
  357.             }
  358.          }
  359.          if(newText !== undefined && !textSet)
  360.          {
  361.             htmlText = newText;
  362.             textSet = false;
  363.          }
  364.          dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
  365.       }
  366.      
  367.       private function htmlLoader_scrollHandler(event:Event) : void
  368.       {
  369.          horizontalScrollPosition = htmlLoader.scrollH;
  370.          verticalScrollPosition = htmlLoader.scrollV;
  371.       }
  372.      
  373.       public function get historyPosition() : int
  374.       {
  375.          if(!htmlLoader)
  376.          {
  377.             return 0;
  378.          }
  379.          return htmlLoader.historyPosition;
  380.       }
  381.      
  382.       override protected function measure() : void
  383.       {
  384.          super.measure();
  385.          var em:EdgeMetrics = viewMetrics;
  386.          em.left = em.left + getStyle("paddingLeft");
  387.          em.top = em.top + getStyle("paddingTop");
  388.          em.right = em.right + getStyle("paddingRight");
  389.          em.bottom = em.bottom + getStyle("paddingBottom");
  390.          measuredWidth = Math.min(htmlLoader.contentWidth + em.left + em.right,MAX_HTML_WIDTH);
  391.          measuredHeight = Math.min(htmlLoader.contentHeight + em.top + em.bottom,MAX_HTML_HEIGHT);
  392.       }
  393.      
  394.       public function set listData(value:BaseListData) : void
  395.       {
  396.          _listData = value;
  397.       }
  398.      
  399.       private function htmlLoader_uncaughtScriptExceptionHandler(event:HTMLUncaughtScriptExceptionEvent) : void
  400.       {
  401.          var clonedEvent:Event = event.clone();
  402.          dispatchEvent(clonedEvent);
  403.          if(clonedEvent.isDefaultPrevented())
  404.          {
  405.             event.preventDefault();
  406.          }
  407.       }
  408.      
  409.       private function htmlLoader_locationChangeHandler(event:Event) : void
  410.       {
  411.          var change:Boolean = _location != htmlLoader.location;
  412.          _location = htmlLoader.location;
  413.          if(change)
  414.          {
  415.             dispatchEvent(event);
  416.          }
  417.       }
  418.      
  419.       public function set htmlHost(value:HTMLHost) : void
  420.       {
  421.          _htmlHost = value;
  422.          htmlHostChanged = true;
  423.          invalidateProperties();
  424.       }
  425.      
  426.       public function get listData() : BaseListData
  427.       {
  428.          return _listData;
  429.       }
  430.      
  431.       public function set htmlText(value:String) : void
  432.       {
  433.          _htmlText = value;
  434.          htmlTextChanged = true;
  435.          _location = null;
  436.          locationChanged = false;
  437.          invalidateProperties();
  438.          invalidateSize();
  439.          invalidateDisplayList();
  440.          dispatchEvent(new Event("htmlTextChanged"));
  441.       }
  442.      
  443.       public function set location(value:String) : void
  444.       {
  445.          _location = value;
  446.          locationChanged = true;
  447.          _htmlText = null;
  448.          htmlTextChanged = false;
  449.          invalidateProperties();
  450.          invalidateSize();
  451.          invalidateDisplayList();
  452.          dispatchEvent(new Event("locationChange"));
  453.       }
  454.      
  455.       public function get htmlHost() : HTMLHost
  456.       {
  457.          return _htmlHost;
  458.       }
  459.      
  460.       override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void
  461.       {
  462.          super.updateDisplayList(unscaledWidth,unscaledHeight);
  463.          var em:EdgeMetrics = viewMetrics;
  464.          em.left = em.left + getStyle("paddingLeft");
  465.          em.top = em.top + getStyle("paddingTop");
  466.          em.right = em.right + getStyle("paddingRight");
  467.          em.bottom = em.bottom + getStyle("paddingBottom");
  468.          htmlLoader.x = em.left;
  469.          htmlLoader.y = em.top;
  470.          var w:Number = Math.max(unscaledWidth - em.left - em.right,1);
  471.          var h:Number = Math.max(unscaledHeight - em.top - em.bottom,1);
  472.          htmlLoader.width = w;
  473.          htmlLoader.height = h;
  474.       }
  475.      
  476.       public function get htmlText() : String
  477.       {
  478.          return _htmlText;
  479.       }
  480.      
  481.       private function htmlLoader_htmlBoundsChangeHandler(event:Event) : void
  482.       {
  483.          invalidateSize();
  484.          adjustScrollBars();
  485.       }
  486.      
  487.       public function get paintsDefaultBackground() : Boolean
  488.       {
  489.          return _paintsDefaultBackground;
  490.       }
  491.      
  492.       public function set paintsDefaultBackground(value:Boolean) : void
  493.       {
  494.          _paintsDefaultBackground = value;
  495.          paintsDefaultBackgroundChanged = true;
  496.          invalidateProperties();
  497.       }
  498.      
  499.       public function set runtimeApplicationDomain(value:ApplicationDomain) : void
  500.       {
  501.          _runtimeApplicationDomain = value;
  502.          runtimeApplicationDomainChanged = true;
  503.          invalidateProperties();
  504.       }
  505.    }
  506. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement