File size: 1,242 Bytes
7f88cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var ngSmartTimeago = angular.module("ngSmartTimeago", []);

ngSmartTimeago.constant("ngSmartTimeagoConfig", {
  attr: "datetime"
});

ngSmartTimeago.directive("timeago", ["ngSmartTimeagoConfig", "$timeout",
  function(ngSmartTimeagoConfig, $timeout) {
    var scope_option = {};
    scope_option[ngSmartTimeagoConfig.attr] = "@";

    return {
      restrict: "E",
      scope: scope_option,
      link: function(scope, element, attrs) {
        var options = angular.extend(
          {},
          ngSmartTimeagoConfig,
          {selector: "timeago"}
        );

        element.timeago(options);

        scope.$watch(ngSmartTimeagoConfig.attr, function() {
          element.data("timeago").refresh();
        });

        // Clear timer before destroying scope
        scope.$on('$destroy', function() {
          element.data("timeago").stopTimer();
        });
      }
    };
  }
]);

ngSmartTimeago.filter('timeago',["smartTimeagoInstance", function(smartTimeagoInstance){
  return function (input) {
    return smartTimeagoInstance.timeAgoInWords(input);
  };
}]);

ngSmartTimeago.factory("smartTimeagoInstance", ["$window", function($window) {
  var instance = new $window.jQuery.fn.timeago.Constructor();

  return instance;
}]);