home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2012 April / ME_04_2012.iso / Video-Tutorial / iPhoto / media / player.swf / scripts / mx / utils / MediaQueryParser.as < prev    next >
Encoding:
Text File  |  2011-11-11  |  9.6 KB  |  333 lines

  1. package mx.utils
  2. {
  3.    import flash.system.Capabilities;
  4.    import mx.core.IFlexModuleFactory;
  5.    import mx.core.mx_internal;
  6.    
  7.    use namespace mx_internal;
  8.    
  9.    public class MediaQueryParser
  10.    {
  11.       private static var _instance:MediaQueryParser;
  12.       
  13.       public static var platformMap:Object = {
  14.          "AND":"android",
  15.          "IOS":"ios",
  16.          "MAC":"macintosh",
  17.          "WIN":"windows",
  18.          "LNX":"linux"
  19.       };
  20.       
  21.       mx_internal var goodQueries:Object = {};
  22.       
  23.       mx_internal var badQueries:Object = {};
  24.       
  25.       public var type:String = "screen";
  26.       
  27.       public var applicationDpi:Number;
  28.       
  29.       public var osPlatform:String;
  30.       
  31.       public function MediaQueryParser(param1:IFlexModuleFactory = null)
  32.       {
  33.          super();
  34.          this.applicationDpi = DensityUtil.getRuntimeDPI();
  35.          if(param1)
  36.          {
  37.             if(param1.info()["applicationDPI"] != null)
  38.             {
  39.                this.applicationDpi = param1.info()["applicationDPI"];
  40.             }
  41.          }
  42.          this.osPlatform = this.getPlatform();
  43.       }
  44.       
  45.       public static function get instance() : MediaQueryParser
  46.       {
  47.          return _instance;
  48.       }
  49.       
  50.       public static function set instance(param1:MediaQueryParser) : void
  51.       {
  52.          if(!_instance)
  53.          {
  54.             _instance = param1;
  55.          }
  56.       }
  57.       
  58.       public function parse(param1:String) : Boolean
  59.       {
  60.          var _loc5_:Boolean = false;
  61.          var _loc6_:String = null;
  62.          var _loc7_:Boolean = false;
  63.          var _loc8_:Array = null;
  64.          var _loc9_:int = 0;
  65.          param1 = StringUtil.trim(param1);
  66.          param1 = param1.toLowerCase();
  67.          if(param1 == "")
  68.          {
  69.             return true;
  70.          }
  71.          if(param1 == "all")
  72.          {
  73.             return true;
  74.          }
  75.          if(this.mx_internal::goodQueries[param1])
  76.          {
  77.             return true;
  78.          }
  79.          if(this.mx_internal::badQueries[param1])
  80.          {
  81.             return false;
  82.          }
  83.          var _loc2_:Array = param1.split(", ");
  84.          var _loc3_:int = int(_loc2_.length);
  85.          var _loc4_:int = 0;
  86.          while(_loc4_ < _loc3_)
  87.          {
  88.             _loc6_ = _loc2_[_loc4_];
  89.             _loc7_ = false;
  90.             if(_loc6_.indexOf("only ") == 0)
  91.             {
  92.                _loc6_ = _loc6_.substr(5);
  93.             }
  94.             if(_loc6_.indexOf("not ") == 0)
  95.             {
  96.                _loc7_ = true;
  97.                _loc6_ = _loc6_.substr(4);
  98.             }
  99.             _loc8_ = this.tokenizeMediaQuery(_loc6_);
  100.             _loc9_ = int(_loc8_.length);
  101.             if(_loc8_[0] == "all" || _loc8_[0] == this.type)
  102.             {
  103.                if(_loc9_ == 1 && !_loc7_)
  104.                {
  105.                   this.mx_internal::goodQueries[param1] = true;
  106.                   return true;
  107.                }
  108.                if(_loc9_ == 2)
  109.                {
  110.                   return false;
  111.                }
  112.                _loc8_.shift();
  113.                _loc8_.shift();
  114.                _loc5_ = this.evalExpressions(_loc8_);
  115.                if((_loc5_) && !_loc7_ || !_loc5_ && _loc7_)
  116.                {
  117.                   this.mx_internal::goodQueries[param1] = true;
  118.                   return true;
  119.                }
  120.             }
  121.             else if(_loc7_)
  122.             {
  123.                this.mx_internal::goodQueries[param1] = true;
  124.                return true;
  125.             }
  126.             _loc4_++;
  127.          }
  128.          this.mx_internal::badQueries[param1] = true;
  129.          return false;
  130.       }
  131.       
  132.       private function tokenizeMediaQuery(param1:String) : Array
  133.       {
  134.          var _loc9_:String = null;
  135.          var _loc2_:Array = [];
  136.          var _loc3_:int = int(param1.indexOf("("));
  137.          if(_loc3_ == 0)
  138.          {
  139.             _loc2_.push("all");
  140.             _loc2_.push("and");
  141.          }
  142.          else if(_loc3_ == -1)
  143.          {
  144.             return [param1];
  145.          }
  146.          var _loc4_:int = 0;
  147.          var _loc5_:Boolean = false;
  148.          var _loc6_:int = param1.length;
  149.          var _loc7_:Array = [];
  150.          var _loc8_:int = 0;
  151.          while(_loc8_ < _loc6_)
  152.          {
  153.             _loc9_ = param1.charAt(_loc8_);
  154.             if(!(StringUtil.isWhitespace(_loc9_) && _loc7_.length == 0))
  155.             {
  156.                if(_loc9_ == "/" && _loc8_ < _loc6_ - 1 && param1.charAt(_loc8_ + 1) == "*")
  157.                {
  158.                   _loc5_ = true;
  159.                   _loc8_++;
  160.                }
  161.                else if(_loc5_)
  162.                {
  163.                   if(_loc9_ == "*" && _loc8_ < _loc6_ - 1 && param1.charAt(_loc8_ + 1) == "/")
  164.                   {
  165.                      _loc5_ = false;
  166.                      _loc8_++;
  167.                   }
  168.                }
  169.                else
  170.                {
  171.                   if(_loc9_ == "(")
  172.                   {
  173.                      _loc4_++;
  174.                   }
  175.                   else if(_loc9_ == ")")
  176.                   {
  177.                      _loc4_--;
  178.                   }
  179.                   else
  180.                   {
  181.                      _loc7_.push(_loc9_);
  182.                   }
  183.                   if(_loc4_ == 0 && (StringUtil.isWhitespace(_loc9_) || _loc9_ == ")"))
  184.                   {
  185.                      if(_loc9_ != ")")
  186.                      {
  187.                         --_loc7_.length;
  188.                      }
  189.                      _loc2_.push(_loc7_.join(""));
  190.                      _loc7_.length = 0;
  191.                   }
  192.                }
  193.             }
  194.             _loc8_++;
  195.          }
  196.          return _loc2_;
  197.       }
  198.       
  199.       private function evalExpressions(param1:Array) : Boolean
  200.       {
  201.          var _loc4_:String = null;
  202.          var _loc5_:Array = null;
  203.          var _loc6_:Boolean = false;
  204.          var _loc7_:Boolean = false;
  205.          var _loc2_:int = int(param1.length);
  206.          var _loc3_:int = 0;
  207.          while(_loc3_ < _loc2_)
  208.          {
  209.             _loc4_ = param1[_loc3_];
  210.             if(_loc4_ != "and")
  211.             {
  212.                _loc5_ = _loc4_.split(":");
  213.                _loc6_ = false;
  214.                _loc7_ = false;
  215.                if(_loc5_[0].indexOf("min-") == 0)
  216.                {
  217.                   _loc6_ = true;
  218.                   _loc5_[0] = _loc5_[0].substr(4);
  219.                }
  220.                else if(_loc5_[0].indexOf("max-") == 0)
  221.                {
  222.                   _loc7_ = true;
  223.                   _loc5_[0] = _loc5_[0].substr(4);
  224.                }
  225.                if(_loc5_[0].indexOf("-") > 0)
  226.                {
  227.                   _loc5_[0] = this.deHyphenate(_loc5_[0]);
  228.                }
  229.                if(_loc5_.length == 1)
  230.                {
  231.                   if(!(_loc5_[0] in this))
  232.                   {
  233.                      return false;
  234.                   }
  235.                }
  236.                if(_loc5_.length == 2)
  237.                {
  238.                   if(!(_loc5_[0] in this))
  239.                   {
  240.                      return false;
  241.                   }
  242.                   if(_loc6_)
  243.                   {
  244.                      if(this[_loc5_[0]] < this.normalize(_loc5_[1],typeof this[_loc5_[0]]))
  245.                      {
  246.                         return false;
  247.                      }
  248.                   }
  249.                   else if(_loc7_)
  250.                   {
  251.                      if(this[_loc5_[0]] > this.normalize(_loc5_[1],typeof this[_loc5_[0]]))
  252.                      {
  253.                         return false;
  254.                      }
  255.                   }
  256.                   else if(this[_loc5_[0]] != this.normalize(_loc5_[1],typeof this[_loc5_[0]]))
  257.                   {
  258.                      return false;
  259.                   }
  260.                }
  261.             }
  262.             _loc3_++;
  263.          }
  264.          return true;
  265.       }
  266.       
  267.       private function normalize(param1:String, param2:String) : Object
  268.       {
  269.          var _loc3_:int = 0;
  270.          if(param1.charAt(0) == " ")
  271.          {
  272.             param1 = param1.substr(1);
  273.          }
  274.          if(param2 == "number")
  275.          {
  276.             _loc3_ = int(param1.indexOf("dpi"));
  277.             if(_loc3_ != -1)
  278.             {
  279.                param1 = param1.substr(0,_loc3_);
  280.             }
  281.             return Number(param1);
  282.          }
  283.          if(param2 == "int")
  284.          {
  285.             return int(param1);
  286.          }
  287.          if(param2 == "string")
  288.          {
  289.             if(param1.indexOf("\"") == 0)
  290.             {
  291.                if(param1.lastIndexOf("\"") == param1.length - 1)
  292.                {
  293.                   param1 = param1.substr(1,param1.length - 2);
  294.                }
  295.                else
  296.                {
  297.                   param1 = param1.substr(1);
  298.                }
  299.             }
  300.          }
  301.          return param1;
  302.       }
  303.       
  304.       private function deHyphenate(param1:String) : String
  305.       {
  306.          var _loc3_:String = null;
  307.          var _loc4_:String = null;
  308.          var _loc2_:int = int(param1.indexOf("-"));
  309.          while(_loc2_ > 0)
  310.          {
  311.             _loc3_ = param1.substr(_loc2_ + 1);
  312.             param1 = param1.substr(0,_loc2_);
  313.             _loc4_ = _loc3_.charAt(0);
  314.             _loc4_ = _loc4_.toUpperCase();
  315.             param1 += _loc4_ + _loc3_.substr(1);
  316.             _loc2_ = int(param1.indexOf("-"));
  317.          }
  318.          return param1;
  319.       }
  320.       
  321.       private function getPlatform() : String
  322.       {
  323.          var _loc1_:String = Capabilities.version.substr(0,3);
  324.          if(platformMap.hasOwnProperty(_loc1_))
  325.          {
  326.             return platformMap[_loc1_] as String;
  327.          }
  328.          return _loc1_.toLowerCase();
  329.       }
  330.    }
  331. }
  332.  
  333.