From 8bbacd9ec9c90d3bd8e3e61c78f2e0cbe5a099a2 Mon Sep 17 00:00:00 2001 From: cobbspur Date: Thu, 4 Jul 2013 19:42:49 +0100 Subject: [PATCH] closes #195 posts date on content page - adds dateFormat handlebars helper for client side with extra option to format in time since style - adds this extra dateFormat option to existing server side helper. - adds scss for draft and scheduled status - adds true/false values to post for draft and published to validate in handlebars - changes admin>content post collection query to order posts by updated_at values in router.js - adds minified moment.js and links to moment.js and helper.js for clientside --- core/admin/assets/js/helpers/index.js | 16 ++++++++++++++++ core/admin/assets/js/models/post.js | 4 ++++ core/admin/assets/js/router.js | 2 +- core/admin/assets/lib/moment.js | 6 ++++++ core/admin/assets/sass/modules/global.scss | 8 ++++++++ core/admin/assets/tpl/list-item.hbs | 7 ++++++- core/admin/assets/tpl/preview.hbs | 2 +- core/admin/views/default.hbs | 2 ++ core/frontend/helpers/index.js | 11 +++++++++-- 9 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 core/admin/assets/js/helpers/index.js create mode 100644 core/admin/assets/lib/moment.js diff --git a/core/admin/assets/js/helpers/index.js b/core/admin/assets/js/helpers/index.js new file mode 100644 index 0000000000..c472f1f819 --- /dev/null +++ b/core/admin/assets/js/helpers/index.js @@ -0,0 +1,16 @@ +/*globals Handlebars, moment +*/ +(function () { + "use strict"; + Handlebars.registerHelper('dateFormat', function (context, block) { + var f = block.hash.format || "MMM Do, YYYY", + timeago = block.hash.timeago, + date; + if (timeago) { + date = moment(context).fromNow(); + } else { + date = moment(context).format(f); + } + return date; + }); +}()); diff --git a/core/admin/assets/js/models/post.js b/core/admin/assets/js/models/post.js index 3895331585..3f58f18547 100644 --- a/core/admin/assets/js/models/post.js +++ b/core/admin/assets/js/models/post.js @@ -9,6 +9,10 @@ }, parse: function (resp) { + if (resp.status) { + resp.published = !!(resp.status === "published"); + resp.draft = !!(resp.status === "draft"); + } if (resp.tags) { // TODO: parse tags into it's own collection on the model (this.tags) return resp; diff --git a/core/admin/assets/js/router.js b/core/admin/assets/js/router.js index 5b5ff65c16..644bde56c6 100644 --- a/core/admin/assets/js/router.js +++ b/core/admin/assets/js/router.js @@ -16,7 +16,7 @@ blog: function () { var posts = new Ghost.Collections.Posts(); - posts.fetch({ data: { status: 'all' } }).then(function () { + posts.fetch({ data: { status: 'all', orderBy: ['updated_at', 'DESC'] } }).then(function () { Ghost.currentView = new Ghost.Views.Blog({ el: '#main', collection: posts }); }); }, diff --git a/core/admin/assets/lib/moment.js b/core/admin/assets/lib/moment.js new file mode 100644 index 0000000000..4e8497a9d7 --- /dev/null +++ b/core/admin/assets/lib/moment.js @@ -0,0 +1,6 @@ +// moment.js +// version : 2.0.0 +// author : Tim Wood +// license : MIT +// momentjs.com +(function(e){function O(e,t){return function(n){return j(e.call(this,n),t)}}function M(e){return function(t){return this.lang().ordinal(e.call(this,t))}}function _(){}function D(e){H(this,e)}function P(e){var t=this._data={},n=e.years||e.year||e.y||0,r=e.months||e.month||e.M||0,i=e.weeks||e.week||e.w||0,s=e.days||e.day||e.d||0,o=e.hours||e.hour||e.h||0,u=e.minutes||e.minute||e.m||0,a=e.seconds||e.second||e.s||0,f=e.milliseconds||e.millisecond||e.ms||0;this._milliseconds=f+a*1e3+u*6e4+o*36e5,this._days=s+i*7,this._months=r+n*12,t.milliseconds=f%1e3,a+=B(f/1e3),t.seconds=a%60,u+=B(a/60),t.minutes=u%60,o+=B(u/60),t.hours=o%24,s+=B(o/24),s+=i*7,t.days=s%30,r+=B(s/30),t.months=r%12,n+=B(r/12),t.years=n}function H(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e}function B(e){return e<0?Math.ceil(e):Math.floor(e)}function j(e,t){var n=e+"";while(n.length68?1900:2e3);break;case"YYYY":case"YYYYY":s[0]=~~t;break;case"a":case"A":n._isPm=(t+"").toLowerCase()==="pm";break;case"H":case"HH":case"h":case"hh":s[3]=~~t;break;case"m":case"mm":s[4]=~~t;break;case"s":case"ss":s[5]=~~t;break;case"S":case"SS":case"SSS":s[6]=~~(("0."+t)*1e3);break;case"X":n._d=new Date(parseFloat(t)*1e3);break;case"Z":case"ZZ":n._useUTC=!0,r=(t+"").match(x),r&&r[1]&&(n._tzh=~~r[1]),r&&r[2]&&(n._tzm=~~r[2]),r&&r[0]==="+"&&(n._tzh=-n._tzh,n._tzm=-n._tzm)}t==null&&(n._isValid=!1)}function J(e){var t,n,r=[];if(e._d)return;for(t=0;t<7;t++)e._a[t]=r[t]=e._a[t]==null?t===2?1:0:e._a[t];r[3]+=e._tzh||0,r[4]+=e._tzm||0,n=new Date(0),e._useUTC?(n.setUTCFullYear(r[0],r[1],r[2]),n.setUTCHours(r[3],r[4],r[5],r[6])):(n.setFullYear(r[0],r[1],r[2]),n.setHours(r[3],r[4],r[5],r[6])),e._d=n}function K(e){var t=e._f.match(a),n=e._i,r,i;e._a=[];for(r=0;r0,f[4]=n,Z.apply({},f)}function tt(e,n,r){var i=r-n,s=r-e.day();return s>i&&(s-=7),s11?n?"pm":"PM":n?"am":"AM"},_calendar:{sameDay:"[Today at] LT",nextDay:"[Tomorrow at] LT",nextWeek:"dddd [at] LT",lastDay:"[Yesterday at] LT",lastWeek:"[last] dddd [at] LT",sameElse:"L"},calendar:function(e,t){var n=this._calendar[e];return typeof n=="function"?n.apply(t):n},_relativeTime:{future:"in %s",past:"%s ago",s:"a few seconds",m:"a minute",mm:"%d minutes",h:"an hour",hh:"%d hours",d:"a day",dd:"%d days",M:"a month",MM:"%d months",y:"a year",yy:"%d years"},relativeTime:function(e,t,n,r){var i=this._relativeTime[n];return typeof i=="function"?i(e,t,n,r):i.replace(/%d/i,e)},pastFuture:function(e,t){var n=this._relativeTime[e>0?"future":"past"];return typeof n=="function"?n(t):n.replace(/%s/i,t)},ordinal:function(e){return this._ordinal.replace("%d",e)},_ordinal:"%d",preparse:function(e){return e},postformat:function(e){return e},week:function(e){return tt(e,this._week.dow,this._week.doy)},_week:{dow:0,doy:6}},t=function(e,t,n){return nt({_i:e,_f:t,_l:n,_isUTC:!1})},t.utc=function(e,t,n){return nt({_useUTC:!0,_isUTC:!0,_l:n,_i:e,_f:t})},t.unix=function(e){return t(e*1e3)},t.duration=function(e,n){var r=t.isDuration(e),i=typeof e=="number",s=r?e._data:i?{}:e,o;return i&&(n?s[n]=e:s.milliseconds=e),o=new P(s),r&&e.hasOwnProperty("_lang")&&(o._lang=e._lang),o},t.version=n,t.defaultFormat=E,t.lang=function(e,n){var r;if(!e)return t.fn._lang._abbr;n?R(e,n):s[e]||U(e),t.duration.fn._lang=t.fn._lang=U(e)},t.langData=function(e){return e&&e._lang&&e._lang._abbr&&(e=e._lang._abbr),U(e)},t.isMoment=function(e){return e instanceof D},t.isDuration=function(e){return e instanceof P},t.fn=D.prototype={clone:function(){return t(this)},valueOf:function(){return+this._d},unix:function(){return Math.floor(+this._d/1e3)},toString:function(){return this.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ")},toDate:function(){return this._d},toJSON:function(){return t.utc(this).format("YYYY-MM-DD[T]HH:mm:ss.SSS[Z]")},toArray:function(){var e=this;return[e.year(),e.month(),e.date(),e.hours(),e.minutes(),e.seconds(),e.milliseconds()]},isValid:function(){return this._isValid==null&&(this._a?this._isValid=!q(this._a,(this._isUTC?t.utc(this._a):t(this._a)).toArray()):this._isValid=!isNaN(this._d.getTime())),!!this._isValid},utc:function(){return this._isUTC=!0,this},local:function(){return this._isUTC=!1,this},format:function(e){var n=X(this,e||t.defaultFormat);return this.lang().postformat(n)},add:function(e,n){var r;return typeof e=="string"?r=t.duration(+n,e):r=t.duration(e,n),F(this,r,1),this},subtract:function(e,n){var r;return typeof e=="string"?r=t.duration(+n,e):r=t.duration(e,n),F(this,r,-1),this},diff:function(e,n,r){var i=this._isUTC?t(e).utc():t(e).local(),s=(this.zone()-i.zone())*6e4,o,u;return n&&(n=n.replace(/s$/,"")),n==="year"||n==="month"?(o=(this.daysInMonth()+i.daysInMonth())*432e5,u=(this.year()-i.year())*12+(this.month()-i.month()),u+=(this-t(this).startOf("month")-(i-t(i).startOf("month")))/o,n==="year"&&(u/=12)):(o=this-i-s,u=n==="second"?o/1e3:n==="minute"?o/6e4:n==="hour"?o/36e5:n==="day"?o/864e5:n==="week"?o/6048e5:o),r?u:B(u)},from:function(e,n){return t.duration(this.diff(e)).lang(this.lang()._abbr).humanize(!n)},fromNow:function(e){return this.from(t(),e)},calendar:function(){var e=this.diff(t().startOf("day"),"days",!0),n=e<-6?"sameElse":e<-1?"lastWeek":e<0?"lastDay":e<1?"sameDay":e<2?"nextDay":e<7?"nextWeek":"sameElse";return this.format(this.lang().calendar(n,this))},isLeapYear:function(){var e=this.year();return e%4===0&&e%100!==0||e%400===0},isDST:function(){return this.zone()+t(e).startOf(n)},isBefore:function(e,n){return n=typeof n!="undefined"?n:"millisecond",+this.clone().startOf(n)<+t(e).startOf(n)},isSame:function(e,n){return n=typeof n!="undefined"?n:"millisecond",+this.clone().startOf(n)===+t(e).startOf(n)},zone:function(){return this._isUTC?0:this._d.getTimezoneOffset()},daysInMonth:function(){return t.utc([this.year(),this.month()+1,0]).date()},dayOfYear:function(e){var n=r((t(this).startOf("day")-t(this).startOf("year"))/864e5)+1;return e==null?n:this.add("d",e-n)},isoWeek:function(e){var t=tt(this,1,4);return e==null?t:this.add("d",(e-t)*7)},week:function(e){var t=this.lang().week(this);return e==null?t:this.add("d",(e-t)*7)},lang:function(t){return t===e?this._lang:(this._lang=U(t),this)}};for(i=0;i

