Debian Codegraph @

js/widgets/widget-formatter.js

raw symbols SCIP
1 /*! Widget: formatter - 2/9/2015 (v2.19.1) *//*
2 * Requires tablesorter v2.8+ and jQuery 1.7+
3 * by Rob Garrison
4 */
5 /*jshint browser:true, jquery:true, unused:false */
6 /*global jQuery: false */
7 ;(function($) {
8 'use strict';
9 var ts = $.tablesorter;
10
11 ts.formatter = {
12 init : function( c ) {
13 var events = c.widgetOptions.formatter_event +
14 ' pagerComplete updateComplete '.split(' ').join('.tsformatter ');
15 c.$table
16 .off( events.replace(/\s+/g, ' ') )
17 .on( events, function() {
18 ts.formatter.setup( c );
19 });
20 ts.formatter.setup( c );
21 },
22 setup : function( c ) {
23 // do nothing for empty tables
24 if ( $.isEmptyObject( c.cache ) ) { return; }
25 var $tbody, tbodyIndex, rowIndex, rows, cell, len, column,
26 wo = c.widgetOptions,
27 data = { config: c, wo: wo },
28 formatter = [],
29 $headers = [];
30 // set up variables
31 for ( column = 0; column < c.columns; column++ ) {
32 $headers[ column ] = c.$headerIndexed[ column ];
33 formatter[ column ] = ts.getColumnData( c.table, wo.formatter_column, column ) || false;
34 }
35 // main loop
36 for ( tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++ ) {
37 $tbody = ts.processTbody( c.table, c.$tbodies.eq( tbodyIndex ), true ); // detach tbody
38 rows = c.cache[ tbodyIndex ];
39 len = rows.normalized.length;
40 for ( rowIndex = 0; rowIndex < len; rowIndex++ ) {
41 data.$row = rows.normalized[ rowIndex ][ c.columns ].$row;
42 data.$cells = data.$row.children( 'th, td' );
43 for ( column = 0; column < c.columns; column++ ) {
44 if ( formatter[ column ] ) {
45 data.columnIndex = column;
46 data.$header = $headers[ column ];
47 data.$cell = data.$cells.eq( column );
48 cell = data.$cell[0];
49 // get text from attribute first, just in case we're updating
50 data.text = cell.getAttribute( c.textAttribute ) || cell.textContent || data.$cell.text();
51 cell.innerHTML = formatter[ column ]( data.text, data );
52 }
53 }
54 }
55 ts.processTbody( c.table, $tbody, false); // restore tbody
56 }
57 }
58 };
59
60 ts.addWidget({
61 id: 'formatter',
62 priority: 100,
63 options: {
64 formatter_column : {},
65 formatter_event : 'applyFormatter'
66 },
67 init: function( table ) {
68 ts.formatter.init( table.config );
69 }
70 });
71
72 })( jQuery );