Repository navigation
Expand file tree
/
Copy path02_2_logger.js
More file actions
78 lines (60 loc) · 2.12 KB
/
Copy path02_2_logger.js
File metadata and controls
78 lines (60 loc) · 2.12 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
// Copyright 2024 Google LLC
/*
* Demonstrate creating a Logger widget
*
*/
function Logger(delay) {
// Create a panel for displaying log messages.
this.panel = ui.Panel(null, ui.Panel.Layout.Flow('vertical'), {
backgroundColor: '#00000000',
color: '00000000',
position: 'bottom-right'
});
this.delay = delay;
// If a delay is not specified, default to 1 second.
if(typeof(this.delay) === 'undefined') {
this.delay = 1000;
}
// Specify a default maximum message count.
this.maxCount = 5;
Map.widgets().add(this.panel);
};
// Add a prototype function that displays a message.
Logger.prototype.info = function(message, color) {
var self = this;
var label = ui.Label(message, {
backgroundColor: '#00000066',
color: typeof(color) == 'undefined' ? 'ffffff' : color,
fontSize: '14px',
margin: '2px',
padding: '2px'
});
self.panel.widgets().add(label);
self.panel.style().set({ shown: true });
if(self.panel.widgets().length() > self.maxCount) {
self.panel.widgets().remove(self.panel.widgets().get(0));
}
// Configure the widget to be removed after a specified time delay
ui.util.setTimeout(function() {
self.panel.widgets().remove(label);
// If all the widget have been removed, hide the panel.
if(self.panel.widgets().length() === 0) {
self.panel.style().set({ shown: false });
}
}, self.delay);
};
Logger.prototype.setMaxCount = function(maxCount) {
this.maxCount = maxCount;
};
// ======================================= test
var log = new Logger(3000);
log.setMaxCount(10);
log.info('Querying image data ...');
log.info('Querying image data v2 ...');
log.info('Querying image data v3 ...');
log.info('Querying image data v4 ...');
// Call functions after a specified delay.
ui.util.setTimeout(function() { log.info('Querying image data v5 ...') }, 1000);
ui.util.setTimeout(function() { log.info('Querying image data v6 ...', 'yellow') }, 500);
ui.util.setTimeout(function() { log.info('Querying image data v7 ...', 'lime') }, 500);
ui.util.setTimeout(function() { log.info('Querying image data v8 ...') }, 500);