2015-05-24 08:47:23 +03:00
|
|
|
import Ember from 'ember';
|
2014-06-06 05:18:03 +04:00
|
|
|
import PostModel from 'ghost/models/post';
|
2014-06-15 00:45:50 +04:00
|
|
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
2015-03-13 12:39:28 +03:00
|
|
|
import imageManager from 'ghost/utils/ed-image-manager';
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// this array will hold properties we need to watch
|
|
|
|
// to know if the model has been changed (`controller.isDirty`)
|
2015-05-26 05:10:50 +03:00
|
|
|
var watchedProps = ['model.scratch', 'model.titleScratch', 'model.isDirty', 'model.tags.[]'];
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2014-10-14 21:29:54 +04:00
|
|
|
PostModel.eachAttribute(function (name) {
|
2014-06-06 05:18:03 +04:00
|
|
|
watchedProps.push('model.' + name);
|
|
|
|
});
|
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
export default Ember.Mixin.create({
|
|
|
|
postSettingsMenuController: Ember.inject.controller('post-settings-menu'),
|
2014-06-23 22:58:19 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
autoSaveId: null,
|
|
|
|
timedSaveId: null,
|
2015-03-13 12:39:28 +03:00
|
|
|
editor: null,
|
2014-12-30 05:11:24 +03:00
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
notifications: Ember.inject.service(),
|
|
|
|
|
2014-06-21 22:58:06 +04:00
|
|
|
init: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
window.onbeforeunload = function () {
|
|
|
|
return self.get('isDirty') ? self.unloadDirtyMessage() : null;
|
|
|
|
};
|
|
|
|
},
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-07-04 21:25:55 +03:00
|
|
|
shouldFocusTitle: Ember.computed.alias('model.isNew'),
|
|
|
|
shouldFocusEditor: false,
|
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
autoSave: Ember.observer('model.scratch', function () {
|
2015-03-17 21:03:41 +03:00
|
|
|
// Don't save just because we swapped out models
|
2015-04-11 18:44:25 +03:00
|
|
|
if (this.get('model.isDraft') && !this.get('model.isNew')) {
|
2015-03-17 21:03:41 +03:00
|
|
|
var autoSaveId,
|
2015-05-10 01:23:14 +03:00
|
|
|
timedSaveId,
|
|
|
|
saveOptions;
|
2015-03-17 21:03:41 +03:00
|
|
|
|
2015-05-10 01:23:14 +03:00
|
|
|
saveOptions = {
|
|
|
|
silent: true,
|
|
|
|
backgroundSave: true
|
|
|
|
};
|
|
|
|
|
|
|
|
timedSaveId = Ember.run.throttle(this, 'send', 'save', saveOptions, 60000, false);
|
2015-03-17 21:03:41 +03:00
|
|
|
this.set('timedSaveId', timedSaveId);
|
|
|
|
|
2015-05-10 01:23:14 +03:00
|
|
|
autoSaveId = Ember.run.debounce(this, 'send', 'save', saveOptions, 3000);
|
2015-03-17 21:03:41 +03:00
|
|
|
this.set('autoSaveId', autoSaveId);
|
|
|
|
}
|
2015-06-03 05:56:42 +03:00
|
|
|
}),
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-06-08 10:02:21 +04:00
|
|
|
/**
|
|
|
|
* By default, a post will not change its publish state.
|
|
|
|
* Only with a user-set value (via setSaveType action)
|
|
|
|
* can the post's status change.
|
|
|
|
*/
|
2014-12-30 05:11:24 +03:00
|
|
|
willPublish: boundOneWay('model.isPublished'),
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// set by the editor route and `isDirty`. useful when checking
|
|
|
|
// whether the number of tags has changed for `isDirty`.
|
|
|
|
previousTagNames: null,
|
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
tagNames: Ember.computed('model.tags.@each.name', function () {
|
|
|
|
return this.get('model.tags').mapBy('name');
|
2014-07-30 05:57:19 +04:00
|
|
|
}),
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-01-08 21:23:48 +03:00
|
|
|
postOrPage: Ember.computed('model.page', function () {
|
|
|
|
return this.get('model.page') ? 'Page' : 'Post';
|
|
|
|
}),
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// compares previousTagNames to tagNames
|
|
|
|
tagNamesEqual: function () {
|
|
|
|
var tagNames = this.get('tagNames'),
|
|
|
|
previousTagNames = this.get('previousTagNames'),
|
|
|
|
hashCurrent,
|
|
|
|
hashPrevious;
|
|
|
|
|
|
|
|
// beware! even if they have the same length,
|
|
|
|
// that doesn't mean they're the same.
|
|
|
|
if (tagNames.length !== previousTagNames.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// instead of comparing with slow, nested for loops,
|
|
|
|
// perform join on each array and compare the strings
|
|
|
|
hashCurrent = tagNames.join('');
|
|
|
|
hashPrevious = previousTagNames.join('');
|
|
|
|
|
|
|
|
return hashCurrent === hashPrevious;
|
|
|
|
},
|
|
|
|
|
2014-11-04 06:31:10 +03:00
|
|
|
// a hook created in editor-base-route's setupController
|
2014-06-28 07:01:56 +04:00
|
|
|
modelSaved: function () {
|
|
|
|
var model = this.get('model');
|
|
|
|
|
|
|
|
// safer to updateTags on save in one place
|
|
|
|
// rather than in all other places save is called
|
|
|
|
model.updateTags();
|
|
|
|
|
|
|
|
// set previousTagNames to current tagNames for isDirty check
|
|
|
|
this.set('previousTagNames', this.get('tagNames'));
|
|
|
|
|
|
|
|
// `updateTags` triggers `isDirty => true`.
|
|
|
|
// for a saved model it would otherwise be false.
|
2014-09-04 22:12:03 +04:00
|
|
|
|
|
|
|
// if the two "scratch" properties (title and content) match the model, then
|
|
|
|
// it's ok to set isDirty to false
|
2014-12-30 05:11:24 +03:00
|
|
|
if (model.get('titleScratch') === model.get('title') &&
|
|
|
|
model.get('scratch') === model.get('markdown')) {
|
2014-09-04 22:12:03 +04:00
|
|
|
this.set('isDirty', false);
|
|
|
|
}
|
2014-06-28 07:01:56 +04:00
|
|
|
},
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// an ugly hack, but necessary to watch all the model's properties
|
|
|
|
// and more, without having to be explicit and do it manually
|
2015-06-03 05:56:42 +03:00
|
|
|
isDirty: Ember.computed.apply(Ember, watchedProps.concat({
|
|
|
|
get: function () {
|
|
|
|
var model = this.get('model'),
|
|
|
|
markdown = model.get('markdown'),
|
|
|
|
title = model.get('title'),
|
|
|
|
titleScratch = model.get('titleScratch'),
|
|
|
|
scratch = this.get('editor').getValue(),
|
|
|
|
changedAttributes;
|
|
|
|
|
|
|
|
if (!this.tagNamesEqual()) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
if (titleScratch !== title) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
// since `scratch` is not model property, we need to check
|
|
|
|
// it explicitly against the model's markdown attribute
|
|
|
|
if (markdown !== scratch) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-07 07:39:19 +04:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
// if the Adapter failed to save the model isError will be true
|
|
|
|
// and we should consider the model still dirty.
|
|
|
|
if (model.get('isError')) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
// models created on the client always return `isDirty: true`,
|
|
|
|
// so we need to see which properties have actually changed.
|
|
|
|
if (model.get('isNew')) {
|
|
|
|
changedAttributes = Ember.keys(model.changedAttributes());
|
2014-11-29 22:42:57 +03:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
if (changedAttributes.length) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
return false;
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
|
|
|
|
2015-06-03 05:56:42 +03:00
|
|
|
// even though we use the `scratch` prop to show edits,
|
|
|
|
// which does *not* change the model's `isDirty` property,
|
|
|
|
// `isDirty` will tell us if the other props have changed,
|
|
|
|
// as long as the model is not new (model.isNew === false).
|
|
|
|
return model.get('isDirty');
|
|
|
|
},
|
|
|
|
set: function (key, value) {
|
|
|
|
return value;
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
|
|
|
})),
|
|
|
|
|
|
|
|
// used on window.onbeforeunload
|
|
|
|
unloadDirtyMessage: function () {
|
|
|
|
return '==============================\n\n' +
|
|
|
|
'Hey there! It looks like you\'re in the middle of writing' +
|
|
|
|
' something and you haven\'t saved all of your content.' +
|
|
|
|
'\n\nSave before you go!\n\n' +
|
|
|
|
'==============================';
|
|
|
|
},
|
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
// TODO: This has to be moved to the I18n localization file.
|
|
|
|
// This structure is supposed to be close to the i18n-localization which will be used soon.
|
2014-06-26 13:42:29 +04:00
|
|
|
messageMap: {
|
|
|
|
errors: {
|
|
|
|
post: {
|
|
|
|
published: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Update failed.',
|
|
|
|
draft: 'Saving failed.'
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
draft: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Publish failed.',
|
|
|
|
draft: 'Saving failed.'
|
2014-06-26 13:42:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
success: {
|
|
|
|
post: {
|
|
|
|
published: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Updated.',
|
|
|
|
draft: 'Saved.'
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
draft: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Published!',
|
|
|
|
draft: 'Saved.'
|
2014-06-26 13:42:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
showSaveNotification: function (prevStatus, status, delay) {
|
2014-12-15 13:11:29 +03:00
|
|
|
var message = this.messageMap.success.post[prevStatus][status],
|
2015-04-09 00:29:26 +03:00
|
|
|
path = this.get('model.absoluteUrl'),
|
2015-05-26 05:10:50 +03:00
|
|
|
type = this.get('postOrPage'),
|
|
|
|
notifications = this.get('notifications');
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2014-12-15 13:11:29 +03:00
|
|
|
if (status === 'published') {
|
2015-04-09 00:29:26 +03:00
|
|
|
message += ` <a href="${path}">View ${type}</a>`;
|
2014-12-15 13:11:29 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
|
|
|
notifications.showSuccess(message.htmlSafe(), {delayed: delay});
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
showErrorNotification: function (prevStatus, status, errors, delay) {
|
2014-11-29 22:42:57 +03:00
|
|
|
var message = this.messageMap.errors.post[prevStatus][status],
|
2015-05-26 05:10:50 +03:00
|
|
|
error = (errors && errors[0] && errors[0].message) || 'Unknown Error',
|
|
|
|
notifications = this.get('notifications');
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2014-11-29 22:42:57 +03:00
|
|
|
message += '<br />' + error;
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
notifications.showError(message.htmlSafe(), {delayed: delay});
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
|
2014-06-08 10:02:21 +04:00
|
|
|
actions: {
|
2014-10-08 18:53:20 +04:00
|
|
|
save: function (options) {
|
2015-05-10 01:23:14 +03:00
|
|
|
var status,
|
2014-12-30 05:11:24 +03:00
|
|
|
prevStatus = this.get('model.status'),
|
|
|
|
isNew = this.get('model.isNew'),
|
2014-10-20 22:03:11 +04:00
|
|
|
autoSaveId = this.get('autoSaveId'),
|
2014-10-22 19:26:27 +04:00
|
|
|
timedSaveId = this.get('timedSaveId'),
|
2014-10-18 22:39:47 +04:00
|
|
|
self = this,
|
2015-05-26 05:10:50 +03:00
|
|
|
psmController = this.get('postSettingsMenuController'),
|
|
|
|
promise,
|
|
|
|
notifications = this.get('notifications');
|
2014-10-20 22:03:11 +04:00
|
|
|
|
2014-10-08 18:53:20 +04:00
|
|
|
options = options || {};
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2015-05-10 01:23:14 +03:00
|
|
|
if (options.backgroundSave) {
|
|
|
|
// do not allow a post's status to be set to published by a background save
|
|
|
|
status = 'draft';
|
|
|
|
} else {
|
|
|
|
status = this.get('willPublish') ? 'published' : 'draft';
|
|
|
|
}
|
|
|
|
|
2014-10-25 01:09:50 +04:00
|
|
|
if (autoSaveId) {
|
2014-10-20 22:03:11 +04:00
|
|
|
Ember.run.cancel(autoSaveId);
|
|
|
|
this.set('autoSaveId', null);
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:26:27 +04:00
|
|
|
if (timedSaveId) {
|
|
|
|
Ember.run.cancel(timedSaveId);
|
|
|
|
this.set('timedSaveId', null);
|
|
|
|
}
|
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
notifications.closePassive();
|
2014-06-24 10:33:24 +04:00
|
|
|
|
2014-07-03 21:09:05 +04:00
|
|
|
// Set the properties that are indirected
|
2014-06-06 05:18:03 +04:00
|
|
|
// set markdown equal to what's in the editor, minus the image markers.
|
2015-03-13 12:39:28 +03:00
|
|
|
this.set('model.markdown', this.get('editor').getValue());
|
2014-12-30 05:11:24 +03:00
|
|
|
this.set('model.status', status);
|
2014-06-21 01:36:44 +04:00
|
|
|
|
2014-10-08 18:53:20 +04:00
|
|
|
// Set a default title
|
2014-12-30 05:11:24 +03:00
|
|
|
if (!this.get('model.titleScratch').trim()) {
|
|
|
|
this.set('model.titleScratch', '(Untitled)');
|
2014-10-08 18:53:20 +04:00
|
|
|
}
|
2014-10-18 15:16:43 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
this.set('model.title', this.get('model.titleScratch'));
|
|
|
|
this.set('model.meta_title', psmController.get('metaTitleScratch'));
|
|
|
|
this.set('model.meta_description', psmController.get('metaDescriptionScratch'));
|
2014-10-08 18:53:20 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
if (!this.get('model.slug')) {
|
2014-10-18 22:39:47 +04:00
|
|
|
// Cancel any pending slug generation that may still be queued in the
|
|
|
|
// run loop because we need to run it before the post is saved.
|
|
|
|
Ember.run.cancel(psmController.get('debounceId'));
|
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
psmController.generateAndSetSlug('model.slug');
|
2014-10-18 22:39:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
promise = Ember.RSVP.resolve(psmController.get('lastPromise')).then(function () {
|
|
|
|
return self.get('model').save(options).then(function (model) {
|
|
|
|
if (!options.silent) {
|
|
|
|
self.showSaveNotification(prevStatus, model.get('status'), isNew ? true : false);
|
|
|
|
}
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-10-18 22:39:47 +04:00
|
|
|
return model;
|
|
|
|
});
|
2014-06-22 01:59:12 +04:00
|
|
|
}).catch(function (errors) {
|
2014-10-08 18:53:20 +04:00
|
|
|
if (!options.silent) {
|
2014-12-30 05:11:24 +03:00
|
|
|
self.showErrorNotification(prevStatus, self.get('model.status'), errors);
|
2014-10-08 18:53:20 +04:00
|
|
|
}
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-12-30 05:11:24 +03:00
|
|
|
self.set('model.status', prevStatus);
|
2014-08-15 00:39:02 +04:00
|
|
|
|
2014-11-29 22:42:57 +03:00
|
|
|
return self.get('model');
|
2014-06-21 01:36:44 +04:00
|
|
|
});
|
2014-10-18 22:39:47 +04:00
|
|
|
|
|
|
|
psmController.set('lastPromise', promise);
|
|
|
|
|
|
|
|
return promise;
|
2014-06-08 10:02:21 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setSaveType: function (newType) {
|
|
|
|
if (newType === 'publish') {
|
|
|
|
this.set('willPublish', true);
|
|
|
|
} else if (newType === 'draft') {
|
|
|
|
this.set('willPublish', false);
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
// set from a `sendAction` on the gh-ed-editor component,
|
2014-06-06 05:18:03 +04:00
|
|
|
// so that we get a reference for handling uploads.
|
2015-03-13 12:39:28 +03:00
|
|
|
setEditor: function (editor) {
|
|
|
|
this.set('editor', editor);
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
// fired from the gh-ed-preview component when an image upload starts
|
|
|
|
disableEditor: function () {
|
|
|
|
this.get('editor').disable();
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
2015-03-13 12:39:28 +03:00
|
|
|
// fired from the gh-ed-preview component when an image upload finishes
|
|
|
|
enableEditor: function () {
|
|
|
|
this.get('editor').enable();
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Match the uploaded file to a line in the editor, and update that line with a path reference
|
|
|
|
// ensuring that everything ends up in the correct place and format.
|
2014-10-25 01:09:50 +04:00
|
|
|
handleImgUpload: function (e, resultSrc) {
|
2015-03-13 12:39:28 +03:00
|
|
|
var editor = this.get('editor'),
|
|
|
|
editorValue = editor.getValue(),
|
|
|
|
replacement = imageManager.getSrcRange(editorValue, e.target),
|
|
|
|
cursorPosition;
|
|
|
|
|
|
|
|
if (replacement) {
|
|
|
|
cursorPosition = replacement.start + resultSrc.length + 1;
|
|
|
|
if (replacement.needsParens) {
|
|
|
|
resultSrc = '(' + resultSrc + ')';
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
2015-03-13 12:39:28 +03:00
|
|
|
editor.replaceSelection(resultSrc, replacement.start, replacement.end, cursorPosition);
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
2014-08-20 21:32:51 +04:00
|
|
|
},
|
|
|
|
|
2014-10-18 15:16:43 +04:00
|
|
|
autoSaveNew: function () {
|
2014-12-30 05:11:24 +03:00
|
|
|
if (this.get('model.isNew')) {
|
2015-05-26 21:24:32 +03:00
|
|
|
this.send('save', {silent: true, backgroundSave: true});
|
2014-10-18 00:57:10 +04:00
|
|
|
}
|
2015-06-13 17:34:09 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
updateEditorScrollInfo: function (scrollInfo) {
|
|
|
|
this.set('editorScrollInfo', scrollInfo);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateHeight: function (height) {
|
|
|
|
this.set('height', height);
|
2014-06-08 10:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|