Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 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
this.editor = undefined;
16
this.selector = selector;
17
this.events = options.events;
18
this.editor = options.editor;
19
this._last_modified = undefined;
20
this._filename = undefined;
21
this.keyboard_manager = options.keyboard_manager;
22
if (this.selector !== undefined) {
23
this.element = $(selector);
24
this.bind_events();
25
}
26
};
27
28
29
SaveWidget.prototype.bind_events = function () {
30
var that = this;
31
this.element.find('span.filename').click(function () {
32
that.rename();
33
});
34
this.events.on('save_status_clean.Editor', function (evt) {
35
that.update_document_title();
36
});
37
this.events.on('save_status_dirty.Editor', function (evt) {
38
that.update_document_title(undefined, true);
39
});
40
this.events.on('file_loaded.Editor', function (evt, model) {
41
that.update_filename(model.name);
42
that.update_document_title(model.name);
43
that.update_last_modified(model.last_modified);
44
});
45
this.events.on('file_saved.Editor', function (evt, model) {
46
that.update_filename(model.name);
47
that.update_document_title(model.name);
48
that.update_last_modified(model.last_modified);
49
});
50
this.events.on('file_renamed.Editor', function (evt, model) {
51
that.update_filename(model.name);
52
that.update_document_title(model.name);
53
that.update_address_bar(model.path);
54
});
55
this.events.on('file_save_failed.Editor', function () {
56
that.set_save_status('Save Failed!');
57
});
58
};
59
60
61
SaveWidget.prototype.rename = function (options) {
62
options = options || {};
63
var that = this;
64
var dialog_body = $('<div/>').append(
65
$("<p/>").addClass("rename-message")
66
.text('Enter a new filename:')
67
).append(
68
$("<br/>")
69
).append(
70
$('<input/>').attr('type','text').attr('size','25').addClass('form-control')
71
.val(that.editor.get_filename())
72
);
73
var d = dialog.modal({
74
title: "Rename File",
75
body: dialog_body,
76
buttons : {
77
"OK": {
78
class: "btn-primary",
79
click: function () {
80
var new_name = d.find('input').val();
81
d.find('.rename-message').text("Renaming...");
82
d.find('input[type="text"]').prop('disabled', true);
83
that.editor.rename(new_name).then(
84
function () {
85
d.modal('hide');
86
}, function (error) {
87
d.find('.rename-message').text(error.message || 'Unknown error');
88
d.find('input[type="text"]').prop('disabled', false).focus().select();
89
}
90
);
91
return false;
92
}
93
},
94
"Cancel": {}
95
},
96
open : function () {
97
// Upon ENTER, click the OK button.
98
d.find('input[type="text"]').keydown(function (event) {
99
if (event.which === keyboard.keycodes.enter) {
100
d.find('.btn-primary').first().click();
101
return false;
102
}
103
});
104
d.find('input[type="text"]').focus().select();
105
}
106
});
107
};
108
109
110
SaveWidget.prototype.update_filename = function (filename) {
111
this.element.find('span.filename').text(filename);
112
};
113
114
SaveWidget.prototype.update_document_title = function (filename, dirty) {
115
if(filename){
116
this._filename = filename;
117
}
118
document.title = (dirty?'*':'')+this._filename;
119
};
120
121
SaveWidget.prototype.update_address_bar = function (path) {
122
var state = {path : path};
123
window.history.replaceState(state, "", utils.url_join_encode(
124
this.editor.base_url,
125
"edit",
126
path)
127
);
128
};
129
130
SaveWidget.prototype.update_last_modified = function (last_modified) {
131
if (last_modified) {
132
this._last_modified = new Date(last_modified);
133
} else {
134
this._last_modified = null;
135
}
136
this._render_last_modified();
137
};
138
139
SaveWidget.prototype._render_last_modified = function () {
140
/** actually set the text in the element, from our _last_modified value
141
142
called directly, and periodically in timeouts.
143
*/
144
this._schedule_render_last_modified();
145
var el = this.element.find('span.last_modified');
146
if (!this._last_modified) {
147
el.text('').attr('title', 'never saved');
148
return;
149
}
150
var chkd = moment(this._last_modified);
151
var long_date = chkd.format('llll');
152
var human_date;
153
var tdelta = Math.ceil(new Date() - this._last_modified);
154
if (tdelta < utils.time.milliseconds.d){
155
// less than 24 hours old, use relative date
156
human_date = chkd.fromNow();
157
} else {
158
// otherwise show calendar
159
// <Today | yesterday|...> at hh,mm,ss
160
human_date = chkd.calendar();
161
}
162
el.text(human_date).attr('title', long_date);
163
};
164
165
SaveWidget.prototype._schedule_render_last_modified = function () {
166
/** schedule the next update to relative date
167
168
periodically updated, so short values like 'a few seconds ago' don't get stale.
169
*/
170
if (!this._last_modified) {
171
return;
172
}
173
if ((this._last_modified_timeout)) {
174
clearTimeout(this._last_modified_timeout);
175
}
176
var dt = Math.ceil(new Date() - this._last_modified);
177
this._last_modified_timeout = setTimeout(
178
$.proxy(this._render_last_modified, this),
179
utils.time.timeout_from_dt(dt)
180
);
181
};
182
183
return {'SaveWidget': SaveWidget};
184
185
});
186
187