jQuery(function($) {
"use strict";
var TEMPLATES = new LuigiTemplate.Cache({
post: [
"
",
"",
"",
" | ",
"",
"%{name|h}",
" ",
"%{label}",
" | ",
"",
"",
"%{site_name|h}",
"",
" | ",
"",
"%{post_url}",
" | ",
"",
"",
"%{user_name|h}",
"",
" | ",
"",
"%{created_at_text|h}",
" | ",
"",
"%{posted_at_text|h}",
" | ",
"
",
],
post_url: [
"",
"%{post_url|h}",
"",
],
post_slug: [
"%{slug|h}",
],
error: [
"",
"",
"",
" ",
"Error: %{error|h}",
" | ",
"
",
],
});
// FIXME: move this to DATA as well?
var POST_STATES = {
draft: {
css: 'bg-warning',
label: [
" ",
"",
].join(''),
},
deleted: {
css: 'bg-danger',
label: [
" ",
"",
].join(''),
},
};
function reload() {
$('#posts-reload').addClass('disabled')
.find('.loading').toggleClass('hidden');
send('post/get_posts', {
type: $('#posts-filter-type li.active a').data('id'),
state: $('#posts-filter-state li.active a').data('id'),
site_id: $('#posts-filter-site li.active a').data('id'),
user_id: $('#posts-filter-user li.active a').data('id'),
q: $('#posts-q').data('q'),
page: 1,
}).always(function() {
$('#posts-reload').removeClass('disabled')
.find('.loading').toggleClass('hidden');
}).fail(function(r) {
var error = r.responseText;
try {
var data = $.parseJSON(r.responseText);
if (data.error)
error = data.error;
} catch (e) {}
$('#posts tbody').html(TEMPLATES.run('error', {
error: error,
}));
}).done(function(r) {
$('#posts tbody').html($.map(r.rows, function(row) {
var css = '',
label = '';
if (POST_STATES[row.state]) {
css = POST_STATES[row.state].css;
label = POST_STATES[row.state].label;
}
var post_url = 'n/a';
if (row.post_url) {
post_url = TEMPLATES.run('post_url', row);
} else {
post_url = TEMPLATES.run('post_slug', row);
}
return TEMPLATES.run('post', $.extend(row, {
css: css,
label: label,
icon: DATA.post_types[row.post_type].icon,
post_url: post_url,
}));
}).join(''));
});
}
$('#admin-tab-posts').on('show.bs.tab', function() {
$('#posts-reload').click();
});
$('.add-post').click(function() {
var type = $(this).data('type');
// dismiss dropdown
$('body').trigger('click');
send(type + '/add').fail(function(r) {
gripe(r, 'add ' + type);
}).done(function(r) {
// show edit dialog
$('#' + type + '-edit-dialog').data('post_id', r.post_id).modal('show')
});
// stop event
return false;
});
$('#posts-reload').click(function() {
reload();
// stop event
return false;
});
$('#posts-q').on('search-update', function() {
reload();
});
$('.posts-filter-menu').on('guff.choose', function() {
// reload posts
reload();
});
$('#posts').on('click', 'a.edit', function() {
var data = $(this).data();
if (data.edit == 'user') {
$('#user-edit-dialog').data(data).modal('show');
} else if (data.edit == 'site') {
$('#site-edit-dialog').data(data).modal('show');
}
// stop event
return false;
});
$('#posts').on('click', 'a.link', function() {
open($(this).attr('href'));
// stop event
return false;
});
$('#posts').on('click', 'tbody tr[data-post_id]', function() {
var data = $(this).data();
$('#' + data.post_type + '-edit-dialog')
.data('post_id', data.post_id)
.modal('show');
// stop event
return false;
});
});