File: /home4/narrativ/wp.narrativa.mx/wp-content/themes/jnews/assets/js/admin/shortcode-generator.js
!function(t,e){"use strict";e.shortcodepopup=new e.Values({defaultConstructor:e.ShortCodeListPopup}),e.shortcodecontainer=new e.Values({defaultConstructor:e.ShortCodeContainer}),e.ShortCodeContainer=e.BaseContainer.extend({setContainerHolder:function(){this.containerHolder=e.shortcodecontainer},attachEvent:function(){t(this.element).formtab()},prepareSegmentData:function(t,e,n){return t.parent=this.id,t.container=this,t.type="tabbed",t.index=n,t},prepareFieldData:function(t,e,n){return t.container=this,t.index=n,t}}),e.ShortCodeItem=e.Class.extend({id:null,data:null,element:null,container:null,popupContainer:null,initialize:function(e,n){this.id=e,this.data=n,this.popupContainer=t(".shortcode-option-wrapper"),this.renderItem(),this.attachEvent()},renderItem:function(){var e=wp.template("shortcode-item");this.element=t(e({id:this.id,name:this.data.name})),this.container=this.data.parent.container.find("[id="+this.data.category+"] .jeg_metabox_body"),this.container.append(this.element)},attachEvent:function(){this.element.on("click",this.showShortCodeOptionWindow.bind(this))},showShortCodeOptionWindow:function(){e.shortcodepopup.instance("jnews").hidePopup(),this.setupPopup()},setupPopup:function(){this.renderPopup(),this.showPopup(),this.resetPosition(),this.renderShortCodeOption()},resetPosition:function(){var e=t(window).height(),n=t(window).width(),i=Math.floor(80*n/100);i=i>750?750:i;var o=Math.floor(80*e/100),a=(n-i)/2,r=(e-(o=o>700?700:o))/2;this.popupContainer.css({width:i,height:o,top:r,left:a});var s=this.popupContainer.find(".popup-header").outerHeight()+this.popupContainer.find(".tabbed-list").outerHeight()+this.popupContainer.find(".popup-footer").outerHeight();this.popupContainer.find(".popup-content").css({height:o-s})},renderPopup:function(){this.popupContainer.html("");var t=wp.template("shortcode-option")({header:this.data.name,close:window.jnews.close,generate:window.jnews.generate});this.popupContainer.append(t),this.attachPopupEvent()},attachPopupEvent:function(){this.popupContainer.find(".close").on("click",this.hidePopup.bind(this)),this.popupContainer.find(".generate").on("click",this.generateShortcode.bind(this)),this.popupContainer.draggable({handle:".popup-header",start:function(){t(".iconpicker-popover").remove()}})},showPopup:function(){this.popupContainer.addClass("active")},hidePopup:function(){this.popupContainer.removeClass("active")},renderShortCodeOption:function(){this.loadAjax().done(this.shortCodeOptionLoaded.bind(this))},loadAjax:function(){return wp.ajax.send(this.id,{data:{nonce:window.jnews.nonce}})},shortCodeOptionLoaded:function(t){e.shortcodecontainer.has(this.id)&&e.shortcodecontainer.remove(this.id),e.shortcodecontainer.add(this.id,new e.ShortCodeContainer(this.id,this.popupContainer,t)),this.resetPosition()},generateShortcode:function(){var t=this,n=e.shortcodecontainer.instance(t.id),i=[];n.segments.each((function(e){e.fields.each((function(e){var n=e.value.get();e.params.default!=n&&(n=t.filterValue(n),i.push(e.id+'="'+n+'"'))}))})),i="["+t.id+" "+i.join(" ")+"]",t.data.editor.execCommand("mceInsertContent",!1,i),t.hidePopup()},filterValue:function(t){return t}}),e.ShortCodeCategory=e.Class.extend({id:null,index:0,data:null,initialize:function(t,e,n){this.id=t,this.data=n,this.index=e,this.renderCategory()},renderCategory:function(){var t=this.data.parent.container,e={id:this.id,text:this.data.category,index:this.index},n=wp.template("shortcode-category-list")(e);t.find(".tabbed-list").append(n);var i=wp.template("shortcode-category")(e);t.find(".tabbed-body").append(i)}}),e.ShortCodeListPopup=e.Class.extend({editor:null,data:null,categories:null,items:null,container:null,initialize:function(n){this.editor=n,this.categories=new e.Values({defaultConstructor:e.ShortCodeCategory}),this.items=new e.Values({defaultConstructor:e.ShortCodeItem}),this.data=window.jnews,this.container=t(".shortcode-popup-list-wrapper"),this.setupPopup(),this.showPopup(),this.resetPosition(!0)},setupPopup:function(){this.setupContainer(),this.setupCategory(),this.setupElement(),this.attachEvent()},setupContainer:function(){var t=wp.template("shortcode-popup")({header:this.data.title});this.container.append(t)},attachEvent:function(){this.container.find(".close").on("click",this.hidePopup.bind(this)),this.container.formtab(),this.container.draggable({handle:".popup-header"}),t(window).bind("resize",this.resetPosition.bind(this))},resetPosition:function(){var e=t(window).height(),n=t(window).width(),i=Math.floor(80*n/100);i=i>1200?1200:i;var o=Math.floor(80*e/100),a=(n-i)/2,r=(e-(o=o>700?700:o))/2;this.container.css({width:i,height:o,top:r,left:a});var s=this.container.find(".popup-header").outerHeight()+this.container.find(".tabbed-list").outerHeight();this.container.find(".popup-content").css({height:o-s})},setupCategory:function(){var t=this,n=0;_.each(t.data.elements,(function(i){var o=t.slugify(i.category);t.categories.has(o)||t.categories.add(o,new e.ShortCodeCategory(o,n++,{category:i.category,parent:t}))}))},setupElement:function(){var t=this;_.each(t.data.elements,(function(n,i){t.items.add(i,new e.ShortCodeItem(i,{id:i,name:n.name,category:t.slugify(n.category),parent:t,editor:t.editor}))}))},showPopup:function(){this.container.addClass("active")},hidePopup:function(){this.container.removeClass("active")},slugify:function(t){return t.toString().toLowerCase().replace(/\s+/g,"-").replace(/[^\w\-]+/g,"").replace(/\-\-+/g,"-").replace(/^-+/,"").replace(/-+$/,"")}})}(jQuery,wp.customize);