Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / src / env / Browser.js
1 /**
2  * @class Ext.env.Browser
3  * @extends Ext.Base
4  * Provide useful information about the current browser.
5  * Should not be manually instantiated unless for unit-testing; access the global instance stored in Ext.browser instead. Example:
6  * <pre><code>
7  * if (Ext.browser.is.IE) {
8  *      // IE specific code here
9  * }
10  *
11  * if (Ext.browser.is.WebKit) {
12  *      // WebKit specific code here
13  * }
14  *
15  * console.log("Version " + Ext.browser.version);
16  * </code></pre>
17  *
18  * For a full list of supported values, refer to: {@link Ext.env.Browser#is}
19  *
20  * @borrows Ext.Base.extend
21  */
22 Ext.define('Ext.env.Browser', {
23     statics: {
24         browserNames: {
25             ie: 'IE',
26             firefox: 'Firefox',
27             safari: 'Safari',
28             chrome: 'Chrome',
29             opera: 'Opera',
30             other: 'Other'
31         },
32         engineNames: {
33             webkit: 'WebKit',
34             gecko: 'Gecko',
35             presto: 'Presto',
36             trident: 'Trident',
37             other: 'Other'
38         },
39         enginePrefixes: {
40             webkit: 'AppleWebKit/',
41             gecko: 'Gecko/',
42             presto: 'Presto/',
43             trident: 'Trident/'
44         },
45         browserPrefixes: {
46             ie: 'MSIE ',
47             firefox: 'Firefox/',
48             chrome: 'Chrome/',
49             safari: 'Version/',
50             opera: 'Opera/'
51         }
52     },
53
54     /**
55      * True if the page is running over SSL
56      * @type Boolean
57      */
58     isSecure: false,
59
60     /**
61      * True if the document is in strict mode
62      * @type Boolean
63      */
64     isStrict: false,
65
66     /**
67      * A "hybrid" property, can be either accessed as a method call, i.e:
68      * <pre><code>
69      * if (Ext.browser.is('IE')) { ... }
70      * </code></pre>
71      *
72      * or as an object with boolean properties, i.e:
73      * <pre><code>
74      * if (Ext.browser.is.IE) { ... }
75      * </code></pre>
76      *
77      * Versions can be conveniently checked as well. For example:
78      * <pre><code>
79      * if (Ext.browser.is.IE6) { ... } // Equivalent to (Ext.browser.is.IE && Ext.browser.version.equals(6))
80      * </code></pre>
81      *
82      * Note that only {@link Ext.Version#getMajor major component}  and {@link Ext.Version#getShortVersion shortVersion}
83      * value of the version are available via direct property checking.
84      *
85      * Supported values are: IE, Firefox, Safari, Chrome, Opera, WebKit, Gecko, Presto, Trident and Other
86      *
87      * @param {String} value The OS name to check
88      * @return {Boolean}
89      */
90     is: Ext.emptyFn,
91
92     /**
93      * Read-only - the full name of the current browser
94      * Possible values are: IE, Firefox, Safari, Chrome, Opera and Other
95      * @type String
96      */
97     name: null,
98
99     /**
100      * Read-only, refer to {@link Ext.Version}
101      * @type Ext.Version
102      */
103     version: null,
104
105     /**
106      * Read-only - the full name of the current browser's engine
107      * Possible values are: WebKit, Gecko, Presto, Trident and Other
108      * @type String
109      */
110     engineName: null,
111
112     /**
113      * Read-only, refer to {@link Ext.Version}
114      * @type Ext.Version
115      */
116     engineVersion: null,
117
118     constructor: function() {
119         var userAgent = this.userAgent = Ext.global.navigator.userAgent,
120             selfClass = this.statics(),
121             browserMatch = userAgent.match(new RegExp('((?:' + Ext.Object.getValues(selfClass.browserPrefixes).join(')|(?:') + '))([\\d\\._]+)')),
122             engineMatch = userAgent.match(new RegExp('((?:' + Ext.Object.getValues(selfClass.enginePrefixes).join(')|(?:') + '))([\\d\\._]+)')),
123             browserName = selfClass.browserNames.other,
124             browserVersion = '',
125             engineName = selfClass.engineNames.other,
126             engineVersion = '';
127
128         this.is = function(name) {
129             return this.is[name] === true;
130         };
131
132         if (browserMatch) {
133             browserName = selfClass.browserNames[Ext.Object.getKey(selfClass.browserPrefixes, browserMatch[1])];
134             browserVersion = browserMatch[2];
135         }
136
137         if (engineMatch) {
138             engineName = selfClass.engineNames[Ext.Object.getKey(selfClass.enginePrefixes, engineMatch[1])];
139             engineVersion = engineMatch[2];
140         }
141
142         Ext.apply(this, {
143             engineName: engineName,
144             engineVersion: new Ext.Version(engineVersion),
145             name: browserName,
146             version: new Ext.Version(browserVersion)
147         });
148
149         this.is[this.name] = true;
150         this.is[this.name + (this.version.getMajor() || '')] = true;
151         this.is[this.name + this.version.getShortVersion()] = true;
152         Ext.Object.each(selfClass.browserNames, function(key, name) {
153             this.is[name] = (this.name === name);
154         }, this);
155
156         this.is[this.name] = true;
157         this.is[this.engineName + (this.engineVersion.getMajor() || '')] = true;
158         this.is[this.engineName + this.engineVersion.getShortVersion()] = true;
159         Ext.Object.each(selfClass.engineNames, function(key, name) {
160             this.is[name] = (this.engineName === name);
161         }, this);
162
163
164         this.isSecure = /^https/i.test(Ext.global.location.protocol);
165
166         this.isStrict = Ext.global.document.compatMode === "CSS1Compat";
167
168         return this;
169     }
170
171 }, function() {
172
173     Ext.browser = new Ext.env.Browser();
174
175 });