Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
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
/**
16
* TODO: Remove circular ref.
17
*/
18
this.notebook = undefined;
19
this.selector = selector;
20
this.events = options.events;
21
this._checkpoint_date = undefined;
22
this.keyboard_manager = options.keyboard_manager;
23
if (this.selector !== undefined) {
24
this.element = $(selector);
25
this.bind_events();
26
}
27
};
28
29
30
SaveWidget.prototype.bind_events = function () {
31
var that = this;
32
this.element.find('span.filename').click(function () {
33
that.rename_notebook({notebook: that.notebook});
34
});
35
this.events.on('notebook_loaded.Notebook', function () {
36
that.update_notebook_name();
37
that.update_document_title();
38
});
39
this.events.on('notebook_saved.Notebook', function () {
40
that.update_notebook_name();
41
that.update_document_title();
42
});
43
this.events.on('notebook_renamed.Notebook', function () {
44
that.update_notebook_name();
45
that.update_document_title();
46
that.update_address_bar();
47
});
48
this.events.on('notebook_save_failed.Notebook', function () {
49
that.set_save_status('Autosave Failed!');
50
});
51
this.events.on('notebook_read_only.Notebook', function () {
52
that.set_save_status('(read only)');
53
// disable future set_save_status
54
that.set_save_status = function () {};
55
});
56
this.events.on('checkpoints_listed.Notebook', function (event, data) {
57
that._set_last_checkpoint(data[0]);
58
});
59
60
this.events.on('checkpoint_created.Notebook', function (event, data) {
61
that._set_last_checkpoint(data);
62
});
63
this.events.on('set_dirty.Notebook', function (event, data) {
64
that.set_autosaved(data.value);
65
});
66
};
67
68
69
SaveWidget.prototype.rename_notebook = function (options) {
70
options = options || {};
71
var that = this;
72
var dialog_body = $('<div/>').append(
73
$("<p/>").addClass("rename-message")
74
.text('Enter a new notebook name:')
75
).append(
76
$("<br/>")
77
).append(
78
$('<input/>').attr('type','text').attr('size','25').addClass('form-control')
79
.val(options.notebook.get_notebook_name())
80
);
81
var d = dialog.modal({
82
title: "Rename Notebook",
83
body: dialog_body,
84
notebook: options.notebook,
85
keyboard_manager: this.keyboard_manager,
86
buttons : {
87
"OK": {
88
class: "btn-primary",
89
click: function () {
90
var new_name = d.find('input').val();
91
if (!options.notebook.test_notebook_name(new_name)) {
92
d.find('.rename-message').text(
93
"Invalid notebook name. Notebook names must "+
94
"have 1 or more characters and can contain any characters " +
95
"except :/\\. Please enter a new notebook name:"
96
);
97
return false;
98
} else {
99
d.find('.rename-message').text("Renaming...");
100
d.find('input[type="text"]').prop('disabled', true);
101
that.notebook.rename(new_name).then(
102
function () {
103
d.modal('hide');
104
}, function (error) {
105
d.find('.rename-message').text(error.message || 'Unknown error');
106
d.find('input[type="text"]').prop('disabled', false).focus().select();
107
}
108
);
109
return false;
110
}
111
}
112
},
113
"Cancel": {}
114
},
115
open : function () {
116
/**
117
* Upon ENTER, click the OK button.
118
*/
119
d.find('input[type="text"]').keydown(function (event) {
120
if (event.which === keyboard.keycodes.enter) {
121
d.find('.btn-primary').first().click();
122
return false;
123
}
124
});
125
d.find('input[type="text"]').focus().select();
126
}
127
});
128
};
129
130
131
SaveWidget.prototype.update_notebook_name = function () {
132
var nbname = this.notebook.get_notebook_name();
133
this.element.find('span.filename').text(nbname);
134
};
135
136
137
SaveWidget.prototype.update_document_title = function () {
138
var nbname = this.notebook.get_notebook_name();
139
document.title = nbname;
140
};
141
142
SaveWidget.prototype.update_address_bar = function(){
143
var base_url = this.notebook.base_url;
144
var path = this.notebook.notebook_path;
145
var state = {path : path};
146
window.history.replaceState(state, "", utils.url_join_encode(
147
base_url,
148
"notebooks",
149
path)
150
);
151
};
152
153
154
SaveWidget.prototype.set_save_status = function (msg) {
155
this.element.find('span.autosave_status').text(msg);
156
};
157
158
SaveWidget.prototype._set_last_checkpoint = function (checkpoint) {
159
if (checkpoint) {
160
this._checkpoint_date = new Date(checkpoint.last_modified);
161
} else {
162
this._checkpoint_date = null;
163
}
164
this._render_checkpoint();
165
};
166
167
SaveWidget.prototype._render_checkpoint = function () {
168
/** actually set the text in the element, from our _checkpoint value
169
170
called directly, and periodically in timeouts.
171
*/
172
this._schedule_render_checkpoint();
173
var el = this.element.find('span.checkpoint_status');
174
if (!this._checkpoint_date) {
175
el.text('').attr('title', 'no checkpoint');
176
return;
177
}
178
var chkd = moment(this._checkpoint_date);
179
var long_date = chkd.format('llll');
180
var human_date;
181
var tdelta = Math.ceil(new Date() - this._checkpoint_date);
182
if (tdelta < utils.time.milliseconds.d){
183
// less than 24 hours old, use relative date
184
human_date = chkd.fromNow();
185
} else {
186
// otherwise show calendar
187
// <Today | yesterday|...> at hh,mm,ss
188
human_date = chkd.calendar();
189
}
190
el.text('Last Checkpoint: ' + human_date).attr('title', long_date);
191
};
192
193
194
SaveWidget.prototype._schedule_render_checkpoint = function () {
195
/** schedule the next update to relative date
196
197
periodically updated, so short values like 'a few seconds ago' don't get stale.
198
*/
199
if (!this._checkpoint_date) {
200
return;
201
}
202
if ((this._checkpoint_timeout)) {
203
clearTimeout(this._checkpoint_timeout);
204
}
205
var dt = Math.ceil(new Date() - this._checkpoint_date);
206
this._checkpoint_timeout = setTimeout(
207
$.proxy(this._render_checkpoint, this),
208
utils.time.timeout_from_dt(dt)
209
);
210
};
211
212
SaveWidget.prototype.set_autosaved = function (dirty) {
213
if (dirty) {
214
this.set_save_status("(unsaved changes)");
215
} else {
216
this.set_save_status("(autosaved)");
217
}
218
};
219
220
// Backwards compatibility.
221
IPython.SaveWidget = SaveWidget;
222
223
return {'SaveWidget': SaveWidget};
224
225
});
226
227