]>
Commit | Line | Data |
---|---|---|
189a5564 PK |
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 | */ | |
3f455f90 | 14 | (function (fit) { |
fff56eea PK |
15 | if (typeof exports === 'object' && typeof module === 'object') { |
16 | /* | |
17 | * CommonJS environment | |
18 | */ | |
6ee52486 | 19 | module.exports = fit(require('../../dist/xterm')); |
fff56eea PK |
20 | } else if (typeof define == 'function') { |
21 | /* | |
22 | * Require.js is available | |
23 | */ | |
6ee52486 | 24 | define(['../../dist/xterm'], fit); |
fff56eea PK |
25 | } else { |
26 | /* | |
27 | * Plain browser environment | |
28 | */ | |
ed1a31d1 | 29 | fit(window.Terminal); |
fff56eea | 30 | } |
3f455f90 | 31 | })(function (Xterm) { |
fff56eea PK |
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 | }; | |
615f633f | 70 | |
fff56eea PK |
71 | exports.fit = function (term) { |
72 | var geometry = exports.proposeGeometry(term); | |
37be8e0f | 73 | |
fff56eea PK |
74 | term.resize(geometry.cols, geometry.rows); |
75 | }; | |
3f455f90 | 76 | |
dc67c945 PK |
77 | Xterm.prototype.proposeGeometry = function () { |
78 | return exports.proposeGeometry(this); | |
fff56eea PK |
79 | }; |
80 | ||
dc67c945 PK |
81 | Xterm.prototype.fit = function () { |
82 | return exports.fit(this); | |
83 | }; | |
fff56eea PK |
84 | |
85 | return exports; | |
d9d99c1c | 86 | }); |