Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/54987


Change subject: Do basic device detection in javascript
......................................................................

Do basic device detection in javascript

* Add a class to the body tag
* Make global function available to other scripts

Change-Id: If2c3be87b638453ce7b6bb921fe8cc32b5f72102
---
M MobileFrontend.php
M includes/MobileFrontend.hooks.php
M includes/skins/SkinMobile.php
A javascripts/startup/device-detection.js
A tests/js/startup/device-detection.js
5 files changed, 81 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/87/54987/1

diff --git a/MobileFrontend.php b/MobileFrontend.php
index e84f72f..5a106aa 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -150,6 +150,14 @@
        'targets' => 'mobile',
 );
 
+$wgResourceModules['mobile.device.detect'] = $wgMFMobileResourceBoilerplate + 
array(
+       'position' => 'top',
+       'scripts' => array(
+               'javascripts/startup/device-detection.js',
+       ),
+       'mobileTargets' => array(),
+);
+
 // Filepages
 $wgResourceModules['mobile.file.styles'] = $wgMFMobileResourceBoilerplate + 
array(
        'dependencies' => array( 'mobile.startup' ),
diff --git a/includes/MobileFrontend.hooks.php 
b/includes/MobileFrontend.hooks.php
index 8eb85ee..6515ad7 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -173,6 +173,7 @@
                        'scripts' => array(
                                'tests/externals/sinon.js',
                                'javascripts/externals/hogan.js',
+                               'javascripts/startup/device-detection.js', 
'tests/js/startup/device-detection.js',
                                'javascripts/common/modules.js',
                                'javascripts/common/eventemitter.js', 
'tests/js/test_eventemitter.js',
                                'tests/js/fixtures.js', 
'javascripts/common/mf-application.js',
diff --git a/includes/skins/SkinMobile.php b/includes/skins/SkinMobile.php
index f516c68..8504ba3 100644
--- a/includes/skins/SkinMobile.php
+++ b/includes/skins/SkinMobile.php
@@ -251,7 +251,9 @@
                // attach modules
                if ( $rlSupport ) {
                        // Initialize ResourceLoader, targeted to mobile...
-                       $headLinks[] = $this->resourceLoaderLink( 'startup', 
'scripts', true, true, 'mobile' );
+                       // FIXME: Move mobile.device.detect into startup module 
itself
+                       $headLinks[] = $this->resourceLoaderLink( array( 
'startup', 'mobile.device.detect' ),
+                               'scripts', true, true, 'mobile' );
                        $headLinks[] = Html::inlineScript(
                                ResourceLoader::makeLoaderConditionalScript(
                                        ResourceLoader::makeConfigSetScript( 
$out->getJSVars() )
diff --git a/javascripts/startup/device-detection.js 
b/javascripts/startup/device-detection.js
new file mode 100644
index 0000000..d74581a
--- /dev/null
+++ b/javascripts/startup/device-detection.js
@@ -0,0 +1,37 @@
+/**
+ * Used to detect device in javascript
+ *
+ * @param {String} ua: User agent (optional) defaults to current user agent
+ * Currently only detects iOS and android devices.
+ *
+ * TODO: Expand as required to provide richer information.
+ * @return {String} Device OS
+ */
+// FIXME: Make part of startup module and attach to mw object
+window.getMobileDevice = function( ua ) {
+       var os;
+       ua = ua || window.navigator.userAgent;
+       ua = ua.toLowerCase();
+
+       if ( ua.match( /opera\/[0-9]+\.[0-9]+ \(/ ) ) {
+               if ( ua.match( /\(android [0-9]+\.[0-9]+;/ ) || ua.match( 
/android;/ ) ) {
+                       os = 'android';
+               } else if ( ua.match( /\(ipad;/ ) || ua.match( /\(iphone;/ ) ) {
+                       os = 'iOS';
+               } else {
+                       os = 'other';
+               }
+       } else if ( ua.match( /android/ ) ) {
+               os = 'android';
+       } else if ( ua.match( /iphone/ ) || ua.match( /ipad/ ) ) {
+               os = 'iOS';
+       } else {
+               os = 'other';
+       }
+       return os;
+};
+
+( function() {
+var device = window.getMobileDevice( window.navigator.userAgent );
+document.documentElement.className += ' device-os-' + device.os;
+} )();
diff --git a/tests/js/startup/device-detection.js 
b/tests/js/startup/device-detection.js
new file mode 100644
index 0000000..ab2aff8
--- /dev/null
+++ b/tests/js/startup/device-detection.js
@@ -0,0 +1,32 @@
+( function ( d, $ ) {
+
+module( 'MobileFrontend: DeviceDetection' );
+
+test( 'detect devices', function() {
+       var tests = [
+               // android 2.1
+               [ 'android', 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus 
One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile 
Safari/530.17' ],
+               // ipod
+               [ 'iOS', 'Mozilla/5.0 (ipod: U;CPU iPhone OS 2_2 like Mac OS X: 
es_es) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b 
Safari/419.3' ],
+               // iPhone MobileSafari
+               [ 'iOS', 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) 
AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3' ],
+               // iPad MobileSafari
+               [ 'iOS', 'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) 
AppleWebKit/534.46 (KHTML, like Gecko ) Version/5.1 Mobile/9B176 
Safari/7534.48.3' ],
+               // Opera Mobile on Android
+               [ 'android', 'Opera/12.02 (Android 4.1; Linux; Opera 
Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02' ],
+               // Opera Mini on iPad
+               [ 'iOS', 'Opera/9.80 (iPad; Opera Mini/7.1.32694/27.1407; U; 
en) Presto/2.8.119 Version/11.10' ],
+               // Opera Mini on Android
+               [ 'android', 'Opera/9.80 (Android; Opera Mini/7.29530/27.1407; 
U; en) Presto/2.8.119 Version/11.10' ],
+               // Opera Mini on J2ME
+               [ 'other', 'Opera/9.80 (J2ME/MIDP; Opera Mini/9 (Compatible; 
MSIE:9.0; iPhone; BlackBerry9700; AppleWebKit/24.746; U; en) Presto/2.5.25 
Version/10.54' ],
+               [ 'other', 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.3+ 
(KHTML, like Gecko) Version/10.0.9.386 Mobile Safari/537.3+' ]
+       ];
+
+       $( tests ).each( function( i ) {
+               var device = d( this[ 1 ] );
+               strictEqual( device, this[ 0 ], 'device test ' + i );
+       } );
+} );
+
+}( window.getMobileDevice, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/54987
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If2c3be87b638453ce7b6bb921fe8cc32b5f72102
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to