!function(e){"use strict";var t,i,n,a=window.JetThemeCoreData||{};i={LibraryLayoutView:null,LibraryHeaderView:null,LibraryLoadingView:null,LibraryErrorView:null,LibraryBodyView:null,LibraryCollectionView:null,FiltersCollectionView:null,LibraryTabsCollectionView:null,LibraryTabsItemView:null,FiltersItemView:null,LibraryTemplateItemView:null,LibraryInsertTemplateBehavior:null,LibraryTabsCollection:null,LibraryCollection:null,CategoriesCollection:null,LibraryTemplateModel:null,CategoryModel:null,TabModel:null,KeywordsModel:null,KeywordsView:null,LibraryPreviewView:null,LibraryHeaderBack:null,LibraryHeaderInsertButton:null,init:function(){var i=this;i.LibraryTemplateModel=Backbone.Model.extend({defaults:{template_id:0,name:"",title:"",thumbnail:"",preview:"",source:"",categories:[],keywords:[]}}),i.CategoryModel=Backbone.Model.extend({defaults:{slug:"",title:""}}),i.CategoryModel=Backbone.Model.extend({defaults:{slug:"",title:""}}),i.TabModel=Backbone.Model.extend({defaults:{slug:"",title:""}}),i.KeywordsModel=Backbone.Model.extend({defaults:{keywords:{}}}),i.LibraryCollection=Backbone.Collection.extend({model:i.LibraryTemplateModel}),i.CategoriesCollection=Backbone.Collection.extend({model:i.CategoryModel}),i.LibraryTabsCollection=Backbone.Collection.extend({model:i.TabModel}),i.LibraryLoadingView=Marionette.ItemView.extend({id:"jet-template-library-loading",template:"#tmpl-jet-template-library-loading"}),i.LibraryErrorView=Marionette.ItemView.extend({id:"jet-template-library-error",template:"#tmpl-jet-template-library-error"}),i.KeywordsView=Marionette.ItemView.extend({id:"jet-template-library-keywords",template:"#tmpl-jet-template-library-keywords",ui:{keywords:".jet-library-keywords"},events:{"change @ui.keywords":"onSelectKeyword"},onSelectKeyword:function(e){var i=e.currentTarget.selectedOptions[0].value;t.setFilter("keyword",i)}}),i.LibraryHeaderView=Marionette.LayoutView.extend({id:"jet-template-library-header",template:"#tmpl-jet-template-library-header",ui:{closeModal:"#jet-template-library-header-close-modal"},events:{"click @ui.closeModal":"onCloseModalClick"},regions:{headerTabs:"#jet-template-library-header-tabs",headerActions:"#jet-template-library-header-actions"},onCloseModalClick:function(){t.closeModal()}}),i.LibraryPreviewView=Marionette.ItemView.extend({template:"#tmpl-jet-template-library-preview",id:"jet-template-library-preview",ui:{img:"img"},onRender:function(){this.ui.img.attr("src",this.getOption("preview"))}}),i.LibraryHeaderBack=Marionette.ItemView.extend({template:"#tmpl-jet-template-library-header-back",id:"jet-template-library-header-back",ui:{button:"button"},events:{"click @ui.button":"onBackClick"},onBackClick:function(){t.setPreview("back")}}),i.LibraryInsertTemplateBehavior=Marionette.Behavior.extend({ui:{insertButton:".jet-template-library-template-insert"},events:{"click @ui.insertButton":"onInsertButtonClick"},onInsertButtonClick:function(){var e=this.view.model,i={};t.layout.showLoadingView(),elementor.templates.requestTemplateContent(e.get("source"),e.get("template_id"),{data:{tab:t.getTab(),page_settings:!0},success:function(n){n.licenseError?t.layout.showLicenseError():(t.closeModal(),elementor.channels.data.trigger("template:before:insert",e),null!==t.atIndex&&(i.at=t.atIndex),elementor.sections?elementor.sections.currentView.addChildModel(n.content,i):elementor.getPreviewView().addChildModel(n.content,i),n.page_settings&&elementor.settings.page.model.set(n.page_settings),elementor.channels.data.trigger("template:after:insert",e),t.atIndex=null,$e&&$e.run("document/save/update"))}})}}),i.LibraryHeaderInsertButton=Marionette.ItemView.extend({template:"#tmpl-jet-template-library-insert-button",id:"jet-template-library-insert-button",behaviors:{insertTemplate:{behaviorClass:i.LibraryInsertTemplateBehavior}}}),i.LibraryTemplateItemView=Marionette.ItemView.extend({template:"#tmpl-jet-template-library-item",className:function(){var e=" jet-template-has-url",t=" elementor-template-library-template-";return""===this.model.get("preview")&&(e=" jet-template-no-url"),"jet-local"===this.model.get("source")?t+="local":t+="remote","elementor-template-library-template"+t+e},ui:function(){return{previewButton:".elementor-template-library-template-preview",cloneButton:".jet-clone-to-library"}},events:function(){return{"click @ui.previewButton":"onPreviewButtonClick","click @ui.cloneButton":"onCloneButtonClick"}},onPreviewButtonClick:function(){""!==this.model.get("preview")&&t.setPreview(this.model)},onCloneButtonClick:function(){t.layout.showLoadingView(),e.ajax({url:ajaxurl,type:"post",dataType:"json",data:{action:"jet_theme_core_clone_template",template:this.model.attributes,tab:t.getTab()}}).done(function(e){!0===e.success?(t.channels.layout.trigger("template:cloned"),t.tabs.local.data={},t.setTab("local")):t.setTab(t.getTab())})},behaviors:{insertTemplate:{behaviorClass:i.LibraryInsertTemplateBehavior}}}),i.FiltersItemView=Marionette.ItemView.extend({template:"#tmpl-jet-template-library-filters-item",className:function(){return"jet-filter-item"},ui:function(){return{filterLabels:".jet-template-library-filter-label"}},events:function(){return{"click @ui.filterLabels":"onFilterClick"}},onFilterClick:function(e){var i=jQuery(e.target);t.setFilter("category",i.val())}}),i.LibraryTabsItemView=Marionette.ItemView.extend({template:"#tmpl-jet-template-library-tabs-item",className:function(){return"elementor-template-library-menu-item"},ui:function(){return{tabsLabels:"label",tabsInput:"input"}},events:function(){return{"click @ui.tabsLabels":"onTabClick"}},onRender:function(){this.model.get("slug")===t.getTab()&&this.ui.tabsInput.attr("checked","checked")},onTabClick:function(e){var i=jQuery(e.target);t.setTab(i.val()),t.setFilter("keyword","")}}),i.LibraryCollectionView=Marionette.CompositeView.extend({template:"#tmpl-jet-template-library-templates",id:"jet-template-library-templates",childViewContainer:"#jet-template-library-templates-container",initialize:function(){this.listenTo(t.channels.templates,"filter:change",this._renderChildren)},filter:function(e){var i=t.getFilter("category"),n=t.getFilter("keyword");return!i&&!n||(n&&!i?_.contains(e.get("keywords"),n):i&&!n?_.contains(e.get("categories"),i):_.contains(e.get("categories"),i)&&_.contains(e.get("keywords"),n))},getChildView:function(e){return i.LibraryTemplateItemView},onRenderCollection:function(){var e=this.$childViewContainer,n=this.$childViewContainer.children(),a=t.getTab();"jet_page"!==a&&"local"!==a&&setTimeout(function(){i.masonry.init({container:e,items:n})},200)}}),i.LibraryTabsCollectionView=Marionette.CompositeView.extend({template:"#tmpl-jet-template-library-tabs",childViewContainer:"#jet-template-library-tabs-items",initialize:function(){this.listenTo(t.channels.layout,"tamplate:cloned",this._renderChildren)},getChildView:function(e){return i.LibraryTabsItemView}}),i.FiltersCollectionView=Marionette.CompositeView.extend({id:"jet-template-library-filters",template:"#tmpl-jet-template-library-filters",childViewContainer:"#jet-template-library-filters-container",getChildView:function(e){return i.FiltersItemView}}),i.LibraryBodyView=Marionette.LayoutView.extend({id:"jet-template-library-content",className:function(){return"library-tab-"+t.getTab()},template:"#tmpl-jet-template-library-content",regions:{contentTemplates:".jet-templates-list",contentFilters:".jet-filters-list",contentKeywords:".jet-keywords-list"}}),i.LibraryLayoutView=Marionette.LayoutView.extend({el:"#jet-template-library-modal",regions:a.modalRegions,initialize:function(){this.getRegion("modalHeader").show(new i.LibraryHeaderView),this.listenTo(t.channels.tabs,"filter:change",this.switchTabs),this.listenTo(t.channels.layout,"preview:change",this.switchPreview)},switchTabs:function(){this.showLoadingView(),t.setFilter("keyword",""),t.requestTemplates(t.getTab())},switchPreview:function(){var e=this.getHeaderView(),n=t.getPreview();if("back"===n)return e.headerTabs.show(new i.LibraryTabsCollectionView({collection:t.collections.tabs})),e.headerActions.empty(),void t.setTab(t.getTab());"initial"!==n?(this.getRegion("modalContent").show(new i.LibraryPreviewView({preview:n.get("preview")})),e.headerTabs.show(new i.LibraryHeaderBack),e.headerActions.show(new i.LibraryHeaderInsertButton({model:n}))):e.headerActions.empty()},getHeaderView:function(){return this.getRegion("modalHeader").currentView},getContentView:function(){return this.getRegion("modalContent").currentView},showLoadingView:function(){this.modalContent.show(new i.LibraryLoadingView)},showLicenseError:function(){this.modalContent.show(new i.LibraryErrorView)},showTemplatesView:function(e,n,a){this.getRegion("modalContent").show(new i.LibraryBodyView);var r=this.getContentView(),l=this.getHeaderView(),o=new i.KeywordsModel({keywords:a});t.collections.tabs=new i.LibraryTabsCollection(t.getTabs()),l.headerTabs.show(new i.LibraryTabsCollectionView({collection:t.collections.tabs})),r.contentTemplates.show(new i.LibraryCollectionView({collection:e})),r.contentFilters.show(new i.FiltersCollectionView({collection:n})),r.contentKeywords.show(new i.KeywordsView({model:o}))}})},masonry:{self:{},elements:{},init:function(t){this.settings=e.extend(this.getDefaultSettings(),t),this.elements=this.getDefaultElements(),this.run()},getSettings:function(e){return e?this.settings[e]:this.settings},getDefaultSettings:function(){return{container:null,items:null,columnsCount:3,verticalSpaceBetween:30}},getDefaultElements:function(){return{$container:jQuery(this.getSettings("container")),$items:jQuery(this.getSettings("items"))}},run:function(){var e=[],t=this.elements.$container.position().top,i=this.getSettings(),n=i.columnsCount;t+=parseInt(this.elements.$container.css("margin-top"),10),this.elements.$container.height(""),this.elements.$items.each(function(a){var r=Math.floor(a/n),l=a%n,o=jQuery(this),s=o.position(),c=o[0].getBoundingClientRect().height+i.verticalSpaceBetween;if(r){var d=s.top-t-e[l];d-=parseInt(o.css("margin-top"),10),d*=-1,o.css("margin-top",d+"px"),e[l]+=c}else e.push(c)}),this.elements.$container.height(Math.max.apply(Math,e))}}},n={JetSearchView:null,init:function(){this.JetSearchView=window.elementor.modules.controls.BaseData.extend({hasTitles:!1,getAjaxUrl:function(t,i){var n="";return i.length>0&&e.each(i,function(e,t){window.elementor.settings.page.model.attributes[t]&&(n+="&"+t+"="+window.elementor.settings.page.model.attributes[t])}),ajaxurl+"?action="+t+n},onReady:function(){var t=this,i=this.model.attributes.action,n=this.model.attributes.query_params;this.ui.select.find("option").each(function(t,i){e(this).attr("selected",!0)}),this.ui.select.select2({ajax:{url:function(){return t.getAjaxUrl(i,n)},dataType:"json"},placeholder:"Please enter 3 or more characters",minimumInputLength:3,allowClear:!0}),this.hasTitles||this.getOptionsTitles()},getOptionsTitles:function(){var t=this,i=this.model.attributes.action,n=this.model.attributes.query_params,a=this.getControlValue();if(a){e.isArray(a)&&(a=a.join());var r=t.getAjaxUrl(i,n)+"&ids="+a;e.ajax({url:r,dataType:"json",beforeSend:function(){t.ui.select.prop("disabled",!0)},success:function(e){t.hasTitles=!0,t.model.set("saved",t.prepareOptions(e.results)),t.render()}})}},prepareOptions:function(t){var i={};return e.each(t,function(e,t){i[t.id]=t.text}),i},onBeforeDestroy:function(){this.ui.select.data("select2")&&this.ui.select.select2("destroy"),this.$el.remove()}}),window.elementor.addControlView("jet_search",this.JetSearchView)}},t={modal:!1,layout:!1,collections:{},tabs:{},defaultTab:"",channels:{},atIndex:null,init:function(){window.elementor.on("preview:loaded",window._.bind(t.onPreviewLoaded,t)),i.init(),n.init()},onPreviewLoaded:function(){this.initMagicButton(),window.elementor.$previewContents.on("click.addJetTemplate",".add-jet-template",_.bind(this.showTemplatesModal,this)),this.channels={templates:Backbone.Radio.channel("JET_THEME_EDITOR:templates"),tabs:Backbone.Radio.channel("JET_THEME_EDITOR:tabs"),layout:Backbone.Radio.channel("JET_THEME_EDITOR:layout")},this.tabs=a.tabs,this.defaultTab=a.defaultTab},initMagicButton:function(){var i='<div class="elementor-add-section-area-button add-jet-template">'+a.libraryButton+"</div>";window.elementor.on("document:loaded",function(){var t=window.elementor.$previewContents.find(".elementor-add-new-section");t.length&&a.libraryButton&&e(i).prependTo(t)}),window.elementor.$previewContents.on("click.addJetTemplate",".elementor-editor-section-settings .elementor-editor-element-add",function(){var n=e(this).closest(".elementor-top-section"),r=n.data("model-cid"),l=null;window.elementor.sections&&window.elementor.sections.currentView.collection.length?l=window.elementor.sections.currentView.collection.models:elementor.getPreviewView().collection.length&&(l=elementor.getPreviewView().collection.models),l&&e.each(l,function(e,i){r===i.cid&&(t.atIndex=e)}),a.libraryButton&&setTimeout(function(){n.prev(".elementor-add-section").find(".elementor-add-new-section").prepend(i)},100)})},getFilter:function(e){return this.channels.templates.request("filter:"+e)},setFilter:function(e,t){this.channels.templates.reply("filter:"+e,t),this.channels.templates.trigger("filter:change")},getTab:function(){return this.channels.tabs.request("filter:tabs")},setTab:function(e,t){this.channels.tabs.reply("filter:tabs",e),t||this.channels.tabs.trigger("filter:change")},getTabs:function(){var e=[];return _.each(this.tabs,function(t,i){e.push({slug:i,title:t.title})}),e},getPreview:function(e){return this.channels.layout.request("preview")},setPreview:function(e,t){this.channels.layout.reply("preview",e),t||this.channels.layout.trigger("preview:change")},getKeywords:function(){return _.each(this.keywords,function(e,t){tabs.push({slug:t,title:e})}),[]},showTemplatesModal:function(){this.getModal().show(),this.layout||(this.layout=new i.LibraryLayoutView,this.layout.showLoadingView()),this.setTab(this.defaultTab,!0),this.requestTemplates(this.defaultTab),this.setPreview("initial")},requestTemplates:function(t){var n=this,a=n.tabs[t];n.setFilter("category",!1),a.data.templates&&a.data.categories?n.layout.showTemplatesView(a.data.templates,a.data.categories,a.data.keywords):e.ajax({url:ajaxurl,type:"get",dataType:"json",data:{action:"jet_theme_get_templates",tab:t},success:function(e){var a=new i.LibraryCollection(e.data.templates),r=new i.CategoriesCollection(e.data.categories);n.tabs[t].data={templates:a,categories:r,keywords:e.data.keywords},n.layout.showTemplatesView(a,r,e.data.keywords)}})},closeModal:function(){this.getModal().hide()},getModal:function(){return this.modal||(this.modal=elementor.dialogsManager.createWidget("lightbox",{id:"jet-template-library-modal",closeButton:!1})),this.modal}},e(window).on("elementor:init",t.init)}(jQuery);