',
' %title%
',
' ',
' ',
''
];
case 'DRAFT':
return [
'',
' ',
' ',
' ',
'
',
' %title%
',
' ',
' ',
''
];
return;
}
}
var showPosts = function(posts) {
var articles = $('.articles');
posts.map(function(post) {
var template = getTemplateByPost(post);
articles.append(template.join('').replace(
/%date%/g,
post.created_at
).replace(
/%permalink%/g,
'/post-editor/?pid=' + post.id
).replace(
/%title%/g,
post.title
).replace(
/%summary%/g,
post.summary
).replace(
/%id%/g,
post.id
)
)
});
};
var publishPost = function(post_id) {
fetchPost(post_id).then(function(post) {
fetch('/_p/posts/' + post.id, {
method: 'PUT',
headers: {
Authorization: 'Bearer ' + window.localStorage.access_token,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
flow_id: post.flow_id,
title: post.title,
hubs: post.hubs.map(function(hub) {return hub.id;}),
body: post.body,
status: 'PUBLISHED'
})
}).then(function(data) {
data.json().then(function() {
window.location.href = '/posts/';
});
});
})
};
var unpublishPost = function(post_id) {
fetchPost(post_id).then(function(post) {
fetch('/_p/posts/' + post.id, {
method: 'PUT',
headers: {
Authorization: 'Bearer ' + window.localStorage.access_token,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
flow_id: post.flow_id,
title: post.title,
hubs: post.hubs.map(function(hub) {return hub.id;}),
body: post.body,
status: 'DRAFT'
})
}).then(function(data) {
data.json().then(function() {
window.location.href = '/posts/?status=published';
});
});
})
};
var hookEvents = function() {
$('.publish').click(function() {
var post_id = $(this).data('id');
confirm('The post will go live. OK?') && publishPost(post_id);
})
$('.unpublish').click(function() {
var post_id = $(this).data('id');
confirm('The post will no longer be live. OK?') && unpublishPost(post_id);
})
};
highlightActiveTab();
fetchPosts(getActiveTab()).then(function(posts) {
showPosts(posts);
hookEvents();
});
});