sessionlist.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. define([
  4. 'jquery',
  5. 'base/js/utils',
  6. 'bidi/bidi',
  7. ], function($, utils, bidi) {
  8. "use strict";
  9. var isRTL = bidi.isMirroringEnabled();
  10. var SesssionList = function (options) {
  11. /**
  12. * Constructor
  13. *
  14. * Parameters:
  15. * options: dictionary
  16. * Dictionary of keyword arguments.
  17. * events: $(Events) instance
  18. * base_url : string
  19. */
  20. this.events = options.events;
  21. this.sessions = {};
  22. this.base_url = options.base_url || utils.get_body_data("baseUrl");
  23. // Add collapse arrows.
  24. $('#running .panel-group .panel .panel-heading a').each(function(index, el) {
  25. var $link = $(el);
  26. var $icon = $('<i />')
  27. .addClass('fa fa-caret-down');
  28. $link.append($icon);
  29. $link.down = true;
  30. $link.click(function () {
  31. if ($link.down) {
  32. $link.down = false;
  33. // jQeury doesn't know how to animate rotations. Abuse
  34. // jQueries animate function by using an unused css attribute
  35. // to do the animation (borderSpacing).
  36. $icon.animate({ borderSpacing: 90 }, {
  37. step: function(now,fx) {
  38. isRTL ? $icon.css('transform','rotate(' + now + 'deg)') : $icon.css('transform','rotate(-' + now + 'deg)');
  39. }
  40. }, 250);
  41. } else {
  42. $link.down = true;
  43. // See comment above.
  44. $icon.animate({ borderSpacing: 0 }, {
  45. step: function(now,fx) {
  46. isRTL ? $icon.css('transform','rotate(' + now + 'deg)') : $icon.css('transform','rotate(-' + now + 'deg)');
  47. }
  48. }, 250);
  49. }
  50. });
  51. });
  52. };
  53. SesssionList.prototype.load_sessions = function(){
  54. var that = this;
  55. var settings = {
  56. processData : false,
  57. cache : false,
  58. type : "GET",
  59. dataType : "json",
  60. success : $.proxy(that.sessions_loaded, this),
  61. error : utils.log_ajax_error,
  62. };
  63. var url = utils.url_path_join(this.base_url, 'api/sessions');
  64. utils.ajax(url, settings);
  65. };
  66. SesssionList.prototype.sessions_loaded = function(data){
  67. this.sessions = {};
  68. var len = data.length;
  69. var nb_path;
  70. for (var i=0; i<len; i++) {
  71. // The classic notebook only knows about sessions for notebooks,
  72. // but the server now knows about more general sessions for
  73. // things like consoles.
  74. if (data[i].type === 'notebook') {
  75. nb_path = data[i].notebook.path;
  76. this.sessions[nb_path] = {
  77. id: data[i].id,
  78. kernel: {
  79. name: data[i].kernel.name
  80. }
  81. };
  82. }
  83. }
  84. this.events.trigger('sessions_loaded.Dashboard', this.sessions);
  85. };
  86. return {'SesssionList': SesssionList};
  87. });