|
1 | 1 | /** |
2 | | - * angular-timer - v1.2.2 - 2015-03-03 1:43 PM |
| 2 | + * angular-timer - v1.3.0 - 2015-03-03 5:32 PM |
3 | 3 | * https://github.com/siddii/angular-timer |
4 | 4 | * |
5 | 5 | * Copyright (c) 2015 Siddique Hameed |
6 | 6 | * Licensed MIT <https://github.com/siddii/angular-timer/blob/master/LICENSE.txt> |
7 | 7 | */ |
| 8 | +var app = angular.module('timer'); |
| 9 | + |
| 10 | +app.factory('I18nService', function() { |
| 11 | + |
| 12 | + var I18nService = function() {}; |
| 13 | + |
| 14 | + I18nService.prototype.language = 'en'; |
| 15 | + I18nService.prototype.timeHumanizer = {}; |
| 16 | + |
| 17 | + I18nService.prototype.init = function init(lang){ |
| 18 | + this.language = lang; |
| 19 | + //moment init |
| 20 | + moment.locale(this.language); //@TODO maybe to remove, it should be handle by the user's application itself, and not inside the directive |
| 21 | + |
| 22 | + //human duration init, using it because momentjs does not allow accurate time ( |
| 23 | + // momentJS: a few moment ago, human duration : 4 seconds ago |
| 24 | + this.timeHumanizer = humanizeDuration.humanizer({ |
| 25 | + language: this.language, |
| 26 | + halfUnit:false |
| 27 | + }); |
| 28 | + }; |
| 29 | + |
| 30 | + /** |
| 31 | + * get time with units from momentJS i18n |
| 32 | + * @param {int} millis |
| 33 | + * @returns {{millis: string, seconds: string, minutes: string, hours: string, days: string, months: string, years: string}} |
| 34 | + */ |
| 35 | + I18nService.prototype.getTimeUnits = function getTimeUnits(millis) { |
| 36 | + var diffFromAlarm = Math.round(millis/1000) * 1000; //time in milliseconds, get rid of the last 3 ms value to avoid 2.12 seconds display |
| 37 | + |
| 38 | + var time = {}; |
| 39 | + |
| 40 | + if (typeof this.timeHumanizer != 'undefined'){ |
| 41 | + time = { |
| 42 | + 'millis' : this.timeHumanizer(diffFromAlarm, { units: ["milliseconds"] }), |
| 43 | + 'seconds' : this.timeHumanizer(diffFromAlarm, { units: ["seconds"] }), |
| 44 | + 'minutes' : this.timeHumanizer(diffFromAlarm, { units: ["minutes", "seconds"] }) , |
| 45 | + 'hours' : this.timeHumanizer(diffFromAlarm, { units: ["hours", "minutes", "seconds"] }) , |
| 46 | + 'days' : this.timeHumanizer(diffFromAlarm, { units: ["days", "hours", "minutes", "seconds"] }) , |
| 47 | + 'months' : this.timeHumanizer(diffFromAlarm, { units: ["months", "days", "hours", "minutes", "seconds"] }) , |
| 48 | + 'years' : this.timeHumanizer(diffFromAlarm, { units: ["years", "months", "days", "hours", "minutes", "seconds"] }) |
| 49 | + }; |
| 50 | + } |
| 51 | + else { |
| 52 | + console.error('i18nService has not been initialized. You must call i18nService.init("en") for example'); |
| 53 | + } |
| 54 | + |
| 55 | + return time; |
| 56 | + }; |
| 57 | + |
| 58 | + return I18nService; |
| 59 | +}); |
| 60 | + |
8 | 61 | var timerModule = angular.module('timer', []) |
9 | 62 | .directive('timer', ['$compile', function ($compile) { |
10 | 63 | return { |
@@ -313,56 +366,3 @@ var timerModule = angular.module('timer', []) |
313 | 366 | if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){ |
314 | 367 | module.exports = timerModule; |
315 | 368 | } |
316 | | - |
317 | | -var app = angular.module('timer'); |
318 | | - |
319 | | -app.factory('I18nService', function() { |
320 | | - |
321 | | - var I18nService = function() {}; |
322 | | - |
323 | | - I18nService.prototype.language = 'en'; |
324 | | - I18nService.prototype.timeHumanizer = {}; |
325 | | - |
326 | | - I18nService.prototype.init = function init(lang){ |
327 | | - this.language = lang; |
328 | | - //moment init |
329 | | - moment.locale(this.language); //@TODO maybe to remove, it should be handle by the user's application itself, and not inside the directive |
330 | | - |
331 | | - //human duration init, using it because momentjs does not allow accurate time ( |
332 | | - // momentJS: a few moment ago, human duration : 4 seconds ago |
333 | | - this.timeHumanizer = humanizeDuration.humanizer({ |
334 | | - language: this.language, |
335 | | - halfUnit:false |
336 | | - }); |
337 | | - }; |
338 | | - |
339 | | - /** |
340 | | - * get time with units from momentJS i18n |
341 | | - * @param {int} millis |
342 | | - * @returns {{millis: string, seconds: string, minutes: string, hours: string, days: string, months: string, years: string}} |
343 | | - */ |
344 | | - I18nService.prototype.getTimeUnits = function getTimeUnits(millis) { |
345 | | - var diffFromAlarm = Math.round(millis/1000) * 1000; //time in milliseconds, get rid of the last 3 ms value to avoid 2.12 seconds display |
346 | | - |
347 | | - var time = {}; |
348 | | - |
349 | | - if (typeof this.timeHumanizer != 'undefined'){ |
350 | | - time = { |
351 | | - 'millis' : this.timeHumanizer(diffFromAlarm, { units: ["milliseconds"] }), |
352 | | - 'seconds' : this.timeHumanizer(diffFromAlarm, { units: ["seconds"] }), |
353 | | - 'minutes' : this.timeHumanizer(diffFromAlarm, { units: ["minutes", "seconds"] }) , |
354 | | - 'hours' : this.timeHumanizer(diffFromAlarm, { units: ["hours", "minutes", "seconds"] }) , |
355 | | - 'days' : this.timeHumanizer(diffFromAlarm, { units: ["days", "hours", "minutes", "seconds"] }) , |
356 | | - 'months' : this.timeHumanizer(diffFromAlarm, { units: ["months", "days", "hours", "minutes", "seconds"] }) , |
357 | | - 'years' : this.timeHumanizer(diffFromAlarm, { units: ["years", "months", "days", "hours", "minutes", "seconds"] }) |
358 | | - }; |
359 | | - } |
360 | | - else { |
361 | | - console.error('i18nService has not been initialized. You must call i18nService.init("en") for example'); |
362 | | - } |
363 | | - |
364 | | - return time; |
365 | | - }; |
366 | | - |
367 | | - return I18nService; |
368 | | -}); |
0 commit comments