2015-01-14 17:46:29 +03:00
|
|
|
var NavigationController,
|
|
|
|
NavItem;
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2015-01-14 17:46:29 +03:00
|
|
|
NavItem = Ember.Object.extend({
|
|
|
|
label: '',
|
|
|
|
url: '',
|
2015-02-03 19:29:01 +03:00
|
|
|
order: '',
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
isComplete: Ember.computed('label', 'url', function () {
|
|
|
|
return !(Ember.isBlank(this.get('label')) || Ember.isBlank(this.get('url')));
|
2015-01-14 17:46:29 +03:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
NavigationController = Ember.Controller.extend({
|
2015-01-18 03:16:54 +03:00
|
|
|
blogUrl: Ember.computed('config.blogUrl', function () {
|
|
|
|
var url = this.get('config.blogUrl');
|
|
|
|
|
|
|
|
return url.slice(-1) !== '/' ? url + '/' : url;
|
|
|
|
}),
|
|
|
|
|
|
|
|
navigationItems: Ember.computed('model.navigation', function () {
|
2015-01-14 17:46:29 +03:00
|
|
|
var navItems,
|
2015-02-18 16:10:43 +03:00
|
|
|
lastItem,
|
|
|
|
order = 0;
|
2015-01-14 17:46:29 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
navItems = JSON.parse(this.get('model.navigation') || [{}]);
|
|
|
|
} catch (e) {
|
|
|
|
navItems = [{}];
|
|
|
|
}
|
|
|
|
|
|
|
|
navItems = navItems.map(function (item) {
|
2015-02-18 16:10:43 +03:00
|
|
|
item.order = order;
|
|
|
|
order = order + 1;
|
2015-01-14 17:46:29 +03:00
|
|
|
return NavItem.create(item);
|
|
|
|
});
|
|
|
|
|
2015-02-03 19:29:01 +03:00
|
|
|
navItems.sortBy('order');
|
|
|
|
|
2015-01-14 17:46:29 +03:00
|
|
|
lastItem = navItems.get('lastObject');
|
2015-01-18 03:16:54 +03:00
|
|
|
if (!lastItem || lastItem.get('isComplete')) {
|
2015-01-14 17:46:29 +03:00
|
|
|
navItems.addObject(NavItem.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
return navItems;
|
|
|
|
}),
|
|
|
|
|
|
|
|
navigationItemsObserver: Ember.observer('navigationItems.[]', function () {
|
|
|
|
var navItems = this.get('navigationItems');
|
|
|
|
|
|
|
|
navItems.forEach(function (item, index, items) {
|
|
|
|
if (index === (items.length - 1)) {
|
|
|
|
item.set('last', true);
|
|
|
|
} else {
|
|
|
|
item.set('last', false);
|
|
|
|
}
|
|
|
|
});
|
2015-01-11 22:55:52 +03:00
|
|
|
}),
|
|
|
|
|
2015-02-03 19:29:01 +03:00
|
|
|
updateOrder: function (indexes) {
|
|
|
|
var navItems = this.get('navigationItems'),
|
|
|
|
order = 0;
|
|
|
|
|
|
|
|
indexes.forEach(function (index) {
|
|
|
|
navItems[index].set('order', order);
|
|
|
|
order = order + 1; // Increment order order by one
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-01-11 22:55:52 +03:00
|
|
|
actions: {
|
|
|
|
addItem: function () {
|
2015-01-14 17:46:29 +03:00
|
|
|
var navItems = this.get('navigationItems'),
|
|
|
|
lastItem = navItems.get('lastObject');
|
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
if (lastItem && lastItem.get('isComplete')) {
|
2015-02-03 19:29:01 +03:00
|
|
|
lastItem.set('order', (navItems.length - 1)); // -1 because order is 0-index, length is 1-index
|
|
|
|
navItems.addObject(NavItem.create()); // Adds new blank navItem
|
2015-01-14 17:46:29 +03:00
|
|
|
}
|
2015-01-11 22:55:52 +03:00
|
|
|
},
|
|
|
|
|
2015-01-14 17:46:29 +03:00
|
|
|
deleteItem: function (item) {
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('navigationItems').removeObject(item);
|
2015-02-03 19:29:01 +03:00
|
|
|
|
|
|
|
var navItems = this.get('navigationItems'),
|
|
|
|
order = 0;
|
|
|
|
|
|
|
|
navItems.forEach(function (item) {
|
2015-02-18 16:10:43 +03:00
|
|
|
if (!item.last) { // Make sure we never apply an `order` attr to the last item
|
|
|
|
item.set('order', order);
|
|
|
|
order = order + 1; // Increment order order by one
|
|
|
|
}
|
2015-02-03 19:29:01 +03:00
|
|
|
});
|
2015-01-11 22:55:52 +03:00
|
|
|
},
|
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
updateUrl: function (url, navItem) {
|
|
|
|
if (!navItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ember.isBlank(url)) {
|
|
|
|
navItem.set('url', this.get('blogUrl'));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
navItem.set('url', url);
|
|
|
|
},
|
|
|
|
|
2015-01-11 22:55:52 +03:00
|
|
|
save: function () {
|
2015-01-14 17:46:29 +03:00
|
|
|
var self = this,
|
|
|
|
navSetting,
|
|
|
|
blogUrl = this.get('config').blogUrl,
|
|
|
|
blogUrlRegex = new RegExp('^' + blogUrl + '(.*)', 'i'),
|
|
|
|
match;
|
|
|
|
|
|
|
|
navSetting = this.get('navigationItems').map(function (item) {
|
|
|
|
var label,
|
2015-02-03 19:29:01 +03:00
|
|
|
url,
|
|
|
|
order;
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
if (!item || !item.get('isComplete')) {
|
2015-01-14 17:46:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
label = item.get('label').trim();
|
|
|
|
url = item.get('url').trim();
|
2015-02-03 19:29:01 +03:00
|
|
|
order = item.get('order');
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-02-16 22:06:35 +03:00
|
|
|
// is this an internal URL?
|
2015-01-14 17:46:29 +03:00
|
|
|
match = url.match(blogUrlRegex);
|
|
|
|
|
|
|
|
if (match) {
|
2015-02-16 22:06:35 +03:00
|
|
|
url = match[1];
|
|
|
|
|
|
|
|
// if the last char is not a slash, then add one,
|
|
|
|
// this also handles the empty case for the homepage
|
|
|
|
if (url[url.length - 1] !== '/') {
|
|
|
|
url += '/';
|
2015-01-14 17:46:29 +03:00
|
|
|
}
|
|
|
|
} else if (!validator.isURL(url) && url !== '' && url[0] !== '/') {
|
|
|
|
url = '/' + url;
|
|
|
|
}
|
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
// if navItem label is empty and URL is still the default, don't save
|
|
|
|
if (!label && url === '/') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-03 19:29:01 +03:00
|
|
|
return {label: label, url: url, order: order};
|
2015-01-14 17:46:29 +03:00
|
|
|
}).compact();
|
|
|
|
|
2015-02-03 19:29:01 +03:00
|
|
|
// Sort JSON so nav items are stored in the correct order order
|
|
|
|
navSetting.sort(function (a, b) {
|
|
|
|
return a.order - b.order;
|
|
|
|
});
|
|
|
|
|
2015-01-14 17:46:29 +03:00
|
|
|
this.set('model.navigation', JSON.stringify(navSetting));
|
|
|
|
|
|
|
|
// trigger change event because even if the final JSON is unchanged
|
|
|
|
// we need to have navigationItems recomputed.
|
|
|
|
this.get('model').notifyPropertyChange('navigation');
|
|
|
|
|
|
|
|
this.notifications.closePassive();
|
|
|
|
|
|
|
|
this.get('model').save().then(function () {
|
|
|
|
self.notifications.showSuccess('Navigation items saved.');
|
|
|
|
}).catch(function (err) {
|
|
|
|
self.notifications.showErrors(err);
|
|
|
|
});
|
2015-01-11 22:55:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default NavigationController;
|