2014-08-02 02:31:33 +00:00
|
|
|
|
/*! tablesorter Editable Content widget - updated 8/1/2014 (core v2.17.6)
|
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 */
|
|
|
|
|
;(function($){
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
$.tablesorter.addWidget({
|
|
|
|
|
id: 'editable',
|
|
|
|
|
options : {
|
|
|
|
|
editable_columns : [],
|
|
|
|
|
editable_enterToAccept : true,
|
2014-07-28 00:28:08 +00:00
|
|
|
|
editable_autoAccept : true,
|
2013-04-13 01:39:07 +00:00
|
|
|
|
editable_autoResort : false,
|
2014-08-27 00:24:43 +00:00
|
|
|
|
editable_wrapContent : '<div>', // wrap the cell content... makes this widget work in IE, and with autocomplete
|
|
|
|
|
editable_validate : null, // function(text, originalText){ return text; }
|
|
|
|
|
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; }
|
2013-10-12 04:07:26 +00:00
|
|
|
|
editable_noEdit : 'no-edit',
|
|
|
|
|
editable_editComplete : 'editComplete'
|
2013-04-13 01:39:07 +00:00
|
|
|
|
},
|
|
|
|
|
init: function(table, thisWidget, c, wo){
|
2014-07-28 00:28:08 +00:00
|
|
|
|
if ( !wo.editable_columns.length ) { return; }
|
|
|
|
|
var indx, tmp, $t,
|
2014-08-26 18:33:56 +00:00
|
|
|
|
cols = [],
|
|
|
|
|
editComplete = function($cell){
|
|
|
|
|
$cell
|
|
|
|
|
.removeClass('tseditable-last-edited-cell')
|
|
|
|
|
.trigger( wo.editable_editComplete, [c] );
|
|
|
|
|
// restore focus last cell after updating
|
|
|
|
|
setTimeout(function(){
|
|
|
|
|
$cell.focus();
|
|
|
|
|
}, 50);
|
2014-08-26 21:19:47 +00:00
|
|
|
|
},
|
|
|
|
|
selectAll = function(cell){
|
|
|
|
|
setTimeout(function(){
|
|
|
|
|
// select all text in contenteditable
|
|
|
|
|
// see http://stackoverflow.com/a/6150060/145346
|
|
|
|
|
var range = document.createRange();
|
|
|
|
|
range.selectNodeContents(cell);
|
|
|
|
|
var sel = window.getSelection();
|
|
|
|
|
sel.removeAllRanges();
|
|
|
|
|
sel.addRange(range);
|
|
|
|
|
}, 100);
|
2014-08-26 18:33:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-07-28 00:28:08 +00:00
|
|
|
|
if ( $.type(wo.editable_columns) === "string" && wo.editable_columns.indexOf('-') >= 0 ) {
|
2013-11-25 12:20:12 +00:00
|
|
|
|
// editable_columns can contain a range string (i.e. "2-4" )
|
|
|
|
|
tmp = wo.editable_columns.split('-');
|
|
|
|
|
indx = parseInt(tmp[0],10) || 0;
|
|
|
|
|
tmp = parseInt(tmp[1],10) || (c.columns - 1);
|
2014-07-28 00:28:08 +00:00
|
|
|
|
if ( tmp > c.columns ) { tmp = c.columns - 1; }
|
|
|
|
|
for ( ; indx <= tmp; indx++ ) {
|
2013-11-25 12:20:12 +00:00
|
|
|
|
cols.push('td:nth-child(' + (indx + 1) + ')');
|
|
|
|
|
}
|
2014-07-28 00:28:08 +00:00
|
|
|
|
} else if ( $.isArray(wo.editable_columns) ) {
|
2013-11-25 12:20:12 +00:00
|
|
|
|
$.each(wo.editable_columns, function(i, col){
|
2014-07-25 12:50:36 +00:00
|
|
|
|
if ( col < c.columns ) {
|
|
|
|
|
cols.push('td:nth-child(' + (col + 1) + ')');
|
|
|
|
|
}
|
2013-11-25 12:20:12 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2014-08-27 00:24:43 +00:00
|
|
|
|
tmp = $('<div>').wrapInner(wo.editable_wrapContent).children().length || $.isFunction(wo.editable_wrapContent);
|
2013-11-02 15:54:11 +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() )
|
2014-07-28 00:28:08 +00:00
|
|
|
|
c.$tbodies.find( cols.join(',') ).not( '.' + wo.editable_noEdit ).each(function(){
|
2013-11-02 15:54:11 +00:00
|
|
|
|
// test for children, if they exist, then make the children editable
|
|
|
|
|
$t = $(this);
|
2014-08-27 00:24:43 +00:00
|
|
|
|
|
|
|
|
|
if (tmp && $t.children().length === 0) {
|
|
|
|
|
$t.wrapInner( wo.editable_wrapContent );
|
|
|
|
|
}
|
|
|
|
|
if ($t.children().length) {
|
|
|
|
|
// make all children content editable
|
|
|
|
|
$t.children().not('.' + wo.editable_noEdit).prop( 'contenteditable', true );
|
|
|
|
|
} else {
|
|
|
|
|
$t.prop( 'contenteditable', true );
|
|
|
|
|
}
|
2013-11-02 15:54:11 +00:00
|
|
|
|
});
|
2013-04-13 01:39:07 +00:00
|
|
|
|
c.$tbodies
|
|
|
|
|
.on('mouseleave.tseditable', function(){
|
2014-07-28 00:28:08 +00:00
|
|
|
|
if ( c.$table.data('contentFocused') ) {
|
|
|
|
|
// change to "true" instead of element to allow focusout to process
|
|
|
|
|
c.$table.data( 'contentFocused', true );
|
2013-04-13 01:39:07 +00:00
|
|
|
|
$(':focus').trigger('blur');
|
|
|
|
|
}
|
|
|
|
|
})
|
2014-07-28 00:28:08 +00:00
|
|
|
|
.on('focus.tseditable', '[contenteditable]', function(e){
|
2014-08-26 21:19:47 +00:00
|
|
|
|
clearTimeout( $(this).data('timer') );
|
2014-07-28 00:28:08 +00:00
|
|
|
|
c.$table.data( 'contentFocused', e.target );
|
|
|
|
|
var $this = $(this),
|
2014-08-26 21:19:47 +00:00
|
|
|
|
selAll = wo.editable_selectAll,
|
|
|
|
|
column = $this.closest('td').index(),
|
2014-08-27 22:26:20 +00:00
|
|
|
|
txt = $.trim( $this.text() );
|
2013-04-13 01:39:07 +00:00
|
|
|
|
if (wo.editable_enterToAccept) {
|
|
|
|
|
// prevent enter from adding into the content
|
|
|
|
|
$this.on('keydown.tseditable', function(e){
|
2014-07-28 00:28:08 +00:00
|
|
|
|
if ( e.which === 13 ) {
|
2013-04-13 01:39:07 +00:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-08-26 21:19:47 +00:00
|
|
|
|
$this.data({ before : txt, original: txt });
|
|
|
|
|
|
|
|
|
|
if (typeof wo.editable_focused === 'function') {
|
|
|
|
|
wo.editable_focused( txt, column, $this );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selAll) {
|
|
|
|
|
if (typeof selAll === 'function') {
|
|
|
|
|
if ( selAll( txt, column, $this ) ) {
|
|
|
|
|
selectAll($this[0]);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
selectAll($this[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-13 01:39:07 +00:00
|
|
|
|
})
|
2014-07-28 00:28:08 +00:00
|
|
|
|
.on('blur focusout keydown '.split(' ').join('.tseditable '), '[contenteditable]', function(e){
|
|
|
|
|
if ( !c.$table.data('contentFocused') ) { return; }
|
2014-08-26 19:23:13 +00:00
|
|
|
|
var t, validate,
|
2014-07-28 00:28:08 +00:00
|
|
|
|
valid = false,
|
2014-08-26 19:23:13 +00:00
|
|
|
|
$this = $(e.target),
|
2014-08-27 22:26:20 +00:00
|
|
|
|
txt = $.trim( $this.text() ),
|
2014-08-26 19:23:13 +00:00
|
|
|
|
column = $this.closest('td').index();
|
2014-07-28 00:28:08 +00:00
|
|
|
|
if ( e.which === 27 ) {
|
2013-04-13 01:39:07 +00:00
|
|
|
|
// user cancelled
|
|
|
|
|
$this.html( $this.data('original') ).trigger('blur.tseditable');
|
2014-07-28 00:28:08 +00:00
|
|
|
|
c.$table.data( 'contentFocused', false );
|
2013-04-13 01:39:07 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-28 00:28:08 +00:00
|
|
|
|
t = e.which === 13 && ( wo.editable_enterToAccept || e.altKey ) || wo.editable_autoAccept && e.type !== 'keydown';
|
2013-04-13 01:39:07 +00:00
|
|
|
|
// change if new or user hits enter (if option set)
|
2014-08-27 22:26:20 +00:00
|
|
|
|
if ( t && $this.data('before') !== txt ) {
|
2014-08-26 19:23:13 +00:00
|
|
|
|
|
|
|
|
|
validate = wo.editable_validate;
|
2014-08-27 22:26:20 +00:00
|
|
|
|
valid = txt;
|
2014-08-26 19:23:13 +00:00
|
|
|
|
|
|
|
|
|
if (typeof(validate) === "function") {
|
2014-08-27 22:26:20 +00:00
|
|
|
|
valid = validate( txt, $this.data('original'), column, $this );
|
2014-08-26 19:23:13 +00:00
|
|
|
|
} else if (typeof (validate = $.tablesorter.getColumnData( table, validate, column )) === 'function') {
|
2014-08-27 22:26:20 +00:00
|
|
|
|
valid = validate( txt, $this.data('original'), column, $this );
|
2014-08-26 19:23:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-28 00:28:08 +00:00
|
|
|
|
if ( t && valid !== false ) {
|
2014-08-26 18:33:56 +00:00
|
|
|
|
c.$table.find('.tseditable-last-edited-cell').removeClass('tseditable-last-edited-cell');
|
2014-07-28 00:28:08 +00:00
|
|
|
|
$this
|
2014-08-26 18:33:56 +00:00
|
|
|
|
.addClass('tseditable-last-edited-cell')
|
2014-07-28 00:28:08 +00:00
|
|
|
|
.html( valid )
|
|
|
|
|
.data('before', valid)
|
2014-08-26 18:33:56 +00:00
|
|
|
|
.data('original', valid)
|
2014-07-28 00:28:08 +00:00
|
|
|
|
.trigger('change');
|
2014-08-27 22:26:20 +00:00
|
|
|
|
c.$table.trigger('updateCell', [ $this.closest('td'), false, function(){
|
2014-08-26 18:33:56 +00:00
|
|
|
|
if (wo.editable_autoResort) {
|
|
|
|
|
setTimeout(function(){
|
|
|
|
|
c.$table.trigger("sorton", [ c.sortList, function(){
|
|
|
|
|
editComplete(c.$table.find('.tseditable-last-edited-cell'));
|
|
|
|
|
}, true ]);
|
|
|
|
|
}, 10);
|
|
|
|
|
} else {
|
|
|
|
|
editComplete(c.$table.find('.tseditable-last-edited-cell'));
|
2014-07-28 00:28:08 +00:00
|
|
|
|
}
|
|
|
|
|
} ]);
|
|
|
|
|
return false;
|
2013-04-13 01:39:07 +00:00
|
|
|
|
}
|
2014-08-26 18:33:56 +00:00
|
|
|
|
} else if ( !valid && e.type !== 'keydown' ) {
|
2014-08-26 21:19:47 +00:00
|
|
|
|
clearTimeout( $this.data('timer') );
|
|
|
|
|
$this.data('timer', setTimeout(function(){
|
|
|
|
|
if ($.isFunction(wo.editable_blur)) {
|
2014-08-27 22:26:20 +00:00
|
|
|
|
wo.editable_blur( $.trim( $this.text() ), column, $this );
|
2014-08-26 21:19:47 +00:00
|
|
|
|
}
|
|
|
|
|
}, 100));
|
2014-07-28 00:28:08 +00:00
|
|
|
|
// restore original content on blur
|
|
|
|
|
$this.html( $this.data('original') );
|
|
|
|
|
}
|
2013-04-13 01:39:07 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})(jQuery);
|