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