consolidate user agent sniffing from util.js and

core.js into a new module useragent.js
This commit is contained in:
Fabian Jakobs 2010-12-16 18:50:26 +01:00
commit ad19a8f98e
6 changed files with 54 additions and 95 deletions

View file

@ -37,7 +37,7 @@
define(function(require, exports, module) {
var core = require("pilot/core");
var useragent = require("pilot/useragent");
var event = require("pilot/event");
var default_mac = require("ace/conf/keybindings/default_mac").bindings;
var default_win = require("ace/conf/keybindings/default_win").bindings;
@ -50,7 +50,7 @@ var KeyBinding = function(element, editor, config) {
var _self = this;
event.addKeyListener(element, function(e) {
// opera on mac swaps ctrl and meta keys
if (core.isOpera && core.isMac)
if (useragent.isOpera && useragent.isMac)
var hashId = 0 | (e.metaKey ? 1 : 0) | (e.altKey ? 2 : 0)
| (e.shiftKey ? 4 : 0) | (e.ctrlKey ? 8 : 0);
else
@ -145,7 +145,7 @@ var KeyBinding = function(element, editor, config) {
}
this.setConfig = function(config) {
this.config = config || (core.isMac
this.config = config || (useragent.isMac
? default_mac
: default_win);
if (typeof this.config.reverse == "undefined")

View file

@ -37,7 +37,7 @@
define(function(require, exports, module) {
var core = require("pilot/core");
var useragent = require("pilot/useragent");
exports.addListener = function(elem, type, callback) {
if (elem.addEventListener) {
@ -215,7 +215,7 @@ exports.addMultiMouseDownListener = function(el, button, count, timeout, callbac
};
exports.addListener(el, "mousedown", listener);
core.isIE && exports.addListener(el, "dblclick", listener);
useragent.isIE && exports.addListener(el, "dblclick", listener);
};
exports.addKeyListener = function(el, callback) {
@ -227,7 +227,7 @@ exports.addKeyListener = function(el, callback) {
});
// repeated keys are fired as keypress and not keydown events
if (core.isMac && (core.isGecko || core.isOpera)) {
if (useragent.isMac && (useragent.isGecko || useragent.isOpera)) {
exports.addListener(el, "keypress", function(e) {
var keyId = e.keyIdentifier || e.keyCode;
if (lastDown !== keyId) {

View file

@ -34,8 +34,7 @@ For more information about SproutCore, visit http://www.sproutcore.com
define(function(require, exports, module) {
var dom = require('pilot/dom');
var util = require('pilot/util');
var useragent = require('pilot/useragent');
/**
* Helper functions and hashes for key handling.
@ -222,7 +221,7 @@ exports.addKeyDownListener = function(element, boundFunction) {
};
element.addEventListener('keydown', function(ev) {
if (util.isMozilla) {
if (useragent.isGecko) {
// Check for function keys (like DELETE, TAB, LEFT, RIGHT...)
if (exports.KeyHelper.FUNCTION_KEYS[ev.keyCode]) {
return true;
@ -241,7 +240,7 @@ exports.addKeyDownListener = function(element, boundFunction) {
}, false);
element.addEventListener('keypress', function(ev) {
if (util.isMozilla) {
if (useragent.isGecko) {
// If this is a function key, we have to use the keyCode.
if (exports.KeyHelper.FUNCTION_KEYS[ev.keyCode]) {
return handleBoundFunction(ev);

View file

@ -1,6 +1,6 @@
define(function(require, exports, module) {
var util = require("pilot/util");
var ua = require("pilot/useragent");
var console = require('pilot/console');
// Changed to suit the specific needs of running within Skywriter
@ -65,11 +65,11 @@ var mode = (function() {
// functionality provided by Firebug. Firebug tries to do the right
// thing here and break, but it happens every time you load the page.
// bug 554105
if (util.isMozilla) {
if (ua.isGecko) {
return 'firefox';
} else if (util.isOpera) {
} else if (ua.isOpera) {
return 'opera';
} else if (util.isSafari) {
} else {
return 'other';
}

View file

@ -38,23 +38,54 @@
define(function(require, exports, module) {
var os = (navigator.platform.match(/mac|win|linux/i) || ["other"])[0].toLowerCase();
var ua = navigator.userAgent;
var av = navigator.appVersion;
/** Is the user using a browser that identifies itself as Windows */
exports.isWin = (os == "win");
/** Is the user using a browser that identifies itself as Mac OS */
exports.isMac = (os == "mac");
/** Is the user using a browser that identifies itself as Linux */
exports.isLinux = (os == "linux");
exports.isIE = ! + "\v1";
exports.isGecko = window.controllers && window.navigator.product === "Gecko";
/** Is this Firefox or related? */
exports.isGecko = exports.isMozilla = window.controllers && window.navigator.product === "Gecko";
/** Is this Opera */
exports.isOpera = window.opera && Object.prototype.toString.call(window.opera) == "[object Opera]";
exports.provide = function(namespace) {
var parts = namespace.split(".");
var obj = window;
for (var i=0; i<parts.length; i++) {
var part = parts[i];
if (!obj[part]) {
obj[part] = {};
}
obj = obj[part];
/** Is the user using a browser that identifies itself as WebKit */
exports.isWebKit = parseFloat(ua.split("WebKit/")[1]) || undefined;
exports.isAIR = ua.indexOf("AdobeAIR") >= 0;
/**
* I hate doing this, but we need some way to determine if the user is on a Mac
* The reason is that users have different expectations of their key combinations.
*
* Take copy as an example, Mac people expect to use CMD or APPLE + C
* Windows folks expect to use CTRL + C
*/
exports.OS = {
LINUX: 'LINUX',
MAC: 'MAC',
WINDOWS: 'WINDOWS'
};
/**
* Return an exports.OS constant
*/
exports.getOS = function() {
if (exports.isMac) {
return exports.OS['MAC'];
} else if (exports.isLinux) {
return exports.OS['LINUX'];
} else {
return exports.OS['WINDOWS'];
}
};

View file

@ -320,77 +320,6 @@ exports.englishFromCamel = function(camel) {
}).trim();
};
/**
* I hate doing this, but we need some way to determine if the user is on a Mac
* The reason is that users have different expectations of their key combinations.
*
* Take copy as an example, Mac people expect to use CMD or APPLE + C
* Windows folks expect to use CTRL + C
*/
exports.OS = {
LINUX: 'LINUX',
MAC: 'MAC',
WINDOWS: 'WINDOWS'
};
var ua = navigator.userAgent;
var av = navigator.appVersion;
/** Is the user using a browser that identifies itself as Linux */
exports.isLinux = av.indexOf("Linux") >= 0;
/** Is the user using a browser that identifies itself as Windows */
exports.isWindows = av.indexOf("Win") >= 0;
/** Is the user using a browser that identifies itself as WebKit */
exports.isWebKit = parseFloat(ua.split("WebKit/")[1]) || undefined;
/** Is the user using a browser that identifies itself as Chrome */
exports.isChrome = parseFloat(ua.split("Chrome/")[1]) || undefined;
/** Is the user using a browser that identifies itself as Mac OS */
exports.isMac = av.indexOf("Macintosh") >= 0;
/* Is this Firefox or related? */
exports.isMozilla = av.indexOf('Gecko/') >= 0;
if (ua.indexOf("AdobeAIR") >= 0) {
exports.isAIR = 1;
}
/**
* Is the user using a browser that identifies itself as Safari
* See also:
* - http://developer.apple.com/internet/safari/faq.html#anchor2
* - http://developer.apple.com/internet/safari/uamatrix.html
*/
var index = Math.max(av.indexOf("WebKit"), av.indexOf("Safari"), 0);
if (index && !exports.isChrome) {
// try to grab the explicit Safari version first. If we don't get
// one, look for less than 419.3 as the indication that we're on something
// "Safari 2-ish".
exports.isSafari = parseFloat(av.split("Version/")[1]);
if (!exports.isSafari || parseFloat(av.substr(index + 7)) <= 419.3) {
exports.isSafari = 2;
}
}
if (ua.indexOf("Gecko") >= 0 && !exports.isWebKit) {
exports.isMozilla = parseFloat(av);
}
/**
* Return a exports.OS constant
*/
exports.getOS = function() {
if (exports.isMac) {
return exports.OS['MAC'];
} else if (exports.isLinux) {
return exports.OS['LINUX'];
} else {
return exports.OS['WINDOWS'];
}
};
/** Returns true if the DOM element "b" is inside the element "a". */
if (typeof(document) !== 'undefined' && document.compareDocumentPosition) {