/**
* Wpfd
*
* We developed this code with our hearts and passion.
* We hope you found it useful, easy to understand and to customize.
* Otherwise, please feel free to contact us at contact@joomunited.com *
* @package WP File Download
* @copyright Copyright (C) 2013 JoomUnited (http://www.joomunited.com). All rights reserved.
* @copyright Copyright (C) 2013 Damien Barrère (http://www.crac-design.com). All rights reserved.
* @license GNU General Public License version 2 or later; http://www.gnu.org/licenses/gpl-2.0.html
*/
jQuery(document).ready(function($) {
var table_tree = $('.wpfd-foldertree-mappa');
var table_hash = window.location.hash;
var table_root_cat = $('.wpfd-content-mappa').data('category');
var table_cParents = {};
$(".wpfd-content-mappa").each(function(index ){
var table_topCat = $(this).data('category');
table_cParents[table_topCat] = {parent:0,term_id: table_topCat,name: $(this).find("h2").text()};
$(this).find(".wpfdcategory.catlink").each(function(index ){
var tempidCat = $(this).data('idcat');
table_cParents[tempidCat]= {parent:table_topCat,term_id:tempidCat,name: $(this).text()};
})
})
//load media tables
$('.wpfd-content .mediaTable').mediaTable();
var tpltable_source = $("#wpfd-template-mappa").html();
var tpltable_sourcecategories = $("#wpfd-template-mappa-categories").html();
Handlebars.registerHelper('bytesToSize', function(bytes) {
return bytes == 'n/a' ? bytes : bytesToSize(parseInt(bytes));
});
function table_initClick(){
$('.wpfd-content-mappa .catlink').click(function(e){
e.preventDefault();
table_load($(this).parents('.wpfd-content-mappa').data('category'),$(this).data('idcat'));
});
}
table_initClick();
table_hash = table_hash.replace('#','');
if (table_hash != '') {
var hasha = table_hash.split('-');
var re = new RegExp("^(p[0-9]+)$");
var page = null;
var stringpage = hasha.pop();
if (re.test(stringpage)) {
page = stringpage.replace('p','');
}
var hash_category_id = hasha[0];
if(!parseInt(hash_category_id)){
return;
}
setTimeout(function () {
table_load($('.wpfd-content-mappa').data('category'), hash_category_id, page);
},100)
}
function table_load(sourcecat,category, page){
var pathname = window.location.pathname;
$(".wpfd-content-multi[data-category="+sourcecat+"]").find('#current_category').val(category);
$(".wpfd-content-multi[data-category="+sourcecat+"]").next('.wpfd-pagination').remove();
$(".wpfd-content-multi[data-category="+sourcecat+"] table tbody").empty();
$(".wpfd-content-multi[data-category="+sourcecat+"] table").after($('#wpfd-loading-wrap').html());
$(".wpfd-content-multi[data-category="+sourcecat+"] .wpfd-categories").empty();
//Get categories
$.ajax({
url: wpfdTableTheme.ajaxurl+"action=wpfd&task=categories.display&view=categories&id="+category+"&top="+sourcecat,
dataType : "json"
}).done(function(categories) {
if (page !=null) {
window.history.pushState('', document.title, pathname + '#'+category+'-'+categories.category.slug+'-p' + page );
} else {
window.history.pushState('', document.title, pathname + '#'+category+'-'+categories.category.slug );
}
$(".wpfd-content-multi[data-category="+sourcecat+"]").find('#current_category_slug').val(categories.category.slug);
var template = Handlebars.compile(tpltable_sourcecategories);
var html = template(categories);
$(".wpfd-content-multi[data-category="+sourcecat+"] .wpfd-categories").prepend(html);
if (table_tree.length) {
table_tree.find('li').removeClass('selected');
table_tree.find('i.md').removeClass('md-folder-open').addClass("md-folder");
table_tree.jaofiletree('open', category);
var el = table_tree.find('a[data-file="'+category+'"]').parent();
el.find(' > i.md').removeClass("md-folder").addClass("md-folder-open");
if (!el.hasClass('selected') ) {
el.addClass('selected');
}
}
//Get files
$.ajax({
url: wpfdTableTheme.ajaxurl+"action=wpfd&task=files.display&view=files&id="+category+"&rootcat="+table_root_cat+"&page=" + page,
dataType : "json"
}).done(function(content) {
// $.extend(content,categories);
$(".wpfd-content-multi[data-category="+sourcecat+"]").after(content.pagination);
delete content.pagination;
var template_table = Handlebars.compile(tpltable_source);
var html = template_table(content);
//html = $('').html(html).val();
$(".wpfd-content-multi[data-category="+sourcecat+"] table tbody").append(html);
$(".wpfd-content-multi[data-category="+sourcecat+"] table tbody").trigger('change');
$(".wpfd-content-multi[data-category="+sourcecat+"] .mediaTableMenu").find('input').trigger('change');
for(i=0;i< categories.categories.length;i++) {
table_cParents[categories.categories[i].term_id]= categories.categories[i];
}
table_breadcrum(sourcecat,category);
table_initClick();
if (typeof wpfdColorboxInit != 'undefined') {
wpfdColorboxInit();
}
wpfdTrackDownload();
table_init_pagination($('.wpfd-pagination'));
wpfd_remove_loading($(".wpfd-content-multi"));
});
});
}
function table_breadcrum(sourcecat,catid) {
links = [];
current_Cat = table_cParents[catid];
if(!current_Cat){
return false;
}
links.unshift(current_Cat);
if (current_Cat.parent != 0) {
while(table_cParents[current_Cat.parent]) {
current_Cat = table_cParents[current_Cat.parent];
links.unshift(current_Cat);
};
}
html = '';
for(i=0;i'+links[i].name+' > ';
}else {
html += ''+links[i].name+'';
}
}
$(".wpfd-content-mappa[data-category="+sourcecat+"] .wpfd-breadcrumbs-mappa").html(html);
}
if (table_tree.length) {
table_tree.each(function( index ) {
var table_topCat = $(this).parents('.wpfd-content-mappa.wpfd-content-multi').data('category');
$(this).jaofiletree({
script : wpfdTableTheme.ajaxurl+'?action=wpfd&task=categories.getSubs',
usecheckboxes : false,
root: table_topCat,
showroot : table_cParents[table_topCat].name,
onclick: function(elem,file){
if (table_topCat != file) {
$(elem).parents('.directory').each(function() {
var $this = $(this);
var category = $this.find(' > a');
var parent = $this.find('.icon-open-close');
if (parent.length > 0) {
if (typeof table_cParents[category.data('file')] =='undefined') {
table_cParents[category.data('file')] = {parent: parent.data('parent_id'),term_id:category.data('file'),name: category.text()};
}
}
});
}
table_load(table_topCat,file);
}
});
})
}
$('.wpfd-pagination').each(function() {
var $this = $(this);
table_init_pagination($this);
})
function table_init_pagination($this) {
var number = $this.find('a:not(.current)');
var wrap = $this.prev('.wpfd-content-mappa');
var current_category = wrap.find('#current_category').val();
var sourcecat = wrap.data('category');
number.unbind('click').bind('click', function () {
var page_number = $(this).attr('data-page');
if (typeof page_number != 'undefined') {
var pathname = window.location.pathname;
var category = $(".wpfd-content-multi[data-category="+sourcecat+"]").find('#current_category').val();
var category_slug = $(".wpfd-content-multi[data-category="+sourcecat+"]").find('#current_category_slug').val();
window.history.pushState('', document.title, pathname + '#'+category+'-'+category_slug+'-p'+page_number );
$(".wpfd-content-multi[data-category="+sourcecat+"] table tbody tr:not(.topheader)").remove();
$(".wpfd-content-multi[data-category="+sourcecat+"] table").after($('#wpfd-loading-wrap').html());
//Get files
$.ajax({
url: wpfdTableTheme.ajaxurl+"action=wpfd&task=files.display&view=files&id="+current_category+"&page=" + page_number,
dataType : "json"
}).done(function(content) {
delete content.category;
wrap.next('.wpfd-pagination').remove();
wrap.after(content.pagination);
delete content.pagination;
var template_table = Handlebars.compile(tpltable_source);
var html = template_table(content);
$(".wpfd-content-multi[data-category="+sourcecat+"] table tbody").append(html);
$(".wpfd-content-multi[data-category="+sourcecat+"] table tbody").trigger('change');
$(".wpfd-content-multi[data-category="+sourcecat+"] .mediaTableMenu").find('input').trigger('change');
if (typeof wpfdColorboxInit != 'undefined') {
wpfdColorboxInit();
}
table_init_pagination($('.wpfd-pagination'));
wpfd_remove_loading($(".wpfd-content-multi"));
});
}
});
}
});