source: gs3-extensions/seaweed-debug/trunk/src/Platform.js@ 25160

Last change on this file since 25160 was 25160, checked in by sjm84, 12 years ago

Initial cut at a version of seaweed for debugging purposes. Check it out live into the web/ext folder

File size: 10.7 KB
Line 
1/*
2 * file: Platform.js
3 *
4 * @BEGINLICENSE
5 * Copyright 2010 Brook Novak (email : [email protected])
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * @ENDLICENSE
18 */
19bootstrap.provides("Platform");
20
21$enqueueInit("Platform", function() {
22
23 // Detect text direction locale - relies on DOM being ready for manipulation
24
25 var container = $createElement("p");
26 container.style.margin = "0 0 0 0";
27 container.style.padding = "0 0 0 0";
28 // container.style.textAlign = "start"; // If CSS 3+
29 container.style.textAlign = ""; // Explicitly override text align that might be assigned via style sheets
30
31 var span = $createElement("span");
32 span.innerHTML = "X";
33
34 container.appendChild(span);
35 docBody.appendChild(container);
36
37 // LTR if text position is nearer left of container, RTL if text position is nearer right of container
38 _localeDirection = span.offsetLeft < (container.offsetWidth - (span.offsetLeft + span.offsetWidth)) ?
39 "ltr" :
40 "rtl";
41
42 debug.println("LOCALE-DIRECTION: " + _localeDirection + "\n");
43
44 // Tidy up
45 docBody.removeChild(container);
46
47});
48
49/**
50 * The clients operating system. Never null, but can be de.Platform.UNKNOWN.
51 * @type Number
52 */
53var _os,
54
55 /**
56 * The clients browser. -1 if unknown.
57 * @type Number
58 */
59 _browser,
60
61 /**
62 * The browser version as a float. Can be -1 if could not determine the version.
63 * @type Number
64 */
65 _browserVersion,
66
67 /**
68 * The clients operating system. -1 if unknown.
69 * @type Number
70 */
71 _engine,
72
73 /**
74 * The layout/rendering engine. -1 if unavailable.
75 * @type Number
76 */
77 _engineVersion,
78
79 /**
80 * An enumeration for browser/engine/os types.
81 * In the release version these are replaced with actual values.
82 */
83 _Platform = {
84
85 /**
86 * Read Only
87 * @final
88 * @type Number
89 */
90 UNKNOWN : -1, // @REPLACE _Platform.UNKNOWN -1
91
92 /* BROWSER CONSTANTS */
93
94 /**
95 * Read Only: A browser constant
96 * @final
97 * @type Number
98 */
99 FIREFOX : 1, // @REPLACE _Platform.FIREFOX 1
100
101 /**
102 * Read Only: A browser constant
103 * @final
104 * @type Number
105 */
106 OPERA : 2, // @REPLACE _Platform.OPERA 2
107
108 /**
109 * Read Only: A browser constant
110 * @final
111 * @type Number
112 */
113 IE : 3, // @REPLACE _Platform.IE 3
114
115 /**
116 * Read Only: A browser constant
117 * @final
118 * @type Number
119 */
120 CHROME : 4, // @REPLACE _Platform.CHROME 4
121
122 /**
123 * Read Only: A browser constant
124 * @final
125 * @type Number
126 */
127 SAFARI : 6, // @REPLACE _Platform.SAFARI 6
128 //ICAB : 101, // Used as engine contant aswell
129
130 /**
131 * Read Only: A browser constant
132 * @final
133 * @type Number
134 */
135 KONQUEROR : 8, // @REPLACE _Platform.KONQUEROR 8
136
137 /**
138 * Read Only: A browser constant
139 * @final
140 * @type Number
141 */
142 NETSCAPE : 9, // @REPLACE _Platform.NETSCAPE 9
143
144
145 // OS CONSTANTS
146 /**
147 * Read Only: An OS constant
148 * @final
149 * @type Number
150 */
151 WINDOWS : 1, // @REPLACE _Platform.WINDOWS 1
152
153 /**
154 * Read Only: An OS constant
155 * @final
156 * @type Number
157 */
158 MAC : 2, // @REPLACE _Platform.MAC 2
159
160 /**
161 * Read Only: An OS constant
162 * @final
163 * @type Number
164 */
165 LINUX : 3, // @REPLACE _Platform.LINUX 3
166
167
168 /* LAYOUT/RENDERING ENGINE CONSTANTS */
169
170 /**
171 * Read Only: A rendering engine constant
172 * @final
173 * @type Number
174 */
175 GECKO : 1, // @REPLACE _Platform.GECKO 1
176
177 /**
178 * Read Only: A rendering engine constant
179 * @final
180 * @type Number
181 */
182 TRIDENT : 2, // @REPLACE _Platform.TRIDENT 2
183
184 /**
185 * Read Only: A rendering engine constant
186 * @final
187 * @type Number
188 */
189 WEBKIT : 3, // @REPLACE _Platform.WEBKIT 3
190
191 /**
192 * Read Only: A rendering engine constant
193 * @final
194 * @type Number
195 */
196 KHTML : 4, // @REPLACE _Platform.KHTML 4
197
198 /**
199 * Read Only: A rendering engine constant
200 * @final
201 * @type Number
202 */
203 PRESTO : 5 // @REPLACE _Platform.PRESTO 5
204
205 };
206
207
208// Perform platform detection
209(function(){
210
211 // References:
212 // - http://unixpapa.com/js/gecko.html
213 // - http://www.quirksmode.org/js/detect.html
214
215 var dataBrowser = [{
216 string: navigator.userAgent,
217 subString: "Chrome",
218 id: _Platform.CHROME,
219 versionSearch: "Chrome"
220 }, {
221 string: navigator.vendor,
222 subString: "Apple",
223 id: _Platform.SAFARI,
224 versionSearch: "Version"
225 }, {
226 prop: window.opera,
227 id: _Platform.OPERA,
228 versionSearch: "Opera"
229 }, {
230 string: navigator.vendor,
231 subString: "KDE",
232 id: _Platform.KONQUEROR,
233 versionSearch: "Konqueror"
234 }, {
235 string: navigator.userAgent,
236 subString: "Firefox",
237 id: _Platform.FIREFOX,
238 versionSearch: "Firefox"
239 }, {
240 // for newer Netscapes (6+)
241 string: navigator.userAgent,
242 subString: "Netscape",
243 id: _Platform.NETSCAPE,
244 versionSearch: "Netscape"
245 }, {
246 string: navigator.userAgent,
247 subString: "MSIE",
248 id: _Platform.IE,
249 versionSearch: "MSIE"
250 }, {
251 // for older Netscapes (4-)
252 string: navigator.userAgent,
253 subString: "Mozilla",
254 id: _Platform.NETSCAPE,
255 versionSearch: "Mozilla"
256 }];
257
258 var dataOS = [{
259 string: navigator.platform,
260 subString: "Win",
261 id: _Platform.WINDOWS
262 }, {
263 string: navigator.platform,
264 subString: "Mac",
265 id: _Platform.MAC
266 }, {
267 string: navigator.platform,
268 subString: "Linux",
269 id: _Platform.LINUX
270 }, ];
271
272 var dataEngine = [{
273 string: navigator.userAgent,
274 subString: "MSIE",
275 id: _Platform.TRIDENT,
276 versionSearch: "MSIE" // The trident versions are same as browser versions
277 }, {
278 // It is important that this is above KHTML - since webkit is forked from KHTML (which is still in webkits useragent/nav strings)
279 string: navigator.userAgent,
280 subString: "WebKit",
281 id: _Platform.WEBKIT,
282 versionSearch: "WebKit"
283 }, {
284 // It is important to have this above gecko data. since the user agent
285 // can contain gecko
286 string: navigator.userAgent,
287 subString: "KHTML",
288 id: _Platform.KHTML,
289 versionSearch: "KHTML"
290 }, {
291 string: navigator.userAgent,
292 subString: "Gecko",
293 id: _Platform.GECKO,
294 versionSearch: "rv"
295 }, {
296 prop: window.opera,
297 id: _Platform.PRESTO,
298 versionSearch: "Presto"
299 }, ];
300
301 function findMatchingPlatform(data){
302
303 for (var i in data) {
304
305 var dataString = data[i].string,
306 dataProp = data[i].prop;
307
308 if (dataString) {
309 if (dataString.indexOf(data[i].subString) != -1)
310 return data[i];
311 } else if (dataProp)
312 return data[i];
313 }
314
315 return null;
316 }
317
318 function extractVersion(dataString, versionSearchString){
319 if (!versionSearchString)
320 return null;
321 var index = dataString.indexOf(versionSearchString);
322 if (index == -1)
323 return null;
324 return parseFloat(dataString.substring(index + versionSearchString.length + 1)); // Add one for the "/" between the identifier and the version
325 }
326
327 debug.println("Inferring platform...");
328
329 // Get the OS
330 _os = findMatchingPlatform(dataOS);
331
332 debug.println("OS: " + (_os ? _os.subString : "UNKNOWN"));
333
334 // Set as the enum...
335 _os = _os ? _os.id || _Platform.UNKNOWN : _Platform.UNKNOWN;
336
337 // Get the browser
338 _browser = findMatchingPlatform(dataBrowser);
339
340 debug.println("BROWSER: " + (_browser ? _browser.versionSearch : "UNKNOWN"));
341
342 _browserVersion = _Platform.UNKNOWN;
343
344 if (_browser) {
345
346 // Extract the version
347 _browserVersion = extractVersion(navigator.userAgent, _browser.versionSearch) ||
348 extractVersion(navigator.appVersion, _browser.versionSearch);
349
350 // Set browser as the enum
351 _browser = _browser.id;
352
353 } else
354 _browser = _Platform.UNKNOWN;
355
356 debug.println("BROWSER-VERSION: " + _browserVersion);
357
358
359 // Get the layout engine
360 _engine = findMatchingPlatform(dataEngine);
361 _engineVersion = _Platform.UNKNOWN;
362
363 debug.println("ENGINE: " + (_engine ? (_engine.subString ? _engine.subString : _engine.versionSearch) : "UNKNOWN"));
364
365
366 if (_engine) {
367 _engineVersion = extractVersion(navigator.userAgent, _engine.versionSearch);
368 _engine = _engine.id;
369 } else
370 _engine = _Platform.UNKNOWN;
371
372 debug.println("ENGINE-VERSION: " + _engineVersion + "\n");
373
374})();
375
376$extend(de, {
377
378 /**
379 * @memberOf de
380 * Exposes internal platform enumaration
381 * @see _Platform
382 */
383 Platform : _Platform,
384
385 /**
386 * @memberOf de
387 * Exposes internal field
388 * @see _os
389 */
390 os : _os,
391
392 /**
393 * @memberOf de
394 * Exposes internal field
395 * @see _browser
396 */
397 browser : _browser,
398
399 /**
400 * @memberOf de
401 * Exposes internal field
402 * @see _browserVersion
403 */
404 browserVersion : _browserVersion,
405
406 /**
407 * @memberOf de
408 * Exposes internal field
409 * @see _engine
410 */
411 engine : _engine,
412
413 /**
414 * @memberOf de
415 * Exposes internal field
416 * @see _engineVersion
417 */
418 engineVersion : _engineVersion,
419
420 /**
421 * @memberOf de
422 * The Local text direction. Either "ltr" for Left to right or "rtl" for right to left.
423 * @type String
424 */
425 localDirection : null /* Detected once API initialized */
426
427});
428
429
Note: See TracBrowser for help on using the repository browser.