notificationarea.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. define([
  4. 'jquery',
  5. 'base/js/notificationwidget',
  6. ], function($, notificationwidget) {
  7. "use strict";
  8. // store reference to the NotificationWidget class
  9. var NotificationWidget = notificationwidget.NotificationWidget;
  10. /**
  11. * Construct the NotificationArea object. Options are:
  12. * events: $(Events) instance
  13. * save_widget: SaveWidget instance
  14. * notebook: Notebook instance
  15. * keyboard_manager: KeyboardManager instance
  16. *
  17. * @constructor
  18. * @param {string} selector - a jQuery selector string for the
  19. * notification area element
  20. * @param {Object} [options] - a dictionary of keyword arguments.
  21. */
  22. var NotificationArea = function (selector, options) {
  23. this.selector = selector;
  24. this.events = options.events;
  25. if (this.selector !== undefined) {
  26. this.element = $(selector);
  27. }
  28. this.widget_dict = {};
  29. };
  30. /**
  31. * Get a widget by name, creating it if it doesn't exist.
  32. *
  33. * @method widget
  34. * @param {string} name - the widget name
  35. */
  36. NotificationArea.prototype.widget = function (name) {
  37. if (this.widget_dict[name] === undefined) {
  38. return this.new_notification_widget(name);
  39. }
  40. return this.get_widget(name);
  41. };
  42. /**
  43. * Get a widget by name, throwing an error if it doesn't exist.
  44. *
  45. * @method get_widget
  46. * @param {string} name - the widget name
  47. */
  48. NotificationArea.prototype.get_widget = function (name) {
  49. if(this.widget_dict[name] === undefined) {
  50. throw new Error('no widgets with this name');
  51. }
  52. return this.widget_dict[name];
  53. };
  54. /**
  55. * Create a new notification widget with the given name. The
  56. * widget must not already exist.
  57. *
  58. * @method new_notification_widget
  59. * @param {string} name - the widget name
  60. */
  61. NotificationArea.prototype.new_notification_widget = function (name) {
  62. if (this.widget_dict[name] !== undefined) {
  63. throw new Error('widget with that name already exists!');
  64. }
  65. // create the element for the notification widget and add it
  66. // to the notification aread element
  67. var div = $('<div/>').attr('id', 'notification_' + name);
  68. $(this.selector).append(div);
  69. // create the widget object and return it
  70. this.widget_dict[name] = new NotificationWidget('#notification_' + name);
  71. return this.widget_dict[name];
  72. };
  73. return {'NotificationArea': NotificationArea};
  74. });