text: !function(t){function e(e){Handsontable.plugins.BasePlugin.call(this,e),this._superClass=Handsontable.plugins.BasePlugin,this.originals={},this.originals.getCopyableData=e.getCopyableData,this.table_id=e.rootElement.id,this.tableDrawRequests=0,this.tableDrawResponses=0,this.tableSettings={},this.$SEARCH_FIELD=t("#"+this.table_id+"_search_filter input"),this.loadLanguages()}(e.prototype=Object.create(Handsontable.plugins.BasePlugin.prototype,{constructor:{writable:!0,configurable:!0,value:e}})).isEnabled=function(){return!!this.hot.getSettings().wpDataTablesExcelPlugin},e.prototype.enablePlugin=function(){this.overrideHotFunctions();for(var e in this.languages)numeral.language(e,this.languages[e]);var a={},o=t(this.hot.rootElement).data("described-by");o&&($descriptionElement=t("#"+o))&&(a=t.parseJSON($descriptionElement.val()),this.tableSettings=a.dataTableParams,delete a.dataTableParams);for(var i in a)this[i]=a[i];this.tableSettings.language="en-"+this.tableSettings.number_format;for(var l=0;l<this.tableSettings.columns.length;l++)"wdt.date"==this.tableSettings.columns[l].type&&(this.tableSettings.columns[l].sortFunction=this.sortDate),this.serverSide&&this.setCellValidator(this.tableSettings.columns[l]);this.serverSide?(this.tableSettings.minSpareRows=1,this.addContextMenuItems(),Handsontable.Search.global.setDefaultCallback(this.wdtExcelSearchCallback),this.addHook("afterChange",this.onAfterChange.bind(this)),this.addHook("init",this.onInit.bind(this)),this.addHook("afterValidate",this.onAfterValidate.bind(this))):(this.tableSettings.data=t.parseJSON(t(a.selector+"_data").val()),this.addHook("init",this.onInit.bind(this))),this.addHook("beforeAutofill",this.onBeforeAutofill),this.addHook("beforeValidate",this.onBeforeValidate),this.addHook("beforeChange",this.onBeforeChange),"object"==typeof wpDtExcelTables[this.table_id]&&t.extend(!0,this.tableSettings,wpDtExcelTables[this.table_id]),this._superClass.prototype.enablePlugin.call(this)},e.prototype.overrideHotFunctions=function(){var t=this.originals.getCopyableData;this.hot.getCopyableData=function(e,a){var o=this.getCellMeta(e,a),i=t.apply(this,arguments);return"numeric"==o.type?i=numeral(i).format(o.format):"wdt.date"==o.type&&(i=moment(i,o.dataSourceDateFormat).format(o.displayDateFormat)),i}},e.prototype.revertOverriddenHotFunctions=function(){this.hot.getCopyableData=this.originals.getCopyableData},e.prototype.initTableSettings=function(){this.hot.updateSettings(this.tableSettings)},e.prototype.disablePlugin=function(){this.revertOverriddenHotFunctions(),this.tableDrawRequests=0,this.tableDrawResponses=0,this.tableSettings={},this.removeEvents(),this.removeHook("beforeAutofill",this.onBeforeAutofill),this.removeHook("beforeValidate",this.onBeforeValidate),this.removeHook("beforeChange",this.onBeforeChange),this._superClass.prototype.disablePlugin.call(this)},e.prototype.updatePlugin=function(){this._superClass.prototype.updatePlugin.call(this)},e.prototype.destroy=function(){this._superClass.prototype.destroy.call(this)},e.prototype.initEvents=function(){var e=this.hot.rootElement;this.serverSide&&(t(e).on("click.wpExcelTable","a.wdt_link_editable",function(e){e.preventDefault(),e.ctrlKey&&window.open(t(this).attr("href"),t(this).attr("target"))}),t(e).on("click.wpExcelTable","a.wdt_email_editable",function(e){e.preventDefault(),e.ctrlKey&&(location.href=t(this).attr("href"))}))},e.prototype.initSearch=function(){var t=this.hot,e=t.getSettings();e.search&&(this.$SEARCH_FIELD.on("keyup.wpExcelTable search.wpExcelTable",function(e){t.search.query(this.value),t.render()}),e.searchDefaultValue&&this.$SEARCH_FIELD.val(e.searchDefaultValue))},e.prototype.removeEvents=function(){var e=this.hot.rootElement;t(e).off("click.wpExcelTable",".wpExcelTable a.wdt_link_editable"),t(e).off("click.wpExcelTable",".wpExcelTable a.wdt_email_editable"),this.$SEARCH_FIELD.off("keyup.wpExcelTable search.wpExcelTable")},e.prototype.getCellValidatorByName=function(t){var e=t.replace(/(?:(wdt)\.)?(.+)/,function(t,e,a){return a?(e=e||"")+a.charAt(0).toUpperCase()+a.slice(1)+"Validator":null});return e&&Handsontable[e]?Handsontable[e]:null},e.prototype.setCellValidator=function(t){var e=t.validator;if(e){var a=this.getCellValidatorByName(e);a&&("dropdown"==t.type||"wdt.multi-select"==t.type?(t.cell_validator=a,delete t.validator):t.validator=a)}},e.prototype.getRemoteTableData=function(){var e=this,a=this.hot,o=a.getSettings();if(o.serverSide){for(var i=o.ajax,l=o.columns,r=o.columnSorting,n=Boolean(r),s=[],h=0;h<l.length;h++){var d={data:h,name:l[h].data,searchable:l[h].search,orderable:n};s.push(d)}var u={draw:e.tableDrawRequests+1,table:"excel",columns:s,start:0,length:-1};if(n){var c=[],p={column:r.column,dir:r.sortOrder?"asc":"desc"};c.push(p),u.order=c}var g={data:u,dataType:"json",success:function(t){t.draw&&t.draw>e.tableDrawResponses&&(e.tableDrawResponses=t.draw,a.loadData(t.data))},error:function(t){console.log(t)},beforeSend:function(t,a){e.tableDrawRequests++,e.toggleTableOverlay("show")},complete:function(){e.toggleTableOverlay("hide")}};t.extend(i,g),t.ajax(i)}},e.prototype.loadLanguages=function(){var e={};e.en={delimiters:{thousands:",",decimal:"."},abbreviations:{thousand:"k",million:"m",billion:"b",trillion:"t"},ordinal:function(t){var e=t%10;return 1==~~(t%100/10)?"th":1===e?"st":2===e?"nd":3===e?"rd":"th"},currency:{symbol:"$"}},e["en-1"]=t.extend(!0,{},e.en),e["en-1"].delimiters.thousands=".",e["en-1"].delimiters.decimal=",",e["en-2"]=t.extend(!0,{},e.en),this.languages=e},e.prototype.queryDateCell=function(t,e,a,o){var i=t.getSettings(),l=this.$SEARCH_FIELD.val(),r=moment(o,i.dataSourceDateFormat).format(i.displayDateFormat);return Handsontable.Search.DEFAULT_QUERY_METHOD(l,r)},e.prototype.wdtExcelSearchCallback=function(t,e,a,o,i){var l=t.getCellMeta(e,a);"wdt.date"!=l.renderer||i?Handsontable.Search.DEFAULT_CALLBACK.apply(this,arguments):l.isSearchResult=t.getPlugin("WpDataTablesExcelPlugin").queryDateCell(t,e,a,o)},e.prototype.prepareDataToSaveRemote=function(e){for(var a,o,i,l,r=this.hot.getSettings().idColumnKey,n=[],s=[],h=[],d=0;d<e.length;d++){if(a=e[d][0],o=e[d][1],i=e[d][2],l=e[d][3],i!=l)if(void 0===n[a]){var u=this.hot.getDataAtRowProp(a,r),c={};c[r]=u,c[o]=l,n[a]=c}else n[a][o]=l;if(-1==t.inArray(a,s)){var p=this.hot.getSourceDataAtRow(a);h.push(p),s.push(a)}}if(n.length>0){var g=[];for(var f in n)g.push(n[f]);n=g}return{cells:n,rows:h}};var a=function(t){t.stopImmediatePropagation(),t.preventDefault()};e.prototype.toggleTableOverlay=function(e){var o=this.hot;"show"==e?(t(o.rootElement).find("table.htCore").addClass("overlayed_elm"),o.addHook("beforeKeyDown",a)):"hide"==e&&(t(o.rootElement).find("table.htCore").removeClass("overlayed_elm"),o.removeHook("beforeKeyDown",a))},e.prototype.saveChangesRemote=function(e){var a=this.hot.getSettings(),o=this.tableWpId,i=this.prepareDataToSaveRemote(e),l=i.cells,r=i.rows,n=this;if(l.length>0){var s={action:"wdt_save_table_cells_frontend",wdtNonce:t("#wdtNonceFrontendEdit").val(),table_id:o,cells:l,rows:r};t.ajax({url:a.adminAjaxBaseUrl,type:"POST",dataType:"json",data:s,success:function(t){n.toggleTableOverlay("hide"),(t.error.length>0||t.has_new)&&n.getRemoteTableData(),t.error.length>0?alert(t.error):t.formula_cells&&n.applyCellsData(t.formula_cells,"updateFormulaValues")},error:function(t){n.toggleTableOverlay("hide"),alert(t.error),n.getRemoteTableData()},beforeSend:function(){n.toggleTableOverlay("show")}})}},e.prototype.applyCellsData=function(t,e){if(0!=t.length){for(var a=[],o=this.hot,i=o.getSettings().idColumnKey,l=o.countRows(),r=0;r<l;r++)for(var n=o.getDataAtRowProp(r,i),s=0;s<t.length;s++){var h=t[s];if(h[i]==n)for(var d in h)d!=i&&a.push([r,d,h[d]])}a.length>0&&o.setDataAtRowProp(a,e)}},e.prototype.onAfterChange=function(t,e){"loadData"!==e&&"updateFormulaValues"!==e&&this.saveChangesRemote(t);var a=this.hot;a.getSettings().search&&(a.search.query(this.$SEARCH_FIELD.val()),a.render())},e.prototype.onInit=function(){this.initTableSettings(),this.initEvents(),this.initSearch(),this.getRemoteTableData()},e.prototype.onAfterValidate=function(e,a,o,i,l){var r=t(document.activeElement),n=void 0;r.is(":input")&&(n=r.closest("#"+this.hot.rootElement.id+" > div")),n&&(n.next("div.tooltip:visible ").length||(n.attr("title",""),n.tooltip({title:wpdatatables_frontend_strings.invalid_value})),e?(n.removeClass("invalid_editor_value"),n.tooltip("destroy")):(n.addClass("invalid_editor_value"),n.tooltip("show")))},e.prototype.onBeforeAutofill=function(t,e,a){for(var o=0;o<a.length;o++)for(var i=0;i<a[o].length;i++){var l=this.getCellMeta(t.row+o,t.col+i);"numeric"==l.type&&numeral.validate(a[o][i])&&(a[o][i]=numeral(a[o][i]).format(l.format))}},e.prototype.onBeforeValidate=function(t,e,a,i){return o.apply(this,arguments)};var o=function(t,e,a,o){var i=this.propToCol(a);if("wdt.date"==this.getDataType(e,i,e,i)){if(!t)return"";if("paste"!=o){var l=this.getSettings();t=moment(t,l.dataSourceDateFormat).format(l.displayDateFormat)}return t}};e.prototype.onBeforeChange=function(t,e){for(var a=0;a<t.length;a++){var o=this.propToCol(t[a][1]),i=this.getCellMeta(t[a][0],o);"wdt.date"==i.type&&"paste"==e&&(t[a][3]=moment(t[a][3],i.displayDateFormat).format(i.dataSourceDateFormat))}},e.prototype.addNewTableRow=function(t,e){var a=e.end.col,o=this.countEmptyRows(!0),i=this.countRows();if(o>0){var l=i-1;this.selectCell(l,a,l,a)}else if(this.countEmptyRows(!1)>0){for(var r=0;r<i;r++)if(this.isEmptyRow(r))return void this.selectCell(r,a,r,a)}else this.alter("insert_row")},e.prototype.deleteTableRows=function(e,a){for(var o=a.start.row,i=a.end.row,l=this.getSettings(),r=l.idColumnKey,n=this.propToCol(r),s=this.getData(o,n,i,n),h=[],d=0;d<s.length;d++)s[d][0]&&h.push(s[d][0]);if(h.length>0){var u=this.getPlugin("wpDataTablesExcelPlugin"),c=u.tableWpId,p={};p[r]=h;var g={action:"wdt_delete_table_rows",table_id:c,rows:p,wdtNonce:t("#wdtNonceFrontendEdit").val()};t.ajax({type:"POST",url:l.adminAjaxBaseUrl,data:g,dataType:"json",success:function(t){if(u.toggleTableOverlay("hide"),t.error.length>0)u.getRemoteTableData();else if(t.success.length>0)for(var e=o;e<=i;e++)this.alter("remove_row",e);else u.getRemoteTableData()},error:function(t){u.toggleTableOverlay("hide"),console.log(t),u.getRemoteTableData()},beforeSend:function(){u.toggleTableOverlay("show")}})}},e.prototype.addContextMenuItems=function(){this.contextMenu={items:{add_row:{name:"New row",callback:this.addNewTableRow},remove_row:{name:"Delete row(s)",callback:this.deleteTableRows,disabled:function(){var t=this.getSelected();return t[0]==t[2]&&this.isEmptyRow(t[0])}}}},this.tableSettings.contextMenu=this.contextMenu},e.prototype.sortDate=function(t){var e=t?1:-1;return function(t,a){var o,i=moment(t[1],"YYYY-MM-DD"),l=moment(a[1],"YYYY-MM-DD");return o=i.isValid()||l.isValid()?i.isValid()?l.isValid()?i.valueOf()-l.valueOf():1:-1:0,e*o}},e.prototype.removeContextMenuItems=function(){},Handsontable.plugins.registerPlugin("wpDataTablesExcelPlugin",e)}(jQuery);