tablesorter/dist/js/widgets/widget-editable.min.js
Rob Garrison 7f082fa019 v2.28.10
2017-05-16 15:33:15 -05:00

2 lines
5.2 KiB
JavaScript

/*! Widget: editable - updated 4/4/2017 (v2.28.7) */
!function(e){"use strict";var t=e.tablesorter.editable={namespace:".tseditable",lastEdited:"tseditable-last-edited-cell",editComplete:function(e,n,i,o){e.$table.find("."+t.lastEdited).removeClass(t.lastEdited).trigger(n.editable_editComplete,[e]),o&&setTimeout(function(){i.focus()},50)},selectAll:function(e){setTimeout(function(){if(document.queryCommandSupported("SelectAll"))document.execCommand("selectAll",!1,null);else{var t,n;document.body.createTextRange?((t=document.body.createTextRange()).moveToElementText(e),t.select()):window.getSelection&&(n=window.getSelection(),(t=document.createRange()).selectNodeContents(e),n.removeAllRanges(),n.addRange(t))}},100)},getColumns:function(t,n){var i,o,a,l,d,s=n.editable_columns,r=[];if("string"==typeof s)for(l=(i=s.replace(/\s+/,"").split(/,/)).length-1;l>=0;){if(i[l].indexOf("-")>=0)for(a=i[l].split("-"),(o=parseInt(a[0],10)||0)>(a=parseInt(a[1],10)||t.columns-1)&&(d=o,o=a,a=d);o<=a;o++)r.push("td:nth-child("+(o+1)+")");else r.push("td:nth-child("+((parseInt(i[l],10)||0)+1)+")");l--}else if(e.isArray(s))for(l=s.length,o=0;o<l;o++)s[o]<t.columns&&r.push("td:nth-child("+(s[o]+1)+")");return r},update:function(n,i){var o,a,l,d,s,r,c,u=e("<div>").wrapInner(i.editable_wrapContent).children().length||e.isFunction(i.editable_wrapContent),f=t.getColumns(n,i).join(",");for(n.$tbodies.find(f).find("[contenteditable]").prop("contenteditable",!1),d=(a=n.$tbodies.find(f).not("."+i.editable_noEdit)).length,l=0;l<d;l++)if(o=a.eq(l),u&&0===o.children("div, span").length&&o.wrapInner(i.editable_wrapContent),s=o.children("div, span").not("."+i.editable_noEdit),c=s.length)for(r=0;r<c;r++){var b=s.eq(r);i.editable_trimContent&&b.html(function(t,n){return e.trim(n)}),b.prop("contenteditable",!0)}else i.editable_trimContent&&o.html(function(t,n){return e.trim(n)}),o.prop("contenteditable",!0)},bindEvents:function(n,i){var o=t.namespace;n.$table.off("updateComplete pagerComplete ".split(" ").join(o+" ").replace(/\s+/g," ")).on("updateComplete pagerComplete ".split(" ").join(o+" "),function(){t.update(n,n.widgetOptions)}).children("thead").add(e(n.namespace+"_extra_table").children("thead")).off("mouseenter"+o).on("mouseenter"+o,function(){n.$table.data("contentFocused")&&(n.$table.data("contentFocused",!0),e(":focus").trigger("focusout"))}),n.$tbodies.off("focus focusout keydown ".split(" ").join(o+" ").replace(/\s+/g," ")).on("focus"+o,"[contenteditable]",function(a){clearTimeout(e(this).data("timer")),n.$table.data("contentFocused",a.target),n.table.isUpdating=!0;var l=e(this),d=i.editable_selectAll,s=l.closest("td").index(),r=l.html();i.editable_trimContent&&(r=e.trim(r)),l.off("keydown"+o).on("keydown"+o,function(e){i.editable_enterToAccept&&13===e.which&&!e.shiftKey&&e.preventDefault()}),l.data({before:r,original:r}),"function"==typeof i.editable_focused&&i.editable_focused(r,s,l),d&&("function"==typeof d?d(r,s,l)&&t.selectAll(l[0]):t.selectAll(l[0]))}).on("focusout keydown ".split(" ").join(o+" "),"[contenteditable]",function(a){if(n.$table.data("contentFocused")){var l,d,s=!1,r=e(a.target),c=r.html(),u=r.closest("td").index();if(i.editable_trimContent&&(c=e.trim(c)),27===a.which)return r.html(r.data("original")).trigger("blur"+o),n.$table.data("contentFocused",!1),n.table.isUpdating=!1,!1;if((l=13===a.which&&!a.shiftKey&&(i.editable_enterToAccept||a.altKey)||i.editable_autoAccept&&"keydown"!==a.type)&&r.data("before")!==c){if(d=i.editable_validate,s=c,"function"==typeof d?s=d(c,r.data("original"),u,r):"function"==typeof(d=e.tablesorter.getColumnData(n.table,d,u))&&(s=d(c,r.data("original"),u,r)),l&&!1!==s)return n.$table.find("."+t.lastEdited).removeClass(t.lastEdited),r.addClass(t.lastEdited).html(s).data("before",s).data("original",s).trigger("change"),n.table.hasInitialized&&e.tablesorter.updateCell(n,r.closest("td"),!1,function(){i.editable_autoResort?setTimeout(function(){e.tablesorter.sortOn(n,n.sortList,function(){t.editComplete(n,i,n.$table.data("contentFocused"),!0)},!0)},10):t.editComplete(n,i,n.$table.data("contentFocused"))}),!1}else s||"keydown"===a.type||(clearTimeout(r.data("timer")),r.data("timer",setTimeout(function(){n.table.isUpdating=!1,e.isFunction(i.editable_blur)&&(c=r.html(),i.editable_blur(i.editable_trimContent?e.trim(c):c,u,r))},100)),r.html(r.data("original")))}}).on("paste"+o,"[contenteditable]",function(){var t,n=e(this);setTimeout(function(){n.is(":focus")&&(t="<div>"+n.html()+"</div>",n.html(e(t).text().trim()))},0)})},destroy:function(e,n){var i=t.namespace,o=t.getColumns(e,n),a="updateComplete pagerComplete ".split(" ").join(i+" ").replace(/\s+/g," ");e.$table.off(a),a="focus focusout keydown paste ".split(" ").join(i+" ").replace(/\s+/g," "),e.$tbodies.off(a).find(o.join(",")).find("[contenteditable]").prop("contenteditable",!1)}};e.tablesorter.addWidget({id:"editable",options:{editable_columns:[],editable_enterToAccept:!0,editable_autoAccept:!0,editable_autoResort:!1,editable_wrapContent:"<div>",editable_trimContent:!0,editable_validate:null,editable_focused:null,editable_blur:null,editable_selectAll:!1,editable_noEdit:"no-edit",editable_editComplete:"editComplete"},init:function(e,n,i,o){o.editable_columns.length&&(t.update(i,o),t.bindEvents(i,o))},remove:function(e,n,i,o){o||t.destroy(n,i)}})}(jQuery);