datatables.net-autofill
Version:
AutoFill for DataTables
4 lines • 12.7 kB
JavaScript
/*! AutoFill 3.0.0 for DataTables
* Copyright (c) SpryMedia Ltd - datatables.net/license
*/
import DataTable,{Dom,util,Api}from"datatables.net";if(!DataTable||!DataTable.versionCheck||!DataTable.versionCheck("3"))throw"Warning: AutoFill requires DataTables 3 or greater";var _instance=0;class AutoFill{enabled(){return this.s.enabled}enable(t=!0){let e=this;var o=this.s.namespace;if(!1===t)return this.disable();this.s.enabled=!0,this._focusListener(),this.dom.handle.on("mousedown touchstart",function(t){return e._mousedown(t),!1});function i(){e.s.handle={height:0,width:0},e.dom.handle.css({height:"",width:""}),e.dom.attachedTo&&e._attach(e.dom.attachedTo)}return Dom.w.on("resize"+o,function(){0<Dom.s("div.dtaf-handle").count()&&e.dom.attachedTo&&e._attach(e.dom.attachedTo)}),Dom.w.on("orientationchange"+o,function(){setTimeout(function(){i(),setTimeout(i,150)},50)}),this}disable(){return this.s.enabled=!1,this._focusListenerRemove(),this}constructor(t,e){this.c=util.object.assignDeep({},AutoFill.defaults,e),this.s={dt:new DataTable.Api(t),enabled:!1,end:{column:0,row:0},handle:{height:0,width:0},namespace:".autoFill"+_instance++,scroll:{windowHeight:0,windowWidth:0,dtTop:0,dtLeft:0,dtHeight:0,dtWidth:0},scrollInterval:null,start:{column:0,row:0}},this.dom={attachedTo:null,container:Dom.c("div").classAdd("dtaf-container"),closeButton:Dom.c("button").classAdd(AutoFill.classes.close).html("×"),dtScroll:null,handle:Dom.c("div").classAdd("dtaf-handle"),list:Dom.c("div").classAdd("dtaf-list").html(this.s.dt.i18n("autoFill.info","")).attr("aria-modal",!0).attr("role","dialog").append(Dom.c("div").classAdd("dtaf-list-items")),offsetParent:null,start:null,select:{top:Dom.c("div").classAdd("dtaf-select top"),right:Dom.c("div").classAdd("dtaf-select right"),bottom:Dom.c("div").classAdd("dtaf-select bottom"),left:Dom.c("div").classAdd("dtaf-select left")}},this.dom.list.appendTo(this.dom.container),this._init()}_init(){var t=this.s.dt,e=Dom.s(this.s.dt.table().container()).find("div.dt-scroll-body");t.settings()[0].autoFill=this,e.count()&&"static"===(this.dom.dtScroll=e).css("position")&&e.css("position","relative"),!1!==this.c.enable&&this.enable(),t.on("destroy.autoFill",()=>{this._focusListenerRemove(),this.dom.handle.off()})}_attach(t){var e=this.s.dt,o=e.cell(t).index(),i=this.dom.handle,l=this.s.handle;o&&-1!==e.columns(this.c.columns).indexes().indexOf(o.column)?(this.dom.offsetParent||(this.dom.offsetParent=e.table().node().offsetParent),l.height&&l.width||(i.appendTo("body"),l.height=i.height("outer"),l.width=i.width("outer")),o=this._getPosition(t,this.dom.offsetParent),this.dom.attachedTo=t,i.css({top:o.top+t.offsetHeight-l.height+"px",left:o.left+t.offsetWidth-l.width+"px"}).appendTo(this.dom.offsetParent)):this._detach()}_actionSelector(o){let i=this,l=this.s.dt,n=AutoFill.actions,s=[];if(util.object.each(n,function(t,e){e.available(l,o)&&s.push(t)}),1===s.length&&!1===this.c.alwaysAsk){var t=n[s[0]].execute(l,o,null);this._update(t,o)}else if(1<s.length||this.c.alwaysAsk){var a=this.dom.list.children("div.dtaf-list-items").empty();s.push("cancel");for(let t=0;t<s.length;t++){let e=s[t];a.append(Dom.c("button").classAdd(AutoFill.classes.btn).html(n[e].option(l,o)).append(Dom.c("span").classAdd("dtaf-button").html(l.i18n("autoFill.button",""))).on("click",function(t){"button"===t.target.nodeName.toLowerCase()&&(t=n[e].execute(l,o,Dom.s(this).closest("button")),i._update(t,o),i.dom.container.remove())}))}this.dom.container.appendTo("body"),this.dom.container.on("click",function(t){t.target===i.dom.container.get(0)&&i.dom.container.remove()}),this.c.closeButton&&(this.dom.list.prepend(this.dom.closeButton).classAdd(AutoFill.classes.closeable),this.dom.closeButton.on("click",function(){return i.dom.container.trigger("click")}))}}_detach(){this.dom.attachedTo=null,this.dom.handle.detach()}_drawSelection(t,e){var o,i=this.s.dt,l=this.s.start,n=Dom.s(this.dom.start),t={row:this.c.vertical?i.rows({page:"current"}).nodes().indexOf(t.parentNode):l.row,column:this.c.horizontal?Dom.s(t).index():l.column},s=i.column.index("toData",t.column),a=i.row(":eq("+t.row+")",{page:"current"}),a=Dom.s(i.cell(a.index(),s).node());i.cell(a.get(0)).any()&&-1!==i.columns(this.c.columns).indexes().indexOf(s)&&-1!==t.row&&(this.s.end=t,i=l.row<t.row?n:a,s=l.row<t.row?a:n,o=l.column<t.column?n:a,l=l.column<t.column?a:n,i=this._getPosition(i.get(0)).top,o=this._getPosition(o.get(0)).left,t=this._getPosition(s.get(0)).top+s.height("outer")-i,a=this._getPosition(l.get(0)).left+l.width("outer")-o,(n=this.dom.select).top.css({top:i+"px",left:o+"px",width:a+"px"}),n.left.css({top:i+"px",left:o+"px",height:t+"px"}),n.bottom.css({top:i+t+"px",left:o+"px",width:a+"px"}),n.right.css({top:i+"px",left:o+a+"px",height:t+"px"}))}_editor(l){var n=this.s.dt;let s=this.c.editor;if(s){var a={},d=[],c=s.fields();for(let i=0,t=l.length;i<t;i++)for(let t=0,e=l[i].length;t<e;t++){var r=l[i][t],u=n.settings()[0].columns[r.index.column];let o=u.editField;if(void 0===o){var h=u.data;for(let t=0,e=c.length;t<e;t++){var f=s.field(c[t]);if(f.dataSrc()===h){o=f.name();break}}}if(!o)throw"Could not automatically determine field data. Please see https://datatables.net/tn/11";if("string"!=typeof o)throw"AutoFill with multiple edit fields is not supported";a[o]||(a[o]={});u=n.row(r.index.row).id();a[o][u]=r.set,d.push(r.index)}s.bubble(d,!1).multiSet(a).submit(null,function(){s.close()})}}_emitEvent(t,e){this.s.dt.trigger(t,e)}_focusListener(){var i=this,e=this.s.dt,t=this.s.namespace,o=null!==this.c.focus?this.c.focus:e.init().keys||e.settings()[0].keytable?"focus":"hover";"focus"===o?e.on("key-focus.autoFill",function(t,e,o){i._attach(o.node())}).on("key-blur.autoFill",function(t,e,o){i._detach()}):"click"===o?(Dom.s(e.table().body()).on("click"+t,"td, th",function(t){i._attach(this)}),Dom.s(document.body).on("click"+t,function(t){Dom.s(t.target).closest(e.table().body()).count()||i._detach()})):Dom.s(e.table().body()).on("mouseenter"+t+" touchstart"+t,"td, th",function(t){i._attach(this)}).on("mouseleave"+t+"touchend"+t,function(t){Dom.s(t.relatedTarget).classHas("dtaf-handle")||i._detach()})}_focusListenerRemove(){var t=this.s.namespace,e=this.s.dt;e.off(".autoFill"),Dom.s(e.table().body()).off(t),Dom.s(document.body).off(t),Dom.w.off(t)}_getPosition(t,e){let o=Dom.s(this.s.dt.table().node().offsetParent),i=t,l,n=0,s=0;e&&(o=Dom.s(e));do{var a=i.offsetTop,d=i.offsetLeft;if(l=Dom.s(i.offsetParent),n+=a+ +parseInt(l.css("border-top-width")||"0"),s+=d+ +parseInt(l.css("border-left-width")||"0"),"body"===i.nodeName.toLowerCase())break}while(i=l.get(0),l.get(0)!==o.get(0));return{top:n,left:s}}_mousedown(t){let e=this;var o=this.s.dt,i=(this.dom.start=this.dom.attachedTo,this.s.start={row:o.rows({page:"current"}).nodes().indexOf(Dom.s(this.dom.start).parent().get(0)),column:Dom.s(this.dom.start).index()},Dom.s(document.body).on("mousemove.autoFill touchmove.autoFill",function(t){e._mousemove(t),"touchmove"===t.type&&Dom.s(document.body).one("touchend.autoFill",function(){e._detach()})}).on("mouseup.autoFill touchend.autoFill",function(t){e._mouseup(t)}),this.dom.select),o=o.table().node().offsetParent,i=(i.top.appendTo(o),i.left.appendTo(o),i.right.appendTo(o),i.bottom.appendTo(o),this._drawSelection(this.dom.start,t),this.dom.handle.css("display","none"),this.dom.dtScroll);this.s.scroll={windowHeight:window.innerHeight,windowWidth:window.innerWidth,dtTop:i?i.offset().top:0,dtLeft:i?i.offset().left:0,dtHeight:i?i.height("outer"):0,dtWidth:i?i.width("outer"):0}}_mousemove(t){var e=t.touches&&t.touches.length?document.elementFromPoint(t.touches[0].clientX,t.touches[0].clientY):t.target,o=e.nodeName.toLowerCase();"td"!==o&&"th"!==o||(this._drawSelection(e,t),this._shiftScroll(t))}_mouseup(e){Dom.s(document.body).off(".autoFill");let o=this,d=this.s.dt;var t=this.dom.select,t=(t.top.remove(),t.left.remove(),t.right.remove(),t.bottom.remove(),this.dom.handle.css("display","block"),this.s.start),i=this.s.end;if(t.row!==i.row||t.column!==i.column){var l=d.cell(":eq("+t.row+")",t.column+":visible",{page:"current"});if(Dom.s(l.node()).find("div.DTE").count()){let t=d.editor();t.on("submitSuccess.dtaf close.dtaf",function(){t.off(".dtaf"),setTimeout(function(){o._mouseup(e)},100)}).on("submitComplete.dtaf preSubmitCancelled.dtaf close.dtaf",function(){t.off(".dtaf")}),void t.submit()}else{let n=this._range(t.row,i.row);var c=this._range(t.column,i.column),r=[];let s=d.settings()[0].columns,a=d.columns(this.c.columns).indexes();for(let l=0;l<n.length;l++)r.push(c.map(function(t){var e=d.row(":eq("+n[l]+")",{page:"current"}),e=d.cell(e.index(),t+":visible");let o=e.data();var t=e.index(),i=s[t.column].editField;if(void 0!==i&&(o=DataTable.util.get(i)(d.row(t.row).data())),-1!==a.indexOf(t.column))return{cell:e,data:o,label:e.data(),index:t}}).filter(t=>!!t));this._actionSelector(r),this.s.scrollInterval&&(clearInterval(this.s.scrollInterval),this.s.scrollInterval=null)}}}_range(t,e){var o,i=[];if(t<=e)for(o=t;o<=e;o++)i.push(o);else for(o=t;e<=o;o--)i.push(o);return i}_shiftScroll(t){var e,o,i,l,n=this,s=this.s.scroll,a=!1,d=t.type.includes("touch")?t.touches[0].clientX:t.pageX-window.scrollX,t=t.type.includes("touch")?t.touches[0].clientY:t.pageY-window.scrollY;t<65?e=-5:t>s.windowHeight-65&&(e=5),d<65?o=-5:d>s.windowWidth-65&&(o=5),null!==s.dtTop&&t<s.dtTop+65?i=-5:null!==s.dtTop&&t>s.dtTop+s.dtHeight-65&&(i=5),null!==s.dtLeft&&d<s.dtLeft+65?l=-5:null!==s.dtLeft&&d>s.dtLeft+s.dtWidth-65&&(l=5),e||o||i||l?(s.windowVert=e,s.windowHoriz=o,s.dtVert=i,s.dtHoriz=l,a=!0):this.s.scrollInterval&&(clearInterval(this.s.scrollInterval),this.s.scrollInterval=null),!this.s.scrollInterval&&a&&(this.s.scrollInterval=setInterval(function(){var t;window.scrollTo(window.scrollX+(s.windowHoriz||0),window.scrollY+(s.windowVert||0)),(s.dtVert||s.dtHoriz)&&((t=null==(t=n.dom.dtScroll)?void 0:t.get(0))&&s.dtVert&&(t.scrollTop+=s.dtVert),t)&&s.dtHoriz&&(t.scrollLeft+=s.dtHoriz)},20))}_update(t,i){if(!1!==t){var l,t=this.s.dt,n=t.columns(this.c.columns).indexes(),e=(this._emitEvent("preAutoFill",[t,i]),this._editor(i),null!==this.c.update?this.c.update:!this.c.editor);if(e){for(let o=0,t=i.length;o<t;o++)for(let t=0,e=i[o].length;t<e;t++)l=i[o][t],-1!==n.indexOf(l.index.column)&&l.cell.data(l.set);t.draw(!1)}this._emitEvent("autoFill",[t,i])}}}AutoFill.actions={increment:{available:function(t,e){e=e[0][0].label;return!isNaN(e-parseFloat(e))},option:function(t,e){return t.i18n("autoFill.increment",'Increment / decrement each cell by: <input type="number" value="1">')},execute:function(t,i,e){let l=+i[0][0].data;var n=e?parseFloat(e.find("input").val()):1;for(let o=0,t=i.length;o<t;o++)for(let t=0,e=i[o].length;t<e;t++)i[o][t].set=l,l+=n}},fill:{available:function(t,e){return!0},option:function(t,e){return t.i18n("autoFill.fill","Fill all cells with <i>%d</i>",e[0][0].label)},execute:function(t,i,e){var l=i[0][0].data;for(let o=0,t=i.length;o<t;o++)for(let t=0,e=i[o].length;t<e;t++)i[o][t].set=l}},fillHorizontal:{available:function(t,e){return 1<e.length&&1<e[0].length},option:function(t,e){return t.i18n("autoFill.fillHorizontal","Fill cells horizontally")},execute:function(t,i,e){for(let o=0,t=i.length;o<t;o++)for(let t=0,e=i[o].length;t<e;t++)i[o][t].set=i[o][0].data}},fillVertical:{available:function(t,e){return 1<e.length&&1<e[0].length},option:function(t,e){return t.i18n("autoFill.fillVertical","Fill cells vertically")},execute:function(t,i,e){for(let o=0,t=i.length;o<t;o++)for(let t=0,e=i[o].length;t<e;t++)i[o][t].set=i[0][t].data}},cancel:{available:function(){return!1},option:function(t){return t.i18n("autoFill.cancel","Cancel")},execute:function(){return!1}}},AutoFill.classes={btn:"btn",close:"dtaf-list-close",closeable:"dtaf-list-closeable"},AutoFill.defaults={alwaysAsk:!1,closeButton:!0,focus:null,columns:"",enable:!0,update:!0,editor:null,vertical:!0,horizontal:!0},AutoFill.version="3.0.0",Api.register("autoFill()",function(){return this.inst(this.context)}),Api.register("autoFill().enabled()",function(){var t=this.context[0];return!!t.autoFill&&t.autoFill.enabled()}),Api.register("autoFill().enable()",function(e){return this.iterator("table",function(t){t.autoFill&&t.autoFill.enable(e)})}),Api.register("autoFill().disable()",function(){return this.iterator("table",function(t){t.autoFill&&t.autoFill.disable()})}),Dom.s(document).on("preInit.dt.autofill",function(t,e){var o,i;"dt"===t.namespace&&(t=e.init.autoFill,o=DataTable.defaults.autoFill,t||o)&&(i={},util.is.plainObject(o)&&util.object.assign(i,o),util.is.plainObject(t)&&util.object.assign(i,t),!1!==t)&&new AutoFill(e,i)}),DataTable.AutoFill=AutoFill;export default DataTable;