| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
;( function( $ ) { |
| 8 |
'use strict'; |
| 9 |
|
| 10 |
var tse = $.tablesorter.editable = { |
| 11 |
namespace : '.tseditable', |
| 12 |
|
| 13 |
lastEdited: 'tseditable-last-edited-cell', |
| 14 |
|
| 15 |
editComplete: function( c, wo, $cell, refocus ) { |
| 16 |
c.$table |
| 17 |
.find( '.' + tse.lastEdited ) |
| 18 |
.removeClass( tse.lastEdited ) |
| 19 |
.trigger( wo.editable_editComplete, [ c ] ); |
| 20 |
|
| 21 |
if ( refocus ) { |
| 22 |
setTimeout( function() { |
| 23 |
$cell.focus(); |
| 24 |
}, 50 ); |
| 25 |
} |
| 26 |
}, |
| 27 |
|
| 28 |
selectAll: function( cell ) { |
| 29 |
setTimeout( function() { |
| 30 |
if ( document.queryCommandSupported( 'SelectAll' ) ) { |
| 31 |
document.execCommand( 'selectAll', false, null ); |
| 32 |
} else { |
| 33 |
|
| 34 |
|
| 35 |
var range, selection; |
| 36 |
if ( document.body.createTextRange ) { |
| 37 |
range = document.body.createTextRange(); |
| 38 |
range.moveToElementText( cell ); |
| 39 |
range.select(); |
| 40 |
} else if ( window.getSelection ) { |
| 41 |
selection = window.getSelection(); |
| 42 |
range = document.createRange(); |
| 43 |
range.selectNodeContents( cell ); |
| 44 |
selection.removeAllRanges(); |
| 45 |
selection.addRange( range ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
}, 100 ); |
| 50 |
}, |
| 51 |
|
| 52 |
getColumns : function( c, wo ) { |
| 53 |
var list, indx, range, len, tmp, |
| 54 |
editableColumns = wo.editable_columns, |
| 55 |
cols = []; |
| 56 |
if ( typeof editableColumns === 'string' ) { |
| 57 |
|
| 58 |
list = editableColumns.replace( /\s+/, '' ).split( /,/ ); |
| 59 |
len = list.length - 1; |
| 60 |
while ( len >= 0 ) { |
| 61 |
if ( list[ len ].indexOf( '-' ) >= 0 ) { |
| 62 |
range = list[ len ].split( '-' ); |
| 63 |
indx = parseInt( range[ 0 ], 10 ) || 0; |
| 64 |
range = parseInt( range[ 1 ], 10) || c.columns - 1; |
| 65 |
if ( indx > range ) { |
| 66 |
|
| 67 |
tmp = indx; indx = range; range = tmp; |
| 68 |
} |
| 69 |
for ( ; indx <= range; indx++ ) { |
| 70 |
cols.push( 'td:nth-child(' + ( indx + 1 ) + ')' ); |
| 71 |
} |
| 72 |
} else { |
| 73 |
cols.push( 'td:nth-child(' + ( ( parseInt( list[ len ], 10 ) || 0 ) + 1 ) + ')' ); |
| 74 |
} |
| 75 |
len--; |
| 76 |
} |
| 77 |
} else if ( $.isArray( editableColumns ) ) { |
| 78 |
len = editableColumns.length; |
| 79 |
for ( indx = 0; indx < len; indx++ ) { |
| 80 |
if ( editableColumns[ indx ] < c.columns ) { |
| 81 |
cols.push( 'td:nth-child(' + ( editableColumns[ indx ] + 1 ) + ')' ); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
return cols; |
| 86 |
}, |
| 87 |
|
| 88 |
trimContent: function( wo, $cell ) { |
| 89 |
if ( wo.editable_trimContent ) { |
| 90 |
var html = $cell.html(); |
| 91 |
if (html.trim() !== html) { |
| 92 |
|
| 93 |
$cell.html( html === '' ? ' ' : html ); |
| 94 |
} |
| 95 |
} |
| 96 |
}, |
| 97 |
|
| 98 |
update: function( c, wo ) { |
| 99 |
var $t, $cells, cellIndex, cellLen, $editable, editableIndex, editableLen, |
| 100 |
tmp = $( '<div>' ).wrapInner( wo.editable_wrapContent ).children().length || $.isFunction( wo.editable_wrapContent ), |
| 101 |
cols = tse.getColumns( c, wo ).join( ',' ); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
c.$tbodies.find( cols ).find( '[contenteditable]' ).prop( 'contenteditable', false ); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
$cells = c.$tbodies.find( cols ).not( '.' + wo.editable_noEdit ); |
| 110 |
cellLen = $cells.length; |
| 111 |
for ( cellIndex = 0; cellIndex < cellLen; cellIndex++ ) { |
| 112 |
|
| 113 |
|
| 114 |
$t = $cells.eq( cellIndex ); |
| 115 |
if ( tmp && $t.children( 'div, span' ).length === 0 ) { |
| 116 |
$t.wrapInner( wo.editable_wrapContent ); |
| 117 |
if ($t.children().text().trim() === '') { |
| 118 |
$t.children().html(' '); |
| 119 |
} |
| 120 |
} |
| 121 |
$editable = $t.children( 'div, span' ).not( '.' + wo.editable_noEdit ); |
| 122 |
editableLen = $editable.length; |
| 123 |
if ( editableLen ) { |
| 124 |
|
| 125 |
for ( editableIndex = 0; editableIndex < editableLen; editableIndex++ ) { |
| 126 |
var $this = $editable.eq( editableIndex ); |
| 127 |
tse.trimContent( wo, $this ); |
| 128 |
$this.prop( 'contenteditable', true ); |
| 129 |
} |
| 130 |
} else { |
| 131 |
tse.trimContent( wo, $t ); |
| 132 |
$t.prop( 'contenteditable', true ); |
| 133 |
} |
| 134 |
} |
| 135 |
}, |
| 136 |
|
| 137 |
bindEvents: function( c, wo ) { |
| 138 |
var namespace = tse.namespace; |
| 139 |
c.$table |
| 140 |
.off( ( 'updateComplete pagerComplete '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' ) ) |
| 141 |
.on( 'updateComplete pagerComplete '.split( ' ' ).join( namespace + ' ' ), function() { |
| 142 |
tse.update( c, c.widgetOptions ); |
| 143 |
}) |
| 144 |
|
| 145 |
|
| 146 |
.children( 'thead' ) |
| 147 |
.add( $( c.namespace + '_extra_table' ).children( 'thead' ) ) |
| 148 |
.off( 'mouseenter' + namespace ) |
| 149 |
.on( 'mouseenter' + namespace, function() { |
| 150 |
if ( c.$table.data( 'contentFocused' ) ) { |
| 151 |
|
| 152 |
c.$table.data( 'contentFocused', true ); |
| 153 |
$( ':focus' ).trigger( 'focusout' ); |
| 154 |
} |
| 155 |
}); |
| 156 |
|
| 157 |
c.$tbodies |
| 158 |
.off( ( 'focus focusout keydown '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' ) ) |
| 159 |
.on( 'focus' + namespace, '[contenteditable]', function( e ) { |
| 160 |
clearTimeout( $( this ).data( 'timer' ) ); |
| 161 |
c.$table.data( 'contentFocused', e.target ); |
| 162 |
c.table.isUpdating = true; |
| 163 |
var $this = $( this ), |
| 164 |
selAll = wo.editable_selectAll, |
| 165 |
column = $this.closest( 'td' ).index(), |
| 166 |
txt = $this.html(); |
| 167 |
if ( wo.editable_trimContent ) { |
| 168 |
txt = $.trim( txt === '' ? ' ' : txt ); |
| 169 |
} |
| 170 |
|
| 171 |
$this |
| 172 |
.off( 'keydown' + namespace ) |
| 173 |
.on( 'keydown' + namespace, function( e ) { |
| 174 |
if ( wo.editable_enterToAccept && e.which === 13 && !e.shiftKey ) { |
| 175 |
e.preventDefault(); |
| 176 |
} |
| 177 |
}); |
| 178 |
$this.data({ before : txt, original: txt }); |
| 179 |
|
| 180 |
if ( typeof wo.editable_focused === 'function' ) { |
| 181 |
wo.editable_focused( txt, column, $this ); |
| 182 |
} |
| 183 |
|
| 184 |
if ( selAll ) { |
| 185 |
if ( typeof selAll === 'function' ) { |
| 186 |
if ( selAll( txt, column, $this ) ) { |
| 187 |
tse.selectAll( $this[0] ); |
| 188 |
} |
| 189 |
} else { |
| 190 |
tse.selectAll( $this[0] ); |
| 191 |
} |
| 192 |
} |
| 193 |
}) |
| 194 |
.on( 'focusout keydown '.split( ' ' ).join( namespace + ' ' ), '[contenteditable]', function( e ) { |
| 195 |
if ( !c.$table.data( 'contentFocused' ) ) { return; } |
| 196 |
var t, validate, |
| 197 |
valid = false, |
| 198 |
$this = $( e.target ), |
| 199 |
txt = $this.html(), |
| 200 |
column = $this.closest( 'td' ).index(); |
| 201 |
if ( wo.editable_trimContent ) { |
| 202 |
txt = $.trim( txt === '' ? ' ' : txt ); |
| 203 |
} |
| 204 |
if ( e.which === 27 ) { |
| 205 |
|
| 206 |
$this |
| 207 |
.html( $this.data( 'original' ) ) |
| 208 |
.trigger( 'blur' + namespace ); |
| 209 |
c.$table.data( 'contentFocused', false ); |
| 210 |
c.table.isUpdating = false; |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
t = e.which === 13 && !e.shiftKey && ( wo.editable_enterToAccept || e.altKey ) || wo.editable_autoAccept && e.type !== 'keydown'; |
| 215 |
|
| 216 |
if ( t && $this.data( 'before' ) !== txt ) { |
| 217 |
|
| 218 |
validate = wo.editable_validate; |
| 219 |
valid = txt; |
| 220 |
|
| 221 |
if ( typeof validate === 'function' ) { |
| 222 |
valid = validate( txt, $this.data( 'original' ), column, $this ); |
| 223 |
} else if ( typeof ( validate = $.tablesorter.getColumnData( c.table, validate, column ) ) === 'function' ) { |
| 224 |
valid = validate( txt, $this.data( 'original' ), column, $this ); |
| 225 |
} |
| 226 |
|
| 227 |
if ( t && valid !== false ) { |
| 228 |
c.$table.find( '.' + tse.lastEdited ).removeClass( tse.lastEdited ); |
| 229 |
$this |
| 230 |
.addClass( tse.lastEdited ) |
| 231 |
.html( valid ) |
| 232 |
.data( 'before', valid ) |
| 233 |
.data( 'original', valid ) |
| 234 |
.trigger( 'change' ); |
| 235 |
|
| 236 |
if ( c.table.hasInitialized ) { |
| 237 |
$.tablesorter.updateCell( c, $this.closest( 'td' ), false, function() { |
| 238 |
if ( wo.editable_autoResort ) { |
| 239 |
setTimeout( function() { |
| 240 |
$.tablesorter.sortOn( c, c.sortList, function() { |
| 241 |
tse.editComplete( c, wo, c.$table.data( 'contentFocused' ), true ); |
| 242 |
}, true ); |
| 243 |
}, 10 ); |
| 244 |
} else { |
| 245 |
tse.editComplete( c, wo, c.$table.data( 'contentFocused' ) ); |
| 246 |
} |
| 247 |
}); |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
} else if ( !valid && e.type !== 'keydown' ) { |
| 252 |
clearTimeout( $this.data( 'timer' ) ); |
| 253 |
$this.data( 'timer', setTimeout( function() { |
| 254 |
c.table.isUpdating = false; |
| 255 |
|
| 256 |
if ( $.isFunction( wo.editable_blur ) ) { |
| 257 |
txt = $this.html(); |
| 258 |
wo.editable_blur( wo.editable_trimContent ? $.trim( txt ) : txt, column, $this ); |
| 259 |
} |
| 260 |
}, 100 ) ); |
| 261 |
|
| 262 |
$this.html( $this.data( 'original' ) ); |
| 263 |
} |
| 264 |
}) |
| 265 |
|
| 266 |
.on('paste' + namespace, '[contenteditable]', function() { |
| 267 |
var content, |
| 268 |
$this = $(this); |
| 269 |
|
| 270 |
setTimeout(function() { |
| 271 |
if ($this.is(':focus')) { |
| 272 |
content = '<div>' + $this.html() + '</div>'; |
| 273 |
$this.html( $(content).text().trim() ); |
| 274 |
} |
| 275 |
}, 0); |
| 276 |
}); |
| 277 |
}, |
| 278 |
destroy : function( c, wo ) { |
| 279 |
var namespace = tse.namespace, |
| 280 |
cols = tse.getColumns( c, wo ), |
| 281 |
|
| 282 |
tmp = ( 'updateComplete pagerComplete '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' ); |
| 283 |
c.$table.off( tmp ); |
| 284 |
|
| 285 |
tmp = ( 'focus focusout keydown paste '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' ); |
| 286 |
c.$tbodies |
| 287 |
.off( tmp ) |
| 288 |
.find( cols.join( ',' ) ) |
| 289 |
.find( '[contenteditable]' ) |
| 290 |
.prop( 'contenteditable', false ); |
| 291 |
} |
| 292 |
|
| 293 |
}; |
| 294 |
|
| 295 |
$.tablesorter.addWidget({ |
| 296 |
id: 'editable', |
| 297 |
options : { |
| 298 |
editable_columns : [], |
| 299 |
editable_enterToAccept : true, |
| 300 |
editable_autoAccept : true, |
| 301 |
editable_autoResort : false, |
| 302 |
editable_wrapContent : '<div>', |
| 303 |
editable_trimContent : true, |
| 304 |
editable_validate : null, |
| 305 |
editable_focused : null, |
| 306 |
editable_blur : null, |
| 307 |
editable_selectAll : false, |
| 308 |
editable_noEdit : 'no-edit', |
| 309 |
editable_editComplete : 'editComplete' |
| 310 |
}, |
| 311 |
init: function( table, thisWidget, c, wo ) { |
| 312 |
if ( !wo.editable_columns.length ) { return; } |
| 313 |
tse.update( c, wo ); |
| 314 |
tse.bindEvents( c, wo ); |
| 315 |
}, |
| 316 |
remove : function( table, c, wo, refreshing ) { |
| 317 |
if ( !refreshing ) { |
| 318 |
tse.destroy( c, wo ) ; |
| 319 |
} |
| 320 |
} |
| 321 |
}); |
| 322 |
|
| 323 |
})( jQuery ); |