-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathnotifier.js
77 lines (65 loc) · 2.28 KB
/
notifier.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
'use strict';
var d3 = require('@plotly/d3');
var isNumeric = require('fast-isnumeric');
var NOTEDATA = [];
/**
* notifier
* @param {String} text The person's user name
* @param {Number} [delay=1000] The delay time in milliseconds
* or 'long' which provides 2000 ms delay time.
* @return {undefined} this function does not return a value
*/
module.exports = function(text, displayLength) {
if(NOTEDATA.indexOf(text) !== -1) return;
NOTEDATA.push(text);
var ts = 1000;
if(isNumeric(displayLength)) ts = displayLength;
else if(displayLength === 'long') ts = 3000;
var notifierContainer = d3.select('body')
.selectAll('.plotly-notifier')
.data([0]);
notifierContainer.enter()
.append('div')
.classed('plotly-notifier', true);
var notes = notifierContainer.selectAll('.notifier-note').data(NOTEDATA);
function killNote(transition) {
transition
.duration(700)
.style('opacity', 0)
.each('end', function(thisText) {
var thisIndex = NOTEDATA.indexOf(thisText);
if(thisIndex !== -1) NOTEDATA.splice(thisIndex, 1);
d3.select(this).remove();
});
}
notes.enter().append('div')
.classed('notifier-note', true)
.style('opacity', 0)
.each(function(thisText) {
var note = d3.select(this);
note.append('button')
.classed('notifier-close', true)
.html('×')
.on('click', function() {
note.transition().call(killNote);
});
var p = note.append('p');
var lines = thisText.split(/<br\s*\/?>/g);
for(var i = 0; i < lines.length; i++) {
if(i) p.append('br');
p.append('span').text(lines[i]);
}
if(displayLength === 'stick') {
note.transition()
.duration(350)
.style('opacity', 1);
} else {
note.transition()
.duration(700)
.style('opacity', 1)
.transition()
.delay(ts)
.call(killNote);
}
});
};