Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 86 additions & 84 deletions core/utils/useragent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,104 +12,106 @@
*/
'use strict';

/**
* @name Blockly.utils.userAgent
* @namespace
*/
goog.provide('Blockly.utils.userAgent');
goog.module('Blockly.utils.userAgent');
goog.module.declareLegacyNamespace();

goog.require('Blockly.utils.global');
const globalUtils = goog.require('Blockly.utils.global');


/** @const {boolean} */
Blockly.utils.userAgent.IE;
/** @type {boolean} */
let IE;
exports.IE = IE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this approach work? It seems like these are all going to be exported as undefined since their values aren't set until later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah: that is not going to work. For a bunch of exported booleans that are only referenced (in this module) for the purposes of setting other exported booleans, I'd suggest just setting them all directly on exports and not bothering with declaring local variables for them at all.


/** @const {boolean} */
Blockly.utils.userAgent.EDGE;
/** @type {boolean} */
let EDGE;
exports.EDGE = EDGE;

/** @const {boolean} */
Blockly.utils.userAgent.JAVA_FX;
/** @type {boolean} */
let JAVA_FX;
exports.JAVA_FX = JAVA_FX;

/** @const {boolean} */
Blockly.utils.userAgent.CHROME;
/** @type {boolean} */
let CHROME;
exports.CHROME = CHROME;

/** @const {boolean} */
Blockly.utils.userAgent.WEBKIT;
/** @type {boolean} */
let WEBKIT;
exports.WEBKIT = WEBKIT;

/** @const {boolean} */
Blockly.utils.userAgent.GECKO;
/** @type {boolean} */
let GECKO;
exports.GECKO = GECKO;

/** @const {boolean} */
Blockly.utils.userAgent.ANDROID;
/** @type {boolean} */
let ANDROID;
exports.ANDROID = ANDROID;

/** @const {boolean} */
Blockly.utils.userAgent.IPAD;
/** @type {boolean} */
let IPAD;
exports.IPAD = IPAD;

/** @const {boolean} */
Blockly.utils.userAgent.IPOD;
/** @type {boolean} */
let IPOD;
exports.IPOD = IPOD;

/** @const {boolean} */
Blockly.utils.userAgent.IPHONE;
/** @type {boolean} */
let IPHONE;
exports.IPHONE = IPHONE;

/** @const {boolean} */
Blockly.utils.userAgent.MAC;
/** @type {boolean} */
let MAC;
exports.MAC = MAC;

/** @const {boolean} */
Blockly.utils.userAgent.TABLET;
/** @type {boolean} */
let TABLET;
exports.TABLET = TABLET;

/** @const {boolean} */
Blockly.utils.userAgent.MOBILE;
/** @type {boolean} */
let MOBILE;
exports.MOBILE = MOBILE;

(function(raw) {
Blockly.utils.userAgent.raw = raw;
var rawUpper = Blockly.utils.userAgent.raw.toUpperCase();
/**
* Case-insensitive test of whether name is in the useragent string.
* @param {string} name Name to test.
* @return {boolean} True if name is present.
*/
function has(name) {
return rawUpper.indexOf(name.toUpperCase()) != -1;
}

// Browsers. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/browser.js
Blockly.utils.userAgent.IE = has('Trident') || has('MSIE');
Blockly.utils.userAgent.EDGE = has('Edge');
// Useragent for JavaFX:
// Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.44
// (KHTML, like Gecko) JavaFX/8.0 Safari/537.44
Blockly.utils.userAgent.JAVA_FX = has('JavaFX');
Blockly.utils.userAgent.CHROME = (has('Chrome') || has('CriOS')) &&
!Blockly.utils.userAgent.EDGE;

// Engines. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/engine.js
Blockly.utils.userAgent.WEBKIT = has('WebKit') &&
!Blockly.utils.userAgent.EDGE;
Blockly.utils.userAgent.GECKO = has('Gecko') &&
!Blockly.utils.userAgent.WEBKIT &&
!Blockly.utils.userAgent.IE &&
!Blockly.utils.userAgent.EDGE;

// Platforms. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/platform.js and
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/extra.js
Blockly.utils.userAgent.ANDROID = has('Android');
var maxTouchPoints = Blockly.utils.global['navigator'] &&
Blockly.utils.global['navigator']['maxTouchPoints'];
Blockly.utils.userAgent.IPAD = has('iPad') ||
has('Macintosh') && maxTouchPoints > 0;
Blockly.utils.userAgent.IPOD = has('iPod');
Blockly.utils.userAgent.IPHONE = has('iPhone') &&
!Blockly.utils.userAgent.IPAD && !Blockly.utils.userAgent.IPOD;
Blockly.utils.userAgent.MAC = has('Macintosh');

// Devices. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/device.js
Blockly.utils.userAgent.TABLET = Blockly.utils.userAgent.IPAD ||
(Blockly.utils.userAgent.ANDROID && !has('Mobile')) || has('Silk');
Blockly.utils.userAgent.MOBILE = !Blockly.utils.userAgent.TABLET &&
(Blockly.utils.userAgent.IPOD || Blockly.utils.userAgent.IPHONE ||
Blockly.utils.userAgent.ANDROID || has('IEMobile'));
})((Blockly.utils.global['navigator'] && Blockly.utils.global['navigator']['userAgent']) || '');
raw = raw;
const rawUpper = raw.toUpperCase();

/**
* Case-insensitive test of whether name is in the useragent string.
* @param {string} name Name to test.
* @return {boolean} True if name is present.
*/
function has(name) {
return rawUpper.indexOf(name.toUpperCase()) != -1;
}

// Browsers. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/browser.js
IE = has('Trident') || has('MSIE');
EDGE = has('Edge');
// Useragent for JavaFX:
// Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.44
// (KHTML, like Gecko) JavaFX/8.0 Safari/537.44
JAVA_FX = has('JavaFX');
CHROME = (has('Chrome') || has('CriOS')) && !EDGE;

// Engines. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/engine.js
WEBKIT = has('WebKit') && !EDGE;
GECKO = has('Gecko') && !WEBKIT && !IE && !EDGE;

// Platforms. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/platform.js
// and
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/extra.js
ANDROID = has('Android');
const maxTouchPoints =
globalUtils['navigator'] && globalUtils['navigator']['maxTouchPoints'];
IPAD = has('iPad') || has('Macintosh') && maxTouchPoints > 0;
IPOD = has('iPod');
IPHONE = has('iPhone') && !IPAD && !IPOD;
MAC = has('Macintosh');

// Devices. Logic from:
// https://github.com/google/closure-library/blob/master/closure/goog/labs/useragent/device.js
TABLET = IPAD || (ANDROID && !has('Mobile')) || has('Silk');
MOBILE = !TABLET && (IPOD || IPHONE || ANDROID || has('IEMobile'));
})((globalUtils['navigator'] && globalUtils['navigator']['userAgent']) || '');
2 changes: 1 addition & 1 deletion tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.