001 
002 /*
003  *  JScripter Standard 1.0 - To Script In Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package jsx.client;
021 
022 import js.Field;
023 import js.Disposable;
024 import js.Js;
025 import js.Static;
026 import js.Var;
027 import js.user.JsHTMLElement;
028 import js.user.JsNavigator;
029 
030 /**
031  * <p>A utility class accessing the properties of {@link js.user.JsWindow#navigator}.</p>
032  * <p>Users are encouraged to use the utilities provided by this class instead of the 
033  * <b>opaque</b> methods of {@link js.user.JsNavigator} in consideration of the reuse 
034  * benefit for re-compilation results.</p>
035  * 
036  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
037  */
038 public final class Browser extends Disposable
039 {
040     private Browser() {}
041 
042     /**
043      * <p>Statically accesses a property of <tt>navigator</tt> by the name of this field. 
044      * @see js.user.JsNavigator
045      * @since 1.0
046      */
047     public final static Var<String> userAgent = new Static<String>(new Field<String>(Win.navigator, JsNavigator.userAgent));
048     /**
049      * <p>Statically accesses a property of <tt>navigator</tt> by the name of this field. 
050      * @see js.user.JsNavigator
051      * @since 1.0
052      */
053     public final static Var<Boolean> cookieEnabled = new Static<Boolean>(new Field<Boolean>(Win.navigator, JsNavigator.cookieEnabled));
054 
055     /**
056      * <p>Determines whether the current browser supports {@link JsHTMLElement#insertAdjacentHTML}.</p>
057      * @since 1.0
058      */
059     public static final boolean canInsertHTML = Js.be(JsHTMLElement.insertAdjacentHTML.with(Document.body.var()));
060     /**
061      * <p>Determines whether the current browser supports {@link Client#XMLHttpRequest}.</p>
062      * @since 1.0
063      */
064     public static final boolean hasXMLHttpRequest = Js.be(Client.XMLHttpRequest.var());
065 
066     /**
067      * <p>Determines whether the {@link #userAgent} of the current browser 
068      * case-insensitively contains a specified string.</p>
069      * @param s A string.
070      * @return <tt>true</tt> if the {@link #userAgent} of the current browser 
071      * case-insensitively contains <tt>s</tt>, <tt>false</tt> otherwise.
072      * @since 1.0
073      */
074     public static final boolean userAgent(String s) {
075         return Js.re(s, "i").test(userAgent.var());
076     }
077     /**
078      * <p>Determines whether the running browser supports AdobeAir.</p>
079      * @since 1.0
080      */
081     public static final boolean isAdobeAir = userAgent("AdobeAir");
082     /**
083      * <p>Determines whether the running browser supports Chrome.</p>
084      * @since 1.0
085      */
086     public static final boolean isChrome   = userAgent("Chrome"  );
087     /**
088      * <p>Determines whether the running browser supports Gecko.</p>
089      * @since 1.0
090      */
091     public static final boolean isGecko    = userAgent("Gecko"   );
092     /**
093      * <p>Determines whether the running browser supports Firefox.</p>
094      * @since 1.0
095      */
096     public static final boolean isFirefox  = userAgent("Firefox" );
097     /**
098      * <p>Determines whether the running browser supports Internet Explorer.</p>
099      * @since 1.0
100      */
101     public static final boolean isIE       = userAgent("MSIE"    );
102     /**
103      * <p>Determines whether the running browser supports Internet Explorer 7.</p>
104      * @since 1.0
105      */
106     public static final boolean isIE7      = userAgent("MSIE 7"  );
107     /**
108      * <p>Determines whether the running browser supports Internet Explorer 8.</p>
109      * @since 1.0
110      */
111     public static final boolean isIE8      = userAgent("MSIE 8"  );
112     /**
113      * <p>Determines whether the running browser supports Internet Explorer 9.</p>
114      * @since 1.0
115      */
116     public static final boolean isIE9      = userAgent("MSIE 9"  );
117     /**
118      * <p>Determines whether the running browser supports Opera.</p>
119      * @since 1.0
120      */
121     public static final boolean isOpera    = userAgent("Opera"   );
122     /**
123      * <p>Determines whether the running browser supports Safari.</p>
124      * @since 1.0
125      */
126     public static final boolean isSafari   = userAgent("Safari"  );
127     /**
128      * <p>Determines whether the running browser supports WebKit.</p>
129      * @since 1.0
130      */
131     public static final boolean isWebKit   = userAgent("WebKit"  );
132 
133     /**
134      * <p>Determines whether the running browser supports Linux.</p>
135      * @since 1.0
136      */
137     public static final boolean isLinux    = userAgent("Linux"             );
138     /**
139      * <p>Determines whether the running browser supports Macintosh.</p>
140      * @since 1.0
141      */
142     public static final boolean isMac      = userAgent("Macintosh|Mac OS X");
143     /**
144      * <p>Determines whether the running browser supports Windows.</p>
145      * @since 1.0
146      */
147     public static final boolean isWindows  = userAgent("Windows|Win32"     );
148 }