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