##// 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 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 require([
4 require([
5 'jquery',
5 'jquery',
6 'base/js/utils',
6 'base/js/utils',
7 'base/js/page',
7 'base/js/page',
8 'base/js/events',
8 'contents',
9 'contents',
9 'codemirror/lib/codemirror',
10 'codemirror/lib/codemirror',
11 'texteditor/js/menubar',
10 'codemirror/mode/meta',
12 'codemirror/mode/meta',
11 'custom/custom',
13 'custom/custom',
12 ], function(
14 ], function(
13 $,
15 $,
14 utils,
16 utils,
15 page,
17 page,
18 events,
16 contents,
19 contents,
17 CodeMirror
20 CodeMirror,
21 menubar
18 ){
22 ){
19 page = new page.Page();
23 page = new page.Page();
20
24
21 var base_url = utils.get_body_data('baseUrl');
25 var base_url = utils.get_body_data('baseUrl');
22 contents = new contents.Contents({base_url: base_url});
26 contents = new contents.Contents({base_url: base_url});
23
27
24 var file_path = utils.get_body_data('filePath');
28 var file_path = utils.get_body_data('filePath');
25 var ix = file_path.lastIndexOf("/");
29 var ix = file_path.lastIndexOf("/");
26 var dir_path, basename;
30 var dir_path, basename;
27 if (ix == -1) {
31 if (ix == -1) {
28 dir_path = '';
32 dir_path = '';
29 basename = file_path;
33 basename = file_path;
30 } else {
34 } else {
31 dir_path = file_path.substring(0, ix);
35 dir_path = file_path.substring(0, ix);
32 basename = file_path.substring(ix);
36 basename = file_path.substring(ix+1);
33 }
37 }
34 contents.load(dir_path, basename, {
38 contents.load(dir_path, basename, {
35 success: function(model) {
39 success: function(model) {
36 page.show();
40 page.show();
37 if (model.type === "file" && model.format === "text") {
41 if (model.type === "file" && model.format === "text") {
38 console.log(modeinfo);
42 console.log(modeinfo);
39 var cm = CodeMirror($('#texteditor-container')[0], {
43 var cm = CodeMirror($('#texteditor-container')[0], {
40 value: model.content,
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 // Find and load the highlighting mode
55 // Find and load the highlighting mode
44 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
56 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
45 if (modeinfo) {
57 if (modeinfo) {
46 utils.requireCodeMirrorMode(modeinfo.mode, function() {
58 utils.requireCodeMirrorMode(modeinfo.mode, function() {
47 cm.setOption('mode', modeinfo.mode);
59 cm.setOption('mode', modeinfo.mode);
48 });
60 });
49 }
61 }
50
62
51 // Attach to document for debugging
63 // Attach to document for debugging
52 document.cm_instance = cm;
64 document.cm_instance = cm;
53 } else {
65 } else {
54 $('#texteditor-container').append(
66 $('#texteditor-container').append(
55 $('<p/>').text(dir_path + " is not a text file")
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 {% extends "page.html" %}
1 {% extends "page.html" %}
2
2
3 {% block title %}{{page_title}}{% endblock %}
3 {% block title %}{{page_title}}{% endblock %}
4
4
5 {% block stylesheet %}
5 {% block stylesheet %}
6 <link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}">
6 <link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}">
7
7
8 {{super()}}
8 {{super()}}
9 {% endblock %}
9 {% endblock %}
10
10
11 {% block params %}
11 {% block params %}
12
12
13 data-base-url="{{base_url}}"
13 data-base-url="{{base_url}}"
14 data-file-path="{{file_path}}"
14 data-file-path="{{file_path}}"
15
15
16 {% endblock %}
16 {% endblock %}
17
17
18 {% block site %}
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 <div id="texteditor-container"></div>
51 <div id="texteditor-container"></div>
21
52
22 {% endblock %}
53 {% endblock %}
23
54
24 {% block script %}
55 {% block script %}
25
56
26 {{super()}}
57 {{super()}}
27
58
28 <script src="{{ static_url("texteditor/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
59 <script src="{{ static_url("texteditor/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
29 {% endblock %}
60 {% endblock %}
General Comments 0
You need to be logged in to leave comments. Login now