2015-05-26 05:10:50 +03:00
|
|
|
import Ember from 'ember';
|
2014-06-08 10:02:21 +04:00
|
|
|
import {parseDateString, formatDate} from 'ghost/utils/date-formatting';
|
2014-12-14 06:09:19 +03:00
|
|
|
import SettingsMenuMixin from 'ghost/mixins/settings-menu-controller';
|
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-08-01 00:29:35 +04:00
|
|
|
import isNumber from 'ghost/utils/isNumber';
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
export default Ember.Controller.extend(SettingsMenuMixin, {
|
2014-12-30 05:11:24 +03:00
|
|
|
debounceId: null,
|
2014-10-18 22:39:47 +04:00
|
|
|
lastPromise: null,
|
2014-08-10 18:40:04 +04:00
|
|
|
selectedAuthor: null,
|
2014-12-30 05:11:24 +03:00
|
|
|
uploaderReference: null,
|
|
|
|
|
2015-05-24 08:47:23 +03:00
|
|
|
application: Ember.inject.controller(),
|
2015-05-26 05:10:50 +03:00
|
|
|
config: Ember.inject.service(),
|
|
|
|
ghostPaths: Ember.inject.service('ghost-paths'),
|
|
|
|
notifications: Ember.inject.service(),
|
2015-05-24 08:47:23 +03:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
initializeSelectedAuthor: Ember.observer('model', function () {
|
2014-08-02 09:21:43 +04:00
|
|
|
var self = this;
|
2014-09-16 02:46:40 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
return this.get('model.author').then(function (author) {
|
2014-08-02 09:21:43 +04:00
|
|
|
self.set('selectedAuthor', author);
|
|
|
|
return author;
|
|
|
|
});
|
2015-06-03 05:56:42 +03:00
|
|
|
}),
|
2014-08-10 18:40:04 +04:00
|
|
|
|
2014-07-30 05:57:19 +04:00
|
|
|
authors: Ember.computed(function () {
|
2014-10-25 01:09:50 +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-09-22 19:30:00 +04:00
|
|
|
deferred.promise = this.store.find('user', {limit: 'all'}).then(function (users) {
|
2014-09-27 18:39:27 +04:00
|
|
|
return users.rejectBy('id', 'me').sortBy('name');
|
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-10-22 01:20:51 +04:00
|
|
|
|
2014-12-01 22:42:35 +03:00
|
|
|
/*jshint unused:false */
|
2015-06-03 05:56:42 +03:00
|
|
|
publishedAtValue: Ember.computed('model.published_at', {
|
|
|
|
get: function () {
|
|
|
|
var pubDate = this.get('model.published_at');
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
if (pubDate) {
|
|
|
|
return formatDate(pubDate);
|
|
|
|
}
|
2014-12-01 22:42:35 +03:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
return formatDate(moment());
|
|
|
|
},
|
|
|
|
set: function (key, value) {
|
|
|
|
// We're using a fake setter to reset
|
|
|
|
// the cache for this property
|
|
|
|
return formatDate(moment());
|
2014-06-09 00:48:14 +04:00
|
|
|
}
|
2014-07-30 05:57:19 +04:00
|
|
|
}),
|
2014-12-01 22:42:35 +03:00
|
|
|
/*jshint unused:true */
|
2014-06-09 00:48:14 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
slugValue: boundOneWay('model.slug'),
|
2014-10-22 01:20:51 +04:00
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
// Lazy load the slug generator
|
2014-06-09 00:48:14 +04:00
|
|
|
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
|
|
|
}),
|
2014-10-25 01:09:50 +04:00
|
|
|
|
|
|
|
// Requests slug from title
|
2014-10-18 00:57:10 +04:00
|
|
|
generateAndSetSlug: function (destination) {
|
2014-06-09 00:48:14 +04:00
|
|
|
var self = this,
|
2014-12-30 05:11:24 +03:00
|
|
|
title = this.get('model.titleScratch'),
|
2014-10-18 22:39:47 +04:00
|
|
|
afterSave = this.get('lastPromise'),
|
|
|
|
promise;
|
|
|
|
|
|
|
|
// Only set an "untitled" slug once per post
|
2014-12-30 05:11:24 +03:00
|
|
|
if (title === '(Untitled)' && this.get('model.slug')) {
|
2014-10-18 22:39:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-07-17 09:54:32 +04:00
|
|
|
|
2014-10-18 22:39:47 +04:00
|
|
|
promise = Ember.RSVP.resolve(afterSave).then(function () {
|
|
|
|
return self.get('slugGenerator').generateSlug(title).then(function (slug) {
|
|
|
|
self.set(destination, slug);
|
2014-11-29 22:42:57 +03:00
|
|
|
}).catch(function () {
|
|
|
|
// Nothing to do (would be nice to log this somewhere though),
|
|
|
|
// but a rejected promise needs to be handled here so that a resolved
|
|
|
|
// promise is returned.
|
2014-10-18 22:39:47 +04:00
|
|
|
});
|
2014-06-09 00:48:14 +04:00
|
|
|
});
|
2014-10-18 22:39:47 +04:00
|
|
|
|
|
|
|
this.set('lastPromise', promise);
|
2014-06-09 00:48:14 +04:00
|
|
|
},
|
2014-09-15 04:40:24 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
metaTitleScratch: boundOneWay('model.meta_title'),
|
|
|
|
metaDescriptionScratch: boundOneWay('model.meta_description'),
|
2014-09-19 03:42:07 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
seoTitle: Ember.computed('model.titleScratch', 'metaTitleScratch', function () {
|
2014-09-21 22:56:30 +04:00
|
|
|
var metaTitle = this.get('metaTitleScratch') || '';
|
2014-09-19 03:42:07 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
metaTitle = metaTitle.length > 0 ? metaTitle : this.get('model.titleScratch');
|
2014-10-17 20:18:52 +04:00
|
|
|
|
|
|
|
if (metaTitle.length > 70) {
|
|
|
|
metaTitle = metaTitle.substring(0, 70).trim();
|
|
|
|
metaTitle = Ember.Handlebars.Utils.escapeExpression(metaTitle);
|
2015-02-09 18:57:50 +03:00
|
|
|
metaTitle = Ember.String.htmlSafe(metaTitle + '…');
|
2014-10-17 20:18:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return metaTitle;
|
2014-09-19 03:42:07 +04:00
|
|
|
}),
|
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
seoDescription: Ember.computed('model.scratch', 'metaDescriptionScratch', function () {
|
2014-09-23 13:37:08 +04:00
|
|
|
var metaDescription = this.get('metaDescriptionScratch') || '',
|
|
|
|
el,
|
|
|
|
html = '',
|
|
|
|
placeholder;
|
|
|
|
|
|
|
|
if (metaDescription.length > 0) {
|
|
|
|
placeholder = metaDescription;
|
|
|
|
} else {
|
|
|
|
el = $('.rendered-markdown');
|
|
|
|
|
|
|
|
// Get rendered markdown
|
2014-10-04 20:38:15 +04:00
|
|
|
if (el !== undefined && el.length > 0) {
|
2014-09-23 13:37:08 +04:00
|
|
|
html = el.clone();
|
2014-11-07 08:22:34 +03:00
|
|
|
html.find('.js-drop-zone').remove();
|
2014-09-23 13:37:08 +04:00
|
|
|
html = html[0].innerHTML;
|
|
|
|
}
|
2014-09-19 03:42:07 +04:00
|
|
|
|
2014-09-23 13:37:08 +04:00
|
|
|
// Strip HTML
|
2014-10-25 01:09:50 +04:00
|
|
|
placeholder = $('<div />', {html: html}).text();
|
2014-09-23 13:37:08 +04:00
|
|
|
// Replace new lines and trim
|
2014-10-25 01:09:50 +04:00
|
|
|
// jscs: disable
|
2014-09-23 13:37:08 +04:00
|
|
|
placeholder = placeholder.replace(/\n+/g, ' ').trim();
|
2014-10-25 01:09:50 +04:00
|
|
|
// jscs: enable
|
2014-09-23 13:37:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (placeholder.length > 156) {
|
|
|
|
// Limit to 156 characters
|
2014-10-07 19:02:11 +04:00
|
|
|
placeholder = placeholder.substring(0, 156).trim();
|
2014-09-23 13:37:08 +04:00
|
|
|
placeholder = Ember.Handlebars.Utils.escapeExpression(placeholder);
|
2015-02-09 18:57:50 +03:00
|
|
|
placeholder = Ember.String.htmlSafe(placeholder + '…');
|
2014-09-23 13:37:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return placeholder;
|
2014-09-19 03:42:07 +04:00
|
|
|
}),
|
|
|
|
|
2015-09-02 21:34:48 +03:00
|
|
|
seoURL: Ember.computed('model.slug', 'config.blogUrl', function () {
|
|
|
|
var blogUrl = this.get('config.blogUrl'),
|
2014-12-30 05:11:24 +03:00
|
|
|
seoSlug = this.get('model.slug') ? this.get('model.slug') : '',
|
2014-10-22 01:20:51 +04:00
|
|
|
seoURL = blogUrl + '/' + seoSlug;
|
|
|
|
|
|
|
|
// only append a slash to the URL if the slug exists
|
|
|
|
if (seoSlug) {
|
|
|
|
seoURL += '/';
|
|
|
|
}
|
2014-10-17 20:18:52 +04:00
|
|
|
|
|
|
|
if (seoURL.length > 70) {
|
|
|
|
seoURL = seoURL.substring(0, 70).trim();
|
2015-02-09 18:57:50 +03:00
|
|
|
seoURL = Ember.String.htmlSafe(seoURL + '…');
|
2014-10-17 20:18:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return seoURL;
|
2014-09-19 03:42:07 +04:00
|
|
|
}),
|
2014-09-15 04:40:24 +04:00
|
|
|
|
|
|
|
// observe titleScratch, keeping the post's slug in sync
|
|
|
|
// with it until saved for the first time.
|
2015-06-03 05:56:42 +03:00
|
|
|
addTitleObserver: Ember.observer('model', function () {
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isNew') || this.get('model.title') === '(Untitled)') {
|
|
|
|
this.addObserver('model.titleScratch', this, 'titleObserver');
|
2014-09-15 04:40:24 +04:00
|
|
|
}
|
2015-06-03 05:56:42 +03:00
|
|
|
}),
|
2014-10-22 01:20:51 +04:00
|
|
|
|
2014-06-09 00:48:14 +04:00
|
|
|
titleObserver: function () {
|
2014-10-22 01:20:51 +04:00
|
|
|
var debounceId,
|
2014-12-30 05:11:24 +03:00
|
|
|
title = this.get('model.title');
|
2014-10-18 22:39:47 +04:00
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
// generate a slug if a post is new and doesn't have a title yet or
|
2014-10-22 01:20:51 +04:00
|
|
|
// if the title is still '(Untitled)' and the slug is unaltered.
|
2014-12-30 05:11:24 +03:00
|
|
|
if ((this.get('model.isNew') && !title) || title === '(Untitled)') {
|
2015-02-01 08:17:24 +03:00
|
|
|
debounceId = Ember.run.debounce(this, 'generateAndSetSlug', 'model.slug', 700);
|
2014-06-16 03:05:37 +04:00
|
|
|
}
|
2014-10-18 22:39:47 +04:00
|
|
|
|
|
|
|
this.set('debounceId', debounceId);
|
2014-06-16 03:05:37 +04:00
|
|
|
},
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2015-08-19 12:16:32 +03:00
|
|
|
// live-query of all tags for tag input autocomplete
|
|
|
|
availableTags: Ember.computed(function () {
|
|
|
|
return this.get('store').filter('tag', {limit: 'all'}, function () {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2014-07-01 16:18:47 +04:00
|
|
|
showErrors: function (errors) {
|
|
|
|
errors = Ember.isArray(errors) ? errors : [errors];
|
2015-05-26 05:10:50 +03:00
|
|
|
this.get('notifications').showErrors(errors);
|
2014-07-01 16:18:47 +04:00
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
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-12-30 05:11:24 +03:00
|
|
|
this.toggleProperty('model.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
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isNew')) {
|
2014-07-01 16:18:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-07-15 08:02:34 +04:00
|
|
|
|
2014-09-22 21:17:09 +04:00
|
|
|
this.get('model').save().catch(function (errors) {
|
2014-07-01 16:18:47 +04:00
|
|
|
self.showErrors(errors);
|
2014-09-23 12:47:35 +04:00
|
|
|
self.get('model').rollback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleFeatured: function () {
|
|
|
|
var self = this;
|
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
this.toggleProperty('model.featured');
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-09-23 12:47:35 +04:00
|
|
|
// If this is a new post. Don't save the model. Defer the save
|
|
|
|
// to the user pressing the save button
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isNew')) {
|
2014-09-23 12:47:35 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('model').save(this.get('saveOptions')).catch(function (errors) {
|
|
|
|
self.showErrors(errors);
|
2014-07-01 16:18:47 +04:00
|
|
|
self.get('model').rollback();
|
|
|
|
});
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-06-09 00:48:14 +04:00
|
|
|
/**
|
|
|
|
* triggered by user manually changing slug
|
|
|
|
*/
|
|
|
|
updateSlug: function (newSlug) {
|
2014-12-30 05:11:24 +03:00
|
|
|
var slug = this.get('model.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-08-01 00:29:35 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
self.set('model.slug', serverSlug);
|
2014-06-16 03:05:37 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
if (self.hasObserverFor('model.titleScratch')) {
|
|
|
|
self.removeObserver('model.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
|
2014-12-30 05:11:24 +03:00
|
|
|
if (self.get('model.isNew')) {
|
2014-06-16 03:05:37 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-22 21:17:09 +04:00
|
|
|
return self.get('model').save();
|
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),
|
2014-12-30 05:11:24 +03:00
|
|
|
publishedAt = this.get('model.published_at'),
|
2014-06-09 00:48:14 +04:00
|
|
|
self = this;
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-09 00:48:14 +04:00
|
|
|
if (!userInput) {
|
2014-10-25 01:09:50 +04:00
|
|
|
// Clear out the published_at field for a draft
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isDraft')) {
|
|
|
|
this.set('model.published_at', null);
|
2014-06-08 10:02:21 +04:00
|
|
|
}
|
2014-10-25 01:09:50 +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-10-25 01:09:50 +04:00
|
|
|
// If errors, notify and exit.
|
2014-06-08 10:02:21 +04:00
|
|
|
if (errMessage) {
|
2015-08-29 22:02:06 +03:00
|
|
|
this.get('model.errors').add('post-setting-date', errMessage);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-01 16:18:47 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
// Validation complete
|
2014-12-30 05:11:24 +03:00
|
|
|
this.set('model.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
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isNew')) {
|
2014-07-01 16:18:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-22 21:17:09 +04:00
|
|
|
this.get('model').save().catch(function (errors) {
|
2014-09-16 02:46:40 +04:00
|
|
|
self.showErrors(errors);
|
|
|
|
self.get('model').rollback();
|
|
|
|
});
|
|
|
|
},
|
2014-09-19 03:42:07 +04:00
|
|
|
|
|
|
|
setMetaTitle: function (metaTitle) {
|
2015-08-29 22:02:06 +03:00
|
|
|
var property = 'meta_title',
|
|
|
|
model = this.get('model'),
|
|
|
|
currentTitle = model.get(property) || '';
|
2014-09-23 13:37:08 +04:00
|
|
|
|
|
|
|
// Only update if the title has changed
|
|
|
|
if (currentTitle === metaTitle) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-19 03:42:07 +04:00
|
|
|
|
2015-08-29 22:02:06 +03:00
|
|
|
model.set(property, metaTitle);
|
2014-09-19 03:42:07 +04:00
|
|
|
|
|
|
|
// If this is a new post. Don't save the model. Defer the save
|
|
|
|
// to the user pressing the save button
|
2015-08-29 22:02:06 +03:00
|
|
|
if (model.get('isNew')) {
|
2014-09-19 03:42:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-29 22:02:06 +03:00
|
|
|
model.save();
|
2014-09-19 03:42:07 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setMetaDescription: function (metaDescription) {
|
2015-08-29 22:02:06 +03:00
|
|
|
var property = 'meta_description',
|
|
|
|
model = this.get('model'),
|
|
|
|
currentDescription = model.get(property) || '';
|
2014-09-23 13:37:08 +04:00
|
|
|
|
|
|
|
// Only update if the description has changed
|
|
|
|
if (currentDescription === metaDescription) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-19 03:42:07 +04:00
|
|
|
|
2015-08-29 22:02:06 +03:00
|
|
|
model.set(property, metaDescription);
|
2014-09-19 03:42:07 +04:00
|
|
|
|
|
|
|
// If this is a new post. Don't save the model. Defer the save
|
|
|
|
// to the user pressing the save button
|
2015-08-29 22:02:06 +03:00
|
|
|
if (model.get('isNew')) {
|
2014-09-19 03:42:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-29 22:02:06 +03:00
|
|
|
model.save();
|
2014-09-19 03:42:07 +04:00
|
|
|
},
|
2014-09-16 02:46:40 +04:00
|
|
|
|
|
|
|
setCoverImage: function (image) {
|
|
|
|
var self = this;
|
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
this.set('model.image', image);
|
2014-09-16 02:46:40 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isNew')) {
|
2014-09-16 02:46:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-22 21:17:09 +04:00
|
|
|
this.get('model').save().catch(function (errors) {
|
2014-09-16 02:46:40 +04:00
|
|
|
self.showErrors(errors);
|
|
|
|
self.get('model').rollback();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clearCoverImage: function () {
|
|
|
|
var self = this;
|
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
this.set('model.image', '');
|
2014-09-16 02:46:40 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isNew')) {
|
2014-09-16 02:46:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-22 21:17:09 +04:00
|
|
|
this.get('model').save().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-09-15 04:40:24 +04:00
|
|
|
},
|
|
|
|
|
2014-11-22 04:42:03 +03:00
|
|
|
resetUploader: function () {
|
|
|
|
var uploader = this.get('uploaderReference');
|
|
|
|
|
|
|
|
if (uploader && uploader[0]) {
|
|
|
|
uploader[0].uploaderUi.reset();
|
|
|
|
}
|
2014-12-01 22:42:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
resetPubDate: function () {
|
|
|
|
this.set('publishedAtValue', '');
|
2015-05-24 08:47:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
closeNavMenu: function () {
|
|
|
|
this.get('application').send('closeNavMenu');
|
2015-06-13 17:34:09 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
changeAuthor: function (newAuthor) {
|
|
|
|
var author = this.get('model.author'),
|
|
|
|
model = this.get('model'),
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
// return if nothing changed
|
|
|
|
if (newAuthor.get('id') === author.get('id')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
model.set('author', newAuthor);
|
|
|
|
|
|
|
|
// if this is a new post (never been saved before), don't try to save it
|
|
|
|
if (this.get('model.isNew')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
model.save().catch(function (errors) {
|
|
|
|
self.showErrors(errors);
|
|
|
|
self.set('selectedAuthor', author);
|
|
|
|
model.rollback();
|
|
|
|
});
|
2015-08-19 12:16:32 +03:00
|
|
|
},
|
|
|
|
|
2015-08-26 23:19:33 +03:00
|
|
|
addTag: function (tagName, index) {
|
2015-08-19 12:16:32 +03:00
|
|
|
var self = this,
|
|
|
|
currentTags = this.get('model.tags'),
|
|
|
|
currentTagNames = currentTags.map(function (tag) { return tag.get('name').toLowerCase(); }),
|
|
|
|
availableTagNames = null,
|
|
|
|
tagToAdd = null;
|
|
|
|
|
2015-08-24 17:32:05 +03:00
|
|
|
tagName = tagName.trim();
|
|
|
|
|
2015-08-19 12:16:32 +03:00
|
|
|
// abort if tag is already selected
|
|
|
|
if (currentTagNames.contains(tagName.toLowerCase())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('availableTags').then(function (availableTags) {
|
|
|
|
availableTagNames = availableTags.map(function (tag) { return tag.get('name').toLowerCase(); });
|
|
|
|
|
|
|
|
// find existing tag or create new
|
|
|
|
if (availableTagNames.contains(tagName.toLowerCase())) {
|
|
|
|
tagToAdd = availableTags.find(function (tag) {
|
|
|
|
return tag.get('name').toLowerCase() === tagName.toLowerCase();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
tagToAdd = self.get('store').createRecord('tag', {
|
|
|
|
name: tagName
|
|
|
|
});
|
2015-08-26 01:52:45 +03:00
|
|
|
|
|
|
|
// we need to set a UUID so that selectize has a unique value
|
|
|
|
// it will be ignored when sent to the server
|
|
|
|
tagToAdd.set('uuid', Ember.guidFor(tagToAdd));
|
2015-08-19 12:16:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// push tag onto post relationship
|
2015-08-26 23:19:33 +03:00
|
|
|
if (tagToAdd) { self.get('model.tags').insertAt(index, tagToAdd); }
|
2015-08-19 12:16:32 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeTag: function (tag) {
|
|
|
|
this.get('model.tags').removeObject(tag);
|
|
|
|
|
|
|
|
if (tag.get('isNew')) {
|
|
|
|
tag.destroyRecord();
|
|
|
|
}
|
2014-06-08 10:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|