Current File : /home/inlingua/www/crm/ninexb/wp-content/plugins/tablesome/src/pages/cpt-page/ajax-events.js |
import{get}from"svelte/store";import{notifications}from"./../../table/svelte/notification/notifications";var tableProps=require("./props"),ajaxEvents={alertMessage:"Table Created, Successfully",saveTable:function(e,t="backend"){const o=this;let n=this.getModifiedRecords(get(e.rowStore.rows)),s=get(e.workflow),a=this.getTriggers(s.triggers);const i=parseInt(e.tableID);let r={table_id:i,records_deleted:get(e.rowStore.recordsDeleted),records_inserted:n.insertedRecords,records_updated:n.updatedRecords,columns_deleted:get(e.columnStore.columnsDeleted),columns_duplicated:get(e.columnStore.columnsDuplicated),mode:get(e.mode)};const c={table_id:i,table_title:tableProps.getTitle(),columns:get(e.columnStore.columns),last_column_id:get(e.columnStore.lastColumnID),triggers:a,editorState:get(e.editorState),access_control:get(e.accessControl).fields,display:get(e.display),style:get(e.style),mode:get(e.mode),sort:{order:get(e.sortOrder),field:get(e.sortField),index:get(e.activeColumnIndex)},recordsData:r,origin_location:t};i>0&&(o.alertMessage="Table Updated, Successfully"),fetch(tablesome_ajax_object.api_endpoints.save_table,{method:"post",headers:{"Content-Type":"application/json","X-WP-Nonce":tablesome_ajax_object.rest_nonce},body:JSON.stringify(c)}).then((function(e){return e.json()})).then((function(e){return e})).then((function(e){o.saveNotice(e),"success"==e.status&&e.table_id>0&&o.afterSave(e.table_id,c.mode)})).catch((function(e){o.errorNotice(e)}))},errorNotice:function(e){let t=jQuery("<span class='save-notice failed'>"+e+"</span>");jQuery(".tablesome__button--wrapper").append(t)},capitalize:function(e){return e.split(" ").map((function(e){return e.charAt(0).toUpperCase()+e.slice(1)})).join(" ")},saveNotice:function(e){let t=jQuery("<span class='save-notice "+e.status+"'>"+this.capitalize(e.status)+" : "+e.message+"</span>");jQuery(".tablesome__button--wrapper").append(t)},sendRecords:function(e,t="editor"){var o=this;fetch(tablesome_ajax_object.api_endpoints.prefix+e.table_id+"/records",{method:"post",headers:{"Content-Type":"application/json","X-WP-Nonce":tablesome_ajax_object.rest_nonce},body:JSON.stringify(e)}).then((function(t){o.afterSave(e.table_id)})).then((function(e){})).catch((function(e){}))},afterSave:function(e,t){notifications.success(this.alertMessage,"",5e3);let o=window.location.href;"editor"==t&&(o=new URL(tablesome_ajax_object.edit_table_url),o.searchParams.set("post",e)),window.location.replace(o)},getModifiedRecords:function(e){let t=[],o=[];return e.map((function(e,n){0==e.record_id?t.push(e):o.push(e)})),{insertedRecords:t,updatedRecords:o}},getTriggers:function(e){return e.map((function(e){return e.actions&&e.actions.length>0&&(e.actions=e.actions.map((function(e){return e.match_columns&&e.match_columns.length>0&&(e.match_columns=e.match_columns.map((function(e){return{...e}}))),e.match_fields&&e.match_fields.length>0&&(e.match_fields=e.match_fields.map((function(e){return{...e}}))),e.conditions&&e.conditions.length>0&&(e.conditions=e.conditions.map((function(e){return{...e}}))),{...e}}))),{...e}}))}};export default ajaxEvents;