From 090c58edb52f78aa027ab67b39d4422ee8c56022 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Sun, 21 Sep 2014 18:56:30 +0000 Subject: [PATCH] Validate and properly color letter count. fixes #4087 - Adds correct validations for meta_title/meta_description - Adds correct coloring of letter count --- ghost/admin/controllers/post-settings-menu.js | 23 +++++++++++-------- .../admin/helpers/gh-count-down-characters.js | 2 +- ghost/admin/templates/post-settings-menu.hbs | 10 ++++---- ghost/admin/validators/post.js | 17 +++++++++++--- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/ghost/admin/controllers/post-settings-menu.js b/ghost/admin/controllers/post-settings-menu.js index 2136a5cde1..7657d7a6b2 100644 --- a/ghost/admin/controllers/post-settings-menu.js +++ b/ghost/admin/controllers/post-settings-menu.js @@ -6,6 +6,7 @@ import boundOneWay from 'ghost/utils/bound-one-way'; var PostSettingsMenuController = Ember.ObjectController.extend({ //State for if the user is viewing a tab's pane. needs: 'application', + isViewingSubview: Ember.computed('controllers.application.showRightOutlet', function (key, value) { // Not viewing a subview if we can't even see the PSM if (!this.get('controllers.application.showRightOutlet')) { @@ -97,13 +98,19 @@ var PostSettingsMenuController = Ember.ObjectController.extend({ }); }, - metaTitleValue: boundOneWay('meta_title'), + metaTitleScratch: boundOneWay('meta_title'), + metaDescriptionScratch: boundOneWay('meta_description'), - metaDescriptionValue: boundOneWay('meta_description'), metaDescriptionPlaceholder: Ember.computed('scratch', function () { - var html = this.get('scratch'), + var el = $('.rendered-markdown'), + html = '', placeholder; + // Get rendered markdown + if (!_.isUndefined(el) && el.length > 0) { + html = el[0].innerHTML; + } + // Strip HTML placeholder = $('
', { html: html }).text(); // Replace new lines and trim @@ -114,14 +121,14 @@ var PostSettingsMenuController = Ember.ObjectController.extend({ return placeholder; }), - seoTitle: Ember.computed('titleScratch', 'metaTitleValue', function () { - var metaTitle = this.get('metaTitleValue') || ''; + seoTitle: Ember.computed('titleScratch', 'metaTitleScratch', function () { + var metaTitle = this.get('metaTitleScratch') || ''; return metaTitle.length > 0 ? metaTitle : this.get('titleScratch'); }), - seoDescription: Ember.computed('scratch', 'metaDescriptionValue', function () { - var metaDescription = this.get('metaDescriptionValue') || ''; + seoDescription: Ember.computed('metaDescriptionScratch', function () { + var metaDescription = this.get('metaDescriptionScratch') || ''; return metaDescription.length > 0 ? metaDescription : this.get('metaDescriptionPlaceholder'); }), @@ -319,7 +326,6 @@ var PostSettingsMenuController = Ember.ObjectController.extend({ this.get('model').save(this.get('saveOptions')).catch(function (errors) { self.showErrors(errors); - self.get('model').rollback(); }); }, @@ -336,7 +342,6 @@ var PostSettingsMenuController = Ember.ObjectController.extend({ this.get('model').save(this.get('saveOptions')).catch(function (errors) { self.showErrors(errors); - self.get('model').rollback(); }); }, diff --git a/ghost/admin/helpers/gh-count-down-characters.js b/ghost/admin/helpers/gh-count-down-characters.js index 0712177702..cc4d460c27 100644 --- a/ghost/admin/helpers/gh-count-down-characters.js +++ b/ghost/admin/helpers/gh-count-down-characters.js @@ -6,7 +6,7 @@ var countDownCharacters = Ember.Handlebars.makeBoundHelper(function (content, ma if (length > maxCharacters) { el.style.color = '#E25440'; } else { - el.style.color = '#9E9D95'; + el.style.color = '#9FBB58'; } el.innerHTML = length; diff --git a/ghost/admin/templates/post-settings-menu.hbs b/ghost/admin/templates/post-settings-menu.hbs index e680552d0b..3de264ff04 100644 --- a/ghost/admin/templates/post-settings-menu.hbs +++ b/ghost/admin/templates/post-settings-menu.hbs @@ -85,16 +85,17 @@
+
- {{gh-input class="post-setting-meta-title" value=metaTitleValue name="post-setting-meta-title" focus-out="setMetaTitle" placeholder=titleScratch stopEnterKeyDownPropagation="true"}} -

Recommended: 70 characters. You’ve used {{gh-count-down-characters metaTitleValue 70}}

+ {{gh-input class="post-setting-meta-title" value=metaTitleScratch name="post-setting-meta-title" focus-out="setMetaTitle" placeholder=titleScratch stopEnterKeyDownPropagation="true"}} +

Recommended: 70 characters. You’ve used {{gh-count-down-characters metaTitleScratch 70}}

- {{gh-textarea class="post-setting-meta-description" value=metaDescriptionValue name="post-setting-meta-description" focus-out="setMetaDescription" placeholder=metaDescriptionPlaceholder stopEnterKeyDownPropagation="true"}} -

Recommended: 156 characters. You’ve used {{gh-count-down-characters metaDescriptionValue 156}}

+ {{gh-textarea class="post-setting-meta-description" value=metaDescriptionScratch name="post-setting-meta-description" focus-out="setMetaDescription" placeholder=metaDescriptionPlaceholder stopEnterKeyDownPropagation="true"}} +

Recommended: 156 characters. You’ve used {{gh-count-down-characters metaDescriptionScratch 156}}

@@ -105,6 +106,7 @@
{{seoDescription}}
+
{{/gh-tab-pane}} diff --git a/ghost/admin/validators/post.js b/ghost/admin/validators/post.js index bd291c1eff..8e64bb7819 100644 --- a/ghost/admin/validators/post.js +++ b/ghost/admin/validators/post.js @@ -1,15 +1,26 @@ var PostValidator = Ember.Object.create({ check: function (model) { var validationErrors = [], + data = model.getProperties('title', 'meta_title', 'meta_description'); - title = model.get('title'); - - if (validator.empty(title)) { + if (validator.empty(data.title)) { validationErrors.push({ message: 'You must specify a title for the post.' }); } + if (!validator.isLength(data.meta_title, 0, 70)) { + validationErrors.push({ + message: 'Meta Title is too long.' + }); + } + + if (!validator.isLength(data.meta_description, 0, 156)) { + validationErrors.push({ + message: 'Meta Description is too long.' + }); + } + return validationErrors; } });