This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnotify.js
79 lines (73 loc) · 2.76 KB
/
notify.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
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
/* global define */
define([
'require',
'jquery',
'underscore'
], function(require, $, _) {
var exports = {};
var $view = $('<div>').appendTo('body'),
timer,
finalized = false,
CLEAR_NOTICE_TIMEOUT = 2 * 1000,
time_when_browser_focus_first_got_detected = 0;
var tmpl = _.template(([
'<div class="urth-notice alert alert-<%= type %> alert-dismissible fade in" role="alert">',
' <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>',
' <%= text %>' +
'</div>'].join('')));
// Set a timer to hide the notice once the browser has been in focus for
// a period of time.
var set_timer = function() {
if(timer) {
// Reset the timer if a new notice arrives
clearInterval(timer);
time_when_browser_focus_first_got_detected = 0;
}
timer = setInterval(function() {
var date = new Date();
if(!document.hasFocus()) {
// Reset timer if we've lost focus
time_when_browser_focus_first_got_detected = 0;
return;
} else {
// First time we've seen the browser get focus, so note it
if (time_when_browser_focus_first_got_detected === 0) {
time_when_browser_focus_first_got_detected = date.getTime();
}
if (date.getTime() - time_when_browser_focus_first_got_detected >= CLEAR_NOTICE_TIMEOUT) {
// We've seen the browser in focus for greater than the max.
clearInterval(timer);
timer = null;
time_when_browser_focus_first_got_detected = 0;
$view.find('.alert').alert('close');
}
}
}, CLEAR_NOTICE_TIMEOUT);
};
// Show a notice. Supports these args:
// * type - Type of notice, one of the Bootstrap alert types
// * text - Text to include in the notice
// * permanent - True to make the notice permanent (e.g., a failure)
exports.show = function(args) {
if(finalized) return;
finalized = !!args.permanent;
$view.html(tmpl(args));
if(!finalized) {
set_timer();
} else {
clearInterval(timer);
timer = null;
}
};
// Load the associated stylesheet
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = require.toUrl("./notify.css");
document.getElementsByTagName("head")[0].appendChild(link);
return exports;
});