##// END OF EJS Templates
add save widget to text editor
Min RK -
Show More
@@ -0,0 +1,202 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 'base/js/dialog',
9 'base/js/keyboard',
10 'moment',
11 ], function(IPython, $, utils, dialog, keyboard, moment) {
12 "use strict";
13
14 var SaveWidget = function (selector, options) {
15 this.editor = undefined;
16 this.selector = selector;
17 this.events = options.events;
18 this.editor = options.editor;
19 this._last_modified = undefined;
20 this.keyboard_manager = options.keyboard_manager;
21 if (this.selector !== undefined) {
22 this.element = $(selector);
23 this.bind_events();
24 }
25 };
26
27
28 SaveWidget.prototype.bind_events = function () {
29 var that = this;
30 this.element.find('span.filename').click(function () {
31 that.rename({editor: that.editor});
32 });
33 this.events.on('file_loaded.Editor', function (evt, model) {
34 that.update_filename(model.name);
35 that.update_document_title(model.name);
36 that.update_last_modified(model.last_modified);
37 });
38 this.events.on('file_saved.Editor', function (evt, model) {
39 that.update_filename(model.name);
40 that.update_document_title(model.name);
41 that.update_last_modified(model.last_modified);
42 });
43 this.events.on('file_renamed.Editor', function (evt, model) {
44 that.update_filename(model.name);
45 that.update_document_title(model.name);
46 that.update_address_bar(model.path);
47 });
48 this.events.on('file_save_failed.Editor', function () {
49 that.set_save_status('Save Failed!');
50 });
51 };
52
53
54 SaveWidget.prototype.rename = function (options) {
55 options = options || {};
56 var that = this;
57 var dialog_body = $('<div/>').append(
58 $("<p/>").addClass("rename-message")
59 .text('Enter a new filename:')
60 ).append(
61 $("<br/>")
62 ).append(
63 $('<input/>').attr('type','text').attr('size','25').addClass('form-control')
64 .val(options.editor.get_filename())
65 );
66 var d = dialog.modal({
67 title: "Rename File",
68 body: dialog_body,
69 buttons : {
70 "OK": {
71 class: "btn-primary",
72 click: function () {
73 var new_name = d.find('input').val();
74 d.find('.rename-message').text("Renaming...");
75 d.find('input[type="text"]').prop('disabled', true);
76 that.editor.rename(new_name).then(
77 function () {
78 d.modal('hide');
79 }, function (error) {
80 d.find('.rename-message').text(error.message || 'Unknown error');
81 d.find('input[type="text"]').prop('disabled', false).focus().select();
82 }
83 );
84 return false;
85 }
86 },
87 "Cancel": {}
88 },
89 open : function () {
90 // Upon ENTER, click the OK button.
91 d.find('input[type="text"]').keydown(function (event) {
92 if (event.which === keyboard.keycodes.enter) {
93 d.find('.btn-primary').first().click();
94 return false;
95 }
96 });
97 d.find('input[type="text"]').focus().select();
98 }
99 });
100 };
101
102
103 SaveWidget.prototype.update_filename = function (filename) {
104 this.element.find('span.filename').text(filename);
105 };
106
107 SaveWidget.prototype.update_document_title = function (filename) {
108 document.title = filename;
109 };
110
111 SaveWidget.prototype.update_address_bar = function (path) {
112 var state = {path : path};
113 window.history.replaceState(state, "", utils.url_join_encode(
114 this.editor.base_url,
115 "edit",
116 path)
117 );
118 };
119
120 SaveWidget.prototype.update_last_modified = function (last_modified) {
121 if (last_modified) {
122 this._last_modified = new Date(last_modified);
123 } else {
124 this._last_modified = null;
125 }
126 this._render_last_modified();
127 };
128
129 SaveWidget.prototype._render_last_modified = function () {
130 /** actually set the text in the element, from our _last_modified value
131
132 called directly, and periodically in timeouts.
133 */
134 this._schedule_render_last_modified();
135 var el = this.element.find('span.last_modified');
136 if (!this._last_modified) {
137 el.text('').attr('title', 'never saved');
138 return;
139 }
140 var chkd = moment(this._last_modified);
141 var long_date = chkd.format('llll');
142 var human_date;
143 var tdelta = Math.ceil(new Date() - this._last_modified);
144 if (tdelta < 24 * H){
145 // less than 24 hours old, use relative date
146 human_date = chkd.fromNow();
147 } else {
148 // otherwise show calendar
149 // otherwise update every hour and show
150 // <Today | yesterday|...> at hh,mm,ss
151 human_date = chkd.calendar();
152 }
153 el.text(human_date).attr('title', long_date);
154 };
155
156
157 var S = 1000;
158 var M = 60*S;
159 var H = 60*M;
160 var thresholds = {
161 s: 45 * S,
162 m: 45 * M,
163 h: 22 * H
164 };
165 var _timeout_from_dt = function (ms) {
166 /** compute a timeout to update the last-modified timeout
167
168 based on the delta in milliseconds
169 */
170 if (ms < thresholds.s) {
171 return 5 * S;
172 } else if (ms < thresholds.m) {
173 return M;
174 } else {
175 return 5 * M;
176 }
177 };
178
179 SaveWidget.prototype._schedule_render_last_modified = function () {
180 /** schedule the next update to relative date
181
182 periodically updated, so short values like 'a few seconds ago' don't get stale.
183 */
184 var that = this;
185 if (!this._last_modified) {
186 return;
187 }
188 if ((this._last_modified_timeout)) {
189 clearTimeout(this._last_modified_timeout);
190 }
191 var dt = Math.ceil(new Date() - this._last_modified);
192 if (dt < 24 * H) {
193 this._last_modified_timeout = setTimeout(
194 $.proxy(this._render_last_modified, this),
195 _timeout_from_dt(dt)
196 );
197 }
198 };
199
200 return {'SaveWidget': SaveWidget};
201
202 });
@@ -1,141 +1,160 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 'jquery',
6 6 'base/js/utils',
7 7 'codemirror/lib/codemirror',
8 8 'codemirror/mode/meta',
9 9 'codemirror/addon/comment/comment',
10 10 'codemirror/addon/dialog/dialog',
11 11 'codemirror/addon/edit/closebrackets',
12 12 'codemirror/addon/edit/matchbrackets',
13 13 'codemirror/addon/search/searchcursor',
14 14 'codemirror/addon/search/search',
15 15 'codemirror/keymap/emacs',
16 16 'codemirror/keymap/sublime',
17 17 'codemirror/keymap/vim',
18 18 ],
19 19 function($,
20 20 utils,
21 21 CodeMirror
22 22 ) {
23 23 "use strict";
24 24
25 25 var Editor = function(selector, options) {
26 26 var that = this;
27 27 this.selector = selector;
28 28 this.contents = options.contents;
29 29 this.events = options.events;
30 30 this.base_url = options.base_url;
31 31 this.file_path = options.file_path;
32 32 this.config = options.config;
33 33 this.codemirror = new CodeMirror($(this.selector)[0]);
34 34 this.generation = -1;
35 35
36 36 // It appears we have to set commands on the CodeMirror class, not the
37 37 // instance. I'd like to be wrong, but since there should only be one CM
38 38 // instance on the page, this is good enough for now.
39 39 CodeMirror.commands.save = $.proxy(this.save, this);
40 40
41 41 this.save_enabled = false;
42 42
43 43 this.config.loaded.then(function () {
44 44 // load codemirror config
45 45 var cfg = that.config.data.Editor || {};
46 46 var cmopts = $.extend(true, {}, // true = recursive copy
47 47 Editor.default_codemirror_options,
48 48 cfg.codemirror_options || {}
49 49 );
50 50 that._set_codemirror_options(cmopts);
51 51 that.events.trigger('config_changed.Editor', {config: that.config});
52 52 });
53 53 };
54 54
55 55 // default CodeMirror options
56 56 Editor.default_codemirror_options = {
57 57 extraKeys: {
58 58 "Tab" : "indentMore",
59 59 },
60 60 indentUnit: 4,
61 61 theme: "ipython",
62 62 lineNumbers: true,
63 63 };
64 64
65 65 Editor.prototype.load = function() {
66 66 /** load the file */
67 67 var that = this;
68 68 var cm = this.codemirror;
69 69 return this.contents.get(this.file_path, {type: 'file', format: 'text'})
70 70 .then(function(model) {
71 71 cm.setValue(model.content);
72 72
73 73 // Setting the file's initial value creates a history entry,
74 74 // which we don't want.
75 75 cm.clearHistory();
76 76
77 77 // Find and load the highlighting mode
78 78 utils.requireCodeMirrorMode(model.mimetype, function(spec) {
79 79 var mode = CodeMirror.getMode({}, spec);
80 80 cm.setOption('mode', mode);
81 81 });
82 82 that.save_enabled = true;
83 83 that.generation = cm.changeGeneration();
84 that.events.trigger("file_loaded.Editor", model);
84 85 },
85 86 function(error) {
86 87 cm.setValue("Error! " + error.message +
87 88 "\nSaving disabled.");
88 89 that.save_enabled = false;
89 90 }
90 91 );
91 92 };
93
94 Editor.prototype.get_filename = function () {
95 return utils.url_path_split(this.file_path)[1];
96
97 }
92 98
93 Editor.prototype.save = function() {
99 Editor.prototype.rename = function (new_name) {
100 /** rename the file */
101 var that = this;
102 var parent = utils.url_path_split(this.file_path)[0];
103 var new_path = utils.url_path_join(parent, new_name);
104 return this.contents.rename(this.file_path, new_path).then(
105 function (json) {
106 that.file_path = json.path;
107 that.events.trigger('file_renamed.Editor', json);
108 }
109 );
110 };
111
112 Editor.prototype.save = function () {
94 113 /** save the file */
95 114 if (!this.save_enabled) {
96 115 console.log("Not saving, save disabled");
97 116 return;
98 117 }
99 118 var model = {
100 119 path: this.file_path,
101 120 type: 'file',
102 121 format: 'text',
103 122 content: this.codemirror.getValue(),
104 123 };
105 124 var that = this;
106 125 // record change generation for isClean
107 126 this.generation = this.codemirror.changeGeneration();
108 return this.contents.save(this.file_path, model).then(function() {
109 that.events.trigger("save_succeeded.TextEditor");
127 return this.contents.save(this.file_path, model).then(function(data) {
128 that.events.trigger("file_saved.Editor", data);
110 129 });
111 130 };
112 131
113 132 Editor.prototype._set_codemirror_options = function (options) {
114 133 // update codemirror options from a dict
115 134 for (var opt in options) {
116 135 if (!options.hasOwnProperty(opt)) {
117 136 continue;
118 137 }
119 138 var value = options[opt];
120 139 if (value === null) {
121 140 value = CodeMirror.defaults[opt];
122 141 }
123 142 this.codemirror.setOption(opt, value);
124 143 }
125 144 };
126 145
127 146 Editor.prototype.update_codemirror_options = function (options) {
128 147 /** update codemirror options locally and save changes in config */
129 148 var that = this;
130 149 this._set_codemirror_options(options);
131 150 return this.config.update({
132 151 Editor: {
133 152 codemirror_options: options
134 153 }
135 154 }).then(
136 155 that.events.trigger('config_changed.Editor', {config: that.config})
137 156 );
138 157 };
139 158
140 159 return {Editor: Editor};
141 160 });
@@ -1,73 +1,80 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 'base/js/namespace',
6 6 'base/js/utils',
7 7 'base/js/page',
8 8 'base/js/events',
9 9 'contents',
10 10 'services/config',
11 11 'edit/js/editor',
12 12 'edit/js/menubar',
13 'edit/js/savewidget',
13 14 'edit/js/notificationarea',
14 15 'custom/custom',
15 16 ], function(
16 17 IPython,
17 18 utils,
18 19 page,
19 20 events,
20 21 contents,
21 22 configmod,
22 23 editmod,
23 24 menubar,
25 savewidget,
24 26 notificationarea
25 27 ){
26 28 page = new page.Page();
27 29
28 30 var base_url = utils.get_body_data('baseUrl');
29 31 var file_path = utils.get_body_data('filePath');
30 32 contents = new contents.Contents({base_url: base_url});
31 33 var config = new configmod.ConfigSection('edit', {base_url: base_url});
32 34 config.load();
33 35
34 36 var editor = new editmod.Editor('#texteditor-container', {
35 37 base_url: base_url,
36 38 events: events,
37 39 contents: contents,
38 40 file_path: file_path,
39 41 config: config,
40 42 });
41 43
42 44 // Make it available for debugging
43 45 IPython.editor = editor;
44 46
45 47 var menus = new menubar.MenuBar('#menubar', {
46 48 base_url: base_url,
47 49 editor: editor,
48 50 events: events,
49 51 });
50 52
53 var save_widget = new savewidget.SaveWidget('span#save_widget', {
54 editor: editor,
55 events: events,
56 });
57
51 58 var notification_area = new notificationarea.EditorNotificationArea(
52 59 '#notification_area', {
53 60 events: events,
54 61 });
55 62 notification_area.init_notification_widgets();
56 63
57 64 config.loaded.then(function() {
58 65 if (config.data.load_extensions) {
59 66 var nbextension_paths = Object.getOwnPropertyNames(
60 67 config.data.load_extensions);
61 68 IPython.load_extensions.apply(this, nbextension_paths);
62 69 }
63 70 });
64 71 editor.load();
65 72 page.show();
66 73
67 74 window.onbeforeunload = function () {
68 75 if (!editor.codemirror.isClean(editor.generation)) {
69 76 return "Unsaved changes will be lost. Close anyway?";
70 77 }
71 78 };
72 79
73 80 });
@@ -1,78 +1,81 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 <link rel="stylesheet" href="{{ static_url('components/codemirror/addon/dialog/dialog.css') }}">
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 header %}
19 19
20 <span id="filename">{{ basename }}</span>
20 <span id="save_widget" class="nav pull-left save_widget">
21 <span class="filename"></span>
22 <span class="last_modified"></span>
23 </span>
21 24
22 25 {% endblock %}
23 26
24 27 {% block site %}
25 28
26 29 <div id="menubar-container" class="container">
27 30 <div id="menubar">
28 31 <div id="menus" class="navbar navbar-default" role="navigation">
29 32 <div class="container-fluid">
30 33 <button type="button" class="btn btn-default navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
31 34 <i class="fa fa-bars"></i>
32 35 <span class="navbar-text">Menu</span>
33 36 </button>
34 37 <ul class="nav navbar-nav navbar-right">
35 38 <li id="notification_area"></li>
36 39 </ul>
37 40 <div class="navbar-collapse collapse">
38 41 <ul class="nav navbar-nav">
39 42 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">File</a>
40 43 <ul id="file-menu" class="dropdown-menu">
41 44 <li id="new-file"><a href="#">New</a></li>
42 45 <li id="save-file"><a href="#">Save</a></li>
43 46 </ul>
44 47 </li>
45 48 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Edit</a>
46 49 <ul id="edit-menu" class="dropdown-menu">
47 50 <li id="menu-find"><a href="#">Find</a></li>
48 51 <li id="menu-replace"><a href="#">Find &amp; Replace</a></li>
49 52 <li class="divider"></li>
50 53 <li class="dropdown-header">Key Map</li>
51 54 <li id="menu-keymap-default"><a href="#">Default<i class="fa"></i></a></li>
52 55 <li id="menu-keymap-sublime"><a href="#">Sublime Text<i class="fa"></i></a></li>
53 56 <li id="menu-keymap-vim"><a href="#">Vim<i class="fa"></i></a></li>
54 57 <li id="menu-keymap-emacs"><a href="#">emacs<i class="fa"></i></a></li>
55 58 </ul>
56 59 </li>
57 60 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">View</a>
58 61 <ul id="view-menu" class="dropdown-menu">
59 62 <li id="menu-line-numbers"><a href="#">Hide Line Numbers</a></li>
60 63 </ul>
61 64 </li>
62 65 </ul>
63 66 </div>
64 67 </div>
65 68 </div>
66 69 </div>
67 70 </div>
68 71
69 72 <div id="texteditor-container" class="container"></div>
70 73
71 74 {% endblock %}
72 75
73 76 {% block script %}
74 77
75 78 {{super()}}
76 79
77 80 <script src="{{ static_url("edit/js/main.js") }}" type="text/javascript" charset="utf-8"></script>
78 81 {% endblock %}
General Comments 0
You need to be logged in to leave comments. Login now