Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / OS.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-env.OS'>/**
2 </span> * @class Ext.env.OS
3  * Provide useful information about the current operating system environment. Access the global instance stored in Ext.os. Example:
4  * &lt;pre&gt;&lt;code&gt;
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(&quot;Version &quot; + Ext.os.version);
14  * &lt;/code&gt;&lt;/pre&gt;
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 &quot;hybrid&quot; property, can be either accessed as a method call, i.e:
41      * &lt;pre&gt;&lt;code&gt;
42      * if (Ext.os.is('Android')) { ... }
43      * &lt;/code&gt;&lt;/pre&gt;
44      *
45      * or as an object with boolean properties, i.e:
46      * &lt;pre&gt;&lt;code&gt;
47      * if (Ext.os.is.Android) { ... }
48      * &lt;/code&gt;&lt;/pre&gt;
49      *
50      * Versions can be conveniently checked as well. For example:
51      * &lt;pre&gt;&lt;code&gt;
52      * if (Ext.os.is.Android2) { ... } // Equivalent to (Ext.os.is.Android &amp;&amp; Ext.os.version.equals(2))
53      *
54      * if (Ext.os.is.iOS32) { ... } // Equivalent to (Ext.os.is.iOS &amp;&amp; Ext.os.version.equals(3.2))
55      * &lt;/code&gt;&lt;/pre&gt;
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      */
65     is: Ext.emptyFn,
66
67     /*
68      * Read-only - the full name of the current operating system
69      * Possible values are: iOS, Android, WebOS, BlackBerry, MacOSX, Windows, Linux and Other
70      * @type String
71      */
72     name: null,
73
74     /*
75      * Read-only, refer to {@link Ext.Version}
76      * @type Ext.Version
77      */
78     version: null,
79
80     constructor: function() {
81         var userAgent = Ext.global.navigator.userAgent,
82             platform = Ext.global.navigator.platform,
83             selfClass = this.statics(),
84             osMatch = userAgent.match(new RegExp('((?:' + Ext.Object.getValues(selfClass.osPrefixes).join(')|(?:') + '))([^\\s;]+)')),
85             name = 'other',
86             version = '',
87             actualVersionMatch;
88
89         if (osMatch) {
90             name = selfClass.osNames[Ext.Object.getKey(selfClass.osPrefixes, osMatch[1])];
91             version = osMatch[2];
92
93             if (name === 'BlackBerry') {
94                 actualVersionMatch = userAgent.match(/Version\/([\d\._]+)/);
95
96                 if (actualVersionMatch) {
97                     version = actualVersionMatch[1];
98                 }
99             }
100         }
101         else {
102             name = selfClass.osNames[(userAgent.toLowerCase().match(/mac|win|linux/i) || ['other'])[0]];
103         }
104
105         Ext.apply(this, {
106             name: name,
107             version: new Ext.Version(version)
108         });
109
110         this.is = function(name) {
111             return this.is[name] === true;
112         };
113
114         if (name === 'iOS') {
115             this.is[platform] = true;
116         }
117
118         this.is[this.name] = true;
119         this.is[this.name + (this.version.getMajor() || '')] = true;
120         this.is[this.name + this.version.getShortVersion()] = true;
121
122         Ext.Object.each(selfClass.osNames, function(key, name) {
123             this.is[name] = (this.name === name);
124         }, this);
125
126         return this;
127     }
128 }, function() {
129
130 Ext.os = new Ext.env.OS();
131
132 });
133 </pre></pre></body></html>