-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
3 lines (3 loc) Β· 10.8 KB
/
index.js
1
2
3
(function(){"use strict";function h(s,t,e,l,a,i,o,n){var r=typeof s=="function"?s.options:s;return t&&(r.render=t,r.staticRenderFns=e,r._compiled=!0),{exports:s,options:r}}const d={extends:"k-field",props:{align:String,duplicate:{type:Boolean,default:!0},empty:String,headers:{type:Boolean,default:!0},index:{type:[Number,Boolean],default:1},minColumns:{type:Number,default:2},maxColumns:{type:Number,default:8},marks:{type:[Boolean,Array],default:!0},sortable:{type:Boolean,default:!0},value:[String,Array]},computed:{values(){return[...this.table]},columns(){return this.hasHeaders?this.table[0]:this.table[0].length>0?Array(this.table[0].length).fill(""):Array(this.minColumns).fill("")},rows(){return this.hasHeaders?this.table.slice(1):this.table},colspan(){let s=this.columns.length;return this.hasIndexColumn&&s++,s},hasHeaders(){return this.headers},hasAlignment(){return this.align},hasOptions(){return!this.disabled&&this.rows.length!==0},hasIndexColumn(){return this.isSortable||this.index!==!1},isSortable(){return!this.disabled&&this.index!==!1&&this.sortable},table(){const s=i=>i.trim().replace(/^[>\|-]?\s*["']?(.*?)["']?$/,"$1")||"",t=i=>/^\s*-\s*$/.test(i),e=i=>/^\s*-\s*[>|]\s*$/.test(i),l=i=>{const o=[];let n=[],r="",u=!1;return i.split(`
`).forEach(c=>{t(c)?(u&&(n.push(s(r)),r="",u=!1),n.length>0&&(o.push(n),n=[])):e(c)?(u&&(n.push(s(r)),r=""),u=!0):u?c.trim().startsWith("-")&&!c.trim().startsWith("- >")?(u=!1,n.push(s(r)),r="",t(c)||n.push(s(c))):r+=(r?`
`:"")+c.trim():n.push(s(c))}),u&&r.trim()&&n.push(s(r)),n.length>0&&o.push(n),o};let a=typeof this.value=="string"?l(this.value):this.value;return a||(a=Array.from({length:this.hasHeaders?2:1},()=>Array(this.minColumns).fill(""))),a.forEach(i=>{for(;i.length<this.minColumns;)i.push("")}),a},dragOptions(){return{disabled:!this.sortable,draggable:".k-table-sortable-row",fallbackClass:"k-table-row-fallback",ghostClass:"k-table-row-ghost"}},tableOptions(){return[{click:()=>this.remove("all"),disabled:this.rows.length===0&&this.columns.length<=this.minColumns,icon:"trash",text:this.$t("delete.all")}]},columnOptions(){return[{disabled:this.columns.length>=this.maxColumns,icon:"angle-left",text:this.$t("insert.before"),click:"insertBefore"},{disabled:this.columns.length>=this.maxColumns,icon:"angle-right",text:this.$t("insert.after"),click:"insertAfter"},"-",{icon:"eraser",text:this.$t("field.table.clear"),click:"clear"},{disabled:!this.duplicate||this.columns.length>=this.maxColumns,icon:"copy",text:this.$t("duplicate"),click:"duplicate"},"-",{disabled:this.columns.length<=this.minColumns,icon:"trash",text:this.$t("delete"),click:"remove"}]},rowOptions(){return[{icon:"angle-up",text:this.$t("insert.before"),click:"insertBefore"},{icon:"angle-down",text:this.$t("insert.after"),click:"insertAfter"},"-",{icon:"eraser",text:this.$t("field.table.clear"),click:"clear"},{disabled:!this.duplicate,icon:"copy",text:this.$t("duplicate"),click:"duplicate"},"-",{disabled:this.rows.length<=1,icon:"trash",text:this.$t("delete"),click:"remove"}]}},methods:{columnOption(s,t){switch(s){case"insertBefore":this.insert("column",t,"before");break;case"insertAfter":this.insert("column",t,"after");break;case"clear":this.clear("column",t);break;case"duplicate":this.duplicate("column",t);break;case"remove":this.remove("column",t);break}},rowOption(s,t){switch(s){case"insertBefore":this.insert("row",t,"before");break;case"insertAfter":this.insert("row",t,"after");break;case"clear":this.clear("row",t);break;case"duplicate":this.duplicate("row",t);break;case"remove":this.remove("row",t);break}},showDialog(s,t){this.$panel.dialog.open({component:"k-remove-dialog",props:{text:s},on:{submit:()=>{t(),this.update(),this.$panel.dialog.close()}}})},add(s){s==="column"?this.table.forEach(t=>t.push("")):s==="row"&&this.table.push(Array(this.columns.length).fill("")),this.update()},insert(s,t,e="before"){const l=s==="column"?t:this.hasHeaders?t+1:t,a=e==="before"?l:l+1;s==="column"?(this.columns.splice(a,0,""),this.rows.forEach(i=>i.splice(a,0,""))):s==="row"&&this.table.splice(a,0,Array(this.columns.length).fill("")),this.update()},clear(s,t){const e=this.hasHeaders?t+1:t;s==="column"?this.table.forEach(l=>l[t]=""):s==="row"&&this.table[e].fill(""),this.update()},duplicate(s,t){const e=this.hasHeaders?t+1:t;s==="column"?(this.columns.splice(t+1,0,this.columns[t]),this.rows.forEach(l=>l.splice(t+1,0,l[t]))):s==="row"&&this.table.splice(e,0,this.table[e]),this.update()},remove(s,t){if(s==="column"){const e=this.$t("field.table.delete.confirm.column");this.showDialog(e,()=>{this.table.forEach(l=>l.splice(t,1))})}else if(s==="row"){const e=this.$t("field.table.delete.confirm.row");this.showDialog(e,()=>{const l=this.hasHeaders?t+1:t;this.table.splice(l,1)})}else if(s==="all"){const e=this.$t("field.table.delete.confirm.all");this.showDialog(e,()=>{this.columns.splice(0,this.columns.length,Array(this.minColumns).fill("")),this.table.splice(0,this.table.length,Array(this.minColumns).fill(""))})}},onDrag(s,t){const e=t==="column"?-1:this.hasHeaders?1:0,l=s.oldIndex+e,a=s.newIndex+e;t==="column"?(this.columns.splice(a,0,...this.columns.splice(l,1)),this.rows.forEach(i=>i.splice(a,0,...i.splice(l,1)))):t==="row"&&this.table.splice(a,0,...this.table.splice(l,1)),this.update()},update(){this.$emit("input",this.table)}}};var b=function(){var t=this,e=t._self._c;return e("k-field",t._b({staticClass:"k-table-field",scopedSlots:t._u([t.disabled?null:{key:"options",fn:function(){return[e("k-button-group",{attrs:{layout:"collapsed"}},[e("k-button",{attrs:{disabled:t.columns.length>=t.maxColumns,text:t.$t("field.table.column"),icon:"add",variant:"filled",size:"xs"},on:{click:function(l){return t.add("column")}}}),e("k-button",{attrs:{icon:"dots",size:"xs",variant:"filled"},on:{click:function(l){return t.$refs.options.toggle()}}}),e("k-dropdown-content",{ref:"options",attrs:{options:t.tableOptions,"align-x":"end"}})],1)]},proxy:!0}],null,!0)},"k-field",t.$props,!1),[e("div",{staticClass:"k-table",attrs:{"aria-disabled":t.disabled}},[e("table",{attrs:{"data-disabled":t.disabled,"data-indexed":t.hasIndexColumn}},[e("thead",[e("k-draggable",{attrs:{list:t.values,options:t.dragOptions,handle:!0,element:"tr"},on:{end:function(l){return t.onDrag(l,"column")}}},[t.hasIndexColumn?e("th",{staticClass:"k-table-index-column",attrs:{"data-mobile":"true"}},[t._v(" # ")]):t._e(),t._l(t.columns,function(l,a){return e("th",{key:a+"-header",class:["k-table-column","k-table-header",{"k-table-sortable-row":t.isSortable}],attrs:{"data-align":t.hasAlignment,"data-mobile":"true"}},[e("k-bar",[t.isSortable?e("k-sort-handle",{staticClass:"k-table-sort-handle"}):t._e(),t.hasHeaders?e("k-text-input",{attrs:{disabled:t.disabled,spellcheck:!1,placeholder:`${t.$t("field.table.column")} ${a+1}`},on:{input:function(i){return t.update()}},model:{value:t.columns[a],callback:function(i){t.$set(t.columns,a,i)},expression:"columns[columnIndex]"}}):e("k-text",[t._v(t._s(t.$t("field.table.column"))+" "+t._s(a+1))]),t.disabled?t._e():e("k-options-dropdown",{attrs:{options:t.columnOptions},on:{option:function(i){return t.columnOption(i,a)}}})],1)],1)}),t.hasOptions?e("th",{staticClass:"k-table-options-column",attrs:{"data-mobile":"true"}}):t._e()],2)],1),e("k-draggable",{attrs:{list:t.values,options:t.dragOptions,handle:!0,element:"tbody"},on:{end:function(l){return t.onDrag(l,"row")}}},[t.rows.length===0?e("tr",[e("td",{staticClass:"k-table-empty",attrs:{colspan:t.colspan}},[t._v(" "+t._s(t.empty??t.$t("field.table.empty.rows"))+" ")])]):t._l(t.rows,function(l,a){return e("tr",{key:a,class:["k-table-row",{"k-table-sortable-row":t.isSortable&&t.rows.length>1}]},[t.hasIndexColumn?e("td",{staticClass:"k-table-index-column",attrs:{"data-sortable":t.isSortable&&t.rows.length>1,"data-mobile":"true"}},[e("div",{staticClass:"k-table-index",domProps:{textContent:t._s(t.index+a)}}),t.isSortable&&t.rows.length>1?e("k-sort-handle",{staticClass:"k-table-sort-handle"}):t._e()],1):t._e(),t._l(l,function(i,o){return e("td",{key:a+"-"+o,staticClass:"k-table-column k-table-cell",attrs:{"data-align":t.hasAlignment,"data-mobile":"true"}},[e("k-writer-input",{attrs:{disabled:t.disabled,inline:!0,nodes:!1,marks:t.marks,spellcheck:!1},on:{input:function(n){return t.update()}},model:{value:l[o],callback:function(n){t.$set(l,o,n)},expression:"row[columnIndex]"}})],1)}),t.hasOptions?e("td",{staticClass:"k-table-options-column",attrs:{"data-mobile":"true"}},[e("k-options-dropdown",{attrs:{options:t.rowOptions},on:{option:function(i){return t.rowOption(i,a)}}})],1):t._e()],2)})],2)],1)]),t.disabled?t._e():e("footer",[e("k-button",{attrs:{text:t.$t("field.table.row"),icon:"add",size:"xs",variant:"filled"},on:{click:function(l){return t.add("row")}}})],1)])},p=[],m=h(d,b,p);const f=m.exports,k={computed:{captionMarks(){return this.field("caption",{marks:!0}).marks},table(){return this.content.table??Array.from({length:2},()=>Array(2).fill(""))},columns(){return this.table[0]},rows(){return this.table.slice(1)},tableField(){return this.field("table")},hasAlignment(){return this.tableField.align}}};var g=function(){var t=this,e=t._self._c;return e("k-block-figure",{attrs:{caption:t.content.caption,"caption-marks":t.captionMarks,disabled:t.disabled},on:{open:t.open,update:t.update}},[e("div",{staticClass:"k-table"},[e("table",[e("thead",[e("tr",t._l(t.columns,function(l,a){return e("th",{key:a+"header",staticClass:"k-table-column",attrs:{"data-align":t.hasAlignment,"data-mobile":"true"}},[e("k-text-input",{attrs:{type:"text",spellcheck:!1,placeholder:`${t.$t("field.table.column")} ${a+1}`},on:{input:t.update},model:{value:t.columns[a],callback:function(i){t.$set(t.columns,a,i)},expression:"columns[columnIndex]"}})],1)}),0)]),e("tbody",t._l(t.rows,function(l,a){return e("tr",{key:a},t._l(l,function(i,o){return e("td",{key:a+"-"+o,staticClass:"k-table-column",attrs:{"data-align":t.hasAlignment,"data-mobile":"true"}},[e("k-writer-input",{attrs:{inline:!0,nodes:!1,marks:t.tableField.marks,spellcheck:!1},on:{input:function(n){return t.update()}},model:{value:l[o],callback:function(n){t.$set(l,o,n)},expression:"row[columnIndex]"}})],1)}),0)}),0)])])])},_=[],w=h(k,g,_);const $=w.exports;panel.plugin("bogdancondorachi/table-field",{fields:{table:f},blocks:{dataTable:$},icons:{table:'<path d="M3 3C2.44772 3 2 3.44772 2 4V20C2 20.5523 2.44772 21 3 21H21C21.5523 21 22 20.5523 22 20V4C22 3.44772 21.5523 3 21 3H3ZM11 5V8H4V5H11ZM4 14V10H11V14H4ZM4 16H11V19H4V16ZM13 16H20V19H13V16ZM20 14H13V10H20V14ZM20 5V8H13V5H20Z"></path>',eraser:'<path d="M8.58564 8.85449L3.63589 13.8042L8.83021 18.9985L9.99985 18.9978V18.9966H11.1714L14.9496 15.2184L8.58564 8.85449ZM9.99985 7.44027L16.3638 13.8042L19.1922 10.9758L12.8283 4.61185L9.99985 7.44027ZM13.9999 18.9966H20.9999V20.9966H11.9999L8.00229 20.9991L1.51457 14.5113C1.12405 14.1208 1.12405 13.4877 1.51457 13.0971L12.1212 2.49053C12.5117 2.1 13.1449 2.1 13.5354 2.49053L21.3136 10.2687C21.7041 10.6592 21.7041 11.2924 21.3136 11.6829L13.9999 18.9966Z"></path>'}})})();