##// END OF EJS Templates
Saving files works
Thomas Kluyver -
Show More
@@ -0,0 +1,70 b''
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 define([
5 'base/js/namespace',
6 'jquery',
7 'base/js/utils',
8 'bootstrap',
9 ], function(IPython, $, utils, bootstrap) {
10 "use strict";
11
12 var MenuBar = function (selector, options) {
13 // Constructor
14 //
15 // A MenuBar Class to generate the menubar of IPython notebook
16 //
17 // Parameters:
18 // selector: string
19 // options: dictionary
20 // Dictionary of keyword arguments.
21 // codemirror: CodeMirror instance
22 // contents: ContentManager instance
23 // events: $(Events) instance
24 // base_url : string
25 // file_path : string
26 options = options || {};
27 this.base_url = options.base_url || utils.get_body_data("baseUrl");
28 this.selector = selector;
29 this.codemirror = options.codemirror;
30 this.contents = options.contents;
31 this.events = options.events;
32 this.file_path = options.file_path;
33
34 if (this.selector !== undefined) {
35 this.element = $(selector);
36 this.bind_events();
37 }
38 };
39
40 MenuBar.prototype.bind_events = function () {
41 // File
42 var that = this;
43 this.element.find('#save_file').click(function () {
44 var ix = that.file_path.lastIndexOf("/");
45 var dir_path, basename;
46 if (ix === -1) {
47 dir_path = '';
48 basename = that.file_path;
49 } else {
50 dir_path = that.file_path.substring(0, ix);
51 basename = that.file_path.substring(ix+1);
52 }
53 var model = {
54 path: dir_path,
55 name: basename,
56 type: 'file',
57 format: 'text',
58 content: that.codemirror.getValue(),
59 };
60 console.log(model);
61 that.contents.save(dir_path, basename, model, {
62 success: function() {
63 that.events.trigger("save_succeeded.TextEditor");
64 }
65 });
66 });
67 };
68
69 return {'MenuBar': MenuBar};
70 });
@@ -1,60 +1,72 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 require([
5 5 'jquery',
6 6 'base/js/utils',
7 7 'base/js/page',
8 'base/js/events',
8 9 'contents',
9 10 'codemirror/lib/codemirror',
11 'texteditor/js/menubar',
10 12 'codemirror/mode/meta',
11 13 'custom/custom',
12 14 ], function(
13 $,
15 $,
14 16 utils,
15 17 page,
18 events,
16 19 contents,
17 CodeMirror
20 CodeMirror,
21 menubar
18 22 ){
19 23 page = new page.Page();
20 24
21 25 var base_url = utils.get_body_data('baseUrl');
22 26 contents = new contents.Contents({base_url: base_url});
23 27
24 28 var file_path = utils.get_body_data('filePath');
25 29 var ix = file_path.lastIndexOf("/");
26 30 var dir_path, basename;
27 31 if (ix == -1) {
28 32 dir_path = '';
29 33 basename = file_path;
30 34 } else {
31 35 dir_path = file_path.substring(0, ix);
32 basename = file_path.substring(ix);
36 basename = file_path.substring(ix+1);
33 37 }
34 38 contents.load(dir_path, basename, {
35 39 success: function(model) {
36 40 page.show();
37 41 if (model.type === "file" && model.format === "text") {
38 42 console.log(modeinfo);
39 43 var cm = CodeMirror($('#texteditor-container')[0], {
40 44 value: model.content,
41 45 });
42 46
47 var menus = new menubar.MenuBar('#menubar', {
48 base_url: base_url,
49 codemirror: cm,
50 contents: contents,
51 events: events,
52 file_path: file_path
53 });
54
43 55 // Find and load the highlighting mode
44 56 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
45 57 if (modeinfo) {
46 58 utils.requireCodeMirrorMode(modeinfo.mode, function() {
47 59 cm.setOption('mode', modeinfo.mode);
48 60 });
49 61 }
50 62
51 63 // Attach to document for debugging
52 64 document.cm_instance = cm;
53 65 } else {
54 66 $('#texteditor-container').append(
55 67 $('<p/>').text(dir_path + " is not a text file")
56 68 );
57 69 }
58 70 }
59 71 });
60 72 });
@@ -1,29 +1,60 b''
1 1 {% extends "page.html" %}
2 2
3 3 {% block title %}{{page_title}}{% endblock %}
4 4
5 5 {% block stylesheet %}
6 6 <link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}">
7 7
8 8 {{super()}}
9 9 {% endblock %}
10 10
11 11 {% block params %}
12 12
13 13 data-base-url="{{base_url}}"
14 14 data-file-path="{{file_path}}"
15 15
16 16 {% endblock %}
17 17
18 18 {% block site %}
19 19
20 <div id="menubar-container" class="container">
21 <div id="menubar">
22 <div id="menus" class="navbar navbar-default" role="navigation">
23 <div class="container-fluid">
24 <button type="button" class="btn btn-default navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
25 <i class="fa fa-bars"></i>
26 <span class="navbar-text">Menu</span>
27 </button>
28 <ul class="nav navbar-nav navbar-right">
29 <li id="kernel_indicator">
30 <i id="kernel_indicator_icon"></i>
31 </li>
32 <li id="modal_indicator">
33 <i id="modal_indicator_icon"></i>
34 </li>
35 <li id="notification_area"></li>
36 </ul>
37 <div class="navbar-collapse collapse">
38 <ul class="nav navbar-nav">
39 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">File</a>
40 <ul id="file_menu" class="dropdown-menu">
41 <li id="save_file"><a href="#">Save</a></li>
42 </ul>
43 </li>
44 </ul>
45 </div>
46 </div>
47 </div>
48 </div>
49 </div>
50
20 51 <div id="texteditor-container"></div>
21 52
22 53 {% endblock %}
23 54
24 55 {% block script %}
25 56
26 57 {{super()}}
27 58
28 59 <script src="{{ static_url("texteditor/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
29 60 {% endblock %}
General Comments 0
You need to be logged in to leave comments. Login now