-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathmoment.js
97 lines (74 loc) · 2.18 KB
/
moment.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
import Ember from 'ember';
import Service from '@ember/service';
import Evented from '@ember/object/evented';
import { getOwner } from '@ember/application';
import moment from 'moment';
import { computed, get, set, getProperties, setProperties } from '@ember/object';
// question unresolved https://github.com/ember-cli/ember-rfc176-data/issues/12#issuecomment-318603308
const { Logger: { warn } } = Ember;
export default Service.extend(Evented, {
_timeZone: null,
locale: null,
localeOptions: null,
defaultFormat: null,
__config__: computed(function() {
let config = getOwner(this).factoryFor('config:environment').class || {};
return get(config, 'moment') || {};
}).readOnly(),
timeZone: computed('_timeZone', {
get() {
return get(this, '_timeZone');
},
set(propertyKey, timeZone) {
if (!moment.tz) {
warn('[ember-moment] attempted to set timezone, but moment-timezone is not setup.');
return;
}
set(this, '_timeZone', timeZone);
return timeZone;
}
}),
setLocale(locale) {
this.changeLocale(locale);
},
updateLocale(locale, localeOptions = {}) {
this.changeLocale(locale, localeOptions);
},
changeLocale(locale, localeOptions = {}) {
setProperties(this, {
locale,
localeOptions
});
moment.updateLocale(locale, localeOptions);
this.trigger('localeChanged', locale);
},
setTimeZone(timeZone) {
this.changeTimeZone(timeZone);
},
changeTimeZone(timeZone) {
set(this, 'timeZone', timeZone);
this.trigger('timeZoneChanged', timeZone);
},
isMoment(obj) {
return moment.isMoment(obj);
},
moment() {
let momentObj = moment(...arguments);
let { locale, timeZone } = getProperties(this, 'locale', 'timeZone');
if (locale && momentObj.locale) {
momentObj = momentObj.locale(locale);
}
if (timeZone && momentObj.tz) {
momentObj = momentObj.tz(timeZone);
}
return momentObj;
},
utc() {
let momentObj = moment.utc(...arguments);
let { locale } = getProperties(this, 'locale');
if (locale && momentObj.locale) {
momentObj = momentObj.locale(locale);
}
return momentObj;
}
});