2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2016-05-24 15:06:59 +03:00
|
|
|
import SettingsSaveMixin from 'ghost-admin/mixins/settings-save';
|
|
|
|
import NavigationItem from 'ghost-admin/models/navigation-item';
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
Controller,
|
|
|
|
RSVP,
|
|
|
|
computed,
|
2016-04-26 12:45:59 +03:00
|
|
|
inject: {service}
|
2016-01-19 16:03:27 +03:00
|
|
|
} = Ember;
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
export default Controller.extend(SettingsSaveMixin, {
|
2016-01-19 16:03:27 +03:00
|
|
|
config: service(),
|
|
|
|
notifications: service(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2016-02-09 20:16:18 +03:00
|
|
|
newNavItem: null,
|
|
|
|
|
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
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
save() {
|
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')) {
|
|
|
|
validationPromises.pushObject(this.send('addItem'));
|
|
|
|
}
|
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
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return RSVP.all(validationPromises).then(() => {
|
|
|
|
return this.get('model').save().catch((err) => {
|
2015-09-16 20:02:06 +03:00
|
|
|
notifications.showErrors(err);
|
|
|
|
});
|
2015-10-28 14:36:45 +03:00
|
|
|
}).catch(() => {
|
2015-09-16 20:02:06 +03:00
|
|
|
// TODO: noop - needed to satisfy spinner button
|
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
|
|
|
},
|
|
|
|
|
2015-01-11 22:55:52 +03:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
addItem() {
|
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
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
deleteItem(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
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
2016-04-26 12:45:59 +03:00
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
2015-01-11 22:55:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|