/
clock_ctrl.js
143 lines (120 loc) · 4.04 KB
/
clock_ctrl.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
import {PanelCtrl} from 'app/plugins/sdk';
import moment from 'moment';
import _ from 'lodash';
import './css/clock-panel.css!';
const panelDefaults = {
mode: 'time',
clockType: '24 hour',
offsetFromUtc: null,
offsetFromUtcMinutes: null,
bgColor: null,
countdownSettings: {
endCountdownTime: moment().seconds(0).milliseconds(0).add(1, 'day').toDate(),
endText: '00:00:00'
},
dateSettings: {
showDate: false,
dateFormat: 'YYYY-MM-DD',
fontSize: '20px',
fontWeight: 'normal'
},
timeSettings: {
customFormat: 'HH:mm:ss',
fontSize: '60px',
fontWeight: 'normal'
}
};
export class ClockCtrl extends PanelCtrl {
constructor($scope, $injector) {
super($scope, $injector);
_.defaults(this.panel, panelDefaults);
_.defaults(this.panel.timeSettings, panelDefaults.timeSettings);
if (!(this.panel.countdownSettings.endCountdownTime instanceof Date)) {
this.panel.countdownSettings.endCountdownTime = moment(this.panel.countdownSettings.endCountdownTime).toDate();
}
this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
this.events.on('panel-teardown', this.onPanelTeardown.bind(this));
this.updateClock();
}
onInitEditMode() {
this.addEditorTab('Options', 'public/plugins/grafana-clock-panel/editor.html', 2);
}
onPanelTeardown() {
this.$timeout.cancel(this.nextTickPromise);
}
updateClock() {
if (this.panel.mode === 'time') {
this.renderTime();
} else {
this.renderCountdown();
}
this.nextTickPromise = this.$timeout(this.updateClock.bind(this), 1000);
}
renderTime() {
let now;
if (this.panel.offsetFromUtc && this.panel.offsetFromUtcMinutes) {
const offsetInMinutes = (parseInt(this.panel.offsetFromUtc, 10) * 60) + parseInt(this.panel.offsetFromUtcMinutes, 10);
now = moment().utcOffset(offsetInMinutes);
} else if (this.panel.offsetFromUtc && !this.panel.offsetFromUtcMinutes) {
now = moment().utcOffset(parseInt(this.panel.offsetFromUtc, 10));
} else {
now = moment();
}
if (this.panel.dateSettings.showDate) {
this.date = now.format(this.panel.dateSettings.dateFormat);
}
this.time = now.format(this.getTimeFormat());
this.render();
}
getTimeFormat() {
if (this.panel.clockType === '24 hour') {
return 'HH:mm:ss';
}
if (this.panel.clockType === '12 hour') {
return 'h:mm:ss A';
}
return this.panel.timeSettings.customFormat;
}
renderCountdown() {
if (!this.panel.countdownSettings.endCountdownTime) {
this.time = this.panel.countdownSettings.endText;
}
const now = moment();
const timeLeft = moment.duration(moment(this.panel.countdownSettings.endCountdownTime).diff(now));
let formattedTimeLeft = '';
if (timeLeft.asSeconds() <= 0) {
this.time = this.panel.countdownSettings.endText;
return;
}
if (timeLeft.years() > 0) {
formattedTimeLeft = timeLeft.years() === 1 ? '1 year, ' : timeLeft.years() + ' years, ';
}
if (timeLeft.months() > 0) {
formattedTimeLeft += timeLeft.months() === 1 ? '1 month, ' : timeLeft.months() + ' months, ';
}
if (timeLeft.days() > 0) {
formattedTimeLeft += timeLeft.days() === 1 ? '1 day, ' : timeLeft.days() + ' days, ';
}
if (timeLeft.hours() > 0) {
formattedTimeLeft += timeLeft.hours() === 1 ? '1 hour, ' : timeLeft.hours() + ' hours, ';
}
if (timeLeft.minutes() > 0) {
formattedTimeLeft += timeLeft.minutes() === 1 ? '1 minute, ' : timeLeft.minutes() + ' minutes, ';
}
if (timeLeft.seconds() > 0) {
formattedTimeLeft += timeLeft.seconds() === 1 ? '1 second, ' : timeLeft.seconds() + ' seconds';
}
this.time = formattedTimeLeft;
}
link(scope, elem) {
this.events.on('render', () => {
const $panelContainer = elem.find('.panel-container');
if (this.panel.bgColor) {
$panelContainer.css('background-color', this.panel.bgColor);
} else {
$panelContainer.css('background-color', '');
}
});
}
}
ClockCtrl.templateUrl = 'module.html';