2013-09-13 21:29:08 +04:00
|
|
|
// The Post Settings Menu available in the content preview screen, as well as the post editor.
|
|
|
|
|
2013-09-16 06:22:09 +04:00
|
|
|
/*global window, document, $, _, Backbone, Ghost, moment */
|
2013-09-13 21:29:08 +04:00
|
|
|
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Ghost.View.PostSettings = Ghost.View.extend({
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'blur .post-setting-slug' : 'editSlug',
|
|
|
|
'click .post-setting-slug' : 'selectSlug',
|
2013-09-16 06:22:09 +04:00
|
|
|
'blur .post-setting-date' : 'editDate',
|
2013-10-17 09:21:56 +04:00
|
|
|
'click .post-setting-static-page' : 'toggleStaticPage',
|
2013-09-13 21:29:08 +04:00
|
|
|
'click .delete' : 'deletePost'
|
|
|
|
},
|
|
|
|
|
2013-09-16 06:22:09 +04:00
|
|
|
initialize: function () {
|
|
|
|
if (this.model) {
|
2013-09-17 14:39:27 +04:00
|
|
|
this.listenTo(this.model, 'change:id', this.render);
|
2013-09-16 06:22:09 +04:00
|
|
|
this.listenTo(this.model, 'change:status', this.render);
|
2013-09-17 13:26:28 +04:00
|
|
|
this.listenTo(this.model, 'change:published_at', this.render);
|
2013-10-17 09:21:56 +04:00
|
|
|
this.listenTo(this.model, 'change:page', this.render);
|
2013-09-16 06:22:09 +04:00
|
|
|
}
|
|
|
|
},
|
2013-09-13 21:29:08 +04:00
|
|
|
|
2013-09-16 06:22:09 +04:00
|
|
|
render: function () {
|
|
|
|
var slug = this.model ? this.model.get('slug') : '',
|
|
|
|
pubDate = this.model ? this.model.get('published_at') : 'Not Published',
|
2013-09-17 14:39:27 +04:00
|
|
|
$pubDateEl = this.$('.post-setting-date');
|
2013-09-13 21:29:08 +04:00
|
|
|
|
|
|
|
$('.post-setting-slug').val(slug);
|
2013-09-16 06:22:09 +04:00
|
|
|
|
2013-10-17 09:21:56 +04:00
|
|
|
// Update page status test if already a page.
|
|
|
|
if (this.model && this.model.get('page')) {
|
|
|
|
$('.post-setting-static-page').prop('checked', this.model.get('page'));
|
|
|
|
}
|
|
|
|
|
2013-09-16 06:22:09 +04:00
|
|
|
// Insert the published date, and make it editable if it exists.
|
|
|
|
if (this.model && this.model.get('published_at')) {
|
|
|
|
pubDate = moment(pubDate).format('DD MMM YY');
|
2013-09-17 14:39:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.model && this.model.get('id')) {
|
2013-10-17 09:21:56 +04:00
|
|
|
this.$('.post-setting-page').removeClass('hidden');
|
2013-09-17 14:39:27 +04:00
|
|
|
this.$('.delete').removeClass('hidden');
|
2013-09-16 06:22:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$pubDateEl.val(pubDate);
|
2013-09-13 21:29:08 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
selectSlug: function (e) {
|
|
|
|
e.currentTarget.select();
|
|
|
|
},
|
|
|
|
|
|
|
|
editSlug: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var self = this,
|
|
|
|
slug = self.model.get('slug'),
|
|
|
|
slugEl = e.currentTarget,
|
|
|
|
newSlug = slugEl.value;
|
|
|
|
|
|
|
|
// Ignore empty or unchanged slugs
|
|
|
|
if (newSlug.length === 0 || slug === newSlug) {
|
|
|
|
slugEl.value = slug === undefined ? '' : slug;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.model.save({
|
|
|
|
slug: newSlug
|
|
|
|
}, {
|
|
|
|
success : function (model, response, options) {
|
|
|
|
// Repopulate slug in case it changed on the server (e.g. 'new-slug-2')
|
|
|
|
slugEl.value = model.get('slug');
|
2013-09-18 04:46:10 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: "Permalink successfully changed to <strong>" + model.get('slug') + '</strong>.',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-09-13 21:29:08 +04:00
|
|
|
},
|
|
|
|
error : function (model, xhr) {
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: Ghost.Views.Utils.getRequestErrorMessage(xhr),
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-09-16 06:22:09 +04:00
|
|
|
editDate: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var self = this,
|
2013-10-16 20:08:58 +04:00
|
|
|
parseDateFormats = ['DD MMM YY', 'DD MMM YYYY', 'DD/MM/YY', 'DD/MM/YYYY', 'DD-MM-YY', 'DD-MM-YYYY'],
|
|
|
|
displayDateFormat = 'DD MMM YY',
|
2013-09-16 06:22:09 +04:00
|
|
|
errMessage = '',
|
|
|
|
pubDate = self.model.get('published_at'),
|
2013-10-16 20:08:58 +04:00
|
|
|
pubDateMoment = moment(pubDate, parseDateFormats),
|
2013-09-16 06:22:09 +04:00
|
|
|
pubDateEl = e.currentTarget,
|
2013-10-16 20:08:58 +04:00
|
|
|
newPubDate = pubDateEl.value,
|
|
|
|
newPubDateMoment = moment(newPubDate, parseDateFormats);
|
2013-09-16 06:22:09 +04:00
|
|
|
|
|
|
|
// Ensure the published date has changed
|
2013-10-16 20:08:58 +04:00
|
|
|
if (newPubDate.length === 0 || pubDateMoment.isSame(newPubDateMoment)) {
|
|
|
|
pubDateEl.value = pubDate === undefined ? 'Not Published' : pubDateMoment.format(displayDateFormat);
|
2013-09-16 06:22:09 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate new Published date
|
2013-10-16 20:08:58 +04:00
|
|
|
if (!newPubDateMoment.isValid()) {
|
2013-09-16 06:22:09 +04:00
|
|
|
errMessage = 'Published Date must be a valid date with format: DD MMM YY (e.g. 6 Dec 14)';
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:08:58 +04:00
|
|
|
if (newPubDateMoment.diff(new Date(), 'h') > 0) {
|
2013-09-16 06:22:09 +04:00
|
|
|
errMessage = 'Published Date cannot currently be in the future.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errMessage.length) {
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: errMessage,
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-10-16 20:08:58 +04:00
|
|
|
pubDateEl.value = pubDateMoment.format(displayDateFormat);
|
2013-09-16 06:22:09 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save new 'Published' date
|
|
|
|
this.model.save({
|
2013-09-18 03:54:01 +04:00
|
|
|
// Temp Fix. Set hour to 12 instead of 00 to avoid some TZ issues.
|
2013-10-16 20:08:58 +04:00
|
|
|
published_at: newPubDateMoment.hour(12).toDate()
|
2013-09-16 06:22:09 +04:00
|
|
|
}, {
|
|
|
|
success : function (model, response, options) {
|
2013-10-16 20:08:58 +04:00
|
|
|
pubDateEl.value = moment(model.get('published_at'), parseDateFormats).format(displayDateFormat);
|
2013-09-18 04:46:10 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
2013-10-16 20:08:58 +04:00
|
|
|
message: 'Publish date successfully changed to <strong>' + pubDateEl.value + '</strong>.',
|
2013-09-18 04:46:10 +04:00
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-09-16 06:22:09 +04:00
|
|
|
},
|
|
|
|
error : function (model, xhr) {
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: Ghost.Views.Utils.getRequestErrorMessage(xhr),
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-17 09:21:56 +04:00
|
|
|
toggleStaticPage: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var pageEl = $(e.currentTarget),
|
|
|
|
page = this.model ? !this.model.get('page') : false;
|
|
|
|
|
|
|
|
this.model.save({
|
|
|
|
page: page
|
|
|
|
}, {
|
|
|
|
success : function (model, response, options) {
|
|
|
|
pageEl.prop('checked', page);
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: "Successfully converted " + (page ? "to static page" : "to post") + '.',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error : function (model, xhr) {
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: Ghost.Views.Utils.getRequestErrorMessage(xhr),
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-09-13 21:29:08 +04:00
|
|
|
deletePost: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var self = this;
|
|
|
|
this.addSubview(new Ghost.Views.Modal({
|
|
|
|
model: {
|
|
|
|
options: {
|
|
|
|
close: false,
|
|
|
|
confirm: {
|
|
|
|
accept: {
|
|
|
|
func: function () {
|
|
|
|
self.model.destroy({
|
|
|
|
wait: true
|
|
|
|
}).then(function () {
|
|
|
|
// Redirect to content screen if deleting post from editor.
|
|
|
|
if (window.location.pathname.indexOf('editor') > -1) {
|
|
|
|
window.location = '/ghost/content/';
|
|
|
|
}
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: 'Your post has been deleted.',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
}, function () {
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: 'Your post could not be deleted. Please try again.',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
text: "Yes"
|
|
|
|
},
|
|
|
|
reject: {
|
|
|
|
func: function () {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
text: "No"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
type: "action",
|
|
|
|
style: ["wide", "centered"],
|
|
|
|
animation: 'fade'
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
template: 'blank',
|
|
|
|
title: 'Are you sure you want to delete this post?'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-16 09:30:25 +04:00
|
|
|
}());
|