2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
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-23 22:10:20 +03:00
|
|
|
last: false,
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
isComplete: Ember.computed('label', 'url', function () {
|
2015-02-23 22:10:20 +03:00
|
|
|
return !(Ember.isBlank(this.get('label').trim()) || 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;
|
|
|
|
}),
|
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
navigationItems: Ember.computed('model.navigation', function () {
|
|
|
|
var navItems,
|
2015-02-23 21:48:25 +03:00
|
|
|
lastItem;
|
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 = [{}];
|
|
|
|
}
|
|
|
|
|
|
|
|
navItems = navItems.map(function (item) {
|
|
|
|
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-02-23 21:48:25 +03:00
|
|
|
updateLastNavItem: Ember.observer('navigationItems.[]', function () {
|
2015-01-14 17:46:29 +03:00
|
|
|
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
|
|
|
}),
|
|
|
|
|
|
|
|
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-25 20:20:42 +03:00
|
|
|
navItems.addObject(NavItem.create({last: true})); // 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;
|
|
|
|
}
|
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
var navItems = this.get('navigationItems');
|
2015-02-03 19:29:01 +03:00
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
navItems.removeObject(item);
|
|
|
|
},
|
|
|
|
|
|
|
|
moveItem: function (index, newIndex) {
|
|
|
|
var navItems = this.get('navigationItems'),
|
|
|
|
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-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'),
|
2015-02-23 22:10:20 +03:00
|
|
|
navItems = this.get('navigationItems'),
|
2015-03-01 15:33:00 +03:00
|
|
|
message = 'One of your navigation items has an empty label. ' +
|
|
|
|
'<br /> Please enter a new label or delete the item before saving.',
|
2015-01-14 17:46:29 +03:00
|
|
|
match;
|
2015-02-25 20:20:42 +03:00
|
|
|
|
2015-02-23 22:10:20 +03:00
|
|
|
// Don't save if there's a blank label.
|
|
|
|
if (navItems.find(function (item) { return !item.get('isComplete') && !item.get('last');})) {
|
2015-03-01 15:33:00 +03:00
|
|
|
self.notifications.showErrors([message.htmlSafe()]);
|
2015-02-23 22:10:20 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-02-25 20:20:42 +03:00
|
|
|
|
2015-02-23 22:10:20 +03:00
|
|
|
navSetting = navItems.map(function (item) {
|
2015-01-14 17:46:29 +03:00
|
|
|
var label,
|
2015-02-25 20:20:42 +03:00
|
|
|
url;
|
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-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,
|
2015-03-08 00:09:50 +03:00
|
|
|
// as long as there is no # or . in the URL (anchor or file extension)
|
2015-02-16 22:06:35 +03:00
|
|
|
// this also handles the empty case for the homepage
|
2015-03-08 00:09:50 +03:00
|
|
|
if (url[url.length - 1] !== '/' && url.indexOf('#') === -1 && url.indexOf('.') === -1) {
|
2015-02-16 22:06:35 +03:00
|
|
|
url += '/';
|
2015-01-14 17:46:29 +03:00
|
|
|
}
|
2015-03-08 21:27:00 +03:00
|
|
|
} else if (!validator.isURL(url) && url !== '' && url[0] !== '/' && url.indexOf('mailto:') !== 0) {
|
2015-01-14 17:46:29 +03:00
|
|
|
url = '/' + url;
|
|
|
|
}
|
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
return {label: label, url: url};
|
2015-01-14 17:46:29 +03:00
|
|
|
}).compact();
|
2015-02-03 19:29:01 +03:00
|
|
|
|
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;
|