]> git.proxmox.com Git - mirror_xterm.js.git/blame - addons/fit/fit.js
[fit addon] Restore row content after proposing geometry
[mirror_xterm.js.git] / addons / fit / fit.js
CommitLineData
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
PK
14(function (fit) {
15 if (typeof define == 'function') {
16 /*
17 * Require.js is available
18 */
19 define(['../../src/xterm'], fit);
20 } else {
21 /*
22 * Plain browser environment
23 */
24 fit(this.Xterm);
25 }
26})(function (Xterm) {
615f633f
PK
27 Xterm.prototype.proposeGeometry = function () {
28 var container = this.rowContainer,
29 subjectRow = this.rowContainer.firstElementChild,
30 rows,
31 contentBuffer,
32 characterWidth,
33 cols;
34
35 subjectRow.style.display = 'inline';
3f455f90 36
c6a7e15b 37 contentBuffer = subjectRow.innerHTML;
3f455f90 38
615f633f
PK
39 subjectRow.innerHTML = ' '; /* Arbitrary character to calculate its dimensions */
40 characterWidth = parseInt(subjectRow.offsetWidth);
41 characterHeight = parseInt(subjectRow.offsetHeight);
3f455f90 42
615f633f 43 subjectRow.style.display = '';
3f455f90 44
615f633f
PK
45 cols = container.offsetWidth / characterWidth;
46 cols = parseInt(cols);
3f455f90 47
615f633f
PK
48 var parentElementStyle = window.getComputedStyle(this.element.parentElement),
49 parentElementHeight = parseInt(parentElementStyle.getPropertyValue('height')),
50 elementStyle = window.getComputedStyle(this.element),
51 elementPadding = parseInt(elementStyle.getPropertyValue('padding-top')) + parseInt(elementStyle.getPropertyValue('padding-bottom')),
52 availableHeight = parentElementHeight - elementPadding,
53 rowHeight = this.rowContainer.firstElementChild.offsetHeight;
3f455f90 54
615f633f
PK
55 rows = parseInt(availableHeight / rowHeight);
56
c6a7e15b
PK
57 subjectRow.innerHTML = contentBuffer; /* Replace original content */
58
615f633f
PK
59 var geometry = {
60 'cols': cols,
87979aee 61 'rows': rows
615f633f 62 };
3f455f90 63
615f633f
PK
64 return geometry;
65 };
66
67 Xterm.prototype.fit = function () {
68 var geometry = this.proposeGeometry();
3f455f90 69
615f633f 70 this.resize(geometry.cols, geometry.rows);
3f455f90
PK
71 };
72});