Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
//----------------------------------------------------------------------------
2
// Copyright (C) 2008-2011 The IPython Development Team
3
//
4
// Distributed under the terms of the BSD License. The full license is in
5
// the file COPYING, distributed as part of this software.
6
//----------------------------------------------------------------------------
7
8
//============================================================================
9
// SaveWidget
10
//============================================================================
11
12
var IPython = (function (IPython) {
13
"use strict";
14
15
var utils = IPython.utils;
16
17
var SaveWidget = function (selector) {
18
this.selector = selector;
19
if (this.selector !== undefined) {
20
this.element = $(selector);
21
this.style();
22
this.bind_events();
23
}
24
};
25
26
27
SaveWidget.prototype.style = function () {
28
};
29
30
31
SaveWidget.prototype.bind_events = function () {
32
var that = this;
33
this.element.find('span#notebook_name').click(function () {
34
that.rename_notebook();
35
});
36
this.element.find('span#notebook_name').hover(function () {
37
$(this).addClass("ui-state-hover");
38
}, function () {
39
$(this).removeClass("ui-state-hover");
40
});
41
$([IPython.events]).on('notebook_loaded.Notebook', function () {
42
that.update_notebook_name();
43
that.update_document_title();
44
});
45
$([IPython.events]).on('notebook_saved.Notebook', function () {
46
that.update_notebook_name();
47
that.update_document_title();
48
});
49
$([IPython.events]).on('notebook_renamed.Notebook', function () {
50
that.update_notebook_name();
51
that.update_document_title();
52
that.update_address_bar();
53
});
54
$([IPython.events]).on('notebook_save_failed.Notebook', function () {
55
that.set_save_status('Autosave Failed!');
56
});
57
$([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
58
that.set_last_checkpoint(data[0]);
59
});
60
61
$([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
62
that.set_last_checkpoint(data);
63
});
64
$([IPython.events]).on('set_dirty.Notebook', function (event, data) {
65
that.set_autosaved(data.value);
66
});
67
};
68
69
70
SaveWidget.prototype.rename_notebook = function () {
71
var that = this;
72
var dialog = $('<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')
79
.val(IPython.notebook.get_notebook_name())
80
);
81
IPython.dialog.modal({
82
title: "Rename Notebook",
83
body: dialog,
84
buttons : {
85
"Cancel": {},
86
"OK": {
87
class: "btn-primary",
88
click: function () {
89
var new_name = $(this).find('input').val();
90
if (!IPython.notebook.test_notebook_name(new_name)) {
91
$(this).find('.rename-message').text(
92
"Invalid notebook name. Notebook names must "+
93
"have 1 or more characters and can contain any characters " +
94
"except :/\\. Please enter a new notebook name:"
95
);
96
return false;
97
} else {
98
IPython.notebook.rename(new_name);
99
}
100
}}
101
},
102
open : function (event, ui) {
103
var that = $(this);
104
// Upon ENTER, click the OK button.
105
that.find('input[type="text"]').keydown(function (event, ui) {
106
if (event.which === IPython.keyboard.keycodes.enter) {
107
that.find('.btn-primary').first().click();
108
return false;
109
}
110
});
111
that.find('input[type="text"]').focus().select();
112
}
113
});
114
}
115
116
117
SaveWidget.prototype.update_notebook_name = function () {
118
var nbname = IPython.notebook.get_notebook_name();
119
this.element.find('span#notebook_name').text(nbname);
120
};
121
122
123
SaveWidget.prototype.update_document_title = function () {
124
var nbname = IPython.notebook.get_notebook_name();
125
document.title = nbname;
126
};
127
128
SaveWidget.prototype.update_address_bar = function(){
129
var base_url = IPython.notebook.base_url;
130
var nbname = IPython.notebook.notebook_name;
131
var path = IPython.notebook.notebook_path;
132
var state = {path : path, name: nbname};
133
window.history.replaceState(state, "", utils.url_join_encode(
134
base_url,
135
"notebooks",
136
path,
137
nbname)
138
);
139
};
140
141
142
SaveWidget.prototype.set_save_status = function (msg) {
143
this.element.find('span#autosave_status').text(msg);
144
}
145
146
SaveWidget.prototype.set_checkpoint_status = function (msg) {
147
this.element.find('span#checkpoint_status').text(msg);
148
}
149
150
SaveWidget.prototype.set_last_checkpoint = function (checkpoint) {
151
if (!checkpoint) {
152
this.set_checkpoint_status("");
153
return;
154
}
155
var d = new Date(checkpoint.last_modified);
156
this.set_checkpoint_status(
157
"Last Checkpoint: " + d.format('mmm dd HH:MM')
158
);
159
}
160
161
SaveWidget.prototype.set_autosaved = function (dirty) {
162
if (dirty) {
163
this.set_save_status("(unsaved changes)");
164
} else {
165
this.set_save_status("(autosaved)");
166
}
167
};
168
169
170
IPython.SaveWidget = SaveWidget;
171
172
return IPython;
173
174
}(IPython));
175
176
177