Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #78 from ctesene/time-in-title
Browse files Browse the repository at this point in the history
Show formatted time as element title
  • Loading branch information
urish committed Sep 10, 2014
2 parents 62b9f60 + 8539af5 commit 3e82244
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
18 changes: 17 additions & 1 deletion angular-moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@
* If set, time ago will be calculated relative to the given value.
* If null, local time will be used. Defaults to null.
*/
serverTime: null
serverTime: null,

/**
* @ngdoc property
* @name angularMoment.config.amTimeAgoConfig#format
* @propertyOf angularMoment.config:amTimeAgoConfig
* @returns {string} The format of the date to be displayed
*
* @description
* Specify the format of the date when displayed. Null by default.
*/
format: null
})

/**
Expand Down Expand Up @@ -147,6 +158,11 @@

function updateTime(momentInstance) {
element.text(momentInstance.from(getNow(), withoutSuffix));

if (!element.attr('title')) {
element.attr('title', momentInstance.local().format(amTimeAgoConfig.format));
}

if (!isBindOnce) {

var howOld = Math.abs(getNow().diff(momentInstance, 'minute'));
Expand Down
29 changes: 29 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,35 @@ describe('module angularMoment', function () {
expect(element.attr('datetime')).toBe('2012-09-20T15:20:12.000Z');
});

describe('setting the element title', function() {
it('should not change the title of the element if the element already has a title', function () {
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="testDate" title="test"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.attr('title')).toBe('test');
});

it('should set the title of the element to the date with the default config', function () {
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
var testDateWithDefaultFormatting = moment($rootScope.testDate).format();
expect(element.attr('title')).toBe(testDateWithDefaultFormatting);
});

it('should set the title of the element to the formatted date as per the config', function () {
amTimeAgoConfig.format = 'MMMM Do YYYY, h:mm:ss a';
$rootScope.testDate = new Date().getTime() / 1000;
var element = angular.element('<span am-time-ago="testDate"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
var testDateWithCustomFormatting = moment($rootScope.testDate).format(amTimeAgoConfig.format);
expect(element.attr('title')).toBe(testDateWithCustomFormatting);
});
});

describe('am-without-suffix attribute', function () {
it('should generate a time string without suffix when true', function () {
$rootScope.testDate = new Date();
Expand Down

0 comments on commit 3e82244

Please sign in to comment.