annotate DataTables-1.9.4/media/src/core/core.sizing.js @ 7:0f2b740536fb draft

Uploaded
author saskia-hiltemann
date Mon, 21 Aug 2017 09:16:07 -0400
parents ac5f9272033b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
1 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
2 * Convert a CSS unit width to pixels (e.g. 2em)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
3 * @param {string} sWidth width to be converted
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
4 * @param {node} nParent parent to get the with for (required for relative widths) - optional
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
5 * @returns {int} iWidth width in pixels
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
6 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
7 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
8 function _fnConvertToWidth ( sWidth, nParent )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
9 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
10 if ( !sWidth || sWidth === null || sWidth === '' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
11 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
12 return 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
13 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
14
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
15 if ( !nParent )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
16 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
17 nParent = document.body;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
18 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
19
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
20 var iWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
21 var nTmp = document.createElement( "div" );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
22 nTmp.style.width = _fnStringToCss( sWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
23
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
24 nParent.appendChild( nTmp );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
25 iWidth = nTmp.offsetWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
26 nParent.removeChild( nTmp );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
27
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
28 return ( iWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
29 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
30
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
31
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
32 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
33 * Calculate the width of columns for the table
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
34 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
35 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
36 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
37 function _fnCalculateColumnWidths ( oSettings )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
38 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
39 var iTableWidth = oSettings.nTable.offsetWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
40 var iUserInputs = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
41 var iTmpWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
42 var iVisibleColumns = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
43 var iColums = oSettings.aoColumns.length;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
44 var i, iIndex, iCorrector, iWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
45 var oHeaders = $('th', oSettings.nTHead);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
46 var widthAttr = oSettings.nTable.getAttribute('width');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
47 var nWrapper = oSettings.nTable.parentNode;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
48
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
49 /* Convert any user input sizes into pixel sizes */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
50 for ( i=0 ; i<iColums ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
51 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
52 if ( oSettings.aoColumns[i].bVisible )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
53 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
54 iVisibleColumns++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
55
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
56 if ( oSettings.aoColumns[i].sWidth !== null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
57 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
58 iTmpWidth = _fnConvertToWidth( oSettings.aoColumns[i].sWidthOrig,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
59 nWrapper );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
60 if ( iTmpWidth !== null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
61 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
62 oSettings.aoColumns[i].sWidth = _fnStringToCss( iTmpWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
63 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
64
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
65 iUserInputs++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
66 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
67 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
68 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
69
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
70 /* If the number of columns in the DOM equals the number that we have to process in
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
71 * DataTables, then we can use the offsets that are created by the web-browser. No custom
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
72 * sizes can be set in order for this to happen, nor scrolling used
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
73 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
74 if ( iColums == oHeaders.length && iUserInputs === 0 && iVisibleColumns == iColums &&
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
75 oSettings.oScroll.sX === "" && oSettings.oScroll.sY === "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
76 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
77 for ( i=0 ; i<oSettings.aoColumns.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
78 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
79 iTmpWidth = $(oHeaders[i]).width();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
80 if ( iTmpWidth !== null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
81 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
82 oSettings.aoColumns[i].sWidth = _fnStringToCss( iTmpWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
83 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
84 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
85 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
86 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
87 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
88 /* Otherwise we are going to have to do some calculations to get the width of each column.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
89 * Construct a 1 row table with the widest node in the data, and any user defined widths,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
90 * then insert it into the DOM and allow the browser to do all the hard work of
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
91 * calculating table widths.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
92 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
93 var
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
94 nCalcTmp = oSettings.nTable.cloneNode( false ),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
95 nTheadClone = oSettings.nTHead.cloneNode(true),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
96 nBody = document.createElement( 'tbody' ),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
97 nTr = document.createElement( 'tr' ),
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
98 nDivSizing;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
99
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
100 nCalcTmp.removeAttribute( "id" );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
101 nCalcTmp.appendChild( nTheadClone );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
102 if ( oSettings.nTFoot !== null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
103 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
104 nCalcTmp.appendChild( oSettings.nTFoot.cloneNode(true) );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
105 _fnApplyToChildren( function(n) {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
106 n.style.width = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
107 }, nCalcTmp.getElementsByTagName('tr') );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
108 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
109
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
110 nCalcTmp.appendChild( nBody );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
111 nBody.appendChild( nTr );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
112
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
113 /* Remove any sizing that was previously applied by the styles */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
114 var jqColSizing = $('thead th', nCalcTmp);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
115 if ( jqColSizing.length === 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
116 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
117 jqColSizing = $('tbody tr:eq(0)>td', nCalcTmp);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
118 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
119
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
120 /* Apply custom sizing to the cloned header */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
121 var nThs = _fnGetUniqueThs( oSettings, nTheadClone );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
122 iCorrector = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
123 for ( i=0 ; i<iColums ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
124 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
125 var oColumn = oSettings.aoColumns[i];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
126 if ( oColumn.bVisible && oColumn.sWidthOrig !== null && oColumn.sWidthOrig !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
127 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
128 nThs[i-iCorrector].style.width = _fnStringToCss( oColumn.sWidthOrig );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
129 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
130 else if ( oColumn.bVisible )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
131 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
132 nThs[i-iCorrector].style.width = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
133 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
134 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
135 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
136 iCorrector++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
137 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
138 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
139
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
140 /* Find the biggest td for each column and put it into the table */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
141 for ( i=0 ; i<iColums ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
142 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
143 if ( oSettings.aoColumns[i].bVisible )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
144 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
145 var nTd = _fnGetWidestNode( oSettings, i );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
146 if ( nTd !== null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
147 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
148 nTd = nTd.cloneNode(true);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
149 if ( oSettings.aoColumns[i].sContentPadding !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
150 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
151 nTd.innerHTML += oSettings.aoColumns[i].sContentPadding;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
152 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
153 nTr.appendChild( nTd );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
154 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
155 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
156 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
157
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
158 /* Build the table and 'display' it */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
159 nWrapper.appendChild( nCalcTmp );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
160
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
161 /* When scrolling (X or Y) we want to set the width of the table as appropriate. However,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
162 * when not scrolling leave the table width as it is. This results in slightly different,
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
163 * but I think correct behaviour
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
164 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
165 if ( oSettings.oScroll.sX !== "" && oSettings.oScroll.sXInner !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
166 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
167 nCalcTmp.style.width = _fnStringToCss(oSettings.oScroll.sXInner);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
168 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
169 else if ( oSettings.oScroll.sX !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
170 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
171 nCalcTmp.style.width = "";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
172 if ( $(nCalcTmp).width() < nWrapper.offsetWidth )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
173 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
174 nCalcTmp.style.width = _fnStringToCss( nWrapper.offsetWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
175 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
176 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
177 else if ( oSettings.oScroll.sY !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
178 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
179 nCalcTmp.style.width = _fnStringToCss( nWrapper.offsetWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
180 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
181 else if ( widthAttr )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
182 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
183 nCalcTmp.style.width = _fnStringToCss( widthAttr );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
184 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
185 nCalcTmp.style.visibility = "hidden";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
186
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
187 /* Scrolling considerations */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
188 _fnScrollingWidthAdjust( oSettings, nCalcTmp );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
189
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
190 /* Read the width's calculated by the browser and store them for use by the caller. We
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
191 * first of all try to use the elements in the body, but it is possible that there are
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
192 * no elements there, under which circumstances we use the header elements
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
193 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
194 var oNodes = $("tbody tr:eq(0)", nCalcTmp).children();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
195 if ( oNodes.length === 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
196 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
197 oNodes = _fnGetUniqueThs( oSettings, $('thead', nCalcTmp)[0] );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
198 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
199
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
200 /* Browsers need a bit of a hand when a width is assigned to any columns when
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
201 * x-scrolling as they tend to collapse the table to the min-width, even if
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
202 * we sent the column widths. So we need to keep track of what the table width
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
203 * should be by summing the user given values, and the automatic values
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
204 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
205 if ( oSettings.oScroll.sX !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
206 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
207 var iTotal = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
208 iCorrector = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
209 for ( i=0 ; i<oSettings.aoColumns.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
210 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
211 if ( oSettings.aoColumns[i].bVisible )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
212 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
213 if ( oSettings.aoColumns[i].sWidthOrig === null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
214 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
215 iTotal += $(oNodes[iCorrector]).outerWidth();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
216 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
217 else
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
218 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
219 iTotal += parseInt(oSettings.aoColumns[i].sWidth.replace('px',''), 10) +
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
220 ($(oNodes[iCorrector]).outerWidth() - $(oNodes[iCorrector]).width());
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
221 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
222 iCorrector++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
223 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
224 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
225
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
226 nCalcTmp.style.width = _fnStringToCss( iTotal );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
227 oSettings.nTable.style.width = _fnStringToCss( iTotal );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
228 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
229
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
230 iCorrector = 0;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
231 for ( i=0 ; i<oSettings.aoColumns.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
232 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
233 if ( oSettings.aoColumns[i].bVisible )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
234 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
235 iWidth = $(oNodes[iCorrector]).width();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
236 if ( iWidth !== null && iWidth > 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
237 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
238 oSettings.aoColumns[i].sWidth = _fnStringToCss( iWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
239 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
240 iCorrector++;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
241 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
242 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
243
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
244 var cssWidth = $(nCalcTmp).css('width');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
245 oSettings.nTable.style.width = (cssWidth.indexOf('%') !== -1) ?
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
246 cssWidth : _fnStringToCss( $(nCalcTmp).outerWidth() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
247 nCalcTmp.parentNode.removeChild( nCalcTmp );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
248 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
249
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
250 if ( widthAttr )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
251 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
252 oSettings.nTable.style.width = _fnStringToCss( widthAttr );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
253 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
254 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
255
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
256
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
257 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
258 * Adjust a table's width to take account of scrolling
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
259 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
260 * @param {node} n table node
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
261 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
262 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
263 function _fnScrollingWidthAdjust ( oSettings, n )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
264 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
265 if ( oSettings.oScroll.sX === "" && oSettings.oScroll.sY !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
266 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
267 /* When y-scrolling only, we want to remove the width of the scroll bar so the table
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
268 * + scroll bar will fit into the area avaialble.
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
269 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
270 var iOrigWidth = $(n).width();
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
271 n.style.width = _fnStringToCss( $(n).outerWidth()-oSettings.oScroll.iBarWidth );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
272 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
273 else if ( oSettings.oScroll.sX !== "" )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
274 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
275 /* When x-scrolling both ways, fix the table at it's current size, without adjusting */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
276 n.style.width = _fnStringToCss( $(n).outerWidth() );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
277 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
278 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
279
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
280
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
281 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
282 * Get the widest node
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
283 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
284 * @param {int} iCol column of interest
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
285 * @returns {node} widest table node
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
286 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
287 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
288 function _fnGetWidestNode( oSettings, iCol )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
289 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
290 var iMaxIndex = _fnGetMaxLenString( oSettings, iCol );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
291 if ( iMaxIndex < 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
292 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
293 return null;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
294 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
295
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
296 if ( oSettings.aoData[iMaxIndex].nTr === null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
297 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
298 var n = document.createElement('td');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
299 n.innerHTML = _fnGetCellData( oSettings, iMaxIndex, iCol, '' );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
300 return n;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
301 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
302 return _fnGetTdNodes(oSettings, iMaxIndex)[iCol];
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
303 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
304
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
305
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
306 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
307 * Get the maximum strlen for each data column
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
308 * @param {object} oSettings dataTables settings object
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
309 * @param {int} iCol column of interest
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
310 * @returns {string} max string length for each column
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
311 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
312 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
313 function _fnGetMaxLenString( oSettings, iCol )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
314 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
315 var iMax = -1;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
316 var iMaxIndex = -1;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
317
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
318 for ( var i=0 ; i<oSettings.aoData.length ; i++ )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
319 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
320 var s = _fnGetCellData( oSettings, i, iCol, 'display' )+"";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
321 s = s.replace( /<.*?>/g, "" );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
322 if ( s.length > iMax )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
323 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
324 iMax = s.length;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
325 iMaxIndex = i;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
326 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
327 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
328
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
329 return iMaxIndex;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
330 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
331
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
332
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
333 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
334 * Append a CSS unit (only if required) to a string
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
335 * @param {array} aArray1 first array
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
336 * @param {array} aArray2 second array
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
337 * @returns {int} 0 if match, 1 if length is different, 2 if no match
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
338 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
339 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
340 function _fnStringToCss( s )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
341 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
342 if ( s === null )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
343 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
344 return "0px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
345 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
346
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
347 if ( typeof s == 'number' )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
348 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
349 if ( s < 0 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
350 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
351 return "0px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
352 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
353 return s+"px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
354 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
355
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
356 /* Check if the last character is not 0-9 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
357 var c = s.charCodeAt( s.length-1 );
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
358 if (c < 0x30 || c > 0x39)
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
359 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
360 return s;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
361 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
362 return s+"px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
363 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
364
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
365
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
366 /**
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
367 * Get the width of a scroll bar in this browser being used
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
368 * @returns {int} width in pixels
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
369 * @memberof DataTable#oApi
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
370 */
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
371 function _fnScrollBarWidth ()
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
372 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
373 var inner = document.createElement('p');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
374 var style = inner.style;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
375 style.width = "100%";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
376 style.height = "200px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
377 style.padding = "0px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
378
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
379 var outer = document.createElement('div');
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
380 style = outer.style;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
381 style.position = "absolute";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
382 style.top = "0px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
383 style.left = "0px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
384 style.visibility = "hidden";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
385 style.width = "200px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
386 style.height = "150px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
387 style.padding = "0px";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
388 style.overflow = "hidden";
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
389 outer.appendChild(inner);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
390
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
391 document.body.appendChild(outer);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
392 var w1 = inner.offsetWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
393 outer.style.overflow = 'scroll';
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
394 var w2 = inner.offsetWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
395 if ( w1 == w2 )
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
396 {
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
397 w2 = outer.clientWidth;
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
398 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
399
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
400 document.body.removeChild(outer);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
401 return (w1 - w2);
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
402 }
ac5f9272033b first upload
saskia-hiltemann
parents:
diff changeset
403