Ubuntu
/* global jQuery, woodmartConfig */
(function($) {
'use strict';
function vcTemplatesLibrary() {
var $head = $('.xts-wpb-templates-heading'),
$list = $('.woodmart-templates-list'),
$search = $head.find('.woodmart-templates-search');
$search.on('keyup', function(e) {
var val = $(this).val().toLowerCase();
$list.find('.woodmart-template-item').each(function() {
var $this = $(this);
if (($this.attr('data-template_name') + $this.attr('data-template_title')).toLowerCase().indexOf(val) > -1) {
$this.removeClass('hide-by-search').addClass('show-by-search');
} else {
$this.addClass('hide-by-search').removeClass('show-by-search');
}
});
});
/* filters */
$list.on('click', '.woodmart-templates-tags a', function(e) {
e.preventDefault();
var slug = $(this).data('slug');
$(this).parent().parent().find('.xts-active').removeClass('xts-active');
$(this).parent().addClass('xts-active');
$list.find('.woodmart-template-item').each(function() {
var $this = $(this);
if ($this.hasClass('tag-' + slug)) {
$this.removeClass('hide-by-tag').addClass('show-by-tag');
} else {
$this.addClass('hide-by-tag').removeClass('show-by-tag');
}
});
});
/* loader function */
$list.on('click', '[data-template-handler]', function() {
$list.addClass('xts-loading element-adding');
});
var $vcTemplatesBtn = $('#vc_templates-editor-button, #vc_templates-more-layouts, #vc-templatera-editor-button'),
templatesLoaded = false,
templates;
$vcTemplatesBtn.on('click', function() {
setTimeout(function() {
$list.find('.woodmart-template-item').show();
if ($list.hasClass('element-adding')) {
$list.removeClass('element-adding xts-loading');
}
$search.val('');
loadTemplates();
}, 100);
});
$('#vc_inline-frame').on('load', function() {
var iframeBody = $('body', $('#vc_inline-frame')[0].contentWindow.document);
$(iframeBody).on('click', '#vc_templates-more-layouts', function() {
$list.find('.woodmart-template-item').show();
if ($list.hasClass('element-adding')) {
$list.removeClass('element-adding xts-loading');
}
$search.val('');
loadTemplates();
});
});
function loadTemplates() {
if (templatesLoaded) {
return;
}
templatesLoaded = true;
$.ajax({
url : woodmartConfig.demoAjaxUrl,
data : {
action: 'woodmart_load_templates',
builder: 'wpbakery'
},
dataType : 'json',
crossDomain: true,
method : 'POST',
success : function(data) {
if (data.count > 0) {
renderElements(data.elements);
renderTags(data.tags, data.count);
}
},
error : function(err) {
$('.woodmart-templates-list').prepend('Can\'t load templates from the server.').removeClass('xts-loading');
console.log('can\'t load templates from the server', err);
}
});
}
function renderTags(tags, count) {
var html = '';
Object.keys(tags).map(function(objectKey, index) {
var tag = tags[objectKey];
html = html + renderTag(tag);
});
html = '';
// console.log(html)
$('.woodmart-templates-list').prepend(html);
}
function renderTag(tag) {
var html = '';
html += '' + tag.title + ' ' + tag.count + '';
return html;
}
function renderElements(elements) {
var html = '';
Object.keys(elements).map(function(objectKey, index) {
var element = elements[objectKey];
html = renderElement(element) + html;
});
// console.log(html)
$('.woodmart-templates-list').prepend(html).removeClass('xts-loading');
}
function renderElement(element) {
var html = '';
html += '';
html += '
';
html += '

';
html += '
';
html += '
';
html += '';
html += '
';
return html;
}
}
jQuery(document).ready(function() {
vcTemplatesLibrary();
});
})(jQuery);