2016-06-30 13:21:47 +03:00
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import Controller from 'ember-controller';
|
2017-02-21 22:04:50 +03:00
|
|
|
import computed, {notEmpty} from 'ember-computed';
|
2017-03-14 16:54:58 +03:00
|
|
|
import {isEmpty} from 'ember-utils';
|
2016-06-30 13:21:47 +03:00
|
|
|
import injectService from 'ember-service/inject';
|
2017-03-08 16:55:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2016-05-24 15:06:59 +03:00
|
|
|
import NavigationItem from 'ghost-admin/models/navigation-item';
|
2017-03-14 16:54:58 +03:00
|
|
|
import {isThemeValidationError} from 'ghost-admin/services/ajax';
|
2017-02-21 22:04:50 +03:00
|
|
|
import $ from 'jquery';
|
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}));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme.destroyRecord().catch((error) => {
|
|
|
|
this.get('notifications').showAPIError(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-01-11 22:55:52 +03:00
|
|
|
actions: {
|
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) => {
|
|
|
|
if (!isEmpty(theme.get('warnings'))) {
|
|
|
|
this.set('themeWarnings', theme.get('warnings'));
|
|
|
|
this.set('showThemeWarningsModal', true);
|
|
|
|
}
|
|
|
|
}).catch((error) => {
|
|
|
|
if (isThemeValidationError(error)) {
|
|
|
|
this.set('themeWarnings', error.errors[0].errorDetails);
|
|
|
|
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);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|