From 3b322929c6660c72b8e5c1a3168b22ffe2108882 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 11 Aug 2013 09:23:24 -0500 Subject: [PATCH] add a default style option. --- example/index.html | 5 +++- lib/term.js | 68 ++++++++++++++++++++++++++++++++++++++-------- test/index.html | 17 ++---------- 3 files changed, 63 insertions(+), 27 deletions(-) diff --git a/example/index.html b/example/index.html index 16e14c71..48334ce9 100644 --- a/example/index.html +++ b/example/index.html @@ -10,6 +10,7 @@ font: 20px/1.5 sans-serif; } +/* .terminal { float: left; border: #000 solid 5px; @@ -23,6 +24,7 @@ color: #000; background: #f0f0f0; } +*/

term.js

@@ -34,7 +36,8 @@ socket.on('connect', function() { var term = new Terminal({ cols: 80, - rows: 24 + rows: 24, + style: true }); term.on('data', function(data) { diff --git a/lib/term.js b/lib/term.js index 953287ce..c464c730 100644 --- a/lib/term.js +++ b/lib/term.js @@ -146,7 +146,7 @@ function Terminal(options) { }; } - this._options = options || {}; + this.options = options || {}; // this.context = options.context || window; // this.document = options.document || document; @@ -402,19 +402,31 @@ Terminal.prototype.blur = function() { Terminal.focus = null; }; +/** + * Initialize global behavior + */ + +Terminal.prototype.initGlobal = function() { + var document = this.document; + + Terminal._boundDocs = Terminal._boundDocs || []; + if (~indexOf(Terminal._boundDocs, document)) { + return; + } + Terminal._boundDocs.push(document); + + Terminal.bindKeys(document); + + if (this.options.style) { + Terminal.insertStyle(document); + } +}; + /** * Global Events for key handling */ Terminal.bindKeys = function(document) { - Terminal._boundDocs = Terminal._boundDocs || []; - - if (~indexOf(Terminal._boundDocs, document)) { - return; - } - - Terminal._boundDocs.push(document); - // We could put an "if (Terminal.focus)" check // here, but it shouldn't be necessary. on(document, 'keydown', function(ev) { @@ -461,6 +473,39 @@ Terminal.bindKeys = function(document) { }); }; +/** + * Insert a default style + */ + +Terminal.insertStyle = function(document) { + var style = document.getElementById('termjs-style'); + if (style) return; + + var head = document.getElementsByTagName('head')[0]; + if (!head) return; + + var style = document.createElement('style'); + style.id = 'termjs-style'; + + // textContent doesn't work well with IE for

term.js test

@@ -33,7 +19,8 @@ var term = new Terminal({ cols: 80, - rows: 30 + rows: 30, + style: true }); window.onload = function() {