{{title}}

\ No newline at end of file diff --git a/core/admin/assets/tpl/preview.hbs b/core/admin/assets/tpl/preview.hbs index 53085eba32..2de7069ca0 100644 --- a/core/admin/assets/tpl/preview.hbs +++ b/core/admin/assets/tpl/preview.hbs @@ -3,7 +3,7 @@ {{! TODO: JavaScript toggle featured/unfeatured}} - Published + {{#if published}}Published{{else}}Written{{/if}} by Joe Bloggs
diff --git a/core/admin/views/default.hbs b/core/admin/views/default.hbs index 143744046a..6d8a1a3737 100644 --- a/core/admin/views/default.hbs +++ b/core/admin/views/default.hbs @@ -53,6 +53,8 @@ + + diff --git a/core/frontend/helpers/index.js b/core/frontend/helpers/index.js index fd9b729125..5213f0fe48 100644 --- a/core/frontend/helpers/index.js +++ b/core/frontend/helpers/index.js @@ -15,8 +15,15 @@ coreHelpers = function (ghost) { * @return {Object} A Moment time / date object */ ghost.registerThemeHelper('dateFormat', function (context, block) { - var f = block.hash.format || "MMM Do, YYYY"; - return moment(context).format(f); + var f = block.hash.format || "MMM Do, YYYY", + timeago = block.hash.timeago, + date; + if (timeago) { + date = moment(context).fromNow(); + } else { + date = moment(context).format(f); + } + return date; }); /**