Skip to content

Commit

Permalink
implemented device type determination using smallest-width qualifier. R…
Browse files Browse the repository at this point in the history
…esolves #22
  • Loading branch information
denich committed Mar 9, 2015
1 parent ca137f8 commit 8dd00df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
29 changes: 15 additions & 14 deletions generate/mobile-detect.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ define(function () {
return 'C';
}

function getDeviceSmallerSide() {
return window.screen.width < window.screen.height ?
window.screen.width :
window.screen.height;
}

/**
* Constructor for MobileDetect object.
* <br>
Expand All @@ -330,19 +336,21 @@ define(function () {
* </pre>
*
* @param {string} userAgent typically taken from window.navigator.userAgent or http_header['User-Agent']
* @param {number} [maxPhoneWidth=650] <strong>only for browsers</strong> specify a value for the maximum
* width (in logical "CSS" pixels) until a device detected as mobile will be handled as phone.
* @param {number} [maxPhoneWidth=600] <strong>only for browsers</strong> specify a value for the maximum
* width of smallest device side (in logical "CSS" pixels) until a device detected as mobile will be handled
* as phone.
* This is only used in cases where the device cannot be classified as phone or tablet.<br>
* See <a href="http://www.html5rocks.com/en/mobile/cross-device/">A non-responsive approach to
* building cross-device webapps</a>.<br>
* See <a href="http://developer.android.com/guide/practices/screens_support.html">Declaring Tablet Layouts
* for Android</a>.<br>
* If you provide a value < 0, then this "fuzzy" check is disabled.
* @constructor
* @global
*/
function MobileDetect(userAgent, maxPhoneWidth) {
this.ua = userAgent || '';
this._cache = {};
this.maxPhoneWidth = maxPhoneWidth || 650;
//600dp is typical 7" tablet minimum width
this.maxPhoneWidth = maxPhoneWidth || 600;
}

MobileDetect.prototype = {
Expand Down Expand Up @@ -549,16 +557,9 @@ define(function () {
};

// environment-dependent
if (typeof window !== 'undefined' && window.screen && window.screen.width) {
if (typeof window !== 'undefined' && window.screen) {
MobileDetect.isPhoneSized = function (maxPhoneWidth) {
if (maxPhoneWidth < 0) {
return undefined;
}
var physicalPixelWidth = window.screen.width,
pixelRatio = window.devicePixelRatio || 1,
cssPixelWidth = physicalPixelWidth / pixelRatio;

return cssPixelWidth <= maxPhoneWidth;
return maxPhoneWidth < 0 ? undefined : getDeviceSmallerSide() <= maxPhoneWidth;
};
} else {
MobileDetect.isPhoneSized = function () {};
Expand Down
29 changes: 15 additions & 14 deletions mobile-detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,12 @@ define(function () {
return 'C';
}

function getDeviceSmallerSide() {
return window.screen.width < window.screen.height ?
window.screen.width :
window.screen.height;
}

/**
* Constructor for MobileDetect object.
* <br>
Expand All @@ -587,19 +593,21 @@ define(function () {
* </pre>
*
* @param {string} userAgent typically taken from window.navigator.userAgent or http_header['User-Agent']
* @param {number} [maxPhoneWidth=650] <strong>only for browsers</strong> specify a value for the maximum
* width (in logical "CSS" pixels) until a device detected as mobile will be handled as phone.
* @param {number} [maxPhoneWidth=600] <strong>only for browsers</strong> specify a value for the maximum
* width of smallest device side (in logical "CSS" pixels) until a device detected as mobile will be handled
* as phone.
* This is only used in cases where the device cannot be classified as phone or tablet.<br>
* See <a href="http://www.html5rocks.com/en/mobile/cross-device/">A non-responsive approach to
* building cross-device webapps</a>.<br>
* See <a href="http://developer.android.com/guide/practices/screens_support.html">Declaring Tablet Layouts
* for Android</a>.<br>
* If you provide a value < 0, then this "fuzzy" check is disabled.
* @constructor
* @global
*/
function MobileDetect(userAgent, maxPhoneWidth) {
this.ua = userAgent || '';
this._cache = {};
this.maxPhoneWidth = maxPhoneWidth || 650;
//600dp is typical 7" tablet minimum width
this.maxPhoneWidth = maxPhoneWidth || 600;
}

MobileDetect.prototype = {
Expand Down Expand Up @@ -840,16 +848,9 @@ define(function () {
};

// environment-dependent
if (typeof window !== 'undefined' && window.screen && window.screen.width) {
if (typeof window !== 'undefined' && window.screen) {
MobileDetect.isPhoneSized = function (maxPhoneWidth) {
if (maxPhoneWidth < 0) {
return undefined;
}
var physicalPixelWidth = window.screen.width,
pixelRatio = window.devicePixelRatio || 1,
cssPixelWidth = physicalPixelWidth / pixelRatio;

return cssPixelWidth <= maxPhoneWidth;
return maxPhoneWidth < 0 ? undefined : getDeviceSmallerSide() <= maxPhoneWidth;
};
} else {
MobileDetect.isPhoneSized = function () {};
Expand Down

0 comments on commit 8dd00df

Please sign in to comment.