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