// Ref: https://github.com/elementor/elementor/blob/v3.4.8/assets/dev/js/admin/admin-feedback.js
/* global jQuery */
( function( $, window ) {
'use strict';
var Xhyre_Template_Kits = {
initConstants: function() {
this.xhyre_template_kits_obj_name = xhyre_template_kits_data.obj_name;
this.xhyre_template_kits_obj = window[ this.xhyre_template_kits_obj_name ];
this.xhyre_template_kits_btn_class = this.xhyre_template_kits_obj.xhyre_template_kits_btn_class;
this.xhyre_template_kits_btn_title = this.xhyre_template_kits_obj.xhyre_template_kits_btn_title;
this.xhyre_template_kits_btn_logo = this.xhyre_template_kits_obj.xhyre_template_kits_btn_logo;
this.xhyre_template_kits_categories = this.xhyre_template_kits_obj.xhyre_template_kits_categories;
this.xhyre_template_kits_mdl_icon = this.xhyre_template_kits_obj.xhyre_template_kits_mdl_icon;
this.xhyre_template_kits_mdl_icon_bg_color = this.xhyre_template_kits_obj.xhyre_template_kits_mdl_icon_bg_color;
this.xhyre_template_kits_mdl_title = this.xhyre_template_kits_obj.xhyre_template_kits_mdl_title;
this.template_data = [];
this.template_data_error = false;
this.compare_versions = window.elementor.helpers.compareVersions;
this.templates_hash = [];
},
bindEvents: function() {
var self = this,
event = ( this.compare_versions( window.elementor.config.version, '2.8.5', '>' ) ) ? 'document:loaded' : 'preview:loaded';
$( 'body' ).on( 'click', '.xhyre-template-kits-template .elementor-template-library-template-insert', function( event ) {
event.preventDefault();
var el = this;
self.insertTemplate( el, event );
});
$( 'body' ).on( 'click', '.elementor-templates-modal__header__close', function( event ) {
event.preventDefault();
self.getModal().hide();
});
// Caregory Filter.
$( 'body' ).on( 'click', '#elementor-template-library-filter .elementor-template-library-filter-tab', function( event ) {
var cat = $(this).attr( 'data-value' );
$('#elementor-template-library-filter .elementor-template-library-filter-tab.selected').removeClass('selected');
$(this).addClass( 'selected' );
$('.elementor-template-library-template.xhyre-template-kits-template').removeClass('cat-hidden');
'all' != cat && $('.elementor-template-library-template.xhyre-template-kits-template').not( self.templates_hash[ cat ] ).addClass('cat-hidden');
});
// Template search.
$( 'body' ).on( 'keyup', '#elementor-template-library-filter-text', function( event ) {
var val = $(this).val();
val.length ? self.searchByName( val ) : self.clearSearch();
});
$( 'body' ).on( 'click', '#elementor-template-library-header-sync', function( event ) {
event.preventDefault();
self.syncLibrary( {
show_templates: true,
resync: true,
});
});
window.elementor.on( event, function() {
self.initTemplateLibrary();
self.syncLibrary();
});
},
searchByName: function( val ) {
var self = this;
$('.elementor-template-library-template.xhyre-template-kits-template').removeClass('search-hidden');
var searched_els = $('.elementor-template-library-template.xhyre-template-kits-template[data-template_id*="' + this.slugify( val ) + '"]');
$('.elementor-template-library-template.xhyre-template-kits-template').not( searched_els ).addClass('search-hidden');
},
clearSearch: function() {
var self = this;
$('.elementor-template-library-template.xhyre-template-kits-template').removeClass('search-hidden');
},
slugify: function( text ) {
var self = this;
return text.toLowerCase().replace(/[^\w ]+/g, "").replace(/ +/g, "-");
},
initTemplateLibrary: function() {
var self = this;
self.insertButton();
window.elementor.$previewContents.find( '.' + this.xhyre_template_kits_btn_class ).on('click', function( event ) {
event.preventDefault();
self.showModal();
});
},
insertButton: function() {
var self = this;
if ( window.elementor.$previewContents.find( '.' + this.xhyre_template_kits_btn_class ).length > 0 ) {
return;
}
var btn_img = $('', {
src: self.xhyre_template_kits_btn_logo,
class: self.xhyre_template_kits_btn_class + '-img'
});
var btn_tooltip = $('