From 3e1b37e194e12e17980506b0709e10e7eb7c510e Mon Sep 17 00:00:00 2001 From: William Dibbern Date: Wed, 16 Oct 2013 11:08:58 -0500 Subject: [PATCH] Simplify publish date checks Fixes #814 - Changed to compare instances of `moment` using it's built in comparison function instead of string compare with the date from the database. - Ensure compared date is parsed with the correct formats that we were supporting before #1107. - Moved date formats to variables to reduce chance for error in future. --- core/client/views/post-settings.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/core/client/views/post-settings.js b/core/client/views/post-settings.js index 696e0c1b98..d17a7dcf45 100644 --- a/core/client/views/post-settings.js +++ b/core/client/views/post-settings.js @@ -83,25 +83,27 @@ editDate: function (e) { e.preventDefault(); var self = this, - momentPubDate, + parseDateFormats = ['DD MMM YY', 'DD MMM YYYY', 'DD/MM/YY', 'DD/MM/YYYY', 'DD-MM-YY', 'DD-MM-YYYY'], + displayDateFormat = 'DD MMM YY', errMessage = '', pubDate = self.model.get('published_at'), + pubDateMoment = moment(pubDate, parseDateFormats), pubDateEl = e.currentTarget, - newPubDate = pubDateEl.value; + newPubDate = pubDateEl.value, + newPubDateMoment = moment(newPubDate, parseDateFormats); // Ensure the published date has changed - if (newPubDate.length === 0 || moment(pubDate).format("DD MMM YY") === newPubDate) { - pubDateEl.value = pubDate === undefined ? 'Not Published' : moment(pubDate).format("DD MMM YY"); + if (newPubDate.length === 0 || pubDateMoment.isSame(newPubDateMoment)) { + pubDateEl.value = pubDate === undefined ? 'Not Published' : pubDateMoment.format(displayDateFormat); return; } // Validate new Published date - momentPubDate = moment(newPubDate, ["DD MMM YY", "DD MMM YYYY", "DD/MM/YY", "DD/MM/YYYY", "DD-MM-YY", "DD-MM-YYYY"]); - if (!momentPubDate.isValid()) { + if (!newPubDateMoment.isValid()) { errMessage = 'Published Date must be a valid date with format: DD MMM YY (e.g. 6 Dec 14)'; } - if (momentPubDate.diff(new Date(), 'h') > 0) { + if (newPubDateMoment.diff(new Date(), 'h') > 0) { errMessage = 'Published Date cannot currently be in the future.'; } @@ -111,20 +113,20 @@ message: errMessage, status: 'passive' }); - pubDateEl.value = moment(pubDate).format("DD MMM YY"); + pubDateEl.value = pubDateMoment.format(displayDateFormat); return; } // Save new 'Published' date this.model.save({ // Temp Fix. Set hour to 12 instead of 00 to avoid some TZ issues. - published_at: momentPubDate.hour(12).toDate() + published_at: newPubDateMoment.hour(12).toDate() }, { success : function (model, response, options) { - pubDateEl.value = moment(model.get('published_at')).format("DD MMM YY"); + pubDateEl.value = moment(model.get('published_at'), parseDateFormats).format(displayDateFormat); Ghost.notifications.addItem({ type: 'success', - message: "Publish date successfully changed to " + pubDateEl.value + '.', + message: 'Publish date successfully changed to ' + pubDateEl.value + '.', status: 'passive' }); },