"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _table=_interopRequireDefault(require("../../table")),_xeUtils=_interopRequireDefault(require("xe-utils/methods/xe-utils")),_conf=_interopRequireDefault(require("../../conf")),_tools=require("../../tools"),_vXETable=_interopRequireWildcard(require("../../v-x-e-table"));function _getRequireWildcardCache(){if("function"!=typeof WeakMap)return null;var t=new WeakMap;return _getRequireWildcardCache=function(){return t},t}function _interopRequireWildcard(t){if(t&&t.__esModule)return t;if(null===t||"object"!==_typeof(t)&&"function"!=typeof t)return{default:t};var e=_getRequireWildcardCache();if(e&&e.has(t))return e.get(t);var o={},i=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var r in t)if(Object.prototype.hasOwnProperty.call(t,r)){var n=i?Object.getOwnPropertyDescriptor(t,r):null;n&&(n.get||n.set)?Object.defineProperty(o,r,n):o[r]=t[r]}return o.default=t,e&&e.set(t,o),o}function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function ownKeys(e,t){var o=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t&&(i=i.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),o.push.apply(o,i)}return o}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(o),!0).forEach(function(t){_defineProperty(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):ownKeys(Object(o)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))})}return e}function _defineProperty(t,e,o){return e in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}var methods={},propKeys=Object.keys(_table.default.props);Object.keys(_table.default.methods).forEach(function(t){methods[t]=function(){return this.$refs.xTable[t].apply(this.$refs.xTable[t],arguments)}});var _default={name:"PlxGrid",props:_objectSpread({columns:Array,pagerConfig:[Boolean,Object],proxyConfig:Object,toolbar:[Boolean,Object],formConfig:[Boolean,Object]},_table.default.props),provide:function(){return{$grid:this}},data:function(){return{tableLoading:!1,maximize:!1,tableData:[],tableCustoms:[],pendingRecords:[],filterData:[],sortData:{},tZindex:0,tablePage:{total:0,pageSize:10,currentPage:1}}},computed:{vSize:function(){return this.size||this.$parent.size||this.$parent.vSize},isMsg:function(){return!1!==this.proxyOpts.message},proxyOpts:function(){return Object.assign({},_conf.default.grid.proxyConfig,this.proxyConfig)},pagerOpts:function(){return Object.assign({},_conf.default.grid.pagerConfig,this.pagerConfig)},toolbarOpts:function(){return Object.assign({},_conf.default.grid.toolbar,this.toolbar)},toolbarSlots:function(){var t=this.$scopedSlots,e=this.toolbar,o=this.toolbarOpts,i=t.buttons,r=t.tools,n={};return e&&o.slots&&(i=o.slots.buttons||i,r=o.slots.tools||r),i&&(n.buttons=i),r&&(n.tools=r),n},renderClass:function(){var t,e=this.tableProps,o=this.vSize,i=this.maximize;return["plx-grid",(_defineProperty(t={},"size--".concat(o),o),_defineProperty(t,"t--animat",e.optimization.animat),_defineProperty(t,"is--maximize",i),t)]},renderStyle:function(){return this.maximize?{zIndex:this.tZindex}:null},tableExtendProps:function(){var e=this,o={};return propKeys.forEach(function(t){o[t]=e[t]}),o},tableProps:function(){var t=this.maximize,e=this.seqConfig,o=this.pagerConfig,i=this.loading,r=this.toolbar,n=this.toolbarOpts,s=this.editConfig,a=this.proxyConfig,l=this.proxyOpts,c=this.tableExtendProps,u=this.tableLoading,d=this.tablePage,h=this.tableData,f=this.tableCustoms,p=this.optimization,g=Object.assign({},c,{optimization:Object.assign({},_conf.default.optimization,p)});return t&&(c.maxHeight?g.maxHeight="auto":g.height="auto"),a&&(Object.assign(g,{loading:i||u,data:h,rowClassName:this.handleRowClassName}),(l.seq||l.index)&&o&&(g.seqConfig=Object.assign({},e,{startIndex:(d.currentPage-1)*d.pageSize}))),r&&(n.setting&&n.setting.storage||(g.customs=f)),s&&(g.editConfig=Object.assign({},s,{activeMethod:this.handleActiveMethod})),g},tableOns:function(){var t=this.$listeners,e=this.toolbar,o=this.proxyConfig,i=this.proxyOpts,r=Object.assign({},t);return o&&(i.sort&&(r["sort-change"]=this.sortChangeEvent),i.filter&&(r["filter-change"]=this.filterChangeEvent)),e&&(r["update:customs"]=this.updateCustomsEent),r},toolbarProps:function(){return Object.assign({loading:this.loading||this.tableLoading},this.toolbarOpts)},pagerProps:function(){return Object.assign({size:this.vSize,loading:this.loading||this.tableLoading},this.pagerOpts,this.proxyConfig?this.tablePage:{})}},watch:{columns:function(t){var e=this;this.$nextTick(function(){return e.loadColumn(t)})},tableCustoms:function(){var t=this.$refs;this.toolbar&&t.toolbar&&t.toolbar.loadStorage()},proxyConfig:function(){this.initProxy()},pagerConfig:function(){this.initPages()}},created:function(){var t=this.customs,e=this.data,o=this.proxyConfig,i=this.proxyOpts,r=i.props;t&&(this.tableCustoms=t),e&&o&&console.warn("[plx-grid] There is a conflict between the props proxy-config and data."),i.index,r&&r.data&&_tools.UtilTools.warn("plx.error.delProp",["proxy-config.props.data","proxy-config.props.result"])},mounted:function(){this.columns&&this.columns.length&&this.loadColumn(this.columns),this.initPages(),this.initProxy()},render:function(t){var e=this.$scopedSlots;return t("div",{class:this.renderClass,style:this.renderStyle},[this.formConfig?t("div",{ref:"form",class:["plx-form",{"is--loading":this.tableLoading}]},e.form?e.form.call(this,{$grid:this},t):[]):null,this.toolbar?t("plx-toolbar",{ref:"toolbar",props:this.toolbarProps,scopedSlots:this.toolbarSlots}):null,t("plx-table",{props:this.tableProps,on:this.tableOns,scopedSlots:e,ref:"xTable"},this.$slots.default),this.pagerConfig?e.pager?e.pager.call(this,{$grid:this},t):t("plx-pager",{props:this.pagerProps,on:{"page-change":this.pageChangeEvent},ref:"pager"}):null])},methods:_objectSpread({},methods,{getParentHeight:function(){return(this.maximize?_tools.DomTools.getDomNode().visibleHeight:this.$el.parentNode.clientHeight)-this.getExcludeHeight()},getExcludeHeight:function(){var t=this.$refs,e=t.form,o=t.toolbar,i=t.pager;return(e?e.offsetHeight:0)+(o&&o.$el?o.$el.offsetHeight:0)+(i&&i.$el?i.$el.offsetHeight:0)},handleRowClassName:function(e){var t=this.rowClassName,o=[];return this.pendingRecords.some(function(t){return t===e.row})&&o.push("row--pending"),o.concat(t?t(e):[])},handleActiveMethod:function(t){var e=this.editConfig.activeMethod;return-1===this.pendingRecords.indexOf(t.row)&&(!e||e(t))},loadColumn:function(t){var i=this.$scopedSlots;t.forEach(function(t){t.slots&&_xeUtils.default.each(t.slots,function(t,e,o){_xeUtils.default.isFunction(t)||(i[t]?o[e]=i[t]:(o[e]=null,_tools.UtilTools.error("plx.error.notSlot",[t])))})}),this.$refs.xTable.loadColumn(t)},reloadColumn:function(t){return this.clearAll(),this.loadColumn(t)},initPages:function(){this.pagerConfig&&this.pagerOpts.pageSize&&(this.tablePage.pageSize=this.pagerOpts.pageSize)},initProxy:function(){var t=this;!this.proxyInited&&this.proxyConfig&&!1!==this.proxyOpts.autoLoad&&(this.proxyInited=!0,this.$nextTick(function(){return t.commitProxy("reload")}))},commitProxy:function(o){var t,i=this,e=this.$refs,r=this.toolbar,n=this.toolbarOpts,s=this.proxyOpts,a=this.tablePage,l=this.pagerConfig,c=this.sortData,u=this.filterData,d=this.isMsg,h=s.beforeQuery,f=s.beforeDelete,p=s.beforeSave,g=s.ajax,b=void 0===g?{}:g,m=s.props,x=void 0===m?{}:m,y=e.xTable,v=_xeUtils.default.slice(arguments,1);if(_xeUtils.default.isString(o)){var _=r?_xeUtils.default.findTree(n.buttons,function(t){return t.code===o},{children:"dropdowns"}):null;t=_?_.item:null}else o=(t=o).code;var O=t?t.params:null;switch(o){case"insert":this.insert();break;case"insert_actived":this.insert().then(function(t){var e=t.row;return i.setActiveRow(e)});break;case"mark_cancel":this.triggerPendingEvent(o);break;case"delete_selection":this.handleDeleteRow(o,"plx.grid.deleteSelectRecord",function(){return i.commitProxy.apply(i,["delete"].concat(v))});break;case"remove_selection":this.handleDeleteRow(o,"plx.grid.removeSelectRecord",function(){return i.removeSelecteds()});break;case"import":this.importData(O);break;case"open_import":this.openImport(O);break;case"export":this.exportData(O);break;case"open_export":this.openExport(O);break;case"reset_custom":this.resetAll();break;case"reload":case"query":var P=b.query;if(P){var C={code:o,$grid:this,sort:c,filters:u,options:P};if(this.tableLoading=!0,l&&(C.page=a),"reload"===o){var R=y.sortOpts.defaultSort,j={};l&&(a.currentPage=1),R&&(j={property:R.field,field:R.field,prop:R.field,order:R.order,$table:y}),this.sortData=C.sort=j,this.filterData=C.filters=[],this.pendingRecords=[],this.clearAll()}return(h||P).apply(this,[C].concat(v)).then(function(t){t?l?(a.total=_xeUtils.default.get(t,x.total||"page.total")||0,i.tableData=_xeUtils.default.get(t,x.result||x.data||"result")||[]):i.tableData=(x.list?_xeUtils.default.get(t,x.list):t)||[]:i.tableData=[],i.tableLoading=!1}).catch(function(t){i.tableLoading=!1,console.error(t)})}_tools.UtilTools.error("plx.error.notFunc",[o]);break;case"delete":var $=b.delete;if($){var S=this.getCheckboxRecords();this.remove(S).then(function(){var t=i.getRemoveRecords(),e={removeRecords:t};if(t.length)return i.tableLoading=!0,(f||$).apply(i,[{$grid:i,code:o,body:e,options:$}].concat(v)).then(function(t){i.tableLoading=!1}).catch(function(t){i.tableLoading=!1,console.error(t)}).then(function(){return i.commitProxy("reload")});d&&!S.length&&_vXETable.default.$modal.message({id:o,message:_conf.default.i18n("plx.grid.selectOneRecord"),status:"warning"})})}else _tools.UtilTools.error("plx.error.notFunc",[o]);break;case"save":var w=b.save;if(w){var E=Object.assign({pendingRecords:this.pendingRecords},this.getRecordset()),z=E.insertRecords,T=E.removeRecords,D=E.updateRecords,k=E.pendingRecords;return z.length&&(E.pendingRecords=k.filter(function(t){return-1===z.indexOf(t)})),k.length&&(E.insertRecords=z.filter(function(t){return-1===k.indexOf(t)})),new Promise(function(e){i.validate(E.insertRecords.concat(D),function(t){t?E.insertRecords.length||T.length||D.length||E.pendingRecords.length?(i.tableLoading=!0,e((p||w).apply(i,[{$grid:i,code:o,body:E,options:w}].concat(v)).then(function(){_vXETable.default.$modal.message({id:o,message:_conf.default.i18n("plx.grid.saveSuccess"),status:"success"}),i.tableLoading=!1}).catch(function(t){i.tableLoading=!1,console.error(t)}).then(function(){return i.commitProxy("reload")}))):(d&&(k.length?i.remove(k):_vXETable.default.$modal.message({id:o,message:_conf.default.i18n("plx.grid.dataUnchanged"),status:"info"})),e()):e(t)})})}_tools.UtilTools.error("plx.error.notFunc",[o]);break;default:var U=_vXETable.Buttons.get(o);U&&U.apply(this,[{code:o,button:t,$grid:this,$table:y}].concat(v))}return this.$nextTick()},handleDeleteRow:function(t,e,o){var i=this.getCheckboxRecords();this.isMsg?i.length?_vXETable.default.$modal.confirm(_conf.default.i18n(e)).then(function(t){"confirm"===t&&o()}):_vXETable.default.$modal.message({id:t,message:_conf.default.i18n("plx.grid.selectOneRecord"),status:"warning"}):i.length&&o()},getPendingRecords:function(){return this.pendingRecords},updateCustomsEent:function(t){this.tableCustoms=t},triggerToolbarBtnEvent:function(t,e){this.commitProxy(t,e),_tools.UtilTools.emitEvent(this,"toolbar-button-click",[{code:t.code,button:t,$grid:this},e])},triggerPendingEvent:function(t){var o=this.pendingRecords,e=this.isMsg,i=this.getCheckboxRecords();if(i.length){var r=[],n=[];i.forEach(function(e){o.some(function(t){return e===t})?n.push(e):r.push(e)}),n.length?this.pendingRecords=o.filter(function(t){return-1===n.indexOf(t)}).concat(r):r.length&&(this.pendingRecords=o.concat(r)),this.clearCheckboxRow()}else e&&_vXETable.default.$modal.message({id:t,message:_conf.default.i18n("plx.grid.selectOneRecord"),status:"warning"})},pageChangeEvent:function(t){var e=this.proxyConfig,o=this.tablePage,i=t.currentPage,r=t.pageSize;o.currentPage=i,o.pageSize=r,"current-change"===t.type?_tools.UtilTools.emitEvent(this,"current-page-change",[i]):_tools.UtilTools.emitEvent(this,"page-size-change",[r]),_tools.UtilTools.emitEvent(this,"page-change",[Object.assign({$grid:this},t)]),e&&this.commitProxy("query")},sortChangeEvent:function(t){var e=this.proxyConfig,o=this.remoteSort,i=t.$table,r=t.column;(_xeUtils.default.isBoolean(r.remoteSort)?r.remoteSort:i.sortOpts.remote||o)&&(this.sortData=t,e&&this.commitProxy("query")),_tools.UtilTools.emitEvent(this,"sort-change",[Object.assign({$grid:this},t)])},filterChangeEvent:function(t){var e=this.remoteFilter,o=t.$table,i=t.filters;(o.filterOpts.remote||e)&&(this.filterData=i,this.commitProxy("query")),_tools.UtilTools.emitEvent(this,"filter-change",[Object.assign({$grid:this},t)])},zoom:function(){var t=this;return this.maximize=!this.maximize,this.maximize&&this.tZindex<_tools.UtilTools.getLastZIndex()&&(this.tZindex=_tools.UtilTools.nextZIndex()),this.$nextTick().then(function(){return t.recalculate(!0)}).then(function(){return t.maximize})},isMaximized:function(){return this.maximize}})};exports.default=_default;