Show More
@@ -0,0 +1,116 b'' | |||||
|
1 | // Test the notification area and widgets | |||
|
2 | ||||
|
3 | casper.notebook_test(function () { | |||
|
4 | var that = this; | |||
|
5 | var widget = function (name) { | |||
|
6 | return that.evaluate(function (name) { | |||
|
7 | return (IPython.notification_area.widget(name) !== undefined); | |||
|
8 | }, name); | |||
|
9 | }; | |||
|
10 | ||||
|
11 | var get_widget = function (name) { | |||
|
12 | return that.evaluate(function (name) { | |||
|
13 | return (IPython.notification_area.get_widget(name) !== undefined); | |||
|
14 | }, name); | |||
|
15 | }; | |||
|
16 | ||||
|
17 | var new_notification_widget = function (name) { | |||
|
18 | return that.evaluate(function (name) { | |||
|
19 | return (IPython.notification_area.new_notification_widget(name) !== undefined); | |||
|
20 | }, name); | |||
|
21 | }; | |||
|
22 | ||||
|
23 | var widget_has_class = function (name, class_name) { | |||
|
24 | return that.evaluate(function (name, class_name) { | |||
|
25 | var w = IPython.notification_area.get_widget(name); | |||
|
26 | return w.element.hasClass(class_name); | |||
|
27 | }, name, class_name); | |||
|
28 | }; | |||
|
29 | ||||
|
30 | var widget_message = function (name) { | |||
|
31 | return that.evaluate(function (name) { | |||
|
32 | var w = IPython.notification_area.get_widget(name); | |||
|
33 | return w.get_message(); | |||
|
34 | }, name); | |||
|
35 | }; | |||
|
36 | ||||
|
37 | this.then(function () { | |||
|
38 | // check that existing widgets are there | |||
|
39 | this.test.assert(get_widget('kernel') && widget('kernel'), 'The kernel notification widget exists'); | |||
|
40 | this.test.assert(get_widget('notebook') && widget('notbook'), 'The notebook notification widget exists'); | |||
|
41 | ||||
|
42 | // try getting a non-existant widget | |||
|
43 | this.test.assertRaises(get_widget, 'foo', 'get_widget: error is thrown'); | |||
|
44 | ||||
|
45 | // try creating a non-existant widget | |||
|
46 | this.test.assert(widget('bar'), 'widget: new widget is created'); | |||
|
47 | ||||
|
48 | // try creating a widget that already exists | |||
|
49 | this.test.assertRaises(new_notification_widget, 'kernel', 'new_notification_widget: error is thrown'); | |||
|
50 | }); | |||
|
51 | ||||
|
52 | // test creating 'info' messages | |||
|
53 | this.thenEvaluate(function () { | |||
|
54 | var tnw = IPython.notification_area.widget('test'); | |||
|
55 | tnw.info('test info'); | |||
|
56 | }); | |||
|
57 | this.waitUntilVisible('#notification_test', function () { | |||
|
58 | this.test.assert(widget_has_class('test', 'info'), 'info: class is correct'); | |||
|
59 | this.test.assertEquals(widget_message('test'), 'test info', 'info: message is correct'); | |||
|
60 | }); | |||
|
61 | ||||
|
62 | // test creating 'warning' messages | |||
|
63 | this.thenEvaluate(function () { | |||
|
64 | var tnw = IPython.notification_area.widget('test'); | |||
|
65 | tnw.warning('test warning'); | |||
|
66 | }); | |||
|
67 | this.waitUntilVisible('#notification_test', function () { | |||
|
68 | this.test.assert(widget_has_class('test', 'warning'), 'warning: class is correct'); | |||
|
69 | this.test.assertEquals(widget_message('test'), 'test warning', 'warning: message is correct'); | |||
|
70 | }); | |||
|
71 | ||||
|
72 | // test creating 'danger' messages | |||
|
73 | this.thenEvaluate(function () { | |||
|
74 | var tnw = IPython.notification_area.widget('test'); | |||
|
75 | tnw.danger('test danger'); | |||
|
76 | }); | |||
|
77 | this.waitUntilVisible('#notification_test', function () { | |||
|
78 | this.test.assert(widget_has_class('test', 'danger'), 'danger: class is correct'); | |||
|
79 | this.test.assertEquals(widget_message('test'), 'test danger', 'danger: message is correct'); | |||
|
80 | }); | |||
|
81 | ||||
|
82 | // test message timeout | |||
|
83 | this.thenEvaluate(function () { | |||
|
84 | var tnw = IPython.notification_area.widget('test'); | |||
|
85 | tnw.set_message('test timeout', 1000); | |||
|
86 | }); | |||
|
87 | this.waitUntilVisible('#notification_test', function () { | |||
|
88 | this.test.assertEquals(widget_message('test'), 'test timeout', 'timeout: message is correct'); | |||
|
89 | }); | |||
|
90 | this.waitWhileVisible('#notification_test', function () { | |||
|
91 | this.test.assertEquals(widget_message('test'), '', 'timeout: message was cleared'); | |||
|
92 | }); | |||
|
93 | ||||
|
94 | // test click callback | |||
|
95 | this.thenEvaluate(function () { | |||
|
96 | var tnw = IPython.notification_area.widget('test'); | |||
|
97 | tnw._clicked = false; | |||
|
98 | tnw.set_message('test click', undefined, function () { | |||
|
99 | tnw._clicked = true; | |||
|
100 | return true; | |||
|
101 | }); | |||
|
102 | }); | |||
|
103 | this.waitUntilVisible('#notification_test', function () { | |||
|
104 | this.test.assertEquals(widget_message('test'), 'test click', 'callback: message is correct'); | |||
|
105 | this.click('#notification_test'); | |||
|
106 | }); | |||
|
107 | this.waitFor(function () { | |||
|
108 | return this.evaluate(function () { | |||
|
109 | return IPython.notification_area.widget('test')._clicked; | |||
|
110 | }); | |||
|
111 | }, function () { | |||
|
112 | this.waitWhileVisible('#notification_test', function () { | |||
|
113 | this.test.assertEquals(widget_message('test'), '', 'callback: message was cleared'); | |||
|
114 | }); | |||
|
115 | }); | |||
|
116 | }); |
@@ -11,16 +11,22 b' define([' | |||||
11 | ], function(IPython, $, utils, dialog, notificationwidget, moment) { |
|
11 | ], function(IPython, $, utils, dialog, notificationwidget, moment) { | |
12 | "use strict"; |
|
12 | "use strict"; | |
13 |
|
13 | |||
|
14 | // store reference to the NotificationWidget class | |||
|
15 | var NotificationWidget = notificationwidget.NotificationWidget; | |||
|
16 | ||||
|
17 | /** | |||
|
18 | * Construct the NotificationArea object. Options are: | |||
|
19 | * events: $(Events) instance | |||
|
20 | * save_widget: SaveWidget instance | |||
|
21 | * notebook: Notebook instance | |||
|
22 | * keyboard_manager: KeyboardManager instance | |||
|
23 | * | |||
|
24 | * @constructor | |||
|
25 | * @param {string} selector - a jQuery selector string for the | |||
|
26 | * notification area element | |||
|
27 | * @param {Object} [options] - a dictionary of keyword arguments. | |||
|
28 | */ | |||
14 | var NotificationArea = function (selector, options) { |
|
29 | var NotificationArea = function (selector, options) { | |
15 | // Constructor |
|
|||
16 | // |
|
|||
17 | // Parameters: |
|
|||
18 | // selector: string |
|
|||
19 | // options: dictionary |
|
|||
20 | // Dictionary of keyword arguments. |
|
|||
21 | // notebook: Notebook instance |
|
|||
22 | // events: $(Events) instance |
|
|||
23 | // save_widget: SaveWidget instance |
|
|||
24 | this.selector = selector; |
|
30 | this.selector = selector; | |
25 | this.events = options.events; |
|
31 | this.events = options.events; | |
26 | this.save_widget = options.save_widget; |
|
32 | this.save_widget = options.save_widget; | |
@@ -32,47 +38,70 b' define([' | |||||
32 | this.widget_dict = {}; |
|
38 | this.widget_dict = {}; | |
33 | }; |
|
39 | }; | |
34 |
|
40 | |||
35 | NotificationArea.prototype.temp_message = function (msg, timeout, css_class) { |
|
41 | /** | |
36 | var tdiv = $('<div>') |
|
42 | * Get a widget by name, creating it if it doesn't exist. | |
37 | .addClass('notification_widget') |
|
43 | * | |
38 | .addClass(css_class) |
|
44 | * @method widget | |
39 | .hide() |
|
45 | * @param {string} name - the widget name | |
40 | .text(msg); |
|
46 | */ | |
41 |
|
47 | NotificationArea.prototype.widget = function (name) { | ||
42 | $(this.selector).append(tdiv); |
|
48 | if (this.widget_dict[name] === undefined) { | |
43 | var tmout = Math.max(1500,(timeout||1500)); |
|
|||
44 | tdiv.fadeIn(100); |
|
|||
45 |
|
||||
46 | setTimeout(function () { |
|
|||
47 | tdiv.fadeOut(100, function () {tdiv.remove();}); |
|
|||
48 | }, tmout); |
|
|||
49 | }; |
|
|||
50 |
|
||||
51 | NotificationArea.prototype.widget = function(name) { |
|
|||
52 | if(this.widget_dict[name] === undefined) { |
|
|||
53 | return this.new_notification_widget(name); |
|
49 | return this.new_notification_widget(name); | |
54 | } |
|
50 | } | |
55 | return this.get_widget(name); |
|
51 | return this.get_widget(name); | |
56 | }; |
|
52 | }; | |
57 |
|
53 | |||
58 | NotificationArea.prototype.get_widget = function(name) { |
|
54 | /** | |
|
55 | * Get a widget by name, throwing an error if it doesn't exist. | |||
|
56 | * | |||
|
57 | * @method get_widget | |||
|
58 | * @param {string} name - the widget name | |||
|
59 | */ | |||
|
60 | NotificationArea.prototype.get_widget = function (name) { | |||
59 | if(this.widget_dict[name] === undefined) { |
|
61 | if(this.widget_dict[name] === undefined) { | |
60 | throw('no widgets with this name'); |
|
62 | throw('no widgets with this name'); | |
61 | } |
|
63 | } | |
62 | return this.widget_dict[name]; |
|
64 | return this.widget_dict[name]; | |
63 | }; |
|
65 | }; | |
64 |
|
66 | |||
65 | NotificationArea.prototype.new_notification_widget = function(name) { |
|
67 | /** | |
66 | if(this.widget_dict[name] !== undefined) { |
|
68 | * Create a new notification widget with the given name. The | |
67 |
|
|
69 | * widget must not already exist. | |
|
70 | * | |||
|
71 | * @method new_notification_widget | |||
|
72 | * @param {string} name - the widget name | |||
|
73 | */ | |||
|
74 | NotificationArea.prototype.new_notification_widget = function (name) { | |||
|
75 | if (this.widget_dict[name] !== undefined) { | |||
|
76 | throw('widget with that name already exists!'); | |||
68 | } |
|
77 | } | |
69 | var div = $('<div/>').attr('id','notification_'+name); |
|
78 | ||
|
79 | // create the element for the notification widget and add it | |||
|
80 | // to the notification aread element | |||
|
81 | var div = $('<div/>').attr('id', 'notification_' + name); | |||
70 | $(this.selector).append(div); |
|
82 | $(this.selector).append(div); | |
71 | this.widget_dict[name] = new notificationwidget.NotificationWidget('#notification_'+name); |
|
83 | ||
|
84 | // create the widget object and return it | |||
|
85 | this.widget_dict[name] = new NotificationWidget('#notification_' + name); | |||
72 | return this.widget_dict[name]; |
|
86 | return this.widget_dict[name]; | |
73 | }; |
|
87 | }; | |
74 |
|
88 | |||
75 | NotificationArea.prototype.init_notification_widgets = function() { |
|
89 | /** | |
|
90 | * Initialize the default set of notification widgets. | |||
|
91 | * | |||
|
92 | * @method init_notification_widgets | |||
|
93 | */ | |||
|
94 | NotificationArea.prototype.init_notification_widgets = function () { | |||
|
95 | this.init_kernel_notification_widget(); | |||
|
96 | this.init_notebook_notification_widget(); | |||
|
97 | }; | |||
|
98 | ||||
|
99 | /** | |||
|
100 | * Initialize the notification widget for kernel status messages. | |||
|
101 | * | |||
|
102 | * @method init_kernel_notification_widget | |||
|
103 | */ | |||
|
104 | NotificationArea.prototype.init_kernel_notification_widget = function () { | |||
76 | var that = this; |
|
105 | var that = this; | |
77 | var knw = this.new_notification_widget('kernel'); |
|
106 | var knw = this.new_notification_widget('kernel'); | |
78 | var $kernel_ind_icon = $("#kernel_indicator_icon"); |
|
107 | var $kernel_ind_icon = $("#kernel_indicator_icon"); | |
@@ -194,8 +223,14 b' define([' | |||||
194 | } |
|
223 | } | |
195 | }); |
|
224 | }); | |
196 | }); |
|
225 | }); | |
|
226 | }; | |||
197 |
|
227 | |||
198 |
|
228 | /** | ||
|
229 | * Initialize the notification widget for notebook status messages. | |||
|
230 | * | |||
|
231 | * @method init_notebook_notification_widget | |||
|
232 | */ | |||
|
233 | NotificationArea.prototype.init_notebook_notification_widget = function () { | |||
199 | var nnw = this.new_notification_widget('notebook'); |
|
234 | var nnw = this.new_notification_widget('notebook'); | |
200 |
|
235 | |||
201 | // Notebook events |
|
236 | // Notebook events | |
@@ -247,7 +282,6 b' define([' | |||||
247 | this.events.on('autosave_enabled.Notebook', function (evt, interval) { |
|
282 | this.events.on('autosave_enabled.Notebook', function (evt, interval) { | |
248 | nnw.set_message("Saving every " + interval / 1000 + "s", 1000); |
|
283 | nnw.set_message("Saving every " + interval / 1000 + "s", 1000); | |
249 | }); |
|
284 | }); | |
250 |
|
||||
251 | }; |
|
285 | }; | |
252 |
|
286 | |||
253 | IPython.NotificationArea = NotificationArea; |
|
287 | IPython.NotificationArea = NotificationArea; |
@@ -7,6 +7,13 b' define([' | |||||
7 | ], function(IPython, $) { |
|
7 | ], function(IPython, $) { | |
8 | "use strict"; |
|
8 | "use strict"; | |
9 |
|
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 | */ | |||
10 | var NotificationWidget = function (selector) { |
|
17 | var NotificationWidget = function (selector) { | |
11 | this.selector = selector; |
|
18 | this.selector = selector; | |
12 | this.timeout = null; |
|
19 | this.timeout = null; | |
@@ -16,27 +23,41 b' define([' | |||||
16 | this.style(); |
|
23 | this.style(); | |
17 | } |
|
24 | } | |
18 | this.element.hide(); |
|
25 | this.element.hide(); | |
19 | var that = this; |
|
|||
20 |
|
||||
21 | this.inner = $('<span/>'); |
|
26 | this.inner = $('<span/>'); | |
22 | this.element.append(this.inner); |
|
27 | this.element.append(this.inner); | |
23 |
|
||||
24 | }; |
|
28 | }; | |
25 |
|
29 | |||
|
30 | /** | |||
|
31 | * Add the 'notification_widget' CSS class to the widget element. | |||
|
32 | * | |||
|
33 | * @method style | |||
|
34 | */ | |||
26 | NotificationWidget.prototype.style = function () { |
|
35 | NotificationWidget.prototype.style = function () { | |
27 | this.element.addClass('notification_widget'); |
|
36 | this.element.addClass('notification_widget'); | |
28 | }; |
|
37 | }; | |
29 |
|
38 | |||
30 | // msg : message to display |
|
39 | /** | |
31 | // timeout : time in ms before diseapearing |
|
40 | * Set the notification widget message to display for a certain | |
32 | // |
|
41 | * amount of time (timeout). The widget will be shown forever if | |
33 | // if timeout <= 0 |
|
42 | * timeout is <= 0 or undefined. If the widget is clicked while it | |
34 | // click_callback : function called if user click on notification |
|
43 | * is still displayed, execute an optional callback | |
35 | // could return false to prevent the notification to be dismissed |
|
44 | * (click_callback). If the callback returns false, it will | |
|
45 | * prevent the notification from being dismissed. | |||
|
46 | * | |||
|
47 | * Options: | |||
|
48 | * class - CSS class name for styling | |||
|
49 | * icon - CSS class name for the widget icon | |||
|
50 | * title - HTML title attribute for the widget | |||
|
51 | * | |||
|
52 | * @method set_message | |||
|
53 | * @param {string} msg - The notification to display | |||
|
54 | * @param {integer} [timeout] - The amount of time in milliseconds to display the widget | |||
|
55 | * @param {function} [click_callback] - The function to run when the widget is clicked | |||
|
56 | * @param {Object} [options] - Additional options | |||
|
57 | */ | |||
36 | NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) { |
|
58 | NotificationWidget.prototype.set_message = function (msg, timeout, click_callback, options) { | |
37 |
|
|
59 | options = options || {}; | |
38 | var callback = click_callback || function() {return true;}; |
|
60 | ||
39 | var that = this; |
|
|||
40 | // unbind potential previous callback |
|
61 | // unbind potential previous callback | |
41 | this.element.unbind('click'); |
|
62 | this.element.unbind('click'); | |
42 | this.inner.attr('class', options.icon); |
|
63 | this.inner.attr('class', options.icon); | |
@@ -47,52 +68,87 b' define([' | |||||
47 | // reset previous set style |
|
68 | // reset previous set style | |
48 | this.element.removeClass(); |
|
69 | this.element.removeClass(); | |
49 | this.style(); |
|
70 | this.style(); | |
50 | if (options.class){ |
|
71 | if (options.class) { | |
51 |
|
72 | this.element.addClass(options.class); | ||
52 | this.element.addClass(options.class) |
|
|||
53 | } |
|
73 | } | |
|
74 | ||||
|
75 | // clear previous timer | |||
54 | if (this.timeout !== null) { |
|
76 | if (this.timeout !== null) { | |
55 | clearTimeout(this.timeout); |
|
77 | clearTimeout(this.timeout); | |
56 | this.timeout = null; |
|
78 | this.timeout = null; | |
57 | } |
|
79 | } | |
58 | if (timeout !== undefined && timeout >=0) { |
|
80 | ||
|
81 | // set the timer if a timeout is given | |||
|
82 | var that = this; | |||
|
83 | if (timeout !== undefined && timeout >= 0) { | |||
59 | this.timeout = setTimeout(function () { |
|
84 | this.timeout = setTimeout(function () { | |
60 | that.element.fadeOut(100, function () {that.inner.text('');}); |
|
85 | that.element.fadeOut(100, function () {that.inner.text('');}); | |
|
86 | that.element.unbind('click'); | |||
61 | that.timeout = null; |
|
87 | that.timeout = null; | |
62 | }, timeout); |
|
88 | }, timeout); | |
63 |
} |
|
89 | } | |
64 | this.element.click(function() { |
|
90 | ||
65 | if( callback() !== false ) { |
|
91 | // bind the click callback if it is given | |
|
92 | if (click_callback !== undefined) { | |||
|
93 | this.element.click(function () { | |||
|
94 | if (click_callback() !== false) { | |||
66 | that.element.fadeOut(100, function () {that.inner.text('');}); |
|
95 | that.element.fadeOut(100, function () {that.inner.text('');}); | |
67 | that.element.unbind('click'); |
|
|||
68 | } |
|
96 | } | |
69 |
|
|
97 | that.element.unbind('click'); | |
70 |
|
|
98 | if (that.timeout !== null) { | |
71 | clearTimeout(that.timeout); |
|
99 | clearTimeout(that.timeout); | |
|
100 | that.timeout = null; | |||
72 | } |
|
101 | } | |
73 | }); |
|
102 | }); | |
74 | } |
|
103 | } | |
75 | }; |
|
104 | }; | |
76 |
|
105 | |||
77 |
|
106 | /** | ||
|
107 | * Display an information message (styled with the 'info' | |||
|
108 | * class). Arguments are the same as in set_message. Default | |||
|
109 | * timeout is 3500 milliseconds. | |||
|
110 | * | |||
|
111 | * @method info | |||
|
112 | */ | |||
78 | NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) { |
|
113 | NotificationWidget.prototype.info = function (msg, timeout, click_callback, options) { | |
79 |
|
|
114 | options = options || {}; | |
80 | options.class = options.class +' info'; |
|
115 | options.class = options.class + ' info'; | |
81 |
|
|
116 | timeout = timeout || 3500; | |
82 | this.set_message(msg, timeout, click_callback, options); |
|
117 | this.set_message(msg, timeout, click_callback, options); | |
83 | } |
|
118 | }; | |
|
119 | ||||
|
120 | /** | |||
|
121 | * Display a warning message (styled with the 'warning' | |||
|
122 | * class). Arguments are the same as in set_message. Messages are | |||
|
123 | * sticky by default. | |||
|
124 | * | |||
|
125 | * @method warning | |||
|
126 | */ | |||
84 | NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) { |
|
127 | NotificationWidget.prototype.warning = function (msg, timeout, click_callback, options) { | |
85 |
|
|
128 | options = options || {}; | |
86 | options.class = options.class +' warning'; |
|
129 | options.class = options.class + ' warning'; | |
87 | this.set_message(msg, timeout, click_callback, options); |
|
130 | this.set_message(msg, timeout, click_callback, options); | |
88 | } |
|
131 | }; | |
|
132 | ||||
|
133 | /** | |||
|
134 | * Display a danger message (styled with the 'danger' | |||
|
135 | * class). Arguments are the same as in set_message. Messages are | |||
|
136 | * sticky by default. | |||
|
137 | * | |||
|
138 | * @method danger | |||
|
139 | */ | |||
89 | NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) { |
|
140 | NotificationWidget.prototype.danger = function (msg, timeout, click_callback, options) { | |
90 |
|
|
141 | options = options || {}; | |
91 | options.class = options.class +' danger'; |
|
142 | options.class = options.class + ' danger'; | |
92 | this.set_message(msg, timeout, click_callback, options); |
|
143 | this.set_message(msg, timeout, click_callback, options); | |
93 | } |
|
144 | }; | |
94 |
|
||||
95 |
|
145 | |||
|
146 | /** | |||
|
147 | * Get the text of the widget message. | |||
|
148 | * | |||
|
149 | * @method get_message | |||
|
150 | * @return {string} - the message text | |||
|
151 | */ | |||
96 | NotificationWidget.prototype.get_message = function () { |
|
152 | NotificationWidget.prototype.get_message = function () { | |
97 | return this.inner.html(); |
|
153 | return this.inner.html(); | |
98 | }; |
|
154 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now