import ajaxEvents from"./ajax-events";import ShortcodeCustomizer from"../../shortcode-customizer/ShortcodeCustomizer.svelte";var selectors={page:".tablesome_cpt",pageAction:".tablesome_cpt [name='post_action']",title:".tablesome_cpt .tablesome__inputText--title",shortcode:".tablesome_cpt .tablesome__fields .tablesome__field--clipboardText",customizeButton:".tablesome_cpt .tablesome__field--customizeButton",shortcodeInput:"#tablesome__field--shortcode",submitCreateMode:".tablesome_cpt .tablesome__button--submit, #tablesome__container .tablesome__button--submit.create-mode",wpSubMenu:"#menu-posts-tablesome_cpt ul.wp-submenu li",spinner:".tablesome__spinner"},page={shortcodeCustomizerInstance:null,init:function(e){this.tableStore=e,this.eventListeners(),this.initShortcodeCustomizer()},eventListeners:function(){this.saveTableCreateMode(),this.setEditPageMenuActive(),this.copyShortcodeToClipboard(),this.initCustomizeButton()},initShortcodeCustomizer:function(){const e=document.getElementById("tablesome-shortcode-customizer-container");if(!e)return;const t=document.querySelector(selectors.shortcodeInput);if(!t)return;const o=parseInt(t.getAttribute("data-table-id"),10)||null,s=t.getAttribute("data-initial-shortcode")||"";this.shortcodeCustomizerInstance=new ShortcodeCustomizer({target:e,props:{tableId:o,initialShortcode:s,isOpen:!1}}),this.shortcodeCustomizerInstance.$on("apply",(e=>{const{shortcode:o}=e.detail;if(t&&o){t.value=o;const e=new Event("change",{bubbles:!0});t.dispatchEvent(e)}}))},initCustomizeButton:function(){const e=this;jQuery(selectors.customizeButton).on("click",(function(){const t=jQuery(this),o=parseInt(t.attr("data-table-id"),10),s=document.querySelector(selectors.shortcodeInput),n=s?s.value:"";e.shortcodeCustomizerInstance&&e.shortcodeCustomizerInstance.$set({tableId:o,initialShortcode:n,isOpen:!0})}))},saveTableCreateMode:function(){var e=this;jQuery(selectors.submitCreateMode).on("click",(function(t){t.preventDefault(),ajaxEvents.saveTable(e.tableStore),page.disableSubmitButton(),page.loadSpinner()}))},setEditPageMenuActive:function(){var e=jQuery(selectors.wpSubMenu);if(0==e.length)return!1;var t=jQuery(selectors.pageAction).val();e.each((function(){var o=jQuery(this).find("a").text();"edit"==t&&"All Tables"===o&&(e.removeClass("current"),jQuery(this).addClass("current"))}))},copyShortcodeToClipboard:function(){jQuery(selectors.shortcode).on("click",(function(){var e=document.getElementById("tablesome__field--shortcode");e.select(),e.setSelectionRange(0,99999),document.execCommand("copy")}))},checkTitleforSubmit:function(){jQuery(selectors.title).on("change",(function(){if(0==jQuery(this).val().trim().length)return page.disableSubmitButton(),!1;page.enableSubmitButton()}))},disableSubmitButton:function(){jQuery(selectors.submit).attr("disabled","disabled")},enableSubmitButton:function(){jQuery(selectors.submit).removeAttr("disabled")},loadSpinner:function(){jQuery(selectors.spinner).addClass("active")},hideSpinner:function(){jQuery(selectors.spinner).removeClass("active")}};export default page;