From 61c75923638627151c076ef423e2171d69e414bc Mon Sep 17 00:00:00 2001 From: Athou Date: Wed, 1 May 2013 15:51:31 +0200 Subject: [PATCH] format date differently if date is today --- .../commafeed/frontend/pages/BasePage.java | 1 + src/main/webapp/WEB-INF/wro.xml | 1 + src/main/webapp/js/controllers.js | 12 +- src/main/webapp/js/filters.js | 15 ++ src/main/webapp/js/main.js | 153 +++++++++--------- src/main/webapp/templates/feeds.view.html | 2 +- .../vendor/momentjs/moment-2.0.0.min.js | 6 + 7 files changed, 107 insertions(+), 83 deletions(-) create mode 100644 src/main/webapp/js/filters.js create mode 100644 src/main/webapp/vendor/momentjs/moment-2.0.0.min.js diff --git a/src/main/java/com/commafeed/frontend/pages/BasePage.java b/src/main/java/com/commafeed/frontend/pages/BasePage.java index 2dc9fdd8..33475de7 100644 --- a/src/main/java/com/commafeed/frontend/pages/BasePage.java +++ b/src/main/java/com/commafeed/frontend/pages/BasePage.java @@ -92,6 +92,7 @@ public abstract class BasePage extends WebPage { response.render(JavaScriptHeaderItem.forUrl("js/main.js")); response.render(JavaScriptHeaderItem.forUrl("js/controllers.js")); response.render(JavaScriptHeaderItem.forUrl("js/directives.js")); + response.render(JavaScriptHeaderItem.forUrl("js/filters.js")); response.render(JavaScriptHeaderItem.forUrl("js/services.js")); } diff --git a/src/main/webapp/WEB-INF/wro.xml b/src/main/webapp/WEB-INF/wro.xml index 41b31c66..a77082bf 100644 --- a/src/main/webapp/WEB-INF/wro.xml +++ b/src/main/webapp/WEB-INF/wro.xml @@ -13,6 +13,7 @@ /vendor/nginfinitescroll/*.js /vendor/ngupload/*.js /vendor/spinjs/*.js + /vendor/momentjs/*.js /vendor/bootstrap/*.css /vendor/angularui/*.css diff --git a/src/main/webapp/js/controllers.js b/src/main/webapp/js/controllers.js index c319a1f9..2740060e 100644 --- a/src/main/webapp/js/controllers.js +++ b/src/main/webapp/js/controllers.js @@ -258,17 +258,17 @@ module.controller('CategoryDetailsCtrl', ['$scope', '$state', '$stateParams', 'F return parseInt($stateParams._id, 10) != $stateParams._id; }; - $scope.filterCurrent = function(elem) { - if (!$scope.category) - return true; - return elem.id != $scope.category.id; - }; + $scope.filterCurrent = function(elem) { + if (!$scope.category) + return true; + return elem.id != $scope.category.id; + }; CategoryService.get(function() { if ($scope.isMeta()) { $scope.category = { id : $stateParams._id, - name : $stateParams._id, + name : $stateParams._id }; return; } diff --git a/src/main/webapp/js/filters.js b/src/main/webapp/js/filters.js new file mode 100644 index 00000000..9abfe2d4 --- /dev/null +++ b/src/main/webapp/js/filters.js @@ -0,0 +1,15 @@ +var module = angular.module('commafeed.filters', []); + +module.filter('entryDate', function() { + return function(timestamp) { + var d = moment(timestamp); + var now = moment(); + var formatted; + if (d.date() === now.date() && Math.abs(d.diff(now)) < 86400000) { + formatted = d.fromNow(); + } else { + formatted = d.format('YYYY-MM-DD HH:mm'); + } + return formatted; + }; +}); \ No newline at end of file diff --git a/src/main/webapp/js/main.js b/src/main/webapp/js/main.js index db1e8d13..05827708 100644 --- a/src/main/webapp/js/main.js +++ b/src/main/webapp/js/main.js @@ -1,84 +1,85 @@ var app = angular.module('commafeed', [ 'ui', 'ui.bootstrap', 'ui.state', 'commafeed.directives', 'commafeed.controllers', 'commafeed.services', - 'ngSanitize', 'ngUpload', 'infinite-scroll', 'ngGrid' ]); + 'commafeed.filters', 'ngSanitize', 'ngUpload', 'infinite-scroll', + 'ngGrid' ]); -app.config(['$routeProvider', '$stateProvider', '$urlRouterProvider', -function($routeProvider, $stateProvider, $urlRouterProvider) { - var trackCtrl = ['AnalyticsService', function(AnalyticsService) { - AnalyticsService.track(); - }]; - - $stateProvider.state('feeds', { - 'abstract' : true, - url : '/feeds', - templateUrl : 'templates/feeds.html' - }); - $stateProvider.state('feeds.view', { - url : '/view/:_type/:_id', - templateUrl : 'templates/feeds.view.html', - controller : 'FeedListCtrl' - }); - $stateProvider.state('feeds.search', { - url : '/search/:_keywords', - templateUrl : 'templates/feeds.view.html', - controller : 'FeedListCtrl' - }); - $stateProvider.state('feeds.feed_details', { - url : '/details/feed/:_id', - templateUrl : 'templates/feeds.feed_details.html', - controller : 'FeedDetailsCtrl' - }); - $stateProvider.state('feeds.category_details', { - url : '/details/category/:_id', - templateUrl : 'templates/feeds.category_details.html', - controller : 'CategoryDetailsCtrl' - }); - $stateProvider.state('feeds.help', { - url : '/help', - templateUrl : 'templates/feeds.help.html', - controller: trackCtrl - }); +app.config([ '$routeProvider', '$stateProvider', '$urlRouterProvider', + function($routeProvider, $stateProvider, $urlRouterProvider) { + var trackCtrl = [ 'AnalyticsService', function(AnalyticsService) { + AnalyticsService.track(); + } ]; - $stateProvider.state('admin', { - 'abstract' : true, - url : '/admin', - templateUrl : 'templates/admin.html' - }); - $stateProvider.state('admin.userlist', { - url : '/user/list', - templateUrl : 'templates/admin.userlist.html', - controller : 'ManageUsersCtrl' - }); - $stateProvider.state('admin.useradd', { - url : '/user/add', - templateUrl : 'templates/admin.useradd.html', - controller : 'ManageUserCtrl' - }); - $stateProvider.state('admin.useredit', { - url : '/user/edit/:_id', - templateUrl : 'templates/admin.useredit.html', - controller : 'ManageUserCtrl' - }); - $stateProvider.state('admin.settings', { - url : '/settings', - templateUrl : 'templates/admin.settings.html', - controller : 'ManageSettingsCtrl' - }); + $stateProvider.state('feeds', { + 'abstract' : true, + url : '/feeds', + templateUrl : 'templates/feeds.html' + }); + $stateProvider.state('feeds.view', { + url : '/view/:_type/:_id', + templateUrl : 'templates/feeds.view.html', + controller : 'FeedListCtrl' + }); + $stateProvider.state('feeds.search', { + url : '/search/:_keywords', + templateUrl : 'templates/feeds.view.html', + controller : 'FeedListCtrl' + }); + $stateProvider.state('feeds.feed_details', { + url : '/details/feed/:_id', + templateUrl : 'templates/feeds.feed_details.html', + controller : 'FeedDetailsCtrl' + }); + $stateProvider.state('feeds.category_details', { + url : '/details/category/:_id', + templateUrl : 'templates/feeds.category_details.html', + controller : 'CategoryDetailsCtrl' + }); + $stateProvider.state('feeds.help', { + url : '/help', + templateUrl : 'templates/feeds.help.html', + controller : trackCtrl + }); - $stateProvider.state('settings', { - url : '/settings', - templateUrl : 'templates/settings.html', - controller : 'SettingsCtrl' - }); + $stateProvider.state('admin', { + 'abstract' : true, + url : '/admin', + templateUrl : 'templates/admin.html' + }); + $stateProvider.state('admin.userlist', { + url : '/user/list', + templateUrl : 'templates/admin.userlist.html', + controller : 'ManageUsersCtrl' + }); + $stateProvider.state('admin.useradd', { + url : '/user/add', + templateUrl : 'templates/admin.useradd.html', + controller : 'ManageUserCtrl' + }); + $stateProvider.state('admin.useredit', { + url : '/user/edit/:_id', + templateUrl : 'templates/admin.useredit.html', + controller : 'ManageUserCtrl' + }); + $stateProvider.state('admin.settings', { + url : '/settings', + templateUrl : 'templates/admin.settings.html', + controller : 'ManageSettingsCtrl' + }); - $stateProvider.state('profile', { - url : '/profile', - templateUrl : 'templates/profile.html', - controller : 'ProfileCtrl' - }); + $stateProvider.state('settings', { + url : '/settings', + templateUrl : 'templates/settings.html', + controller : 'SettingsCtrl' + }); - $urlRouterProvider.when('/', '/feeds/view/category/all'); - $urlRouterProvider.when('/admin', '/admin/settings'); - $urlRouterProvider.otherwise('/'); + $stateProvider.state('profile', { + url : '/profile', + templateUrl : 'templates/profile.html', + controller : 'ProfileCtrl' + }); -}]); \ No newline at end of file + $urlRouterProvider.when('/', '/feeds/view/category/all'); + $urlRouterProvider.when('/admin', '/admin/settings'); + $urlRouterProvider.otherwise('/'); + + } ]); \ No newline at end of file diff --git a/src/main/webapp/templates/feeds.view.html b/src/main/webapp/templates/feeds.view.html index f7d178bf..c145b495 100644 --- a/src/main/webapp/templates/feeds.view.html +++ b/src/main/webapp/templates/feeds.view.html @@ -16,7 +16,7 @@ {{entry.feedName}} - + diff --git a/src/main/webapp/vendor/momentjs/moment-2.0.0.min.js b/src/main/webapp/vendor/momentjs/moment-2.0.0.min.js new file mode 100644 index 00000000..4e8497a9 --- /dev/null +++ b/src/main/webapp/vendor/momentjs/moment-2.0.0.min.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