2017-05-29 21:50:03 +03:00
|
|
|
import $ from 'jquery';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-05-29 21:50:03 +03:00
|
|
|
import NavigationItem from 'ghost-admin/models/navigation-item';
|
|
|
|
import RSVP from 'rsvp';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {inject as injectService} from '@ember/service';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
2017-03-14 16:54:58 +03:00
|
|
|
import {isThemeValidationError} from 'ghost-admin/services/ajax';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {notEmpty} from '@ember/object/computed';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
export default Controller.extend({
|
2016-06-30 13:21:47 +03:00
|
|
|
config: injectService(),
|
2017-02-21 22:04:50 +03:00
|
|
|
ghostPaths: injectService(),
|
2016-06-30 13:21:47 +03:00
|
|
|
notifications: injectService(),
|
2017-02-21 22:04:50 +03:00
|
|
|
session: injectService(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
newNavItem: null,
|
|
|
|
|
2017-02-21 22:04:50 +03:00
|
|
|
themes: null,
|
|
|
|
themeToDelete: null,
|
|
|
|
showDeleteThemeModal: notEmpty('themeToDelete'),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
blogUrl: computed('config.blogUrl', function () {
|
|
|
|
let url = this.get('config.blogUrl');
|
2015-01-18 03:16:54 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return url.slice(-1) !== '/' ? `${url}/` : url;
|
2015-01-18 03:16:54 +03:00
|
|
|
}),
|
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2016-04-26 12:45:59 +03:00
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
2016-02-09 20:16:18 +03:00
|
|
|
},
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
save: task(function* () {
|
2016-04-26 12:45:59 +03:00
|
|
|
let navItems = this.get('model.navigation');
|
2016-02-09 20:16:18 +03:00
|
|
|
let newNavItem = this.get('newNavItem');
|
2015-10-28 14:36:45 +03:00
|
|
|
let notifications = this.get('notifications');
|
2016-02-09 20:16:18 +03:00
|
|
|
let validationPromises = [];
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
if (!newNavItem.get('isBlank')) {
|
2017-02-21 22:04:50 +03:00
|
|
|
validationPromises.pushObject(this.send('addNavItem'));
|
2016-02-09 20:16:18 +03:00
|
|
|
}
|
2016-02-04 18:33:33 +03:00
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
navItems.map((item) => {
|
|
|
|
validationPromises.pushObject(item.validate());
|
2015-09-16 20:02:06 +03:00
|
|
|
});
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
try {
|
|
|
|
yield RSVP.all(validationPromises);
|
|
|
|
return yield this.get('model').save();
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
notifications.showAPIError(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
addNewNavItem() {
|
2016-04-26 12:45:59 +03:00
|
|
|
let navItems = this.get('model.navigation');
|
2016-02-09 20:16:18 +03:00
|
|
|
let newNavItem = this.get('newNavItem');
|
|
|
|
|
|
|
|
newNavItem.set('isNew', false);
|
|
|
|
navItems.pushObject(newNavItem);
|
2016-04-26 12:45:59 +03:00
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
2017-08-24 14:53:14 +03:00
|
|
|
$('.gh-blognav-line:last input:first').focus();
|
2016-02-09 20:16:18 +03:00
|
|
|
},
|
|
|
|
|
2017-02-21 22:04:50 +03:00
|
|
|
_deleteTheme() {
|
|
|
|
let theme = this.get('store').peekRecord('theme', this.get('themeToDelete').name);
|
|
|
|
|
|
|
|
if (!theme) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:44:04 +03:00
|
|
|
return theme.destroyRecord().then(() => {
|
|
|
|
// HACK: this is a private method, we need to unload from the store
|
|
|
|
// here so that uploading another theme with the same "id" doesn't
|
|
|
|
// attempt to update the deleted record
|
|
|
|
theme.unloadRecord();
|
|
|
|
}).catch((error) => {
|
2017-02-21 22:04:50 +03:00
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-01-11 22:55:52 +03:00
|
|
|
actions: {
|
2017-05-18 13:48:37 +03:00
|
|
|
save() {
|
|
|
|
this.get('save').perform();
|
|
|
|
},
|
|
|
|
|
2017-02-21 22:04:50 +03:00
|
|
|
addNavItem() {
|
2016-02-09 20:16:18 +03:00
|
|
|
let newNavItem = this.get('newNavItem');
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2016-03-18 18:32:03 +03:00
|
|
|
// If the url sent through is blank (user never edited the url)
|
|
|
|
if (newNavItem.get('url') === '') {
|
|
|
|
newNavItem.set('url', '/');
|
|
|
|
}
|
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
return newNavItem.validate().then(() => {
|
|
|
|
this.addNewNavItem();
|
|
|
|
});
|
2015-01-11 22:55:52 +03:00
|
|
|
},
|
|
|
|
|
2017-02-21 22:04:50 +03:00
|
|
|
deleteNavItem(item) {
|
2015-01-14 17:46:29 +03:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-26 12:45:59 +03:00
|
|
|
let navItems = this.get('model.navigation');
|
2015-02-03 19:29:01 +03:00
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
navItems.removeObject(item);
|
|
|
|
},
|
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
reorderItems(navItems) {
|
2016-04-26 12:45:59 +03:00
|
|
|
this.set('model.navigation', navItems);
|
2015-01-11 22:55:52 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
updateUrl(url, navItem) {
|
2015-01-18 03:16:54 +03:00
|
|
|
if (!navItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
navItem.set('url', url);
|
2016-02-09 20:16:18 +03:00
|
|
|
},
|
|
|
|
|
2017-03-03 18:31:42 +03:00
|
|
|
activateTheme(theme) {
|
2017-03-14 16:54:58 +03:00
|
|
|
return theme.activate().then((theme) => {
|
2017-06-22 20:19:01 +03:00
|
|
|
let themeName = theme.get('name');
|
|
|
|
|
2017-03-14 16:54:58 +03:00
|
|
|
if (!isEmpty(theme.get('warnings'))) {
|
|
|
|
this.set('themeWarnings', theme.get('warnings'));
|
|
|
|
this.set('showThemeWarningsModal', true);
|
|
|
|
}
|
2017-06-22 20:19:01 +03:00
|
|
|
|
|
|
|
if (!isEmpty(theme.get('errors'))) {
|
|
|
|
this.set('themeErrors', theme.get('errors'));
|
|
|
|
this.set('showThemeWarningsModal', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('themeErrors') || this.get('themeWarnings')) {
|
|
|
|
let message = `${themeName} activated successfully but some warnings/errors were detected.
|
|
|
|
You are still able to use and activate the theme. Here is your report...`;
|
|
|
|
this.set('message', message);
|
|
|
|
}
|
2017-03-14 16:54:58 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
if (isThemeValidationError(error)) {
|
2017-06-22 20:19:01 +03:00
|
|
|
let errors = error.errors[0].errorDetails;
|
|
|
|
let fatalErrors = [];
|
|
|
|
let normalErrors = [];
|
|
|
|
|
|
|
|
// to have a proper grouping of fatal errors and none fatal, we need to check
|
|
|
|
// our errors for the fatal property
|
|
|
|
if (errors.length > 0) {
|
|
|
|
for (let i = 0; i < errors.length; i++) {
|
|
|
|
if (errors[i].fatal) {
|
|
|
|
fatalErrors.push(errors[i]);
|
|
|
|
} else {
|
|
|
|
normalErrors.push(errors[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('themeErrors', normalErrors);
|
|
|
|
this.set('themeFatalErrors', fatalErrors);
|
2017-03-14 16:54:58 +03:00
|
|
|
this.set('showThemeErrorsModal', true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
2017-02-21 22:04:50 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
downloadTheme(theme) {
|
|
|
|
let themeURL = `${this.get('ghostPaths.apiRoot')}/themes/${theme.name}`;
|
|
|
|
let accessToken = this.get('session.data.authenticated.access_token');
|
|
|
|
let downloadURL = `${themeURL}/download/?access_token=${accessToken}`;
|
|
|
|
let iframe = $('#iframeDownload');
|
|
|
|
|
|
|
|
if (iframe.length === 0) {
|
|
|
|
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe.attr('src', downloadURL);
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteTheme(theme) {
|
|
|
|
if (theme) {
|
|
|
|
return this.set('themeToDelete', theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._deleteTheme();
|
|
|
|
},
|
|
|
|
|
|
|
|
hideDeleteThemeModal() {
|
|
|
|
this.set('themeToDelete', null);
|
|
|
|
},
|
|
|
|
|
2017-03-14 16:54:58 +03:00
|
|
|
hideThemeWarningsModal() {
|
|
|
|
this.set('themeWarnings', null);
|
2017-06-22 20:19:01 +03:00
|
|
|
this.set('themeErrors', null);
|
|
|
|
this.set('themeFatalErrors', null);
|
2017-03-14 16:54:58 +03:00
|
|
|
this.set('showThemeWarningsModal', false);
|
|
|
|
this.set('showThemeErrorsModal', false);
|
|
|
|
},
|
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
reset() {
|
2016-04-26 12:45:59 +03:00
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
2015-01-11 22:55:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|