Ghost/ghost/admin/app/validators/nav-item.js
Kevin Ansfield d8728aad57 Fix nav regressions in admin client
issue #5841

- fix relative link checks in navlink url input component
- fix navlink url input component sending absolute URLs instead of relative URLs to action handler
- remove URL manipulation in navigation settings controller (url input handles URL manipulation, validator flags anything that's still incorrect)
- capture cmd-s in url input to ensure changes are actioned before save
- automatically add mailto: to e-mail addresses
- add gh-validation-state-container component so .error/.success validation classes can be applied to any container element
- add validation-state mixin that can be mixed in to any other component to give it access to validation status (used in gh-navitem component to keep alignment when inline error message elements are added)
- validate and display inline errors on save
- improve ember test coverage for navigation settings related controller and components
2015-09-23 17:05:41 +01:00

53 lines
1.6 KiB
JavaScript

import BaseValidator from './base';
export default BaseValidator.create({
properties: ['label', 'url'],
label: function (model) {
var label = model.get('label'),
hasValidated = model.get('hasValidated');
if (this.canBeIgnored(model)) { return; }
if (validator.empty(label)) {
model.get('errors').add('label', 'You must specify a label');
this.invalidate();
}
hasValidated.addObject('label');
},
url: function (model) {
var url = model.get('url'),
hasValidated = model.get('hasValidated'),
validatorOptions = {require_protocol: true},
urlRegex = new RegExp(/^(\/|#|[a-zA-Z0-9\-]+:)/);
if (this.canBeIgnored(model)) { return; }
if (validator.empty(url)) {
model.get('errors').add('url', 'You must specify a URL or relative path');
this.invalidate();
} else if (url.match(/\s/) || (!validator.isURL(url, validatorOptions) && !url.match(urlRegex))) {
model.get('errors').add('url', 'You must specify a valid URL or relative path');
this.invalidate();
}
hasValidated.addObject('url');
},
canBeIgnored: function (model) {
var label = model.get('label'),
url = model.get('url'),
isLast = model.get('last');
// if nav item is last and completely blank, mark it valid and skip
if (isLast && (validator.empty(url) || url === '/') && validator.empty(label)) {
model.get('errors').clear();
return true;
}
return false;
}
});