2016-03-02 05:00:22 +00:00
|
|
|
/*! Widget: editable - updated 3/1/2016 (v2.25.5) *//*
|
2013-04-13 01:39:07 +00:00
|
|
|
* Requires tablesorter v2.8+ and jQuery 1.7+
|
|
|
|
* by Rob Garrison
|
|
|
|
*/
|
2013-04-13 02:15:57 +00:00
|
|
|
/*jshint browser:true, jquery:true, unused:false */
|
2013-04-13 01:39:07 +00:00
|
|
|
/*global jQuery: false */
|
2016-02-22 03:50:11 +00:00
|
|
|
;( function( $ ) {
|
2014-09-27 16:04:13 +00:00
|
|
|
'use strict';
|
2013-04-13 01:39:07 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
var tse = $.tablesorter.editable = {
|
|
|
|
namespace : '.tseditable',
|
|
|
|
// last edited class name
|
|
|
|
lastEdited: 'tseditable-last-edited-cell',
|
2014-09-27 16:04:13 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
editComplete: function( c, wo, $cell, refocus ) {
|
|
|
|
c.$table
|
|
|
|
.find( '.' + tse.lastEdited )
|
|
|
|
.removeClass( tse.lastEdited )
|
|
|
|
.trigger( wo.editable_editComplete, [ c ] );
|
|
|
|
// restore focus last cell after updating
|
|
|
|
if ( refocus ) {
|
|
|
|
setTimeout( function() {
|
|
|
|
$cell.focus();
|
|
|
|
}, 50 );
|
2015-04-26 14:35:06 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
},
|
2014-09-27 16:04:13 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
selectAll: function( cell ) {
|
|
|
|
setTimeout( function() {
|
2016-01-22 22:52:35 +00:00
|
|
|
if ( document.queryCommandSupported( 'SelectAll' ) ) {
|
|
|
|
document.execCommand( 'selectAll', false, null );
|
|
|
|
} else {
|
|
|
|
// select all text in contenteditable
|
|
|
|
// see http://stackoverflow.com/a/6150060/145346
|
|
|
|
var range, selection;
|
|
|
|
if ( document.body.createTextRange ) {
|
|
|
|
range = document.body.createTextRange();
|
|
|
|
range.moveToElementText( cell );
|
|
|
|
range.select();
|
|
|
|
} else if ( window.getSelection ) {
|
|
|
|
selection = window.getSelection();
|
|
|
|
range = document.createRange();
|
|
|
|
range.selectNodeContents( cell );
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange( range );
|
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
}
|
2016-02-15 20:19:36 +00:00
|
|
|
// need delay of at least 100ms or last contenteditable will get refocused
|
2015-07-23 04:29:51 +00:00
|
|
|
}, 100 );
|
|
|
|
},
|
|
|
|
|
|
|
|
getColumns : function( c, wo ) {
|
2015-07-31 01:19:32 +00:00
|
|
|
var list, indx, range, len, tmp,
|
|
|
|
editableColumns = wo.editable_columns,
|
2015-07-23 04:29:51 +00:00
|
|
|
cols = [];
|
2015-07-31 01:19:32 +00:00
|
|
|
if ( typeof editableColumns === 'string' ) {
|
|
|
|
// editable_columns can contain a range string, or comma separated values (e.g. '1,2-4,7')
|
|
|
|
list = editableColumns.replace( /\s+/, '' ).split( /,/ );
|
|
|
|
len = list.length - 1;
|
|
|
|
while ( len >= 0 ) {
|
|
|
|
if ( list[ len ].indexOf( '-' ) >= 0 ) {
|
|
|
|
range = list[ len ].split( '-' );
|
|
|
|
indx = parseInt( range[ 0 ], 10 ) || 0;
|
|
|
|
range = parseInt( range[ 1 ], 10) || c.columns - 1;
|
|
|
|
if ( indx > range ) {
|
|
|
|
// in case someone does '5-3'
|
|
|
|
tmp = indx; indx = range; range = tmp;
|
|
|
|
}
|
|
|
|
for ( ; indx <= range; indx++ ) {
|
|
|
|
cols.push( 'td:nth-child(' + ( indx + 1 ) + ')' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cols.push( 'td:nth-child(' + ( ( parseInt( list[ len ], 10 ) || 0 ) + 1 ) + ')' );
|
|
|
|
}
|
|
|
|
len--;
|
2015-07-23 04:29:51 +00:00
|
|
|
}
|
2015-07-31 01:19:32 +00:00
|
|
|
} else if ( $.isArray( editableColumns ) ) {
|
|
|
|
len = editableColumns.length;
|
|
|
|
for ( indx = 0; indx < len; indx++ ) {
|
|
|
|
if ( editableColumns[ indx ] < c.columns ) {
|
|
|
|
cols.push( 'td:nth-child(' + ( editableColumns[ indx ] + 1 ) + ')' );
|
2015-07-23 04:29:51 +00:00
|
|
|
}
|
2015-07-31 01:19:32 +00:00
|
|
|
}
|
2014-09-27 16:04:13 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
return cols;
|
|
|
|
},
|
2015-05-16 23:19:12 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
update: function( c, wo ) {
|
2015-07-31 01:19:32 +00:00
|
|
|
var $t, $cells, cellIndex, cellLen, $editable, editableIndex, editableLen,
|
2015-07-23 04:29:51 +00:00
|
|
|
tmp = $( '<div>' ).wrapInner( wo.editable_wrapContent ).children().length || $.isFunction( wo.editable_wrapContent ),
|
|
|
|
cols = tse.getColumns( c, wo ).join( ',' );
|
2015-05-16 23:19:12 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
// turn off contenteditable to allow dynamically setting the wo.editable_noEdit
|
|
|
|
// class on table cells - see issue #900
|
|
|
|
c.$tbodies.find( cols ).find( '[contenteditable]' ).prop( 'contenteditable', false );
|
2015-05-16 23:19:12 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
// IE does not allow making TR/TH/TD cells directly editable ( issue #404 )
|
|
|
|
// so add a div or span inside ( it's faster than using wrapInner() )
|
2015-07-31 01:19:32 +00:00
|
|
|
$cells = c.$tbodies.find( cols ).not( '.' + wo.editable_noEdit );
|
|
|
|
cellLen = $cells.length;
|
|
|
|
for ( cellIndex = 0; cellIndex < cellLen; cellIndex++ ) {
|
|
|
|
/*jshint loopfunc:true */
|
2015-07-23 04:29:51 +00:00
|
|
|
// test for children, if they exist, then make the children editable
|
2015-07-31 01:19:32 +00:00
|
|
|
$t = $cells.eq( cellIndex );
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( tmp && $t.children( 'div, span' ).length === 0 ) {
|
|
|
|
$t.wrapInner( wo.editable_wrapContent );
|
|
|
|
}
|
2015-07-31 01:19:32 +00:00
|
|
|
$editable = $t.children( 'div, span' ).not( '.' + wo.editable_noEdit );
|
|
|
|
editableLen = $editable.length;
|
|
|
|
if ( editableLen ) {
|
2015-07-23 04:29:51 +00:00
|
|
|
// make div/span children content editable
|
2015-07-31 01:19:32 +00:00
|
|
|
for ( editableIndex = 0; editableIndex < editableLen; editableIndex++ ) {
|
|
|
|
var $this = $editable.eq( editableIndex );
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( wo.editable_trimContent ) {
|
|
|
|
$this.html( function( i, txt ) {
|
|
|
|
return $.trim( txt );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$this.prop( 'contenteditable', true );
|
2015-07-31 01:19:32 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
} else {
|
2014-09-27 16:04:13 +00:00
|
|
|
if ( wo.editable_trimContent ) {
|
2015-07-23 04:29:51 +00:00
|
|
|
$t.html( function( i, txt ) {
|
2014-09-27 16:04:13 +00:00
|
|
|
return $.trim( txt );
|
|
|
|
});
|
2014-07-25 12:50:36 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
$t.prop( 'contenteditable', true );
|
2014-08-27 00:24:43 +00:00
|
|
|
}
|
2015-07-31 01:19:32 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
},
|
2015-04-26 14:35:06 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
bindEvents: function( c, wo ) {
|
|
|
|
var namespace = tse.namespace;
|
|
|
|
c.$table
|
|
|
|
.off( ( 'updateComplete pagerComplete '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' ) )
|
|
|
|
.on( 'updateComplete pagerComplete '.split( ' ' ).join( namespace + ' ' ), function() {
|
|
|
|
tse.update( c, c.widgetOptions );
|
|
|
|
})
|
|
|
|
// prevent sort initialized by user click on the header from changing the row indexing before
|
|
|
|
// updateCell can finish processing the change
|
|
|
|
.children( 'thead' )
|
|
|
|
.add( $( c.namespace + '_extra_table' ).children( 'thead' ) )
|
|
|
|
.off( 'mouseenter' + namespace )
|
|
|
|
.on( 'mouseenter' + namespace, function() {
|
|
|
|
if ( c.$table.data( 'contentFocused' ) ) {
|
|
|
|
// change to 'true' instead of element to allow focusout to process
|
|
|
|
c.$table.data( 'contentFocused', true );
|
|
|
|
$( ':focus' ).trigger( 'focusout' );
|
2013-04-13 01:39:07 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
});
|
2014-08-26 21:19:47 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
c.$tbodies
|
|
|
|
.off( ( 'focus blur focusout keydown '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' ) )
|
|
|
|
.on( 'focus' + namespace, '[contenteditable]', function( e ) {
|
|
|
|
clearTimeout( $( this ).data( 'timer' ) );
|
|
|
|
c.$table.data( 'contentFocused', e.target );
|
|
|
|
c.table.isUpdating = true; // prevent sorting while editing
|
|
|
|
var $this = $( this ),
|
|
|
|
selAll = wo.editable_selectAll,
|
|
|
|
column = $this.closest( 'td' ).index(),
|
|
|
|
txt = $this.html();
|
|
|
|
if ( wo.editable_trimContent ) {
|
|
|
|
txt = $.trim( txt );
|
|
|
|
}
|
|
|
|
// prevent enter from adding into the content
|
|
|
|
$this
|
|
|
|
.off( 'keydown' + namespace )
|
2016-02-22 03:50:11 +00:00
|
|
|
.on( 'keydown' + namespace, function( e ) {
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( wo.editable_enterToAccept && e.which === 13 && !e.shiftKey ) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$this.data({ before : txt, original: txt });
|
2014-09-27 16:04:13 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( typeof wo.editable_focused === 'function' ) {
|
|
|
|
wo.editable_focused( txt, column, $this );
|
2014-08-26 21:19:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( selAll ) {
|
|
|
|
if ( typeof selAll === 'function' ) {
|
|
|
|
if ( selAll( txt, column, $this ) ) {
|
|
|
|
tse.selectAll( $this[0] );
|
2014-08-26 21:19:47 +00:00
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
} else {
|
|
|
|
tse.selectAll( $this[0] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on( 'blur focusout keydown '.split( ' ' ).join( namespace + ' ' ), '[contenteditable]', function( e ) {
|
|
|
|
if ( !c.$table.data( 'contentFocused' ) ) { return; }
|
|
|
|
var t, validate,
|
|
|
|
valid = false,
|
|
|
|
$this = $( e.target ),
|
|
|
|
txt = $this.html(),
|
|
|
|
column = $this.closest( 'td' ).index();
|
|
|
|
if ( wo.editable_trimContent ) {
|
|
|
|
txt = $.trim( txt );
|
|
|
|
}
|
|
|
|
if ( e.which === 27 ) {
|
|
|
|
// user cancelled
|
2015-10-22 21:41:34 +00:00
|
|
|
$this
|
|
|
|
.html( $this.data( 'original' ) )
|
|
|
|
.trigger( 'blur' + namespace );
|
2015-07-23 04:29:51 +00:00
|
|
|
c.$table.data( 'contentFocused', false );
|
|
|
|
c.table.isUpdating = false;
|
2013-04-13 01:39:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
// accept on enter ( if set ), alt-enter ( always ) or if autoAccept is set and element is blurred or unfocused
|
|
|
|
t = e.which === 13 && !e.shiftKey && ( wo.editable_enterToAccept || e.altKey ) || wo.editable_autoAccept && e.type !== 'keydown';
|
|
|
|
// change if new or user hits enter ( if option set )
|
|
|
|
if ( t && $this.data( 'before' ) !== txt ) {
|
|
|
|
|
|
|
|
validate = wo.editable_validate;
|
|
|
|
valid = txt;
|
2015-06-27 02:57:32 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( typeof validate === 'function' ) {
|
|
|
|
valid = validate( txt, $this.data( 'original' ), column, $this );
|
|
|
|
} else if ( typeof ( validate = $.tablesorter.getColumnData( c.table, validate, column ) ) === 'function' ) {
|
|
|
|
valid = validate( txt, $this.data( 'original' ), column, $this );
|
2014-08-26 19:23:13 +00:00
|
|
|
}
|
2015-05-16 23:19:12 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( t && valid !== false ) {
|
|
|
|
c.$table.find( '.' + tse.lastEdited ).removeClass( tse.lastEdited );
|
|
|
|
$this
|
|
|
|
.addClass( tse.lastEdited )
|
|
|
|
.html( valid )
|
|
|
|
.data( 'before', valid )
|
|
|
|
.data( 'original', valid )
|
|
|
|
.trigger( 'change' );
|
2015-12-11 05:03:34 +00:00
|
|
|
// prevent error if table was destroyed - see #1099
|
|
|
|
if ( c.table.hasInitialized ) {
|
|
|
|
$.tablesorter.updateCell( c, $this.closest( 'td' ), false, function() {
|
|
|
|
if ( wo.editable_autoResort ) {
|
|
|
|
setTimeout( function() {
|
|
|
|
$.tablesorter.sortOn( c, c.sortList, function() {
|
|
|
|
tse.editComplete( c, wo, c.$table.data( 'contentFocused' ), true );
|
|
|
|
}, true );
|
|
|
|
}, 10 );
|
|
|
|
} else {
|
|
|
|
tse.editComplete( c, wo, c.$table.data( 'contentFocused' ) );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-23 04:29:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ( !valid && e.type !== 'keydown' ) {
|
|
|
|
clearTimeout( $this.data( 'timer' ) );
|
|
|
|
$this.data( 'timer', setTimeout( function() {
|
|
|
|
c.table.isUpdating = false; // clear flag or sorting will be disabled
|
2015-05-16 23:19:12 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
if ( $.isFunction( wo.editable_blur ) ) {
|
|
|
|
txt = $this.html();
|
|
|
|
wo.editable_blur( wo.editable_trimContent ? $.trim( txt ) : txt, column, $this );
|
|
|
|
}
|
|
|
|
}, 100 ) );
|
|
|
|
// restore original content on blur
|
|
|
|
$this.html( $this.data( 'original' ) );
|
|
|
|
}
|
2016-02-22 03:50:11 +00:00
|
|
|
})
|
|
|
|
// paste plain text from Excel - fixes #994
|
|
|
|
.on('paste' + namespace, '[contenteditable]', function() {
|
|
|
|
var content,
|
|
|
|
$this = $(this);
|
|
|
|
// setTimeout needed to get pasted-in content
|
|
|
|
setTimeout(function() {
|
|
|
|
if ($this.is(':focus')) {
|
|
|
|
content = '<div>' + $this.html() + '</div>';
|
|
|
|
$this.html( $(content).text().trim() );
|
|
|
|
}
|
|
|
|
}, 0);
|
2015-07-23 04:29:51 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
destroy : function( c, wo ) {
|
|
|
|
var namespace = tse.namespace,
|
|
|
|
cols = tse.getColumns( c, wo ),
|
|
|
|
|
|
|
|
tmp = ( 'updateComplete pagerComplete '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' );
|
|
|
|
c.$table.off( tmp );
|
|
|
|
|
2016-02-22 03:50:11 +00:00
|
|
|
tmp = ( 'focus blur focusout keydown paste '.split( ' ' ).join( namespace + ' ' ) ).replace( /\s+/g, ' ' );
|
2015-07-23 04:29:51 +00:00
|
|
|
c.$tbodies
|
|
|
|
.off( tmp )
|
|
|
|
.find( cols.join( ',' ) )
|
|
|
|
.find( '[contenteditable]' )
|
|
|
|
.prop( 'contenteditable', false );
|
|
|
|
}
|
2014-08-26 19:23:13 +00:00
|
|
|
|
2015-07-23 04:29:51 +00:00
|
|
|
};
|
2014-09-27 16:04:13 +00:00
|
|
|
|
|
|
|
$.tablesorter.addWidget({
|
|
|
|
id: 'editable',
|
|
|
|
options : {
|
|
|
|
editable_columns : [],
|
|
|
|
editable_enterToAccept : true,
|
|
|
|
editable_autoAccept : true,
|
|
|
|
editable_autoResort : false,
|
|
|
|
editable_wrapContent : '<div>', // wrap the cell content... makes this widget work in IE, and with autocomplete
|
|
|
|
editable_trimContent : true, // trim content inside of contenteditable ( remove tabs & carriage returns )
|
2016-02-22 03:50:11 +00:00
|
|
|
editable_validate : null, // function( text, originalText ) { return text; }
|
2014-09-27 16:04:13 +00:00
|
|
|
editable_focused : null, // function( text, columnIndex, $element ) {}
|
|
|
|
editable_blur : null, // function( text, columnIndex, $element ) { }
|
|
|
|
editable_selectAll : false, // true/false or function( text, columnIndex, $element ) { return true; }
|
|
|
|
editable_noEdit : 'no-edit',
|
|
|
|
editable_editComplete : 'editComplete'
|
|
|
|
},
|
2016-02-22 03:50:11 +00:00
|
|
|
init: function( table, thisWidget, c, wo ) {
|
2014-09-27 16:04:13 +00:00
|
|
|
if ( !wo.editable_columns.length ) { return; }
|
|
|
|
tse.update( c, wo );
|
|
|
|
tse.bindEvents( c, wo );
|
2015-05-16 23:19:12 +00:00
|
|
|
},
|
|
|
|
remove : function( table, c, wo, refreshing ) {
|
|
|
|
if ( !refreshing ) {
|
|
|
|
tse.destroy( c, wo ) ;
|
|
|
|
}
|
2013-04-13 01:39:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-27 16:04:13 +00:00
|
|
|
})( jQuery );
|