newnotebook.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. define([
  4. 'jquery',
  5. 'base/js/namespace',
  6. 'base/js/utils',
  7. 'base/js/i18n',
  8. 'base/js/dialog',
  9. ], function ($, IPython, utils, i18n, dialog) {
  10. "use strict";
  11. var NewNotebookWidget = function (selector, options) {
  12. this.selector = selector;
  13. this.base_url = options.base_url;
  14. this.contents = options.contents;
  15. this.events = options.events;
  16. this.default_kernel = null;
  17. this.kernelspecs = {};
  18. if (this.selector !== undefined) {
  19. this.element = $(selector);
  20. this.request_kernelspecs();
  21. }
  22. this.bind_events();
  23. };
  24. NewNotebookWidget.prototype.bind_events = function () {
  25. var that = this;
  26. this.element.find('#new_notebook').click(function () {
  27. that.new_notebook();
  28. });
  29. };
  30. NewNotebookWidget.prototype.request_kernelspecs = function () {
  31. /** request and then load kernel specs */
  32. var url = utils.url_path_join(this.base_url, 'api/kernelspecs');
  33. utils.promising_ajax(url).then($.proxy(this._load_kernelspecs, this));
  34. };
  35. NewNotebookWidget.prototype._load_kernelspecs = function (data) {
  36. /** load kernelspec list */
  37. var that = this;
  38. this.kernelspecs = data.kernelspecs;
  39. var menu = this.element.find("#notebook-kernels");
  40. var keys = Object.keys(data.kernelspecs).sort(function (a, b) {
  41. var da = data.kernelspecs[a].spec.display_name;
  42. var db = data.kernelspecs[b].spec.display_name;
  43. if (da === db) {
  44. return 0;
  45. } else if (da > db) {
  46. return 1;
  47. } else {
  48. return -1;
  49. }
  50. });
  51. // Create the kernel list in reverse order because
  52. // the .after insertion causes each item to be added
  53. // to the top of the list.
  54. for (var i = keys.length - 1; i >= 0; i--) {
  55. var ks = this.kernelspecs[keys[i]];
  56. var li = $("<li>")
  57. .attr("id", "kernel-" +ks.name)
  58. .data('kernelspec', ks).append(
  59. $('<a>')
  60. .attr('href', '#')
  61. .click($.proxy(this.new_notebook, this, ks.name))
  62. .text(ks.spec.display_name)
  63. .attr('title', i18n.sprintf(i18n._('Create a new notebook with %s'), ks.spec.display_name))
  64. );
  65. menu.after(li);
  66. }
  67. this.events.trigger('kernelspecs_loaded.KernelSpec', data.kernelspecs);
  68. };
  69. NewNotebookWidget.prototype.new_notebook = function (kernel_name, evt) {
  70. /** create and open a new notebook */
  71. var that = this;
  72. kernel_name = kernel_name || this.default_kernel;
  73. var w = window.open(undefined, IPython._target);
  74. var dir_path = $('body').attr('data-notebook-path');
  75. this.contents.new_untitled(dir_path, {type: "notebook"}).then(
  76. function (data) {
  77. var url = utils.url_path_join(
  78. that.base_url, 'notebooks',
  79. utils.encode_uri_components(data.path)
  80. );
  81. if (kernel_name) {
  82. url += "?kernel_name=" + kernel_name;
  83. }
  84. w.location = url;
  85. }).catch(function (e) {
  86. w.close();
  87. // This statement is used simply so that message extraction
  88. // will pick up the strings. The actual setting of the text
  89. // for the button is in dialog.js.
  90. var button_labels = [ i18n._("OK")];
  91. dialog.modal({
  92. title : i18n._('Creating Notebook Failed'),
  93. body : $('<div/>')
  94. .text(i18n._("An error occurred while creating a new notebook."))
  95. .append($('<div/>')
  96. .addClass('alert alert-danger')
  97. .text(e.message || e)),
  98. buttons: {
  99. OK: {'class' : 'btn-primary'}
  100. }
  101. });
  102. });
  103. if (evt !== undefined) {
  104. evt.preventDefault();
  105. }
  106. };
  107. return {'NewNotebookWidget': NewNotebookWidget};
  108. });