1 var Layout =
function(table){
7 Layout.prototype.initialize =
function(layout){
9 if(this.modes[layout]){
12 console.warn(
"Layout Error - invalid mode set, defaulting to 'fitData' : " + layout);
13 this.mode =
'fitData';
16 this.table.element.setAttribute(
"tabulator-layout", this.mode);
19 Layout.prototype.getMode =
function(){
24 Layout.prototype.layout =
function(){
25 this.modes[this.mode].call(
this, this.table.columnManager.columnsByIndex);
29 Layout.prototype.modes = {
32 "fitData":
function(columns){
33 columns.forEach(
function(column){
34 column.reinitializeWidth();
37 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
38 this.table.modules.responsiveLayout.update();
43 "fitDataFill":
function(columns){
44 columns.forEach(
function(column){
45 column.reinitializeWidth();
48 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
49 this.table.modules.responsiveLayout.update();
54 "fitDataStretch":
function(columns){
56 tableWidth = this.table.rowManager.element.clientWidth,
60 columns.forEach((column, i) => {
61 if(!column.widthFixed){
62 column.reinitializeWidth();
65 if(this.table.options.responsiveLayout ? column.modules.responsive.visible : column.visible){
70 colsWidth += column.getWidth();
75 gap = tableWidth - colsWidth + lastCol.getWidth();
77 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
79 this.table.modules.responsiveLayout.update();
83 lastCol.setWidth(gap);
85 lastCol.reinitializeWidth();
88 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
89 this.table.modules.responsiveLayout.update();
95 "fitColumns":
function(columns){
98 var totalWidth =
self.table.element.clientWidth;
101 var flexGrowUnits = 0;
102 var flexColWidth = 0;
103 var flexColumns = [];
104 var fixedShrinkColumns = [];
105 var flexShrinkUnits = 0;
106 var overflowWidth = 0;
109 function calcWidth(width){
112 if(typeof(width) ==
"string"){
113 if(width.indexOf(
"%") > -1){
114 colWidth = (totalWidth / 100) * parseInt(width);
116 colWidth = parseInt(width);
126 function scaleColumns(columns, freeSpace, colWidth, shrinkCols){
128 var oversizeCols = [],
136 function calcGrow(col){
137 return (colWidth * (col.column.definition.widthGrow || 1));
140 function calcShrink(col){
141 return (calcWidth(col.width) - (colWidth * (col.column.definition.widthShrink || 0)))
144 columns.forEach(
function(col, i){
145 var width = shrinkCols ? calcShrink(col) : calcGrow(col);
146 if(col.column.minWidth >= width){
147 oversizeCols.push(col);
149 undersizeCols.push(col);
150 changeUnits += shrinkCols ? (col.column.definition.widthShrink || 1) : (col.column.definition.widthGrow || 1);
154 if(oversizeCols.length){
155 oversizeCols.forEach(
function(col){
156 oversizeSpace += shrinkCols ? col.width - col.column.minWidth : col.column.minWidth;
157 col.width = col.column.minWidth;
160 remainingSpace = freeSpace - oversizeSpace;
162 nextColWidth = changeUnits ? Math.floor(remainingSpace/changeUnits) : remainingSpace;
164 gap = remainingSpace - (nextColWidth * changeUnits);
166 gap += scaleColumns(undersizeCols, remainingSpace, nextColWidth, shrinkCols);
168 gap = changeUnits ? freeSpace - (Math.floor(freeSpace/changeUnits) * changeUnits) : freeSpace;
170 undersizeCols.forEach(
function(column){
171 column.width = shrinkCols ? calcShrink(column) : calcGrow(column);
179 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
180 this.table.modules.responsiveLayout.update();
184 if(this.table.rowManager.element.scrollHeight >
this.table.rowManager.element.clientHeight){
185 totalWidth -= this.table.rowManager.element.offsetWidth - this.table.rowManager.element.clientWidth;
188 columns.forEach(
function(column){
189 var width, minWidth, colWidth;
193 width = column.definition.width;
194 minWidth = parseInt(column.minWidth);
198 colWidth = calcWidth(width);
200 fixedWidth += colWidth > minWidth ? colWidth : minWidth;
202 if(column.definition.widthShrink){
203 fixedShrinkColumns.push({
205 width:colWidth > minWidth ? colWidth : minWidth
207 flexShrinkUnits += column.definition.widthShrink;
215 flexGrowUnits += column.definition.widthGrow || 1;
222 flexWidth = totalWidth - fixedWidth;
225 flexColWidth = Math.floor(flexWidth / flexGrowUnits)
228 var gapFill = scaleColumns(flexColumns, flexWidth, flexColWidth,
false);
231 if(flexColumns.length && gapFill > 0){
232 flexColumns[flexColumns.length-1].width += + gapFill;
236 flexColumns.forEach(
function(col){
237 flexWidth -= col.width;
240 overflowWidth = Math.abs(gapFill) + flexWidth;
244 if(overflowWidth > 0 && flexShrinkUnits){
245 gapFill = scaleColumns(fixedShrinkColumns, overflowWidth, Math.floor(overflowWidth / flexShrinkUnits),
true);
249 if(fixedShrinkColumns.length){
250 fixedShrinkColumns[fixedShrinkColumns.length-1].width -= gapFill;
254 flexColumns.forEach(
function(col){
255 col.column.setWidth(col.width);
258 fixedShrinkColumns.forEach(
function(col){
259 col.column.setWidth(col.width);
266 Tabulator.prototype.registerModule(
"layout", Layout);