| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A table sorting decorator. |
| 17 |
* |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
* @see ../demos/tablesorter.html |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.ui.TableSorter'); |
| 23 |
goog.provide('goog.ui.TableSorter.EventType'); |
| 24 |
|
| 25 |
goog.require('goog.array'); |
| 26 |
goog.require('goog.dom'); |
| 27 |
goog.require('goog.dom.TagName'); |
| 28 |
goog.require('goog.dom.classlist'); |
| 29 |
goog.require('goog.events.EventType'); |
| 30 |
goog.require('goog.functions'); |
| 31 |
goog.require('goog.ui.Component'); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* A table sorter allows for sorting of a table by column. This component can |
| 37 |
* be used to decorate an already existing TABLE element with sorting |
| 38 |
* features. |
| 39 |
* |
| 40 |
* The TABLE should use a THEAD containing TH elements for the table column |
| 41 |
* headers. |
| 42 |
* |
| 43 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for |
| 44 |
* document interaction. |
| 45 |
* @constructor |
| 46 |
* @extends {goog.ui.Component} |
| 47 |
*/ |
| 48 |
goog.ui.TableSorter = function(opt_domHelper) { |
| 49 |
goog.ui.Component.call(this, opt_domHelper); |
| 50 |
|
| 51 |
/** |
| 52 |
* The current sort header of the table, or null if none. |
| 53 |
* @type {HTMLTableCellElement} |
| 54 |
* @private |
| 55 |
*/ |
| 56 |
this.header_ = null; |
| 57 |
|
| 58 |
/** |
| 59 |
* Whether the last sort was in reverse. |
| 60 |
* @type {boolean} |
| 61 |
* @private |
| 62 |
*/ |
| 63 |
this.reversed_ = false; |
| 64 |
|
| 65 |
/** |
| 66 |
* The default sorting function. |
| 67 |
* @type {function(*, *) : number} |
| 68 |
* @private |
| 69 |
*/ |
| 70 |
this.defaultSortFunction_ = goog.ui.TableSorter.numericSort; |
| 71 |
|
| 72 |
/** |
| 73 |
* Array of custom sorting functions per colun. |
| 74 |
* @type {Array<function(*, *) : number>} |
| 75 |
* @private |
| 76 |
*/ |
| 77 |
this.sortFunctions_ = []; |
| 78 |
}; |
| 79 |
goog.inherits(goog.ui.TableSorter, goog.ui.Component); |
| 80 |
goog.tagUnsealableClass(goog.ui.TableSorter); |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Row number (in <thead>) to use for sorting. |
| 85 |
* @type {number} |
| 86 |
* @private |
| 87 |
*/ |
| 88 |
goog.ui.TableSorter.prototype.sortableHeaderRowIndex_ = 0; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Sets the row index (in <thead>) to be used for sorting. |
| 93 |
* By default, the first row (index 0) is used. |
| 94 |
* Must be called before decorate() is called. |
| 95 |
* @param {number} index The row index. |
| 96 |
*/ |
| 97 |
goog.ui.TableSorter.prototype.setSortableHeaderRowIndex = function(index) { |
| 98 |
if (this.isInDocument()) { |
| 99 |
throw new Error(goog.ui.Component.Error.ALREADY_RENDERED); |
| 100 |
} |
| 101 |
this.sortableHeaderRowIndex_ = index; |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Table sorter events. |
| 107 |
* @enum {string} |
| 108 |
*/ |
| 109 |
goog.ui.TableSorter.EventType = { |
| 110 |
BEFORESORT: 'beforesort', |
| 111 |
SORT: 'sort' |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
/** @override */ |
| 116 |
goog.ui.TableSorter.prototype.canDecorate = function(element) { |
| 117 |
return element.tagName == goog.dom.TagName.TABLE; |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
/** @override */ |
| 122 |
goog.ui.TableSorter.prototype.enterDocument = function() { |
| 123 |
goog.ui.TableSorter.superClass_.enterDocument.call(this); |
| 124 |
|
| 125 |
var table = this.getElement(); |
| 126 |
var headerRow = table.tHead.rows[this.sortableHeaderRowIndex_]; |
| 127 |
|
| 128 |
this.getHandler().listen(headerRow, goog.events.EventType.CLICK, this.sort_); |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* @return {number} The current sort column of the table, or -1 if none. |
| 134 |
*/ |
| 135 |
goog.ui.TableSorter.prototype.getSortColumn = function() { |
| 136 |
return this.header_ ? this.header_.cellIndex : -1; |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* @return {boolean} Whether the last sort was in reverse. |
| 142 |
*/ |
| 143 |
goog.ui.TableSorter.prototype.isSortReversed = function() { |
| 144 |
return this.reversed_; |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* @return {function(*, *) : number} The default sort function to be used by |
| 150 |
* all columns. |
| 151 |
*/ |
| 152 |
goog.ui.TableSorter.prototype.getDefaultSortFunction = function() { |
| 153 |
return this.defaultSortFunction_; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Sets the default sort function to be used by all columns. If not set |
| 159 |
* explicitly, this defaults to numeric sorting. |
| 160 |
* @param {function(*, *) : number} sortFunction The new default sort function. |
| 161 |
*/ |
| 162 |
goog.ui.TableSorter.prototype.setDefaultSortFunction = function(sortFunction) { |
| 163 |
this.defaultSortFunction_ = sortFunction; |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Gets the sort function to be used by the given column. Returns the default |
| 169 |
* sort function if no sort function is explicitly set for this column. |
| 170 |
* @param {number} column The column index. |
| 171 |
* @return {function(*, *) : number} The sort function used by the column. |
| 172 |
*/ |
| 173 |
goog.ui.TableSorter.prototype.getSortFunction = function(column) { |
| 174 |
return this.sortFunctions_[column] || this.defaultSortFunction_; |
| 175 |
}; |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Set the sort function for the given column, overriding the default sort |
| 180 |
* function. |
| 181 |
* @param {number} column The column index. |
| 182 |
* @param {function(*, *) : number} sortFunction The new sort function. |
| 183 |
*/ |
| 184 |
goog.ui.TableSorter.prototype.setSortFunction = function(column, sortFunction) { |
| 185 |
this.sortFunctions_[column] = sortFunction; |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Sort the table contents by the values in the given column. |
| 191 |
* @param {goog.events.BrowserEvent} e The click event. |
| 192 |
* @private |
| 193 |
*/ |
| 194 |
goog.ui.TableSorter.prototype.sort_ = function(e) { |
| 195 |
// Determine what column was clicked. |
| 196 |
// TODO(robbyw): If this table cell contains another table, this could break. |
| 197 |
var target = e.target; |
| 198 |
var th = goog.dom.getAncestorByTagNameAndClass(target, goog.dom.TagName.TH); |
| 199 |
|
| 200 |
// If the user clicks on the same column, sort it in reverse of what it is |
| 201 |
// now. Otherwise, sort forward. |
| 202 |
var reverse = th == this.header_ ? !this.reversed_ : false; |
| 203 |
|
| 204 |
// Perform the sort. |
| 205 |
if (this.dispatchEvent(goog.ui.TableSorter.EventType.BEFORESORT)) { |
| 206 |
if (this.sort(th.cellIndex, reverse)) { |
| 207 |
this.dispatchEvent(goog.ui.TableSorter.EventType.SORT); |
| 208 |
} |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* Sort the table contents by the values in the given column. |
| 215 |
* @param {number} column The column to sort by. |
| 216 |
* @param {boolean=} opt_reverse Whether to sort in reverse. |
| 217 |
* @return {boolean} Whether the sort was executed. |
| 218 |
*/ |
| 219 |
goog.ui.TableSorter.prototype.sort = function(column, opt_reverse) { |
| 220 |
var sortFunction = this.getSortFunction(column); |
| 221 |
if (sortFunction === goog.ui.TableSorter.noSort) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
// Remove old header classes. |
| 226 |
if (this.header_) { |
| 227 |
goog.dom.classlist.remove( |
| 228 |
this.header_, this.reversed_ ? |
| 229 |
goog.getCssName('goog-tablesorter-sorted-reverse') : |
| 230 |
goog.getCssName('goog-tablesorter-sorted')); |
| 231 |
} |
| 232 |
|
| 233 |
// If the user clicks on the same column, sort it in reverse of what it is |
| 234 |
// now. Otherwise, sort forward. |
| 235 |
this.reversed_ = !!opt_reverse; |
| 236 |
var multiplier = this.reversed_ ? -1 : 1; |
| 237 |
var cmpFn = function(a, b) { |
| 238 |
return multiplier * sortFunction(a[0], b[0]) || a[1] - b[1]; |
| 239 |
}; |
| 240 |
|
| 241 |
// Sort all tBodies |
| 242 |
var table = this.getElement(); |
| 243 |
goog.array.forEach(table.tBodies, function(tBody) { |
| 244 |
// Collect all of the rows into an array. |
| 245 |
var values = goog.array.map(tBody.rows, function(row, rowIndex) { |
| 246 |
return [goog.dom.getTextContent(row.cells[column]), rowIndex, row]; |
| 247 |
}); |
| 248 |
|
| 249 |
goog.array.sort(values, cmpFn); |
| 250 |
|
| 251 |
// Remove the tBody temporarily since this speeds up the sort on some |
| 252 |
// browsers. |
| 253 |
var nextSibling = tBody.nextSibling; |
| 254 |
table.removeChild(tBody); |
| 255 |
|
| 256 |
// Sort the rows, using the resulting array. |
| 257 |
goog.array.forEach(values, function(row) { tBody.appendChild(row[2]); }); |
| 258 |
|
| 259 |
// Reinstate the tBody. |
| 260 |
table.insertBefore(tBody, nextSibling); |
| 261 |
}); |
| 262 |
|
| 263 |
// Mark this as the last sorted column. |
| 264 |
this.header_ = /** @type {!HTMLTableCellElement} */ |
| 265 |
(table.tHead.rows[this.sortableHeaderRowIndex_].cells[column]); |
| 266 |
|
| 267 |
// Update the header class. |
| 268 |
goog.dom.classlist.add( |
| 269 |
this.header_, this.reversed_ ? |
| 270 |
goog.getCssName('goog-tablesorter-sorted-reverse') : |
| 271 |
goog.getCssName('goog-tablesorter-sorted')); |
| 272 |
|
| 273 |
return true; |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
/** |
| 278 |
* Disables sorting on the specified column |
| 279 |
* @param {*} a First sort value. |
| 280 |
* @param {*} b Second sort value. |
| 281 |
* @return {number} Negative if a < b, 0 if a = b, and positive if a > b. |
| 282 |
*/ |
| 283 |
goog.ui.TableSorter.noSort = goog.functions.error('no sort'); |
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* A numeric sort function. NaN values (or values that do not parse as float |
| 288 |
* numbers) compare equal to each other and greater to any other number. |
| 289 |
* @param {*} a First sort value. |
| 290 |
* @param {*} b Second sort value. |
| 291 |
* @return {number} Negative if a < b, 0 if a = b, and positive if a > b. |
| 292 |
*/ |
| 293 |
goog.ui.TableSorter.numericSort = function(a, b) { |
| 294 |
a = parseFloat(a); |
| 295 |
b = parseFloat(b); |
| 296 |
// foo == foo is false if and only if foo is NaN. |
| 297 |
if (a == a) { |
| 298 |
return b == b ? a - b : -1; |
| 299 |
} else { |
| 300 |
return b == b ? 1 : 0; |
| 301 |
} |
| 302 |
}; |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* Alphabetic sort function. |
| 307 |
* @param {*} a First sort value. |
| 308 |
* @param {*} b Second sort value. |
| 309 |
* @return {number} Negative if a < b, 0 if a = b, and positive if a > b. |
| 310 |
*/ |
| 311 |
goog.ui.TableSorter.alphaSort = goog.array.defaultCompare; |
| 312 |
|
| 313 |
|
| 314 |
/** |
| 315 |
* Returns a function that is the given sort function in reverse. |
| 316 |
* @param {function(*, *) : number} sortFunction The original sort function. |
| 317 |
* @return {function(*, *) : number} A new sort function that reverses the |
| 318 |
* given sort function. |
| 319 |
*/ |
| 320 |
goog.ui.TableSorter.createReverseSort = function(sortFunction) { |
| 321 |
return function(a, b) { return -1 * sortFunction(a, b); }; |
| 322 |
}; |