angular-moment
Advanced tools
Comparing version 0.6.1 to 0.6.2
@@ -1,4 +0,4 @@ | ||
/* angular-moment.js / v0.6.1 / (c) 2013, 2014 Uri Shaked / MIT Licence */ | ||
/* angular-moment.js / v0.6.2 / (c) 2013, 2014 Uri Shaked / MIT Licence */ | ||
(function(){ | ||
(function () { | ||
'use strict'; | ||
@@ -25,5 +25,5 @@ | ||
angular.module('angularMoment', []) | ||
/** | ||
* Common configuration of the angularMoment module | ||
*/ | ||
/** | ||
* Common configuration of the angularMoment module | ||
*/ | ||
.constant('angularMomentConfig', { | ||
@@ -111,4 +111,19 @@ timezone: '' // e.g. 'Europe/London' | ||
}); | ||
scope.$on('amMoment:languageChange', function () { | ||
updateMoment(); | ||
}); | ||
}; | ||
}]) | ||
.factory('amMoment', ['$window', '$rootScope', function ($window, $rootScope) { | ||
return { | ||
changeLanguage: function (lang) { | ||
var result = $window.moment.lang(lang); | ||
if (angular.isDefined(lang)) { | ||
$rootScope.$broadcast('amMoment:languageChange'); | ||
} | ||
return result; | ||
} | ||
}; | ||
}]) | ||
.filter('amCalendar', ['$window', '$log', 'angularMomentConfig', function ($window, $log, angularMomentConfig) { | ||
@@ -158,2 +173,2 @@ | ||
})(); | ||
})(); |
@@ -1,1 +0,1 @@ | ||
!function(){"use strict";function a(a,b,c){return a&&b&&(a.tz?a=a.tz(b):c.warn("angular-moment: timezone specified but moment.tz() is undefined. Did you forget to include moment-timezone.js?")),a}angular.module("angularMoment",[]).constant("angularMomentConfig",{timezone:""}).constant("amTimeAgoConfig",{withoutSuffix:!1}).directive("amTimeAgo",["$window","amTimeAgoConfig",function(a,b){return function(c,d,e){function f(){k&&(a.clearTimeout(k),k=null)}function g(b){d.text(b.fromNow(l));var c=a.moment().diff(b,"minute"),e=3600;1>c?e=1:60>c?e=30:180>c&&(e=300),k=a.setTimeout(function(){g(b)},1e3*e)}function h(){f(),g(a.moment(i,j))}var i,j,k=null,l=b.withoutSuffix;c.$watch(e.amTimeAgo,function(a){return"undefined"==typeof a||null===a||""===a?(f(),void(i&&(d.text(""),i=null))):(angular.isNumber(a)&&(a=new Date(a)),i=a,void h())}),angular.isDefined(e.amWithoutSuffix)&&c.$watch(e.amWithoutSuffix,function(a){"boolean"==typeof a?(l=a,h()):l=b.withoutSuffix}),e.$observe("amFormat",function(a){j=a,i&&h()}),c.$on("$destroy",function(){f()})}}]).filter("amCalendar",["$window","$log","angularMomentConfig",function(b,c,d){return function(e){return"undefined"==typeof e||null===e?"":(!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10))),a(b.moment(e),d.timezone,c).calendar())}}]).filter("amDateFormat",["$window","$log","angularMomentConfig",function(b,c,d){return function(e,f){return"undefined"==typeof e||null===e?"":(!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10))),a(b.moment(e),d.timezone,c).format(f))}}]).filter("amDurationFormat",["$window",function(a){return function(b,c,d){return"undefined"==typeof b||null===b?"":a.moment.duration(b,c).humanize(d)}}])}(); | ||
!function(){"use strict";function a(a,b,c){return a&&b&&(a.tz?a=a.tz(b):c.warn("angular-moment: timezone specified but moment.tz() is undefined. Did you forget to include moment-timezone.js?")),a}angular.module("angularMoment",[]).constant("angularMomentConfig",{timezone:""}).constant("amTimeAgoConfig",{withoutSuffix:!1}).directive("amTimeAgo",["$window","amTimeAgoConfig",function(a,b){return function(c,d,e){function f(){k&&(a.clearTimeout(k),k=null)}function g(b){d.text(b.fromNow(l));var c=a.moment().diff(b,"minute"),e=3600;1>c?e=1:60>c?e=30:180>c&&(e=300),k=a.setTimeout(function(){g(b)},1e3*e)}function h(){f(),g(a.moment(i,j))}var i,j,k=null,l=b.withoutSuffix;c.$watch(e.amTimeAgo,function(a){return"undefined"==typeof a||null===a||""===a?(f(),void(i&&(d.text(""),i=null))):(angular.isNumber(a)&&(a=new Date(a)),i=a,void h())}),angular.isDefined(e.amWithoutSuffix)&&c.$watch(e.amWithoutSuffix,function(a){"boolean"==typeof a?(l=a,h()):l=b.withoutSuffix}),e.$observe("amFormat",function(a){j=a,i&&h()}),c.$on("$destroy",function(){f()}),c.$on("amMoment:languageChange",function(){h()})}}]).factory("amMoment",["$window","$rootScope",function(a,b){return{changeLanguage:function(c){var d=a.moment.lang(c);return angular.isDefined(c)&&b.$broadcast("amMoment:languageChange"),d}}}]).filter("amCalendar",["$window","$log","angularMomentConfig",function(b,c,d){return function(e){return"undefined"==typeof e||null===e?"":(!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10))),a(b.moment(e),d.timezone,c).calendar())}}]).filter("amDateFormat",["$window","$log","angularMomentConfig",function(b,c,d){return function(e,f){return"undefined"==typeof e||null===e?"":(!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10))),a(b.moment(e),d.timezone,c).format(f))}}]).filter("amDurationFormat",["$window",function(a){return function(b,c,d){return"undefined"==typeof b||null===b?"":a.moment.duration(b,c).humanize(d)}}])}(); |
{ | ||
"name": "angular-moment", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"description": "Moment.JS directives & filters for Angular.JS (timeago alternative)", | ||
@@ -13,7 +13,7 @@ "author": "Uri Shaked", | ||
"angular": ">=1.0.0 <1.3.0", | ||
"moment": ">=2.0.0 <2.6.0", | ||
"moment-timezone": "~0.0.3" | ||
"moment": ">=2.0.0 <2.6.0" | ||
}, | ||
"devDependencies": { | ||
"angular-mocks": "1.2.x" | ||
"angular-mocks": "1.2.x", | ||
"moment-timezone": "~0.0.3" | ||
}, | ||
@@ -20,0 +20,0 @@ "repository": { |
# Changelog | ||
## 0.6.2 - 2014-02-05 | ||
- Add `amMoment` service with a `changeLanguage()` method ([#32](https://github.com/urish/angular-moment/pull/32), contributed by [Ornthalas](https://github.com/Ornthalas)) | ||
- bower.json: Move `moment-timezone` to devDependencies (fixes [#34](https://github.com/urish/angular-moment/issues/34)) | ||
## 0.6.1 - 2014-01-31 | ||
@@ -4,0 +8,0 @@ - Add optional timezone support to `amCalendar` and `amDateFormat` filters ([#27](https://github.com/urish/angular-moment/pull/27), contributed by [kayhadrin](https://github.com/kayhadrin)) |
@@ -19,2 +19,3 @@ /* License: MIT. | ||
'components/moment/moment.js', | ||
'components/moment/lang/fr.js', | ||
'components/moment-timezone/moment-timezone.js', | ||
@@ -21,0 +22,0 @@ 'angular-moment.js', |
{ | ||
"name": "angular-moment", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
42
tests.js
@@ -11,3 +11,3 @@ /* License: MIT. | ||
var $rootScope, $compile, $window, $filter, amTimeAgoConfig, originalTimeAgoConfig, angularMomentConfig, | ||
originalAngularMomentConfig; | ||
originalAngularMomentConfig, amMoment; | ||
@@ -21,2 +21,3 @@ beforeEach(module('angularMoment')); | ||
$filter = $injector.get('$filter'); | ||
amMoment = $injector.get('amMoment'); | ||
amTimeAgoConfig = $injector.get('amTimeAgoConfig'); | ||
@@ -26,2 +27,5 @@ angularMomentConfig = $injector.get('angularMomentConfig'); | ||
originalAngularMomentConfig = angular.copy(angularMomentConfig); | ||
// Ensure the language of moment.js is set to english by default | ||
$window.moment.lang('en'); | ||
})); | ||
@@ -167,2 +171,12 @@ | ||
it('should generate update the text following a language change via amMoment.changeLanguage() method', function () { | ||
$rootScope.testDate = new Date(); | ||
var element = angular.element('<span am-time-ago="testDate"></span>'); | ||
element = $compile(element)($rootScope); | ||
$rootScope.$digest(); | ||
expect(element.text()).toBe('a few seconds ago'); | ||
amMoment.changeLanguage('fr'); | ||
expect(element.text()).toBe('il y a quelques secondes'); | ||
}); | ||
describe('am-without-suffix attribute', function () { | ||
@@ -335,2 +349,28 @@ it('should generate a time string without suffix when true', function () { | ||
describe('amMoment service', function () { | ||
describe('#changeLanguage', function () { | ||
it('should return the current language', function () { | ||
expect(amMoment.changeLanguage()).toBe('en'); | ||
}); | ||
it('should broadcast an angularMoment:languageChange event on the root scope if a language is specified', function () { | ||
var eventBroadcasted = false; | ||
$rootScope.$on('amMoment:languageChange', function () { | ||
eventBroadcasted = true; | ||
}); | ||
amMoment.changeLanguage('fr'); | ||
expect(eventBroadcasted).toBe(true); | ||
}); | ||
it('should not broadcast an angularMoment:languageChange event on the root scope if no language is specified', function () { | ||
var eventBroadcasted = false; | ||
$rootScope.$on('amMoment:languageChange', function () { | ||
eventBroadcasted = true; | ||
}); | ||
amMoment.changeLanguage(); | ||
expect(eventBroadcasted).toBe(false); | ||
}); | ||
}); | ||
}); | ||
describe('amTimeAgoConfig constant', function () { | ||
@@ -337,0 +377,0 @@ it('should generate time with suffix by default', function () { |
32177
565