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