[270] | 1 | /*
|
---|
| 2 | * Fit terminal columns and rows to the dimensions of its
|
---|
| 3 | * DOM element.
|
---|
| 4 | *
|
---|
| 5 | * Approach:
|
---|
| 6 | * - Rows: Truncate the division of the terminal parent element height
|
---|
| 7 | * by the terminal row height
|
---|
| 8 | *
|
---|
| 9 | * - Columns: Truncate the division of the terminal parent element width by
|
---|
| 10 | * the terminal character width (apply display: inline at the
|
---|
| 11 | * terminal row and truncate its width with the current number
|
---|
| 12 | * of columns)
|
---|
| 13 | */
|
---|
| 14 | (function (fit) {
|
---|
| 15 | if (typeof exports === 'object' && typeof module === 'object') {
|
---|
| 16 | /*
|
---|
| 17 | * CommonJS environment
|
---|
| 18 | */
|
---|
| 19 | module.exports = fit(require('../../src/xterm'));
|
---|
| 20 | } else if (typeof define == 'function') {
|
---|
| 21 | /*
|
---|
| 22 | * Require.js is available
|
---|
| 23 | */
|
---|
| 24 | define(['../../src/xterm'], fit);
|
---|
| 25 | } else {
|
---|
| 26 | /*
|
---|
| 27 | * Plain browser environment
|
---|
| 28 | */
|
---|
| 29 | fit(this.Xterm);
|
---|
| 30 | }
|
---|
| 31 | })(function (Xterm) {
|
---|
| 32 | /**
|
---|
| 33 | * This module provides methods for fitting a terminal's size to a parent container.
|
---|
| 34 | *
|
---|
| 35 | * @module xterm/addons/fit/fit
|
---|
| 36 | */
|
---|
| 37 | var exports = {};
|
---|
| 38 |
|
---|
| 39 | exports.proposeGeometry = function (term) {
|
---|
| 40 | var parentElementStyle = window.getComputedStyle(term.element.parentElement),
|
---|
| 41 | parentElementHeight = parseInt(parentElementStyle.getPropertyValue('height')),
|
---|
| 42 | parentElementWidth = parseInt(parentElementStyle.getPropertyValue('width')),
|
---|
| 43 | elementStyle = window.getComputedStyle(term.element),
|
---|
| 44 | elementPaddingVer = parseInt(elementStyle.getPropertyValue('padding-top')) + parseInt(elementStyle.getPropertyValue('padding-bottom')),
|
---|
| 45 | elementPaddingHor = parseInt(elementStyle.getPropertyValue('padding-right')) + parseInt(elementStyle.getPropertyValue('padding-left')),
|
---|
| 46 | availableHeight = parentElementHeight - elementPaddingVer,
|
---|
| 47 | availableWidth = parentElementWidth - elementPaddingHor,
|
---|
| 48 | container = term.rowContainer,
|
---|
| 49 | subjectRow = term.rowContainer.firstElementChild,
|
---|
| 50 | contentBuffer = subjectRow.innerHTML,
|
---|
| 51 | characterHeight,
|
---|
| 52 | rows,
|
---|
| 53 | characterWidth,
|
---|
| 54 | cols,
|
---|
| 55 | geometry;
|
---|
| 56 |
|
---|
| 57 | subjectRow.style.display = 'inline';
|
---|
| 58 | subjectRow.innerHTML = 'W'; // Common character for measuring width, although on monospace
|
---|
| 59 | characterWidth = subjectRow.getBoundingClientRect().width;
|
---|
| 60 | subjectRow.style.display = ''; // Revert style before calculating height, since they differ.
|
---|
| 61 | characterHeight = parseInt(subjectRow.offsetHeight);
|
---|
| 62 | subjectRow.innerHTML = contentBuffer;
|
---|
| 63 |
|
---|
| 64 | rows = parseInt(availableHeight / characterHeight);
|
---|
| 65 | cols = parseInt(availableWidth / characterWidth) - 1;
|
---|
| 66 |
|
---|
| 67 | geometry = {cols: cols, rows: rows};
|
---|
| 68 | return geometry;
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | exports.fit = function (term) {
|
---|
| 72 | var geometry = exports.proposeGeometry(term);
|
---|
| 73 |
|
---|
| 74 | term.resize(geometry.cols, geometry.rows);
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | Xterm.prototype.proposeGeometry = function () {
|
---|
| 78 | return exports.proposeGeometry(this);
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | Xterm.prototype.fit = function () {
|
---|
| 82 | return exports.fit(this);
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | return exports;
|
---|
| 86 | });
|
---|