Move all browser/platform detecting in utils/Browser.js

This commit is contained in:
Paris Kasidiaris 2016-11-04 18:41:00 +02:00
parent 356377975b
commit d426c99866
3 changed files with 32 additions and 29 deletions

View File

@ -9,9 +9,18 @@
* @module xterm/utils/Browser
*/
import { contains } from './Generic.js';
let userAgent = navigator.userAgent;
let platform = navigator.platform;
export let isFirefox = !!~userAgent.indexOf('Firefox');
export let isMSIE = !!~userAgent.indexOf('MSIE');
// Find the users platform. We use this to interpret the meta key
// and ISO third level shifts.
// http://stackoverflow.com/q/19877924/577598
export let isMac = contains(platform, ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']);
export let isIpad = platform === 'iPad';
export let isIphone = platform === 'iPhone';
export let isMSWindows = contains(platform, ['Windows', 'Win16', 'Win32', 'WinCE']);

19
src/utils/Generic.js Normal file
View File

@ -0,0 +1,19 @@
/**
* xterm.js: xterm, in the browser
* Copyright (c) 2016, SourceLair Private Company <www.sourcelair.com> (MIT License)
*/
/**
* Generic utilities module. This module contains generic methods that can be helpful at
* different parts of the code base.
* @module xterm/utils/Generic
*/
export let contains = function(el, arr) {
for (var i = 0; i < arr.length; i += 1) {
if (el === arr[i]) {
return true;
}
}
return false;
};

View File

@ -35,7 +35,7 @@ import { CompositionHelper } from './CompositionHelper.js';
import { EventEmitter } from './EventEmitter.js';
import { Viewport } from './Viewport.js';
import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard.js';
import { isFirefox, isMSIE } from './utils/Browser';
import { isFirefox, isMSIE, isMac, isIpad, isIphone, isMSWindows } from './utils/Browser';
/**
* Terminal Emulation References:
@ -536,22 +536,6 @@ Terminal.prototype.open = function(parent) {
this.document = this.parent.ownerDocument;
this.body = this.document.getElementsByTagName('body')[0];
// Find the users platform. We use this to interpret the meta key
// and ISO third level shifts.
// http://stackoverflow.com/q/19877924/577598
if (this.context.navigator && this.context.navigator.platform) {
this.isMac = contains(
this.context.navigator.platform,
['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
);
this.isIpad = this.context.navigator.platform === 'iPad';
this.isIphone = this.context.navigator.platform === 'iPhone';
this.isMSWindows = contains(
this.context.navigator.platform,
['Windows', 'Win16', 'Win32', 'WinCE']
);
}
//Create main element container
this.element = this.document.createElement('div');
this.element.classList.add('terminal');
@ -2720,7 +2704,7 @@ Terminal.prototype.evaluateKeyEscapeSequence = function(ev) {
// ^] - group sep
result.key = String.fromCharCode(29);
}
} else if (!this.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) {
} else if (!isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) {
// On Mac this is a third level shift. Use <Esc> instead.
if (ev.keyCode >= 65 && ev.keyCode <= 90) {
result.key = '\x1b' + String.fromCharCode(ev.keyCode + 32);
@ -4875,15 +4859,6 @@ Terminal.charsets.ISOLatin = null; // /A
* Helpers
*/
function contains(el, arr) {
for (var i = 0; i < arr.length; i += 1) {
if (el === arr[i]) {
return true;
}
}
return false;
}
function on(el, type, handler, capture) {
if (!Array.isArray(el)) {
el = [el];
@ -4938,8 +4913,8 @@ function indexOf(obj, el) {
function isThirdLevelShift(term, ev) {
var thirdLevelKey =
(term.isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) ||
(term.isMSWindows && ev.altKey && ev.ctrlKey && !ev.metaKey);
(isMac && ev.altKey && !ev.ctrlKey && !ev.metaKey) ||
(isMSWindows && ev.altKey && ev.ctrlKey && !ev.metaKey);
if (ev.type == 'keypress') {
return thirdLevelKey;