text: var runningAutoSave=!1,PageNewForm=new Vue({el:"#PageNewForm-root",data:{debug:DEBUGMODE,loader:!0,editMode:!1,page_id:null,user:null,form:new VueForm({title:{value:null,required:!0,type:"username",maxLength:120,minLength:5,customPattern:/[a-zA-Z0-9,#.-\s]+/},subtitle:{value:null,required:!1,type:"username",maxLength:120,minLength:5,customPattern:/[a-zA-Z0-9,#.-\s]+/}}),status:!1,path:"",content:"Content of the page",visibility:1,publishondate:!0,datepublish:"",timepublish:"",template:"default",layout:"default",categorie_id:"0",subcategorie_id:"0",page_type_id:"1",layouts:[],mainImage:[],templates:[],pageTypes:[],categories:[],subcategories:[],page_data:{title:""},customMetas:[],metas:[{name:"author",content:new User(JSON.parse(localStorage.getItem("userdata"))).get_fullname()},{name:"keywords",content:""},{name:"description",content:""},{name:"ROBOTS",content:"NOODP"},{name:"GOOGLEBOT",content:"INDEX, FOLLOW"},{property:"og:title",content:""},{property:"og:description",content:""},{property:"og:site_name",content:SITE_TITLE},{property:"og:url",content:""},{property:"og:image",content:""},{property:"og:type",content:"article"},{property:"twitter:card",content:"summary"},{property:"twitter:title",content:""},{property:"twitter:description",content:""},{property:"twitter:image",content:""}]},mixins:[mixins],computed:{btnEnable:function(){let e=!!this.form.fields.title.value&&!!this.path||!1;return e&&this.autoSave(),e},getDateTimePublish:function(){return this.datepublish&&this.timepublish?this.datepublish+" "+this.timepublish+":00":null},preview_link:function(){return this.page_id?BASEURL+"admin/paginas/preview?page_id="+this.page_id:""},getMainImagenPath(){return this.mainImage.length>0?this.mainImage[0].file_id:null},getPagePath(){let e=this.getPathSegments().filter((e,t)=>e.length>0);return e=e.map((e,t)=>this.string_to_slug(e)),e.join("/")}},watch:{"form.fields.title.value":function(e){this.setPath(e)},publishondate:function(e){e&&(this.datepublish="",this.timepublish="")}},filters:{capitalize:function(e){return e?(e=e.toString()).charAt(0).toUpperCase()+e.slice(1):""}},methods:{addCustomMeta(){this.customMetas.push({name:"",content:""})},removeMeta(e,t=!0){t?this.customMetas.splice(e,1):this.metas.splice(e,1)},setMetaContent(e,t,a){void 0===a?this.metas=this.metas.map(a=>(a.property!=t&&a.name!=t||(a.content=e),a)):this.metas[a].content=e},getPathSegments(){let e="";1==this.page_type_id?e="":(e=this.getSelectedPageType(),e=e.length?e[0].page_type_name:"");let t=this.getSelectedCategorie();t=t[0]?t[0].name:"",t=""==e?"":t;let a=this.getSelectedSubCategorie();return a=a[0]?a[0].name:"",a=""==e?"":a,[e,t,a,this.path]},onChangeTitle(e){this.page_data.title=e,this.setMetaContent(e,"og:title"),this.setMetaContent(e,"twitter:title"),this.setMetaContent(e,"keywords")},autoSave(){this.status||runningAutoSave||(runningAutoSave=!0,setTimeout(()=>{this.runSaveData(),this.debug&&console.log("running autosave..."),runningAutoSave=!1},3e3))},removeImage(e){this.mainImage.length>0&&this.mainImage.splice(e,1),0==this.mainImage.length&&(this.mainImage=[])},getFileImagenPath(e){return BASEURL+e.file_path.substr(2)+this.getFileImagenName(e)},getFileImagenName:e=>e.file_name+"."+e.file_type,setPath(e){let t=this.string_to_slug(e);this.path=t,this.setMetaContent(BASEURL+t,"og:url")},string_to_slug:function(e){if(0==e.length)return"";e=(e=e.replace(/^\s+|\s+$/g,"")).toLowerCase();for(var t="àáäâèéëêìíïîòóöôùúüûñç·/_,:;",a=0,i=t.length;a<i;a++)e=e.replace(new RegExp(t.charAt(a),"g"),"aaaaeeeeiiiioooouuuunc------".charAt(a));return e=e.replace(/[^a-z0-9 -]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-")},validateField(e){let t=UserNewForm;if(!t.form.validateField(e))return t.form.fields[e].valid;t.serverValidation(e)},validateForm(){this.form.validate();return this.publishondate||this.datepublish||this.timepublish||(error=!1),0==this.form.errors.length&&!0},save(){this.validateForm()?(this.loader=!0,this.runSaveData((function(e){if(1==e.data.status)var t='<span>Page saved </span><a target="_blank" href="'+BASEURL+e.data.path+'" class="btn-flat toast-action">View in site</a>';else t='<span>Draf saved </span><a target="_blank" href="'+BASEURL+"admin/paginas/preview?page_id="+e.data.page_id+'" class="btn-flat toast-action">Preview</a>';M.toast({html:t})}))):M.toast({html:"Verifique todos los campos del formulario"})},runSaveData(e){var t=this,a=BASEURL+"api/v1/pages";$.ajax({type:"POST",url:a,data:t.getData(),dataType:"json",success:function(i){t.debug&&console.log(a,i),setTimeout(()=>{t.loader=!1},1500),200==i.code?(t.editMode=!0,t.page_id=i.data.page_id,"function"==typeof e&&e(i)):(M.toast({html:i.responseJSON.error_message}),t.loader=!1)},error:function(e){M.toast({html:e.responseJSON.error_message}),t.loader=!1}})},getData:function(){return{title:this.form.fields.title.value||"",subtitle:this.form.fields.subtitle.value||"",path:this.getPagePath||"",page_type_id:this.page_type_id||1,status:this.status?1:2,content:wp.data.select("core/editor").getEditedPostContent()||"Content of the page...",page_id:this.page_id||null,publishondate:this.publishondate,date_publish:this.getDateTimePublish,visibility:this.visibility,template:this.template||"default",layout:this.layout||"default",categorie_id:this.categorie_id||0,subcategorie_id:this.subcategorie_id||0,mainImage:this.getMainImagenPath,page_data:{tags:this.getPageTags(),title:this.page_data.title,footer_includes:this.page_data.footer_includes,headers_includes:this.page_data.headers_includes,meta:[...this.metas,...this.customMetas]}}},getPageTags(){let e=[];return M.Chips.getInstance(document.getElementById("pageTags")).chipsData.forEach(t=>{e.push(t.tag)}),e},getTemplates(){var e=this,t=BASEURL+"api/v1/pages/templates";$.ajax({type:"GET",url:t,data:{},dataType:"json",success:function(a){e.loader=!1,e.debug&&console.log(t,a),200==a.code&&(e.templates=a.data.templates.map((function(e){let t=e.split(".")[0];return"template"==t?"default":t})),e.layouts=a.data.layouts.map((function(e){let t=e.split(".")[0];return"site"==t?"default":t})))},error:function(t){e.debug&&console.log(t),e.loader=!1}})},getSelectedPageType(){return this.pageTypes.filter((e,t)=>this.page_type_id==e.page_type_id)},getSelectedCategorie(){return this.categories.filter((e,t)=>this.categorie_id==e.categorie_id)},getSelectedSubCategorie(){return this.subcategories.filter((e,t)=>this.subcategorie_id==e.categorie_id)},serverValidation(e){var t=this,a=BASEURL+"admin/usuarios/ajax_check_field";$.ajax({type:"POST",url:a,data:{field:e,value:t.form.fields[e].value},dataType:"json",success:function(i){t.debug&&console.log(a,i),i.code&&(t.form.fields[e].valid=i.data,i.data?t.form.markFieldAsValid(e):t.form.fields[e].errorText="The "+e+" is already registered",t.$forceUpdate())}})},getPageTypes(){var e=this,t=BASEURL+"api/v1/pages/types";$.ajax({type:"GET",url:t,data:{},dataType:"json",success:function(a){e.debug&&console.log(t,a),e.loader=!1,200==a.code&&(e.pageTypes=a.data)},error:function(t){e.debug&&console.log(t),e.loader=!1}})},getCategories(){var e=this,t=BASEURL+"api/v1/categorie/type/page";$.ajax({type:"GET",url:t,data:{},dataType:"json",success:function(a){e.loader=!1,e.debug&&console.log(t,a),200==a.code&&(e.categories=a.data)},error:function(t){e.debug&&console.log(t),e.loader=!1}})},getSubCategories(){var e=this,t=BASEURL+"api/v1/categorie/subcategorie/"+e.categorie_id;$.ajax({type:"GET",url:t,data:{},dataType:"json",success:function(a){e.debug&&console.log(t,a),e.loader=!1,200==a.code&&(e.subcategories=a.data,PageNewForm.initSelects())},error:function(t){e.debug&&console.log(t),e.loader=!1}})},checkEditMode(){var e=document.getElementById("page_id").value,t=document.getElementById("editMode").value;if(localStorage.removeItem("g-editor-page"),e&&"edit"==t){var a=this;a.editMode=!0;var i=BASEURL+"api/v1/pages/editpageinfo/"+e;fetch(i).then(e=>e.json()).then(e=>{if(a.loader=!1,a.debug&&console.log(i,e),200==e.code){a.form.fields.title.value=e.data.page.title,a.form.fields.subtitle.value=e.data.page.subtitle,a.page_id=e.data.page.page_id,a.status="1"==e.data.page.status,a.path=e.data.page.path,a.visibility=e.data.page.visibility,a.publishondate=!!e.data.page.date_publish,a.template=e.data.page.template,a.categorie_id=e.data.page.categorie_id||0,a.subcategories_id=e.data.page.subcategories_id||0,a.pageTypes=e.data.page_types,a.page_type_id=e.data.page.page_type_id,a.page_data=e.data.page.page_data;const t=M.Chips.getInstance(document.getElementById("pageTags"));a.page_data.tags&&a.page_data.tags.forEach(e=>{t.addChip({tag:e})}),e.data.page.page_data.meta&&(a.metas=e.data.page.page_data.meta),a.user=new User(e.data.page.user),e.data.page.main_image&&a.mainImage.push(e.data.page.main_image),a.templates=e.data.templates.map((function(e){let t=e.split(".")[0];return"template"==t?"default":t})),a.layouts=e.data.layouts.map((function(e){let t=e.split(".")[0];return"site"==t?"default":t})),a.content=e.data.page,a.setEditorContent(e.data.page)}setTimeout(()=>{M.updateTextFields()},1e3)}).catch(e=>{M.toast({html:e.responseJSON.error_message}),a.loader=!1})}else this.getPageTypes(),this.getTemplates()},copyCallcack(e){let t=e[0];this.mainImage.push(t),M.Modal.getInstance($("#fileUploader")).close()},initPlugins(){M.Chips.init(document.getElementById("pageTags"),{}),tinymce.init({selector:"#id_cazary",plugins:["link table code"],setup:e=>{e.on("Change",e=>{PageNewForm.content=tinymce.editors.id_cazary.getContent();let t=this.getcontentText(PageNewForm.content,200);t=t.replace(/(\r\n|\n|\r)/gm,""),this.setMetaContent(t,"description"),this.setMetaContent(t,"og:description"),this.setMetaContent(t,"twitter:description")})}}),setTimeout(()=>{M.Tabs.init(document.getElementById("formTabs"),{});var e=document.getElementById("pageMetas");M.Collapsible.init(e,{accordion:!1}),e=document.querySelectorAll(".datepicker");M.Datepicker.init(e,{format:"yyyy-mm-dd",onClose:function(){PageNewForm.datepublish=document.getElementById("datepublish").value}});e=document.querySelectorAll(".timepicker");M.Timepicker.init(e,{twelveHour:!1,defaultTime:"now",onCloseEnd:function(){PageNewForm.timepublish=document.getElementById("timepublish").value}}),this.initSelects()},1e3)},initSelects(){setTimeout(()=>{var e=document.querySelectorAll("select");M.FormSelect.init(e,{})},1e3)},setEditorContent:function(e){console.log({page:e});let t={id:1,content:{raw:e.content,rendered:e.content},date:"2020-11-24T21:22:54.913Z",date_gmt:"2020-11-24T21:22:54.913Z",title:{raw:"Preview page",rendered:"Preview page"},excerpt:{raw:"",rendered:""},status:"draft",revisions:{count:0,last_id:0},parent:0,theme_style:!0,type:"page",link:"http://localhost:8000/preview",categories:[],featured_media:0,permalink_template:"http://localhost:8000/preview",preview_link:"http://localhost:8000/preview",_links:{"wp:action-assign-categories":[],"wp:action-create-categories":[]}};localStorage.setItem("g-editor-page",JSON.stringify(t))}},mounted:function(){this.$nextTick((function(){this.debug&&console.log("mounted PageNewForm"),this.initPlugins(),this.checkEditMode(),this.getCategories()}))}});