Upgrade to ExtJS 4.0.1 - Released 05/18/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      * @method
90      */
91     is: Ext.emptyFn,
92
93     /**
94      * Read-only - the full name of the current browser
95      * Possible values are: IE, Firefox, Safari, Chrome, Opera and Other
96      * @type String
97      */
98     name: null,
99
100     /**
101      * Read-only, refer to {@link Ext.Version}
102      * @type Ext.Version
103      */
104     version: null,
105
106     /**
107      * Read-only - the full name of the current browser's engine
108      * Possible values are: WebKit, Gecko, Presto, Trident and Other
109      * @type String
110      */
111     engineName: null,
112
113     /**
114      * Read-only, refer to {@link Ext.Version}
115      * @type Ext.Version
116      */
117     engineVersion: null,
118
119     constructor: function() {
120         var userAgent = this.userAgent = Ext.global.navigator.userAgent,
121             selfClass = this.statics(),
122             browserMatch = userAgent.match(new RegExp('((?:' + Ext.Object.getValues(selfClass.browserPrefixes).join(')|(?:') + '))([\\d\\._]+)')),
123             engineMatch = userAgent.match(new RegExp('((?:' + Ext.Object.getValues(selfClass.enginePrefixes).join(')|(?:') + '))([\\d\\._]+)')),
124             browserName = selfClass.browserNames.other,
125             browserVersion = '',
126             engineName = selfClass.engineNames.other,
127             engineVersion = '';
128
129         this.is = function(name) {
130             return this.is[name] === true;
131         };
132
133         if (browserMatch) {
134             browserName = selfClass.browserNames[Ext.Object.getKey(selfClass.browserPrefixes, browserMatch[1])];
135             browserVersion = browserMatch[2];
136         }
137
138         if (engineMatch) {
139             engineName = selfClass.engineNames[Ext.Object.getKey(selfClass.enginePrefixes, engineMatch[1])];
140             engineVersion = engineMatch[2];
141         }
142
143         Ext.apply(this, {
144             engineName: engineName,
145             engineVersion: new Ext.Version(engineVersion),
146             name: browserName,
147             version: new Ext.Version(browserVersion)
148         });
149
150         this.is[this.name] = true;
151         this.is[this.name + (this.version.getMajor() || '')] = true;
152         this.is[this.name + this.version.getShortVersion()] = true;
153         Ext.Object.each(selfClass.browserNames, function(key, name) {
154             this.is[name] = (this.name === name);
155         }, this);
156
157         this.is[this.name] = true;
158         this.is[this.engineName + (this.engineVersion.getMajor() || '')] = true;
159         this.is[this.engineName + this.engineVersion.getShortVersion()] = true;
160         Ext.Object.each(selfClass.engineNames, function(key, name) {
161             this.is[name] = (this.engineName === name);
162         }, this);
163
164
165         this.isSecure = /^https/i.test(Ext.global.location.protocol);
166
167         this.isStrict = Ext.global.document.compatMode === "CSS1Compat";
168
169         return this;
170     }
171
172 }, function() {
173
174     Ext.browser = new Ext.env.Browser();
175
176 });