Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'base/js/namespace',
6
'jquery',
7
], function(IPython, $) {
8
"use strict";
9
10
/**
11
* Construct a NotificationWidget object.
12
*
13
* @constructor
14
* @param {string} selector - a jQuery selector string for the
15
* notification widget element
16
*/
17
var NotificationWidget = function (selector) {
18
this.selector = selector;
19
this.timeout = null;
20
this.busy = false;
21
if (this.selector !== undefined) {
22
this.element = $(selector);
23
this.style();
24
}
25
this.element.hide();
26
this.inner = $('<span/>');
27
this.element.append(this.inner);
28
};
29
30
/**
31
* Add the 'notification_widget' CSS class to the widget element.
32
*
33
* @method style
34
*/
35
NotificationWidget.prototype.style = function () {
36
// use explicit bootstrap classes here,
37
// because multiple inheritance in LESS doesn't work
38
// for this particular combination
39
this.element.addClass('notification_widget btn btn-xs navbar-btn');
40
};
41
42
/**
43
* hide the widget and empty the text
44
**/
45
NotificationWidget.prototype.hide = function () {
46
var that = this;
47
this.element.fadeOut(100, function(){that.inner.text('');});
48
};
49
50
/**
51
* Set the notification widget message to display for a certain
52
* amount of time (timeout). The widget will be shown forever if
53
* timeout is <= 0 or undefined. If the widget is clicked while it
54
* is still displayed, execute an optional callback
55
* (click_callback). If the callback returns false, it will
56
* prevent the notification from being dismissed.
57
*
58
* Options:
59
* class - CSS class name for styling
60
* icon - CSS class name for the widget icon
61
* title - HTML title attribute for the widget
62
*
63
* @method set_message
64
* @param {string} msg - The notification to display
65
* @param {integer} [timeout] - The amount of time in milliseconds to display the widget
66
* @param {function} [click_callback] - The function to run when the widget is clicked
67
* @param {Object} [options] - Additional options
68
*/
69
NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) {
70
options = options || {};
71
72
// unbind potential previous callback
73
this.element.unbind('click');
74
this.inner.attr('class', options.icon);
75
this.inner.attr('title', options.title);
76
this.inner.text(msg);
77
this.element.fadeIn(100);
78
79
// reset previous set style
80
this.element.removeClass();
81
this.style();
82
if (options.class) {
83
this.element.addClass(options.class);
84
}
85
86
// clear previous timer
87
if (this.timeout !== null) {
88
clearTimeout(this.timeout);
89
this.timeout = null;
90
}
91
92
// set the timer if a timeout is given
93
var that = this;
94
if (timeout !== undefined && timeout >= 0) {
95
this.timeout = setTimeout(function () {
96
that.element.fadeOut(100, function () {that.inner.text('');});
97
that.element.unbind('click');
98
that.timeout = null;
99
}, timeout);
100
}
101
102
// if no click callback assume we will just dismiss the notification
103
if (click_callback === undefined) {
104
click_callback = function(){return true};
105
}
106
// on click, remove widget if click callback say so
107
// and unbind click event.
108
this.element.click(function () {
109
if (click_callback() !== false) {
110
that.element.fadeOut(100, function () {that.inner.text('');});
111
that.element.unbind('click');
112
}
113
if (that.timeout !== null) {
114
clearTimeout(that.timeout);
115
that.timeout = null;
116
}
117
});
118
};
119
120
/**
121
* Display an information message (styled with the 'info'
122
* class). Arguments are the same as in set_message. Default
123
* timeout is 3500 milliseconds.
124
*
125
* @method info
126
*/
127
NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) {
128
options = options || {};
129
options.class = options.class + ' info';
130
timeout = timeout || 3500;
131
this.set_message(msg, timeout, click_callback, options);
132
};
133
134
/**
135
* Display a warning message (styled with the 'warning'
136
* class). Arguments are the same as in set_message. Messages are
137
* sticky by default.
138
*
139
* @method warning
140
*/
141
NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) {
142
options = options || {};
143
options.class = options.class + ' warning';
144
this.set_message(msg, timeout, click_callback, options);
145
};
146
147
/**
148
* Display a danger message (styled with the 'danger'
149
* class). Arguments are the same as in set_message. Messages are
150
* sticky by default.
151
*
152
* @method danger
153
*/
154
NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) {
155
options = options || {};
156
options.class = options.class + ' danger';
157
this.set_message(msg, timeout, click_callback, options);
158
};
159
160
/**
161
* Get the text of the widget message.
162
*
163
* @method get_message
164
* @return {string} - the message text
165
*/
166
NotificationWidget.prototype.get_message = function () {
167
return this.inner.html();
168
};
169
170
// For backwards compatibility.
171
IPython.NotificationWidget = NotificationWidget;
172
173
return {'NotificationWidget': NotificationWidget};
174
});
175
176