-
Notifications
You must be signed in to change notification settings - Fork 8
/
moment-import-behavior.html
165 lines (153 loc) · 5.09 KB
/
moment-import-behavior.html
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
<script>
window.MomentJs = window.MomentJs || {};
// Language shared by all instances of `moment-js`.
window.MomentJs.language = navigator.language || 'en';
/**
* @polymerBehavior MomentJs.MomentImportBehavior
*/
window.MomentJs.MomentImportBehavior = {
properties: {
/**
* Locale settings.
*/
locale: {
type: String,
value: window.MomentJs.language,
observer: '_localeChanged'
}
},
ready: function() {
if (typeof window.moment === 'undefined') {
if (!window.MomentJs.isLoading) {
window.MomentJs.isLoading = true;
this._loadMomentJs();
} else {
this._waitToLoadMomentJs();
}
} else {
// Moment is already loaded. e.g. by other components.
this.fire('moment-is-ready');
}
},
/**
* Dispatches a custom event. The event is composed, cancellable and bubbles
* through the DOM tree. It does not contain any detail.
*
* @param {String} name The name of the event to be dispatched.
*/
fire: function(name) {
this.dispatchEvent(new CustomEvent(name, {
bubbles: true,
composed: true,
cancellable: true,
}));
},
/**
* Loads momentjs library from local.
*
* @private
*/
_loadMomentJs: function() {
console.debug('[moment-js] momentjs library is loading...');
var script = document.createElement('script');
script.src = this.resolveUrl('../moment/min/moment.min.js');
script.onload = function() {
window.MomentJs.isLoading = false;
console.debug('[moment-js] momentjs library is loaded!');
this._localizeGlobalMoment().then(() => this.fire('moment-is-ready'));
}.bind(this);
document.body.appendChild(script);
},
/**
* Wait till loading momentjs complete and then fire the event `moment-is-ready`.
*
* @private
*/
_waitToLoadMomentJs: function() {
if (typeof window.moment !== 'undefined') {
this.fire('moment-is-ready');
} else {
window.setTimeout(this._waitToLoadMomentJs.bind(this), 1);
}
},
/**
* Checks if the given locale is valid. A valid locale contains only letters,
* has a length of 2 (en, de, fr, etc...) or between 5 and 8 with a dash
* (en-ca, de-ch, fr-ca, gom-latn, x-pseudo, etc...).
*
* @private
*/
_isValidLocale: function(locale) {
return !!locale && locale.length >= 2 &&
/^[a-zA-Z]{1,3}(-[a-zA-Z]{2,6})?$/.test(locale);
},
/**
* Updates all `moment-js` instances when the locale of one changed. The
* value of `locale` must pass the test `_isValidLocale` else the setting
* stay unchanged.
*
* @private
*/
_localeChanged: function(language) {
var lang = language || navigator.language || 'en';
if (lang === window.MomentJs.language) {
this.fire('moment-locale-changed');
} else if (this._isValidLocale(lang)) {
window.MomentJs.language = lang;
this._localizeGlobalMoment().then(this._syncMomentsLanguage);
}
},
/**
* Synchronizes all moment-js language with the global momentjs locale
* setting.
*
* @private
*/
_syncMomentsLanguage() {
let moments = document.querySelectorAll('moment-js');
let lang = window.MomentJs.language;
for (let i = 0, l = moments.length; i < l; i++) {
moments[i].locale = lang;
}
},
/**
* Asks to `momentjs` to use the defined locales to display the dates. All
* instances use the same language (defined by MomentJs.language). If the
* locales file is not loaded, the first element having changed will load it
* and the others wait until the end of the import. Then the global of
* `momentjs` locale is changed and all instances refreshes their display.
*
* @private
*/
_localizeGlobalMoment: function() {
return new Promise((resolve) => {
// Check if the script is already loaded.
var lang = window.MomentJs.language;
var fileName = '/moment/locale/' + lang + '.js';
var scripts = document.querySelectorAll('script');
for (var i = 0; i < scripts.length; i++) {
if (scripts[i].src.includes(fileName)) {
// Update momentjs locale once.
if (window.moment.locale() !== lang) {
window.moment.locale(lang);
}
this.fire('moment-locale-changed');
return resolve();
}
}
// Load language and set it as momentjs locale.
var script = document.createElement('script');
script.src = this.resolveUrl('..' + fileName);
script.onload = () => {
console.debug('[moment-js] locales file for "' + lang + '" loaded');
window.moment.locale(lang);
console.debug('[moment-js] language set to "' + lang + '"');
this.fire('moment-locale-changed');
resolve();
};
script.onerror = () => resolve();
document.body.appendChild(script);
});
}
};
</script>