]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/xterm.js
Refactor TS build and fix testing
[mirror_xterm.js.git] / src / xterm.js
index 021cbfdff61f8b34cf0a4543ea05d80d105b1253..ce49d917a76fe7402704db06735ac9822a702e3f 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * xterm.js: xterm, in the browser
- * Copyright (c) 2014, SourceLair Limited <www.sourcelair.com> (MIT License)
+ * Copyright (c) 2014-2014, SourceLair Private Company <www.sourcelair.com> (MIT License)
  * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
  * https://github.com/chjj/term.js
  *
@@ -34,6 +34,7 @@
 import { CompositionHelper } from './CompositionHelper.js';
 import { EventEmitter } from './EventEmitter.js';
 import { Viewport } from './Viewport.js';
+import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard.js';
 
 /**
  * Terminal Emulation References:
@@ -62,7 +63,9 @@ var normal = 0, escaped = 1, csi = 2, osc = 3, charset = 4, dcs = 5, ignore = 6;
  * Creates a new `Terminal` object.
  *
  * @param {object} options An object containing a set of options, the available options are:
- *   - cursorBlink (boolean): Whether the terminal cursor blinks
+ *   - `cursorBlink` (boolean): Whether the terminal cursor blinks
+ *   - `cols` (number): The number of columns of the terminal (horizontal size)
+ *   - `rows` (number): The number of rows of the terminal (vertical size)
  *
  * @public
  * @class Xterm Xterm
@@ -124,6 +127,7 @@ function Terminal(options) {
 
   this.cols = options.cols || options.geometry[0];
   this.rows = options.rows || options.geometry[1];
+  this.geometry = [this.cols, this.rows];
 
   if (options.handler) {
     this.on('data', options.handler);
@@ -431,52 +435,20 @@ Terminal.bindBlur = function (term) {
  * Initialize default behavior
  */
 Terminal.prototype.initGlobal = function() {
-  Terminal.bindPaste(this);
+  var term = this;
+
   Terminal.bindKeys(this);
-  Terminal.bindCopy(this);
   Terminal.bindFocus(this);
   Terminal.bindBlur(this);
-};
 
