Diffstat (limited to 'frontend/beta/js/YUI-extensions/grid/DefaultColumnModel.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/grid/DefaultColumnModel.js | 325 |
1 files changed, 325 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/grid/DefaultColumnModel.js b/frontend/beta/js/YUI-extensions/grid/DefaultColumnModel.js new file mode 100644 index 0000000..fbdba26 --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/grid/DefaultColumnModel.js | |||
@@ -0,0 +1,325 @@ | |||
1 | /** | ||
2 | * @class YAHOO.ext.grid.DefaultColumnModel | ||
3 | * @extends YAHOO.ext.grid.AbstractColumnModel | ||
4 | * This is the default implementation of a ColumnModel used by the Grid. It defines | ||
5 | * the columns in the grid. | ||
6 | * <br>Usage:<br> | ||
7 | <pre><code> | ||
8 | var sort = YAHOO.ext.grid.DefaultColumnModel.sortTypes; | ||
9 | var myColumns = [ | ||
10 | {header: "Ticker", width: 60, sortable: true, sortType: sort.asUCString}, | ||
11 | {header: "Company Name", width: 150, sortable: true, sortType: sort.asUCString}, | ||
12 | {header: "Market Cap.", width: 100, sortable: true, sortType: sort.asFloat}, | ||
13 | {header: "$ Sales", width: 100, sortable: true, sortType: sort.asFloat, renderer: money}, | ||
14 | {header: "Employees", width: 100, sortable: true, sortType: sort.asFloat} | ||
15 | ]; | ||
16 | var colModel = new YAHOO.ext.grid.DefaultColumnModel(myColumns); | ||
17 | </code></pre> | ||
18 | * @constructor | ||
19 | * @param {Object} config The config object | ||
20 | */ | ||
21 | YAHOO.ext.grid.DefaultColumnModel = function(config){ | ||
22 | YAHOO.ext.grid.DefaultColumnModel.superclass.constructor.call(this); | ||
23 | /** | ||
24 | * The config passed into the constructor | ||
25 | */ | ||
26 | this.config = config; | ||
27 | |||
28 | /** | ||
29 | * The width of columns which have no width specified (defaults to 100) | ||
30 | * @type Number | ||
31 | */ | ||
32 | this.defaultWidth = 100; | ||
33 | /** | ||
34 | * Default sortable of columns which have no sortable specified (defaults to false) | ||
35 | * @type Boolean | ||
36 | */ | ||
37 | this.defaultSortable = false; | ||
38 | }; | ||
39 | YAHOO.extendX(YAHOO.ext.grid.DefaultColumnModel, YAHOO.ext.grid.AbstractColumnModel, { | ||
40 | |||
41 | /** | ||
42 | * Returns the number of columns. | ||
43 | * @return {Number} | ||
44 | */ | ||
45 | getColumnCount : function(){ | ||
46 | return this.config.length; | ||
47 | }, | ||
48 | |||
49 | /** | ||
50 | * Returns true if the specified column is sortable. | ||
51 | * @param {Number} col The column index | ||
52 | * @return {Boolean} | ||
53 | */ | ||
54 | isSortable : function(col){ | ||
55 | if(typeof this.config[col].sortable == 'undefined'){ | ||
56 | return this.defaultSortable; | ||
57 | } | ||
58 | return this.config[col].sortable; | ||
59 | }, | ||
60 | |||
61 | /** | ||
62 | * Returns the sorting comparison function defined for the column (defaults to sortTypes.none). | ||
63 | * @param {Number} col The column index | ||
64 | * @return {Function} | ||
65 | */ | ||
66 | getSortType : function(col){ | ||
67 | if(!this.dataMap){ | ||
68 | // build a lookup so we don't search every time | ||
69 | var map = []; | ||
70 | for(var i = 0, len = this.config.length; i < len; i++){ | ||
71 | map[this.getDataIndex(i)] = i; | ||
72 | } | ||
73 | this.dataMap = map; | ||
74 | } | ||
75 | col = this.dataMap[col]; | ||
76 | if(!this.config[col].sortType){ | ||
77 | return YAHOO.ext.grid.DefaultColumnModel.sortTypes.none; | ||
78 | } | ||
79 | return this.config[col].sortType; | ||
80 | }, | ||
81 | |||
82 | /** | ||
83 | * Sets the sorting comparison function for a column. | ||
84 | * @param {Number} col The column index | ||
85 | * @param {Function} fn | ||
86 | */ | ||
87 | setSortType : function(col, fn){ | ||
88 | this.config[col].sortType = fn; | ||
89 | }, | ||
90 | |||
91 | |||
92 | /** | ||
93 | * Returns the rendering (formatting) function defined for the column. | ||
94 | * @param {Number} col The column index | ||
95 | * @return {Function} | ||
96 | */ | ||
97 | getRenderer : function(col){ | ||
98 | if(!this.config[col].renderer){ | ||
99 | return YAHOO.ext.grid.DefaultColumnModel.defaultRenderer; | ||
100 | } | ||
101 | return this.config[col].renderer; | ||
102 | }, | ||
103 | |||
104 | /** | ||
105 | * Sets the rendering (formatting) function for a column. | ||
106 | * @param {Number} col The column index | ||
107 | * @param {Function} fn | ||
108 | */ | ||
109 | setRenderer : function(col, fn){ | ||
110 | this.config[col].renderer = fn; | ||
111 | }, | ||
112 | |||
113 | /** | ||
114 | * Returns the width for the specified column. | ||
115 | * @param {Number} col The column index | ||
116 | * @return {Number} | ||
117 | */ | ||
118 | getColumnWidth : function(col){ | ||
119 | return this.config[col].width || this.defaultWidth; | ||
120 | }, | ||
121 | |||
122 | /** | ||
123 | * Sets the width for a column. | ||
124 | * @param {Number} col The column index | ||
125 | * @param {Number} width The new width | ||
126 | */ | ||
127 | setColumnWidth : function(col, width, suppressEvent){ | ||
128 | this.config[col].width = width; | ||
129 | this.totalWidth = null; | ||
130 | if(!suppressEvent){ | ||
131 | this.onWidthChange.fireDirect(this, col, width); | ||
132 | } | ||
133 | }, | ||
134 | |||
135 | /** | ||
136 | * Returns the total width of all columns. | ||
137 | * @param {Boolean} includeHidden True to include hidden column widths | ||
138 | * @return {Number} | ||
139 | */ | ||
140 | getTotalWidth : function(includeHidden){ | ||
141 | if(!this.totalWidth){ | ||
142 | this.totalWidth = 0; | ||
143 | for(var i = 0; i < this.config.length; i++){ | ||
144 | if(includeHidden || !this.isHidden(i)){ | ||
145 | this.totalWidth += this.getColumnWidth(i); | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | return this.totalWidth; | ||
150 | }, | ||
151 | |||
152 | /** | ||
153 | * Returns the header for the specified column. | ||
154 | * @param {Number} col The column index | ||
155 | * @return {String} | ||
156 | */ | ||
157 | getColumnHeader : function(col){ | ||
158 | return this.config[col].header; | ||
159 | }, | ||
160 | |||
161 | /** | ||
162 | * Sets the header for a column. | ||
163 | * @param {Number} col The column index | ||
164 | * @param {String} header The new header | ||
165 | */ | ||
166 | setColumnHeader : function(col, header){ | ||
167 | this.config[col].header = header; | ||
168 | this.onHeaderChange.fireDirect(this, col, header); | ||
169 | }, | ||
170 | |||
171 | /** | ||
172 | * Returns the tooltip for the specified column. | ||
173 | * @param {Number} col The column index | ||
174 | * @return {String} | ||
175 | */ | ||
176 | getColumnTooltip : function(col){ | ||
177 | return this.config[col].tooltip; | ||
178 | }, | ||
179 | /** | ||
180 | * Sets the tooltip for a column. | ||
181 | * @param {Number} col The column index | ||
182 | * @param {String} tooltip The new tooltip | ||
183 | */ | ||
184 | setColumnTooltip : function(col, header){ | ||
185 | this.config[col].tooltip = tooltip; | ||
186 | }, | ||
187 | |||
188 | /** | ||
189 | * Returns the dataIndex for the specified column. | ||
190 | * @param {Number} col The column index | ||
191 | * @return {Number} | ||
192 | */ | ||
193 | getDataIndex : function(col){ | ||
194 | if(typeof this.config[col].dataIndex != 'number'){ | ||
195 | return col; | ||
196 | } | ||
197 | return this.config[col].dataIndex; | ||
198 | }, | ||
199 | |||
200 | /** | ||
201 | * Sets the dataIndex for a column. | ||
202 | * @param {Number} col The column index | ||
203 | * @param {Number} dataIndex The new dataIndex | ||
204 | */ | ||
205 | setDataIndex : function(col, dataIndex){ | ||
206 | this.config[col].dataIndex = dataIndex; | ||
207 | }, | ||
208 | /** | ||
209 | * Returns true if the cell is editable. | ||
210 | * @param {Number} colIndex The column index | ||
211 | * @param {Number} rowIndex The row index | ||
212 | * @return {Boolean} | ||
213 | */ | ||
214 | isCellEditable : function(colIndex, rowIndex){ | ||
215 | return this.config[colIndex].editable || (typeof this.config[colIndex].editable == 'undefined' && this.config[colIndex].editor); | ||
216 | }, | ||
217 | |||
218 | /** | ||
219 | * Returns the editor defined for the cell/column. | ||
220 | * @param {Number} colIndex The column index | ||
221 | * @param {Number} rowIndex The row index | ||
222 | * @return {Object} | ||
223 | */ | ||
224 | getCellEditor : function(colIndex, rowIndex){ | ||
225 | return this.config[colIndex].editor; | ||
226 | }, | ||
227 | |||
228 | /** | ||
229 | * Sets if a column is editable. | ||
230 | * @param {Number} col The column index | ||
231 | * @param {Boolean} editable True if the column is editable | ||
232 | */ | ||
233 | setEditable : function(col, editable){ | ||
234 | this.config[col].editable = editable; | ||
235 | }, | ||
236 | |||
237 | |||
238 | /** | ||
239 | * Returns true if the column is hidden. | ||
240 | * @param {Number} colIndex The column index | ||
241 | * @return {Boolean} | ||
242 | */ | ||
243 | isHidden : function(colIndex){ | ||
244 | return this.config[colIndex].hidden; | ||
245 | }, | ||
246 | |||
247 | |||
248 | /** | ||
249 | * Returns true if the column width cannot be changed | ||
250 | */ | ||
251 | isFixed : function(colIndex){ | ||
252 | return this.config[colIndex].fixed; | ||
253 | }, | ||
254 | |||
255 | /** | ||
256 | * Returns true if the column cannot be resized | ||
257 | * @return {Boolean} | ||
258 | */ | ||
259 | isResizable : function(colIndex){ | ||
260 | return this.config[colIndex].resizable !== false; | ||
261 | }, | ||
262 | /** | ||
263 | * Sets if a column is hidden. | ||
264 | * @param {Number} colIndex The column index | ||
265 | */ | ||
266 | setHidden : function(colIndex, hidden){ | ||
267 | this.config[colIndex].hidden = hidden; | ||
268 | this.totalWidth = null; | ||
269 | this.fireHiddenChange(colIndex, hidden); | ||
270 | }, | ||
271 | |||
272 | /** | ||
273 | * Sets the editor for a column. | ||
274 | * @param {Number} col The column index | ||
275 | * @param {Object} editor The editor object | ||
276 | */ | ||
277 | setEditor : function(col, editor){ | ||
278 | this.config[col].editor = editor; | ||
279 | } | ||
280 | }); | ||
281 | |||
282 | /** | ||
283 | * Defines the default sorting (casting?) comparison functions used when sorting data: | ||
284 | * <br> sortTypes.none - sorts data as it is without casting or parsing (the default) | ||
285 | * <br> sortTypes.asUCString - case insensitive string | ||
286 | * <br> sortTypes.asDate - attempts to parse data as a date | ||
287 | * <br> sortTypes.asFloat | ||
288 | * <br> sortTypes.asInt | ||
289 | * @static | ||
290 | */ | ||
291 | YAHOO.ext.grid.DefaultColumnModel.sortTypes = { | ||
292 | none : function(s) { | ||
293 | return s; | ||
294 | }, | ||
295 | |||
296 | asUCString : function(s) { | ||
297 | return String(s).toUpperCase(); | ||
298 | }, | ||
299 | |||
300 | asDate : function(s) { | ||
301 | if(s instanceof Date){ | ||
302 | return s.getTime(); | ||
303 | } | ||
304 | return Date.parse(String(s)); | ||
305 | }, | ||
306 | |||
307 | asFloat : function(s) { | ||
308 | var val = parseFloat(String(s).replace(/,/g, '')); | ||
309 | if(isNaN(val)) val = 0; | ||
310 | return val; | ||
311 | }, | ||
312 | |||
313 | asInt : function(s) { | ||
314 | var val = parseInt(String(s).replace(/,/g, '')); | ||
315 | if(isNaN(val)) val = 0; | ||
316 | return val; | ||
317 | } | ||
318 | }; | ||
319 | |||
320 | YAHOO.ext.grid.DefaultColumnModel.defaultRenderer = function(value){ | ||
321 | if(typeof value == 'string' && value.length < 1){ | ||
322 | return ' '; | ||
323 | } | ||
324 | return value; | ||
325 | } | ||