      1 // ==========================================================================
      2 // Project:   The M-Project - Mobile HTML5 Application Framework
      3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
      4 // Creator:   Sebastian
      5 // Date:      22.11.2010
      6 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
      7 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
      8 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
      9 // ==========================================================================
     10
     11 m_require('core/foundation/object.js');
     12
     13 /**
     14  * A constant value for being offline.
     15  *
     16  * @type String
     17  */
     18 M.OFFLINE = 'offline';
     19
     20 /**
     21  * A constant value for being online.
     22  *
     23  * @type String
     24  */
     25 M.ONLINE = 'online';
     26
     27
     28 /**
     29  * @class
     30  *
     31  * M.Environment encapsulates methods to retrieve information about the
     32  * environment, like browser used, platform, user agent (based on navigator
     33  * object) or whether or not the device is online (determined via an ajax
     34  * request).
     35  *
     36  * @extends M.Object
     37  */
     38 M.Environment = M.Object.extend(
     39 /** @scope M.Environment.prototype */ {
     40
     41     /**
     42      * Checks the connection status by sending an ajax request
     43      * and waiting for the response to decide whether online or offline.
     44      *
     45      * The callback is called when the request returns successful or times out. The parameter to callback is a
     46      * string saying either offline or online.
     47      *
     48      * @param {function} callback The function to be called when request returns.
     49      * @param {String} url Optional. The request url. When not given, a request is made to google.com. (Note: Add a proxy: /google)
     50      * @param {Number} timeout Optional. Time in milliseconds until request is considered to be timed out. Defaults to 5 seconds.
     51      */
     52     getConnectionStatus: function(callback, url, timeout){
     53         M.Request.init({
     54             url: url ? url : '/google',
     55             isJSON: NO,
     56             timeout: timeout ? timeout : 5000,
     57             onSuccess: function(data){
     58                 callback(M.ONLINE);
     59             },
     60             onError: function(data){
     61                 callback(M.OFFLINE);
     62             }
     63         }).send();
     64     },
     65
     66     /**
     67      * Returns the userAgent as received from navigator object.
     68      * E.g. "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7"
     69      *
     70      * @returns {String} The user agent.
     71      */
     72     getUserAgent: function() {
     73         return navigator.userAgent;
     74     },
     75
     76     /**
     77      * Returns the platform as received from navigator object.
     78      * E.g. "MacIntel"
     79      *
     80      * @returns {String} The user's platform.
     81      */
     82     getPlatform: function() {
     83         return navigator.platform;
     84     },
     85
     86     /**
     87      * Returns the browser version as received from navigator object.
     88      * E.g. "5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7"
     89      *
     90      * @returns {String} The user's browser.
     91      */
     92     getBrowserName: function() {
     93         return navigator.appName;
     94     },
     95
     96     /**
     97      * Returns the currently available width and height of the browser window
     98      * as an array:
     99      *
    100      * 0 -> width
    101      * 1 -> height
    102      *
    103      * @returns {Array} The widht and height of the user's browser window.
    104      */
    105     getSize: function() {
    106         var viewportWidth;
    107         var viewportHeight;
    108
    109         if(typeof window.innerWidth != 'undefined') {
    110             viewportWidth = window.innerWidth,
    111             viewportHeight = window.innerHeight
    112         } else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
    113             viewportWidth = document.documentElement.clientWidth,
    114             viewportHeight = document.documentElement.clientHeight
    115         } else {
    116             viewportWidth = document.getElementsByTagName('body')[0].clientWidth,
    117             viewportHeight = document.getElementsByTagName('body')[0].clientHeight
    118         }
    119
    120         return [viewportWidth, viewportHeight];
    121     },
    122
    123     /**
    124      * Returns the currently available width of the browser window.
    125      *
    126      * @returns {Number} The width of the user's browser window.
    127      */
    128     getWidth: function() {
    129         return this.getSize()[0];
    130     },
    131
    132     /**
    133      * Returns the currently available height of the browser window.
    134      *
    135      * @returns {Number} The height of the user's browser window.
    136      */
    137     getHeight: function() {
    138         return this.getSize()[1];
    139     }
    140
    141 });