menubar.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/dialog',
  8. 'codemirror/lib/codemirror',
  9. 'codemirror/mode/meta',
  10. ], function($, IPython, utils, dialog, CodeMirror) {
  11. "use strict";
  12. var MenuBar = function (selector, options) {
  13. /**
  14. * Constructor
  15. *
  16. * A MenuBar Class to generate the menubar of IPython notebook
  17. *
  18. * Parameters:
  19. * selector: string
  20. * options: dictionary
  21. * Dictionary of keyword arguments.
  22. * codemirror: CodeMirror instance
  23. * contents: ContentManager instance
  24. * events: $(Events) instance
  25. * base_url : string
  26. * file_path : string
  27. */
  28. options = options || {};
  29. this.base_url = options.base_url || utils.get_body_data("baseUrl");
  30. this.selector = selector;
  31. this.editor = options.editor;
  32. this.events = options.events;
  33. this.save_widget = options.save_widget;
  34. if (this.selector !== undefined) {
  35. this.element = $(selector);
  36. this.bind_events();
  37. }
  38. this._load_mode_menu();
  39. Object.seal(this);
  40. };
  41. MenuBar.prototype.bind_events = function () {
  42. var that = this;
  43. var editor = that.editor;
  44. // File
  45. this.element.find('#new-file').click(function () {
  46. var w = window.open(undefined, IPython._target);
  47. // Create a new file in the current directory
  48. var parent = utils.url_path_split(editor.file_path)[0];
  49. editor.contents.new_untitled(parent, {type: "file"}).then(
  50. function (data) {
  51. w.location = utils.url_path_join(
  52. that.base_url, 'edit', utils.encode_uri_components(data.path)
  53. );
  54. },
  55. function(error) {
  56. w.close();
  57. dialog.modal({
  58. title : 'Creating New File Failed',
  59. body : "The error was: " + error.message,
  60. buttons : {'OK' : {'class' : 'btn-primary'}}
  61. });
  62. }
  63. );
  64. });
  65. this.element.find('#save-file').click(function () {
  66. editor.save();
  67. });
  68. this.element.find('#rename-file').click(function () {
  69. that.save_widget.rename();
  70. });
  71. this.element.find('#download-file').click(function () {
  72. window.open(utils.url_path_join(
  73. that.base_url, 'files',
  74. utils.encode_uri_components(that.editor.file_path)
  75. ) + '?download=1');
  76. });
  77. // Edit
  78. this.element.find('#menu-find').click(function () {
  79. editor.codemirror.execCommand("find");
  80. });
  81. this.element.find('#menu-replace').click(function () {
  82. editor.codemirror.execCommand("replace");
  83. });
  84. this.element.find('#menu-keymap-default').click(function () {
  85. editor.update_codemirror_options({
  86. vimMode: false,
  87. keyMap: 'default'
  88. });
  89. });
  90. this.element.find('#menu-keymap-sublime').click(function () {
  91. editor.update_codemirror_options({
  92. vimMode: false,
  93. keyMap: 'sublime'
  94. });
  95. });
  96. this.element.find('#menu-keymap-emacs').click(function () {
  97. editor.update_codemirror_options({
  98. vimMode: false,
  99. keyMap: 'emacs'
  100. });
  101. });
  102. this.element.find('#menu-keymap-vim').click(function () {
  103. editor.update_codemirror_options({
  104. vimMode: true,
  105. keyMap: 'vim'
  106. });
  107. });
  108. // View
  109. this.element.find('#toggle_header').click(function (){
  110. $("#header-container").toggle();
  111. });
  112. this.element.find('#menu-line-numbers').click(function () {
  113. var current = editor.codemirror.getOption('lineNumbers');
  114. var value = Boolean(1-current);
  115. editor.update_codemirror_options({lineNumbers: value});
  116. });
  117. this.events.on("config_changed.Editor", function () {
  118. var keyMap = editor.codemirror.getOption('keyMap') || 'default';
  119. that.element.find(".selected-keymap").removeClass("selected-keymap");
  120. that.element.find("#menu-keymap-" + keyMap).addClass("selected-keymap");
  121. });
  122. this.events.on("mode_changed.Editor", function (evt, modeinfo) {
  123. that.element.find("#current-mode")
  124. .text(modeinfo.name)
  125. .attr(
  126. 'title',
  127. "The current language is " + modeinfo.name
  128. );
  129. });
  130. };
  131. MenuBar.prototype._load_mode_menu = function () {
  132. var list = this.element.find("#mode-menu");
  133. var editor = this.editor;
  134. function make_set_mode(info) {
  135. return function () {
  136. editor.set_codemirror_mode(info);
  137. // save codemirror mode for extension when explicitly selected
  138. editor.save_codemirror_mode(info);
  139. };
  140. }
  141. for (var i = 0; i < CodeMirror.modeInfo.length; i++) {
  142. var info = CodeMirror.modeInfo[i];
  143. list.append($("<li>").append(
  144. $("<a>").attr("href", "#")
  145. .text(info.name)
  146. .click(make_set_mode(info))
  147. .attr('title',
  148. "Set language to " + info.name
  149. )
  150. ));
  151. }
  152. };
  153. return {'MenuBar': MenuBar};
  154. });