]>
git.proxmox.com Git - mirror_xterm.js.git/blob - addons/fit/fit.js
2 * Fit terminal columns and rows to the dimensions of its
6 * - Rows: Truncate the division of the terminal parent element height
7 * by the terminal row height
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
15 if (typeof exports
=== 'object' && typeof module
=== 'object') {
17 * CommonJS environment
19 module
.exports
= fit(require('../../build/xterm'));
20 } else if (typeof define
== 'function') {
22 * Require.js is available
24 define(['../../build/xterm'], fit
);
27 * Plain browser environment
33 * This module provides methods for fitting a terminal's size to a parent container.
35 * @module xterm/addons/fit/fit
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
,
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
;
64 rows
= parseInt(availableHeight
/ characterHeight
);
65 cols
= parseInt(availableWidth
/ characterWidth
) - 1;
67 geometry
= {cols
: cols
, rows
: rows
};
71 exports
.fit = function (term
) {
72 var geometry
= exports
.proposeGeometry(term
);
74 term
.resize(geometry
.cols
, geometry
.rows
);
77 Xterm
.prototype.proposeGeometry = function () {
78 return exports
.proposeGeometry(this);
81 Xterm
.prototype.fit = function () {
82 return exports
.fit(this);