0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00
ghost/core/client/controllers/post-settings-menu.js
David Arvelo 4a987bfc15 Split PostController amongst new Editor/PostSettingsMenu Controllers
closes #2910
- create EditorController, PostSettingsMenuController
- remove `posts.post` controllerName dependency on EditorRoute/NewRoute
- `{{render}}` the PostSettingsMenuView with its own controller
- break up properties/methods from PostsPostController into all three controllers
- fix EditorRoute pagination options to now be comparable to PostsRoute
- add NewController to force NewRoute to refresh editor with blank model. Identical to Editor's
- EditorController and NewController are based on a shared mixin
- NewView created, templateName is 'editor'
2014-06-08 14:01:32 -04:00

138 lines
4.7 KiB
JavaScript

import {parseDateString, formatDate} from 'ghost/utils/date-formatting';
var PostSettingsMenuController = Ember.ObjectController.extend({
isStaticPage: function (key, val) {
var self = this;
if (arguments.length > 1) {
this.set('page', val ? 1 : 0);
return this.get('model').save().then(function () {
self.notifications.showSuccess('Successfully converted to ' + (val ? 'static page' : 'post'));
return !!self.get('page');
}, this.notifications.showErrors);
}
return !!this.get('page');
}.property('page'),
newSlugBinding: Ember.computed.oneWay('slug'),
slugPlaceholder: function () {
return this.get('model').generateSlug();
}.property('title'),
actions: {
updateSlug: function () {
var newSlug = this.get('newSlug'),
slug = this.get('slug'),
placeholder = this.get('slugPlaceholder'),
self = this;
newSlug = (!newSlug && placeholder) ? placeholder : newSlug;
// Ignore unchanged slugs
if (slug === newSlug) {
return;
}
//reset to model's slug on empty string
if (!newSlug) {
this.set('newSlug', slug);
return;
}
//Validation complete
this.set('slug', newSlug);
// If the model doesn't currently
// exist on the server
// then just update the model's value
if (!this.get('isNew')) {
return;
}
this.get('model').save().then(function () {
self.notifications.showSuccess('Permalink successfully changed to <strong>' +
self.get('slug') + '</strong>.');
}, this.notifications.showErrors);
},
updatePublishedAt: function (userInput) {
var self = this,
errMessage = '',
newPubDate = formatDate(parseDateString(userInput)),
pubDate = this.get('publishedAt'),
newPubDateMoment,
pubDateMoment;
// if there is no new pub date, mark that until the post is published,
// when we'll fill in with the current time.
if (!newPubDate) {
this.set('publishedAt', '');
return;
}
// Check for missing time stamp on new data
// If no time specified, add a 12:00
if (newPubDate && !newPubDate.slice(-5).match(/\d+:\d\d/)) {
newPubDate += ' 12:00';
}
newPubDateMoment = parseDateString(newPubDate);
// If there was a published date already set
if (pubDate) {
// Check for missing time stamp on current model
// If no time specified, add a 12:00
if (!pubDate.slice(-5).match(/\d+:\d\d/)) {
pubDate += ' 12:00';
}
pubDateMoment = parseDateString(pubDate);
// Quit if the new date is the same
if (pubDateMoment.isSame(newPubDateMoment)) {
return;
}
}
// Validate new Published date
if (!newPubDateMoment.isValid() || newPubDate.substr(0, 12) === 'Invalid date') {
errMessage = 'Published Date must be a valid date with format: ' +
'DD MMM YY @ HH:mm (e.g. 6 Dec 14 @ 15:00)';
}
if (newPubDateMoment.diff(new Date(), 'h') > 0) {
errMessage = 'Published Date cannot currently be in the future.';
}
if (errMessage) {
// Show error message
this.notifications.showError(errMessage);
//Hack to push a "change" when it's actually staying
// the same.
//This alerts the listener on post-settings-menu
this.notifyPropertyChange('publishedAt');
return;
}
//Validation complete
this.set('published_at', newPubDateMoment.toDate());
// If the model doesn't currently
// exist on the server
// then just update the model's value
if (!this.get('isNew')) {
return;
}
this.get('model').save().then(function () {
this.notifications.showSuccess('Publish date successfully changed to <strong>' +
self.get('publishedAt') + '</strong>.');
}, this.notifications.showErrors);
}
}
});
export default PostSettingsMenuController;