2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-09-16 20:02:06 +03:00
|
|
|
import DS from 'ember-data';
|
2015-08-10 18:43:49 +03:00
|
|
|
import SettingsSaveMixin from 'ghost/mixins/settings-save';
|
2015-09-16 20:02:06 +03:00
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
Controller,
|
|
|
|
RSVP,
|
|
|
|
computed,
|
|
|
|
inject: {service},
|
|
|
|
isBlank,
|
|
|
|
observer
|
|
|
|
} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Errors} = DS;
|
|
|
|
const emberA = Ember.A;
|
|
|
|
|
2015-09-16 20:02:06 +03:00
|
|
|
export const NavItem = Ember.Object.extend(ValidationEngine, {
|
2015-01-14 17:46:29 +03:00
|
|
|
label: '',
|
|
|
|
url: '',
|
2015-02-23 22:10:20 +03:00
|
|
|
last: false,
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-09-16 20:02:06 +03:00
|
|
|
validationType: 'navItem',
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
isComplete: computed('label', 'url', function () {
|
|
|
|
return !(isBlank(this.get('label').trim()) || isBlank(this.get('url')));
|
2015-09-16 20:02:06 +03:00
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
init() {
|
2015-09-16 20:02:06 +03:00
|
|
|
this._super(...arguments);
|
2015-10-28 14:36:45 +03:00
|
|
|
this.set('errors', Errors.create());
|
|
|
|
this.set('hasValidated', emberA());
|
2015-09-16 20:02:06 +03:00
|
|
|
}
|
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
|
|
|
|
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
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
navigationItems: computed('model.navigation', function () {
|
|
|
|
let lastItem,
|
|
|
|
navItems;
|
2015-02-25 20:20:42 +03:00
|
|
|
|
2015-01-14 17:46:29 +03:00
|
|
|
try {
|
|
|
|
navItems = JSON.parse(this.get('model.navigation') || [{}]);
|
|
|
|
} catch (e) {
|
|
|
|
navItems = [{}];
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
navItems = navItems.map((item) => {
|
2015-01-14 17:46:29 +03:00
|
|
|
return NavItem.create(item);
|
|
|
|
});
|
|
|
|
|
|
|
|
lastItem = navItems.get('lastObject');
|
2015-01-18 03:16:54 +03:00
|
|
|
if (!lastItem || lastItem.get('isComplete')) {
|
2015-02-25 20:20:42 +03:00
|
|
|
navItems.addObject(NavItem.create({last: true}));
|
2015-01-14 17:46:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return navItems;
|
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
updateLastNavItem: observer('navigationItems.[]', function () {
|
|
|
|
let navItems = this.get('navigationItems');
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
navItems.forEach((item, index, items) => {
|
2015-01-14 17:46:29 +03:00
|
|
|
if (index === (items.length - 1)) {
|
|
|
|
item.set('last', true);
|
|
|
|
} else {
|
|
|
|
item.set('last', false);
|
|
|
|
}
|
|
|
|
});
|
2015-01-11 22:55:52 +03:00
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
save() {
|
|
|
|
let navItems = this.get('navigationItems');
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
let navSetting,
|
|
|
|
validationPromises;
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
validationPromises = navItems.map((item) => {
|
2015-09-16 20:02:06 +03:00
|
|
|
return item.validate();
|
|
|
|
});
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return RSVP.all(validationPromises).then(() => {
|
|
|
|
navSetting = navItems.map((item) => {
|
|
|
|
let label = item.get('label').trim();
|
|
|
|
let url = item.get('url').trim();
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2015-09-16 20:02:06 +03:00
|
|
|
if (item.get('last') && !item.get('isComplete')) {
|
|
|
|
return null;
|
2015-08-10 18:43:49 +03:00
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return {label, url};
|
2015-09-16 20:02:06 +03:00
|
|
|
}).compact();
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
this.set('model.navigation', JSON.stringify(navSetting));
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2015-09-16 20:02:06 +03:00
|
|
|
// trigger change event because even if the final JSON is unchanged
|
|
|
|
// we need to have navigationItems recomputed.
|
2015-10-28 14:36:45 +03:00
|
|
|
this.get('model').notifyPropertyChange('navigation');
|
2015-08-10 18:43:49 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-01-11 22:55:52 +03:00
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
addItem() {
|
|
|
|
let navItems = this.get('navigationItems');
|
|
|
|
let lastItem = navItems.get('lastObject');
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
if (lastItem && lastItem.get('isComplete')) {
|
2015-10-28 14:36:45 +03:00
|
|
|
// Add new blank navItem
|
|
|
|
navItems.addObject(NavItem.create({last: true}));
|
2015-01-14 17:46:29 +03:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
let navItems = this.get('navigationItems');
|
2015-02-03 19:29:01 +03:00
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
navItems.removeObject(item);
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
moveItem(index, newIndex) {
|
|
|
|
let navItems = this.get('navigationItems');
|
|
|
|
let item = navItems.objectAt(index);
|
2015-02-23 21:48:25 +03:00
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
navItems.removeAt(index);
|
|
|
|
navItems.insertAt(newIndex, item);
|
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);
|
2015-01-11 22:55:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|