-/**
- * Bind to paste event and allow both keyboard and right-click pasting, without having the
- * contentEditable value set to true.
- */
-Terminal.bindPaste = function(term) {
-  on([term.textarea, term.element], 'paste', function(ev) {
-    ev.stopPropagation();
-    if (ev.clipboardData) {
-      var text = ev.clipboardData.getData('text/plain');
-      term.handler(text);
-      term.textarea.value = '';
-      return term.cancel(ev);
-    }
+  // Bind clipboard functionality
+  on(this.element, 'copy', copyHandler);
+  on(this.textarea, 'paste', function (ev) {
+    pasteHandler.call(this, ev, term);
+  });
+  on(this.element, 'contextmenu', function (ev) {
+    rightClickHandler.call(this, ev, term);
   });
-};
-
-/**
- * Prepares text copied from terminal selection, to be saved in the clipboard by:
- *   1. stripping all trailing white spaces
- *   2. converting all non-breaking spaces to regular spaces
- * @param {string} text The copied text that needs processing for storing in clipboard
- * @returns {string}
- * @static
- */
-Terminal.prepareCopiedTextForClipboard = function (text) {
-  var space = String.fromCharCode(32),
-      nonBreakingSpace = String.fromCharCode(160),
-      allNonBreakingSpaces = new RegExp(nonBreakingSpace, 'g'),
-      processedText = text.split('\n').map(function (line) {
-        /**
-         * Strip all trailing white spaces and convert all non-breaking spaces to regular
-         * spaces.
-         */
-        var processedLine = line.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space);
-
-        return processedLine;
-      }).join('\n');
-
-  return processedText;
 };
 
 /**
@@ -515,16 +487,6 @@ Terminal.bindKeys = function(term) {
   term.on('refresh', term.compositionHelper.updateCompositionElements.bind(term.compositionHelper));
 };
 
-/**
- * Binds copy functionality to the given terminal.
- * @static
- */
-Terminal.bindCopy = function(term) {
-  on(term.element, 'copy', function(ev) {
-    return; // temporary
-  });
-};
-
 
 /**
  * Insert the given row to the terminal or produce a new one
@@ -683,10 +645,10 @@ Terminal.prototype.open = function(parent) {
 Terminal.loadAddon = function(addon, callback) {
   if (typeof exports === 'object' && typeof module === 'object') {
     // CommonJS
-    return require(__dirname + '/../addons/' + addon);
+    return require('./addons/' + addon + '/' + addon);
   } else if (typeof define == 'function') {
     // RequireJS
-    return require(['../addons/' + addon + '/' + addon], callback);
+    return require(['./addons/' + addon + '/' + addon], callback);
   } else {
     console.error('Cannot load a module without a CommonJS or RequireJS environment.');
     return false;
@@ -1017,7 +979,6 @@ Terminal.prototype.bindMouse = function() {
   // the shell for example
   on(el, 'wheel', function(ev) {
     if (self.mouseEvents) return;
-    if (self.applicationKeypad) return;
     self.viewport.onWheel(ev);
     return self.cancel(ev);
   });
@@ -1349,6 +1310,28 @@ Terminal.prototype.scrollDisp = function(disp, suppressScrollEvent) {
   this.refresh(0, this.rows - 1);
 };
 
+/**
+ * Scroll the display of the terminal by a number of pages.
+ * @param {number} pageCount The number of pages to scroll (negative scrolls up).
+ */
+Terminal.prototype.scrollPages = function(pageCount) {
+  this.scrollDisp(pageCount * (this.rows - 1));
+}
+
+/**
+ * Scrolls the display of the terminal to the top.
+ */
+Terminal.prototype.scrollToTop = function() {
+  this.scrollDisp(-this.ydisp);
+}
+
+/**
+ * Scrolls the display of the terminal to the bottom.
+ */
+Terminal.prototype.scrollToBottom = function() {
+  this.scrollDisp(this.ybase - this.ydisp);
+}
+
 /**
  * Writes text to the terminal.
  * @param {string} text The text to write to the terminal.
@@ -1692,7 +1675,7 @@ Terminal.prototype.write = function(data) {
           case '=':
             this.log('Serial port requested application keypad.');
             this.applicationKeypad = true;
-            this.viewport.setApplicationMode(true);
+            this.viewport.syncScrollArea();
             this.state = normal;
             break;
 
@@ -1700,7 +1683,7 @@ Terminal.prototype.write = function(data) {
           case '>':
             this.log('Switching back to normal keypad.');
             this.applicationKeypad = false;
-            this.viewport.setApplicationMode(false);
+            this.viewport.syncScrollArea();
             this.state = normal;
             break;
 
@@ -2962,6 +2945,7 @@ Terminal.prototype.resize = function(x, y) {
 
   this.normal = null;
 
+  this.geometry = [this.cols, this.rows];
   this.emit('resize', {terminal: this, cols: x, rows: y});
 };
 
@@ -3141,9 +3125,9 @@ Terminal.prototype.is = function(term) {
 
 
 /**
    * Emit the 'data' event and populate the given data.
    * @param {string} data The data to populate in the event.
    */
+ * Emit the 'data' event and populate the given data.
+ * @param {string} data The data to populate in the event.
+ */
 Terminal.prototype.handler = function(data) {
   this.emit('data', data);
 };
@@ -4065,7 +4049,7 @@ Terminal.prototype.setMode = function(params) {
       case 66:
         this.log('Serial port requested application keypad.');
         this.applicationKeypad = true;
-        this.viewport.setApplicationMode(true);
+        this.viewport.syncScrollArea();
         break;
       case 9: // X10 Mouse
         // no release, no motion, no wheel, no modifiers.
@@ -4265,7 +4249,7 @@ Terminal.prototype.resetMode = function(params) {
       case 66:
         this.log('Switching back to normal keypad.');
         this.applicationKeypad = false;
-        this.viewport.setApplicationMode(false);
+        this.viewport.syncScrollArea();
         break;
       case 9: // X10 Mouse
       case 1000: // vt200 mouse
@@ -4386,8 +4370,8 @@ Terminal.prototype.scrollUp = function(params) {
 
 
 /**
    * CSI Ps T  Scroll down Ps lines (default = 1) (SD).
    */
+ * CSI Ps T  Scroll down Ps lines (default = 1) (SD).
+ */
 Terminal.prototype.scrollDown = function(params) {
   var param = params[0] || 1;
   while (param--) {
@@ -4552,7 +4536,7 @@ Terminal.prototype.softReset = function(params) {
   this.originMode = false;
   this.wraparoundMode = false; // autowrap
   this.applicationKeypad = false; // ?
-  this.viewport.setApplicationMode(false);
+  this.viewport.syncScrollArea();
   this.applicationCursor = false;
   this.scrollTop = 0;
   this.scrollBottom = this.rows - 1;