-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathangular-momentjs.js
171 lines (171 loc) · 6.46 KB
/
angular-momentjs.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
angular-momentjs - v0.2.2
2015-07-10
*/
(function(window, angular, undefined) {
angular.module("angular-moment", [ "gdi2290.moment" ]);
angular.module("angular-momentjs", [ "gdi2290.moment" ]);
angular.module("ngMoment", [ "gdi2290.moment" ]);
angular.module("ngMomentjs", [ "gdi2290.moment" ]);
angular.module("ngMomentJS", [ "gdi2290.moment" ]);
angular.module("gdi2290.amTimeAgo", []);
angular.module("gdi2290.amDateFormat", []);
angular.module("gdi2290.moment-service", []);
angular.module("gdi2290.moment", [ "gdi2290.moment-service", "gdi2290.amDateFormat", "gdi2290.amTimeAgo" ]);
"use strict";
angular.module("gdi2290.amDateFormat").filter("amDateFormat", [ "$moment", function($moment) {
return function(value, format) {
if (typeof value === "undefined" || value === null) {
return "";
}
if (!isNaN(parseFloat(value)) && isFinite(value)) {
value = new Date(parseInt(value, 10));
}
if ($moment.then) {
$moment().then(function(moment) {
return moment(value).format(format);
});
} else {
return $moment(value).format(format);
}
};
} ]);
"use strict";
angular.module("gdi2290.amTimeAgo").directive("amTimeAgo", [ "$moment", "$timeout", function($moment, $timeout) {
function isUndefined(value) {
return typeof value === "undefined" || value === null || value === "";
}
return function(scope, element, attrs) {
var activeTimeout = null;
var currentValue;
var currentFormat;
function cancelTimer() {
if (activeTimeout) {
$timeout.cancel(activeTimeout);
activeTimeout = null;
}
}
function updateTime(momentInstance) {
element.text(momentInstance.fromNow());
var howOld;
if ($moment.then) {
$moment().then(function(moment) {
howOld = moment().diff(momentInstance, "minute");
});
} else {
howOld = $moment().diff(momentInstance, "minute");
}
var secondsUntilUpdate = 3600;
if (howOld < 1) {
secondsUntilUpdate = 1;
} else if (howOld < 60) {
secondsUntilUpdate = 30;
} else if (howOld < 180) {
secondsUntilUpdate = 300;
}
activeTimeout = $timeout(function() {
updateTime(momentInstance);
}, secondsUntilUpdate * 1e3, false);
}
function updateMoment() {
cancelTimer();
if ($moment().then) {
$moment.then(function(moment) {
updateTime(moment(currentValue, currentFormat));
});
} else {
updateTime($moment(currentValue, currentFormat));
}
}
scope.$watch(attrs.amTimeAgo, function(value) {
if (isUndefined(value)) {
cancelTimer();
if (currentValue) {
element.text("");
currentValue = null;
}
return;
}
if (angular.isNumber(value)) {
value = new Date(value);
}
currentValue = value;
updateMoment();
});
attrs.$observe("amFormat", function(format) {
currentFormat = format;
if (currentValue) {
updateMoment();
}
});
scope.$on("$destroy", function() {
cancelTimer();
});
};
} ]);
"use strict";
angular.module("gdi2290.moment-service").provider("$moment", function() {
var _asyncLoading = false;
var _scriptUrl = "//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js";
var _localeConfig = {};
var _locale;
this.asyncLoading = function(config) {
_asyncLoading = config || _asyncLoading;
return this;
};
this.scriptUrl = function(url) {
_scriptUrl = url || _scriptUrl;
return this;
};
this.locale = function(locale, config) {
_locale = locale || _locale;
_localeConfig = config || _localeConfig;
return this;
};
function createScript(callback) {
if (!document) {
return;
}
var scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
scriptTag.async = true;
scriptTag.src = _scriptUrl;
scriptTag.onreadystatechange = function() {
if (this.readyState === "complete") {
callback();
}
};
scriptTag.onload = callback;
var s = document.getElementsByTagName("head")[0];
s.appendChild(scriptTag);
}
this.$get = [ "$timeout", "$q", "$window", function($timeout, $q, $window) {
var deferred = $q.defer();
var _moment = $window.moment;
if (_asyncLoading) {
var onScriptLoad = function(callback) {
$timeout(function() {
if (_locale) {
if ($window.moment.lang) {
$window.moment.lang(_locale, _localeConfig);
} else {
$window.moment.locale(_locale, _localeConfig);
}
}
deferred.resolve($window.moment);
});
};
createScript(onScriptLoad);
} else {
if (_locale) {
if ($window.moment.lang) {
$window.moment.lang(_locale, _localeConfig);
} else {
$window.moment.locale(_locale, _localeConfig);
}
}
}
return _asyncLoading ? deferred.promise : _moment;
} ];
});
})(this, this.angular, void 0);