2014-06-09 00:48:14 +04:00
|
|
|
/* global moment */
|
2014-06-08 10:02:21 +04:00
|
|
|
import {parseDateString, formatDate} from 'ghost/utils/date-formatting';
|
2014-06-09 00:48:14 +04:00
|
|
|
import SlugGenerator from 'ghost/models/slug-generator';
|
2014-06-15 00:45:50 +04:00
|
|
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
2014-06-08 10:02:21 +04:00
|
|
|
|
|
|
|
var PostSettingsMenuController = Ember.ObjectController.extend({
|
2014-06-16 03:05:37 +04:00
|
|
|
init: function () {
|
|
|
|
this._super();
|
2014-09-04 17:56:07 +04:00
|
|
|
},
|
2014-06-16 03:05:37 +04:00
|
|
|
|
2014-09-04 17:56:07 +04:00
|
|
|
initializeObserver: function () {
|
2014-06-16 03:05:37 +04:00
|
|
|
// when creating a new post we want to observe the title
|
|
|
|
// to generate the post's slug
|
|
|
|
if (this.get('isNew')) {
|
2014-07-03 21:09:05 +04:00
|
|
|
this.addObserver('titleScratch', this, 'titleObserver');
|
2014-06-16 03:05:37 +04:00
|
|
|
}
|
2014-09-04 17:56:07 +04:00
|
|
|
}.observes('model'),
|
2014-08-10 18:40:04 +04:00
|
|
|
|
|
|
|
selectedAuthor: null,
|
2014-09-04 17:56:07 +04:00
|
|
|
initializeSelectedAuthor: function () {
|
2014-08-02 09:21:43 +04:00
|
|
|
var self = this;
|
2014-09-04 17:56:07 +04:00
|
|
|
|
2014-08-02 09:21:43 +04:00
|
|
|
return this.get('author').then(function (author) {
|
|
|
|
self.set('selectedAuthor', author);
|
|
|
|
return author;
|
|
|
|
});
|
2014-09-04 17:56:07 +04:00
|
|
|
}.observes('model'),
|
2014-08-10 18:40:04 +04:00
|
|
|
|
2014-07-17 09:54:32 +04:00
|
|
|
changeAuthor: function () {
|
|
|
|
var author = this.get('author'),
|
|
|
|
selectedAuthor = this.get('selectedAuthor'),
|
|
|
|
model = this.get('model'),
|
|
|
|
self = this;
|
|
|
|
//return if nothing changed
|
|
|
|
if (selectedAuthor.get('id') === author.get('id')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
model.set('author', selectedAuthor);
|
2014-08-02 22:27:12 +04:00
|
|
|
|
|
|
|
//if this is a new post (never been saved before), don't try to save it
|
|
|
|
if (this.get('isNew')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-17 09:54:32 +04:00
|
|
|
model.save(this.get('saveOptions')).catch(function (errors) {
|
|
|
|
self.showErrors(errors);
|
|
|
|
self.set('selectedAuthor', author);
|
|
|
|
model.rollback();
|
|
|
|
});
|
|
|
|
}.observes('selectedAuthor'),
|
2014-07-30 05:57:19 +04:00
|
|
|
authors: Ember.computed(function () {
|
2014-07-17 09:54:32 +04:00
|
|
|
//Loaded asynchronously, so must use promise proxies.
|
2014-08-02 09:21:43 +04:00
|
|
|
var deferred = {};
|
2014-08-01 00:19:52 +04:00
|
|
|
|
2014-07-17 09:54:32 +04:00
|
|
|
deferred.promise = this.store.find('user').then(function (users) {
|
|
|
|
return users.rejectBy('id', 'me');
|
2014-08-01 00:19:52 +04:00
|
|
|
}).then(function (users) {
|
2014-08-02 09:21:43 +04:00
|
|
|
return users.filter(function (user) {
|
|
|
|
return user.get('active');
|
|
|
|
});
|
2014-07-17 09:54:32 +04:00
|
|
|
});
|
2014-08-01 00:19:52 +04:00
|
|
|
|
2014-07-17 09:54:32 +04:00
|
|
|
return Ember.ArrayProxy
|
|
|
|
.extend(Ember.PromiseProxyMixin)
|
|
|
|
.create(deferred);
|
2014-07-30 05:57:19 +04:00
|
|
|
}),
|
2014-07-06 00:44:19 +04:00
|
|
|
//Changes in the PSM are too minor to warrant NProgress firing
|
|
|
|
saveOptions: {disableNProgress: true},
|
2014-06-09 00:48:14 +04:00
|
|
|
/**
|
|
|
|
* The placeholder is the published date of the post,
|
|
|
|
* or the current date if the pubdate has not been set.
|
|
|
|
*/
|
2014-07-30 05:57:19 +04:00
|
|
|
publishedAtPlaceholder: Ember.computed('publishedAtValue', function () {
|
2014-06-09 00:48:14 +04:00
|
|
|
var pubDate = this.get('published_at');
|
|
|
|
if (pubDate) {
|
|
|
|
return formatDate(pubDate);
|
|
|
|
}
|
|
|
|
return formatDate(moment());
|
2014-07-30 05:57:19 +04:00
|
|
|
}),
|
2014-06-15 00:45:50 +04:00
|
|
|
publishedAtValue: boundOneWay('published_at', formatDate),
|
2014-06-09 00:48:14 +04:00
|
|
|
|
2014-06-15 00:45:50 +04:00
|
|
|
slugValue: boundOneWay('slug'),
|
2014-06-09 00:48:14 +04:00
|
|
|
//Lazy load the slug generator for slugPlaceholder
|
|
|
|
slugGenerator: Ember.computed(function () {
|
2014-07-01 16:18:47 +04:00
|
|
|
return SlugGenerator.create({
|
2014-07-31 08:25:42 +04:00
|
|
|
ghostPaths: this.get('ghostPaths'),
|
|
|
|
slugType: 'post'
|
2014-07-01 16:18:47 +04:00
|
|
|
});
|
2014-06-09 00:48:14 +04:00
|
|
|
}),
|
|
|
|
//Requests slug from title
|
|
|
|
generateSlugPlaceholder: function () {
|
|
|
|
var self = this,
|
2014-07-03 21:09:05 +04:00
|
|
|
title = this.get('titleScratch');
|
2014-07-17 09:54:32 +04:00
|
|
|
|
2014-07-01 16:18:47 +04:00
|
|
|
this.get('slugGenerator').generateSlug(title).then(function (slug) {
|
2014-06-16 03:05:37 +04:00
|
|
|
self.set('slugPlaceholder', slug);
|
2014-06-09 00:48:14 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
titleObserver: function () {
|
2014-07-18 19:01:42 +04:00
|
|
|
if (this.get('isNew') && !this.get('title')) {
|
2014-06-16 03:05:37 +04:00
|
|
|
Ember.run.debounce(this, 'generateSlugPlaceholder', 700);
|
|
|
|
}
|
|
|
|
},
|
2014-07-30 05:57:19 +04:00
|
|
|
slugPlaceholder: Ember.computed(function (key, value) {
|
2014-06-09 00:48:14 +04:00
|
|
|
var slug = this.get('slug');
|
|
|
|
|
|
|
|
//If the post has a slug, that's its placeholder.
|
|
|
|
if (slug) {
|
|
|
|
return slug;
|
|
|
|
}
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-09 00:48:14 +04:00
|
|
|
//Otherwise, it's whatever value was set by the
|
|
|
|
// slugGenerator (below)
|
|
|
|
if (arguments.length > 1) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
//The title will stand in until the actual slug has been generated
|
2014-07-03 21:09:05 +04:00
|
|
|
return this.get('titleScratch');
|
2014-07-30 05:57:19 +04:00
|
|
|
}),
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-07-01 16:18:47 +04:00
|
|
|
showErrors: function (errors) {
|
|
|
|
errors = Ember.isArray(errors) ? errors : [errors];
|
|
|
|
this.notifications.showErrors(errors);
|
|
|
|
},
|
|
|
|
showSuccess: function (message) {
|
|
|
|
this.notifications.showSuccess(message);
|
|
|
|
},
|
2014-06-08 10:02:21 +04:00
|
|
|
actions: {
|
2014-07-01 16:18:47 +04:00
|
|
|
togglePage: function () {
|
2014-07-15 08:02:34 +04:00
|
|
|
var self = this;
|
2014-07-01 16:18:47 +04:00
|
|
|
|
2014-07-15 08:02:34 +04:00
|
|
|
this.toggleProperty('page');
|
2014-07-01 16:18:47 +04:00
|
|
|
// If this is a new post. Don't save the model. Defer the save
|
|
|
|
// to the user pressing the save button
|
|
|
|
if (this.get('isNew')) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-15 08:02:34 +04:00
|
|
|
|
|
|
|
this.get('model').save(this.get('saveOptions')).catch(function (errors) {
|
2014-07-01 16:18:47 +04:00
|
|
|
self.showErrors(errors);
|
|
|
|
self.get('model').rollback();
|
|
|
|
});
|
|
|
|
},
|
2014-06-09 00:48:14 +04:00
|
|
|
/**
|
|
|
|
* triggered by user manually changing slug
|
|
|
|
*/
|
|
|
|
updateSlug: function (newSlug) {
|
|
|
|
var slug = this.get('slug'),
|
2014-06-08 10:02:21 +04:00
|
|
|
self = this;
|
2014-07-17 09:54:32 +04:00
|
|
|
|
2014-06-16 03:05:37 +04:00
|
|
|
newSlug = newSlug || slug;
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-08-12 18:46:14 +04:00
|
|
|
newSlug = newSlug && newSlug.trim();
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-16 03:05:37 +04:00
|
|
|
// Ignore unchanged slugs or candidate slugs that are empty
|
|
|
|
if (!newSlug || slug === newSlug) {
|
2014-08-12 21:00:09 +04:00
|
|
|
// reset the input to its previous state
|
|
|
|
this.set('slugValue', slug);
|
|
|
|
|
2014-06-08 10:02:21 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-16 03:05:37 +04:00
|
|
|
this.get('slugGenerator').generateSlug(newSlug).then(function (serverSlug) {
|
|
|
|
// If after getting the sanitized and unique slug back from the API
|
|
|
|
// we end up with a slug that matches the existing slug, abort the change
|
|
|
|
if (serverSlug === slug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because the server transforms the candidate slug by stripping
|
|
|
|
// certain characters and appending a number onto the end of slugs
|
2014-06-21 01:36:44 +04:00
|
|
|
// to enforce uniqueness, there are cases where we can get back a
|
2014-06-16 03:05:37 +04:00
|
|
|
// candidate slug that is a duplicate of the original except for
|
|
|
|
// the trailing incrementor (e.g., this-is-a-slug and this-is-a-slug-2)
|
|
|
|
|
|
|
|
// get the last token out of the slug candidate and see if it's a number
|
|
|
|
var slugTokens = serverSlug.split('-'),
|
|
|
|
check = Number(slugTokens.pop());
|
|
|
|
|
|
|
|
// if the candidate slug is the same as the existing slug except
|
|
|
|
// for the incrementor then the existing slug should be used
|
2014-06-17 01:44:09 +04:00
|
|
|
if (_.isNumber(check) && check > 0) {
|
2014-06-16 03:05:37 +04:00
|
|
|
if (slug === slugTokens.join('-') && serverSlug !== newSlug) {
|
2014-08-12 21:00:09 +04:00
|
|
|
self.set('slugValue', slug);
|
|
|
|
|
2014-06-16 03:05:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.set('slug', serverSlug);
|
|
|
|
|
2014-07-03 21:09:05 +04:00
|
|
|
if (self.hasObserverFor('titleScratch')) {
|
|
|
|
self.removeObserver('titleScratch', self, 'titleObserver');
|
2014-06-16 03:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a new post. Don't save the model. Defer the save
|
|
|
|
// to the user pressing the save button
|
|
|
|
if (self.get('isNew')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-06 00:44:19 +04:00
|
|
|
return self.get('model').save(self.get('saveOptions'));
|
2014-08-12 21:00:09 +04:00
|
|
|
}).then(function (changed) {
|
|
|
|
if (changed) {
|
|
|
|
self.showSuccess('Permalink successfully changed to <strong>' +
|
2014-07-01 16:18:47 +04:00
|
|
|
self.get('slug') + '</strong>.');
|
2014-08-12 21:00:09 +04:00
|
|
|
}
|
2014-07-01 16:18:47 +04:00
|
|
|
}).catch(function (errors) {
|
|
|
|
self.showErrors(errors);
|
|
|
|
self.get('model').rollback();
|
2014-06-16 03:05:37 +04:00
|
|
|
});
|
2014-06-08 10:02:21 +04:00
|
|
|
},
|
|
|
|
|
2014-06-09 00:48:14 +04:00
|
|
|
/**
|
|
|
|
* Parse user's set published date.
|
|
|
|
* Action sent by post settings menu view.
|
|
|
|
* (#1351)
|
|
|
|
*/
|
|
|
|
setPublishedAt: function (userInput) {
|
|
|
|
var errMessage = '',
|
|
|
|
newPublishedAt = parseDateString(userInput),
|
|
|
|
publishedAt = this.get('published_at'),
|
|
|
|
self = this;
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-09 00:48:14 +04:00
|
|
|
if (!userInput) {
|
|
|
|
//Clear out the published_at field for a draft
|
|
|
|
if (this.get('isDraft')) {
|
|
|
|
this.set('published_at', null);
|
2014-06-08 10:02:21 +04:00
|
|
|
}
|
2014-06-09 00:48:14 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-08 10:02:21 +04:00
|
|
|
|
|
|
|
// Validate new Published date
|
2014-06-09 00:48:14 +04:00
|
|
|
if (!newPublishedAt.isValid()) {
|
2014-06-08 10:02:21 +04:00
|
|
|
errMessage = 'Published Date must be a valid date with format: ' +
|
|
|
|
'DD MMM YY @ HH:mm (e.g. 6 Dec 14 @ 15:00)';
|
|
|
|
}
|
2014-06-09 00:48:14 +04:00
|
|
|
if (newPublishedAt.diff(new Date(), 'h') > 0) {
|
2014-06-08 10:02:21 +04:00
|
|
|
errMessage = 'Published Date cannot currently be in the future.';
|
|
|
|
}
|
|
|
|
|
2014-06-09 00:48:14 +04:00
|
|
|
//If errors, notify and exit.
|
2014-06-08 10:02:21 +04:00
|
|
|
if (errMessage) {
|
2014-07-01 16:18:47 +04:00
|
|
|
this.showErrors(errMessage);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do nothing if the user didn't actually change the date
|
|
|
|
if (publishedAt && publishedAt.isSame(newPublishedAt)) {
|
2014-06-08 10:02:21 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Validation complete
|
2014-06-09 00:48:14 +04:00
|
|
|
this.set('published_at', newPublishedAt);
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-07-01 16:18:47 +04:00
|
|
|
// If this is a new post. Don't save the model. Defer the save
|
|
|
|
// to the user pressing the save button
|
|
|
|
if (this.get('isNew')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-15 08:02:34 +04:00
|
|
|
this.get('model').save(this.get('saveOptions')).catch(function (errors) {
|
2014-07-01 16:18:47 +04:00
|
|
|
self.showErrors(errors);
|
|
|
|
self.get('model').rollback();
|
2014-06-21 01:36:44 +04:00
|
|
|
});
|
2014-06-08 10:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-02 09:21:43 +04:00
|
|
|
export default PostSettingsMenuController;
|