]>
git.proxmox.com Git - mirror_xterm.js.git/blob - dist/addons/fit/fit.js
46b79e9b3be5443002f2a9597a26b152ae3432c1
2 * Fit terminal columns and rows to the dimensions of its DOM element.
5 * - Rows: Truncate the division of the terminal parent element height by the terminal row height.
7 * - Columns: Truncate the division of the terminal parent element width by the terminal character
8 * width (apply display: inline at the terminal row and truncate its width with the current
10 * @module xterm/addons/fit/fit
15 if (typeof exports
=== 'object' && typeof module
=== 'object') {
17 * CommonJS environment
19 module
.exports
= fit(require('../../xterm'));
20 } else if (typeof define
== 'function') {
22 * Require.js is available
24 define(['../../xterm'], fit
);
27 * Plain browser environment
34 exports
.proposeGeometry = function (term
) {
35 if (!term
.element
.parentElement
) {
38 var parentElementStyle
= window
.getComputedStyle(term
.element
.parentElement
),
39 parentElementHeight
= parseInt(parentElementStyle
.getPropertyValue('height')),
40 parentElementWidth
= Math
.max(0, parseInt(parentElementStyle
.getPropertyValue('width')) - 17),
41 elementStyle
= window
.getComputedStyle(term
.element
),
42 elementPaddingVer
= parseInt(elementStyle
.getPropertyValue('padding-top')) + parseInt(elementStyle
.getPropertyValue('padding-bottom')),
43 elementPaddingHor
= parseInt(elementStyle
.getPropertyValue('padding-right')) + parseInt(elementStyle
.getPropertyValue('padding-left')),
44 availableHeight
= parentElementHeight
- elementPaddingVer
,
45 availableWidth
= parentElementWidth
- elementPaddingHor
,
46 container
= term
.rowContainer
,
47 subjectRow
= term
.rowContainer
.firstElementChild
,
48 contentBuffer
= subjectRow
.innerHTML
,
55 subjectRow
.style
.display
= 'inline';
56 subjectRow
.innerHTML
= 'W'; // Common character for measuring width, although on monospace
57 characterWidth
= subjectRow
.getBoundingClientRect().width
;
58 subjectRow
.style
.display
= ''; // Revert style before calculating height, since they differ.
59 characterHeight
= subjectRow
.getBoundingClientRect().height
;
60 subjectRow
.innerHTML
= contentBuffer
;
62 rows
= parseInt(availableHeight
/ characterHeight
);
63 cols
= parseInt(availableWidth
/ characterWidth
);
65 geometry
= {cols
: cols
, rows
: rows
};
69 exports
.fit = function (term
) {
70 var geometry
= exports
.proposeGeometry(term
);
73 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);