Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / core / src / env / OS.js
1 /**
2  * @class Ext.env.OS
3  * Provide useful information about the current operating system environment. Access the global instance stored in Ext.os. Example:
4  * <pre><code>
5  * if (Ext.os.is.Windows) {
6  *      // Windows specific code here
7  * }
8  *
9  * if (Ext.os.is.iOS) {
10  *      // iPad, iPod, iPhone, etc.
11  * }
12  *
13  * console.log("Version " + Ext.os.version);
14  * </code></pre>
15  *
16  * For a full list of supported values, refer to: {@link Ext.env.OS#is Ext.env.OS.is}
17  */
18 Ext.define('Ext.env.OS', {
19
20     statics: {
21         osNames: {
22             ios: 'iOS',
23             android: 'Android',
24             webos: 'WebOS',
25             blackberry: 'BlackBerry',
26             mac: 'MacOSX',
27             win: 'Windows',
28             linux: 'Linux',
29             other: 'Other'
30         },
31         osPrefixes: {
32             ios: 'iPhone OS ',
33             android: 'Android ',
34             blackberry: 'BlackBerry ',
35             webos: 'webOS/'
36         }
37     },
38
39     /**
40      * A "hybrid" property, can be either accessed as a method call, i.e:
41      * <pre><code>
42      * if (Ext.os.is('Android')) { ... }
43      * </code></pre>
44      *
45      * or as an object with boolean properties, i.e:
46      * <pre><code>
47      * if (Ext.os.is.Android) { ... }
48      * </code></pre>
49      *
50      * Versions can be conveniently checked as well. For example:
51      * <pre><code>
52      * if (Ext.os.is.Android2) { ... } // Equivalent to (Ext.os.is.Android && Ext.os.version.equals(2))
53      *
54      * if (Ext.os.is.iOS32) { ... } // Equivalent to (Ext.os.is.iOS && Ext.os.version.equals(3.2))
55      * </code></pre>
56      *
57      * Note that only {@link Ext.Version#getMajor major component}  and {@link Ext.Version#getShortVersion shortVersion}
58      * value of the version are available via direct property checking.
59      *
60      * Supported values are: iOS, iPad, iPhone, iPod, Android, WebOS, BlackBerry, MacOSX, Windows, Linux and Other
61      *
62      * @param {String} value The OS name to check
63      * @return {Boolean}
64      * @method
65      */
66     is: Ext.emptyFn,
67
68     /**
69      * Read-only - the full name of the current operating system
70      * Possible values are: iOS, Android, WebOS, BlackBerry, MacOSX, Windows, Linux and Other
71      * @type String
72      */
73     name: null,
74
75     /**
76      * Read-only, refer to {@link Ext.Version}
77      * @type Ext.Version
78      */
79     version: null,
80
81     constructor: function() {
82         var userAgent = Ext.global.navigator.userAgent,
83             platform = Ext.global.navigator.platform,
84             selfClass = this.statics(),
85             osMatch = userAgent.match(new RegExp('((?:' + Ext.Object.getValues(selfClass.osPrefixes).join(')|(?:') + '))([^\\s;]+)')),
86             name = 'other',
87             version = '',
88             actualVersionMatch;
89
90         if (osMatch) {
91             name = selfClass.osNames[Ext.Object.getKey(selfClass.osPrefixes, osMatch[1])];
92             version = osMatch[2];
93
94             if (name === 'BlackBerry') {
95                 actualVersionMatch = userAgent.match(/Version\/([\d\._]+)/);
96
97                 if (actualVersionMatch) {
98                     version = actualVersionMatch[1];
99                 }
100             }
101         }
102         else {
103             name = selfClass.osNames[(userAgent.toLowerCase().match(/mac|win|linux/i) || ['other'])[0]];
104         }
105
106         Ext.apply(this, {
107             name: name,
108             version: new Ext.Version(version)
109         });
110
111         this.is = function(name) {
112             return this.is[name] === true;
113         };
114
115         if (name === 'iOS') {
116             this.is[platform] = true;
117         }
118
119         this.is[this.name] = true;
120         this.is[this.name + (this.version.getMajor() || '')] = true;
121         this.is[this.name + this.version.getShortVersion()] = true;
122
123         Ext.Object.each(selfClass.osNames, function(key, name) {
124             this.is[name] = (this.name === name);
125         }, this);
126
127         return this;
128     }
129 }, function() {
130
131 Ext.os = new Ext.env.OS();
132
133 });