Repository navigation
Expand file tree
/
Copy pathbanner.js
More file actions
101 lines (87 loc) · 3.09 KB
/
Copy pathbanner.js
File metadata and controls
101 lines (87 loc) · 3.09 KB
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
define(function(require) {
'use strict';
var Template = require('shared/js/template');
var Utils = require('utils');
var mozL10n = require('l10n');
function Banner(node, tmplId) {
// Accept a reference to an element or the element id
if (typeof node === 'string') {
this.notice = document.getElementById(node);
} else {
this.notice = node;
}
// Accept an optional reference to template element id
this.tmpl = new Template(tmplId);
// Store a reference to timeout to debounce banner
this.timeout = null;
return this;
}
Banner.prototype = {
constructor: Banner,
render: function bn_render(alarmTime) {
var timeLeft, displayTime, tl, countdownType, localTimes, unitObj;
timeLeft = +alarmTime - Date.now();
// generate human readable numbers to pass to localization function
tl = Utils.dateMath.fromMS(timeLeft, {
unitsPartial: ['days', 'hours', 'minutes']
});
// Match properties to localizations string types
// e.g. minutes maps to nMinutes if there are no hours but
// nRemainMinutes if hours > 0
if (tl.days) {
//countdown-moreThanADay localized only for en-US while 913466 is open
countdownType = 'countdown-moreThanADay';
localTimes = [
['days', 'nRemainDays', tl.days],
['hours', 'nAndRemainHours', tl.hours]
];
} else if (tl.hours > 0) {
countdownType = 'countdown-moreThanAnHour';
localTimes = [
['hours', 'nHours', tl.hours],
['minutes', 'nRemainMinutes', tl.minutes]
];
} else {
countdownType = 'countdown-lessThanAnHour';
localTimes = [
['minutes', 'nMinutes', tl.minutes]
];
}
// Create an object to pass to mozL10n.get
// e.g. {minutes: mozL10n.get('nMinutes', {n: 3})}
unitObj = localTimes.reduce(function(lcl, time) {
lcl[time[0]] = mozL10n.get(time[1], {n: time[2]});
return lcl;
}, {});
// mozL10n.get interpolates the units in unitObj inside the
// localization string for countdownType
return mozL10n.get(countdownType, unitObj);
},
show: function bn_show(alarmTime) {
// Render the Banner notice
this.notice.innerHTML = this.tmpl.interpolate(
{notice: this.render(alarmTime)},
// Localization strings contain <strong> tags
{safe: ['notice']}
);
// 'visible' class controls the animation
this.notice.classList.add('visible');
// use this object rather than a function to retain context
this.notice.addEventListener('click', this);
// Debounce timer in case alarms are added more quickly than 4 seconds
if (this.timeout) {
clearTimeout(this.timeout);
}
// After 4 seconds, remove the banner
this.timeout = setTimeout(this.hide.bind(this), 4000);
},
hide: function bn_hide() {
this.notice.classList.remove('visible');
this.notice.removeEventListener('click', this);
},
handleEvent: function bn_handleEvent() {
this.hide();
}
};
return Banner;
});