diff --git a/angular-moment.js b/angular-moment.js index 36bfc37..5b0ca3e 100644 --- a/angular-moment.js +++ b/angular-moment.js @@ -30,7 +30,7 @@ }) .constant('moment', window.moment) .constant('amTimeAgoConfig', { withoutSuffix: false}) - .directive('amTimeAgo', ['$timeout', 'moment', 'amTimeAgoConfig', function ($timeout, moment, amTimeAgoConfig) { + .directive('amTimeAgo', ['$window', 'moment', 'amTimeAgoConfig', function ($window, moment, amTimeAgoConfig) { return function (scope, element, attr) { var activeTimeout = null; @@ -40,7 +40,7 @@ function cancelTimer() { if (activeTimeout) { - $timeout.cancel(activeTimeout); + $window.clearTimeout(activeTimeout); activeTimeout = null; } } @@ -57,7 +57,7 @@ secondsUntilUpdate = 300; } - activeTimeout = $timeout(function () { + activeTimeout = $window.setTimeout(function () { updateTime(momentInstance); }, secondsUntilUpdate * 1000); } diff --git a/angular-moment.min.js b/angular-moment.min.js index d1d2bf4..7299c49 100644 --- a/angular-moment.min.js +++ b/angular-moment.min.js @@ -1 +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()}),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){if("undefined"==typeof e||null===e)return"";!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10)));var f=b.moment(e);return f.isValid()?a(f,d.timezone,c).calendar():""}}]).filter("amDateFormat",["$window","$log","angularMomentConfig",function(b,c,d){return function(e,f){if("undefined"==typeof e||null===e)return"";!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10)));var g=b.moment(e);return g.isValid()?a(g,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)}}])}(); \ No newline at end of file +!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("moment",window.moment).constant("amTimeAgoConfig",{withoutSuffix:!1}).directive("amTimeAgo",["$window","moment","amTimeAgoConfig",function(a,b,c){return function(d,e,f){function g(){l&&(a.clearTimeout(l),l=null)}function h(c){e.text(c.fromNow(m));var d=b().diff(c,"minute"),f=3600;1>d?f=1:60>d?f=30:180>d&&(f=300),l=a.setTimeout(function(){h(c)},1e3*f)}function i(){g(),h(b(j,k))}var j,k,l=null,m=c.withoutSuffix;d.$watch(f.amTimeAgo,function(a){return"undefined"==typeof a||null===a||""===a?(g(),void(j&&(e.text(""),j=null))):(angular.isNumber(a)&&(a=new Date(a)),j=a,void i())}),angular.isDefined(f.amWithoutSuffix)&&d.$watch(f.amWithoutSuffix,function(a){"boolean"==typeof a?(m=a,i()):m=c.withoutSuffix}),f.$observe("amFormat",function(a){k=a,j&&i()}),d.$on("$destroy",function(){g()}),d.$on("amMoment:languageChange",function(){i()})}}]).factory("amMoment",["moment","$rootScope",function(a,b){return{changeLanguage:function(c){var d=a.lang(c);return angular.isDefined(c)&&b.$broadcast("amMoment:languageChange"),d}}}]).filter("amCalendar",["moment","$log","angularMomentConfig",function(b,c,d){return function(e){if("undefined"==typeof e||null===e)return"";!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10)));var f=b(e);return f.isValid()?a(f,d.timezone,c).calendar():""}}]).filter("amDateFormat",["moment","$log","angularMomentConfig",function(b,c,d){return function(e,f){if("undefined"==typeof e||null===e)return"";!isNaN(parseFloat(e))&&isFinite(e)&&(e=new Date(parseInt(e,10)));var g=b(e);return g.isValid()?a(g,d.timezone,c).format(f):""}}]).filter("amDurationFormat",["moment",function(a){return function(b,c,d){return"undefined"==typeof b||null===b?"":a.duration(b,c).humanize(d)}}])}(); \ No newline at end of file diff --git a/tests.js b/tests.js index 3095ceb..016b050 100644 --- a/tests.js +++ b/tests.js @@ -2,12 +2,12 @@ * Copyright (C) 2013, 2014, Uri Shaked. */ -/* global describe, inject, module, beforeEach, afterEach, it, expect */ +/* global describe, inject, module, beforeEach, afterEach, it, expect, spyOn */ 'use strict'; describe('module angularMoment', function () { - var $rootScope, $compile, $filter, $timeout, moment, amTimeAgoConfig, originalTimeAgoConfig, angularMomentConfig, + var $rootScope, $compile, $window, $filter, moment, amTimeAgoConfig, originalTimeAgoConfig, angularMomentConfig, originalAngularMomentConfig, amMoment; beforeEach(module('angularMoment')); @@ -15,8 +15,8 @@ describe('module angularMoment', function () { beforeEach(inject(function ($injector) { $rootScope = $injector.get('$rootScope'); $compile = $injector.get('$compile'); + $window = $injector.get('$window'); $filter = $injector.get('$filter'); - $timeout = $injector.get('$timeout'); moment = $injector.get('moment'); amMoment = $injector.get('amMoment'); amTimeAgoConfig = $injector.get('amTimeAgoConfig'); @@ -99,7 +99,7 @@ describe('module angularMoment', function () { $rootScope.testDate = new Date(new Date().getTime() - 44000); var element = angular.element('
'); element = $compile(element)($rootScope); - $timeout.flush(); + $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); var waitsInterval = setInterval(function () { @@ -109,7 +109,7 @@ describe('module angularMoment', function () { } clearInterval(waitsInterval); - $timeout.flush(); + $rootScope.$digest(); expect(element.text()).toBe('a minute ago'); done(); }, 50); @@ -129,11 +129,12 @@ describe('module angularMoment', function () { $rootScope.testDate = new Date().getTime(); var element = angular.element('
'); element = $compile(element)($rootScope); - $timeout.flush(); + $rootScope.$digest(); expect(element.text()).toBe('a few seconds ago'); $rootScope.testDate = ''; + spyOn($window, 'clearTimeout').and.callThrough(); $rootScope.$digest(); - $timeout.verifyNoPendingTasks(); + expect($window.clearTimeout).toHaveBeenCalled(); expect(element.text()).toBe(''); }); @@ -141,7 +142,7 @@ describe('module angularMoment', function () { $rootScope.testDate = null; var element = angular.element('
Initial text
'); element = $compile(element)($rootScope); - $timeout.flush(); + $rootScope.$digest(); expect(element.text()).toBe('Initial text'); $rootScope.testDate = new Date().getTime(); $rootScope.$digest(); @@ -153,9 +154,10 @@ describe('module angularMoment', function () { $rootScope.testDate = new Date(); var element = angular.element(''); element = $compile(element)(scope); - $timeout.flush(); + $rootScope.$digest(); + spyOn($window, 'clearTimeout').and.callThrough(); scope.$destroy(); - $timeout.verifyNoPendingTasks(); + expect($window.clearTimeout).toHaveBeenCalled(); }); it('should generate a time string without suffix when configured to do so', function () {