Show More
@@ -1,177 +1,173 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 | define([ |
|
4 | define([ | |
5 | 'base/js/namespace', |
|
5 | 'base/js/namespace', | |
6 | 'jquery', |
|
6 | 'jquery', | |
7 | 'base/js/utils', |
|
7 | 'base/js/utils', | |
8 | 'base/js/dialog', |
|
8 | 'base/js/dialog', | |
9 | 'base/js/keyboard', |
|
9 | 'base/js/keyboard', | |
10 | 'dateformat', |
|
10 | 'dateformat', | |
11 | ], function(IPython, $, utils, dialog, keyboard) { |
|
11 | ], function(IPython, $, utils, dialog, keyboard) { | |
12 | "use strict"; |
|
12 | "use strict"; | |
13 |
|
13 | |||
14 | var SaveWidget = function (selector, options) { |
|
14 | var SaveWidget = function (selector, options) { | |
15 | // TODO: Remove circulat ref. |
|
15 | // TODO: Remove circulat ref. | |
16 | this.notebook = undefined; |
|
16 | this.notebook = undefined; | |
17 | this.selector = selector; |
|
17 | this.selector = selector; | |
18 | this.events = options.events; |
|
18 | this.events = options.events; | |
19 | this.keyboard_manager = options.keyboard_manager; |
|
19 | this.keyboard_manager = options.keyboard_manager; | |
20 | if (this.selector !== undefined) { |
|
20 | if (this.selector !== undefined) { | |
21 | this.element = $(selector); |
|
21 | this.element = $(selector); | |
22 | this.style(); |
|
|||
23 | this.bind_events(); |
|
22 | this.bind_events(); | |
24 | } |
|
23 | } | |
25 | }; |
|
24 | }; | |
26 |
|
25 | |||
27 | SaveWidget.prototype.style = function () { |
|
|||
28 | }; |
|
|||
29 |
|
||||
30 |
|
26 | |||
31 | SaveWidget.prototype.bind_events = function () { |
|
27 | SaveWidget.prototype.bind_events = function () { | |
32 | var that = this; |
|
28 | var that = this; | |
33 | this.element.find('span#notebook_name').click(function () { |
|
29 | this.element.find('span#notebook_name').click(function () { | |
34 | that.rename_notebook(); |
|
30 | that.rename_notebook(); | |
35 | }); |
|
31 | }); | |
36 | this.element.find('span#notebook_name').hover(function () { |
|
32 | this.element.find('span#notebook_name').hover(function () { | |
37 | $(this).addClass("ui-state-hover"); |
|
33 | $(this).addClass("ui-state-hover"); | |
38 | }, function () { |
|
34 | }, function () { | |
39 | $(this).removeClass("ui-state-hover"); |
|
35 | $(this).removeClass("ui-state-hover"); | |
40 | }); |
|
36 | }); | |
41 | this.events.on('notebook_loaded.Notebook', function () { |
|
37 | this.events.on('notebook_loaded.Notebook', function () { | |
42 | that.update_notebook_name(); |
|
38 | that.update_notebook_name(); | |
43 | that.update_document_title(); |
|
39 | that.update_document_title(); | |
44 | }); |
|
40 | }); | |
45 | this.events.on('notebook_saved.Notebook', function () { |
|
41 | this.events.on('notebook_saved.Notebook', function () { | |
46 | that.update_notebook_name(); |
|
42 | that.update_notebook_name(); | |
47 | that.update_document_title(); |
|
43 | that.update_document_title(); | |
48 | }); |
|
44 | }); | |
49 | this.events.on('notebook_renamed.Notebook', function () { |
|
45 | this.events.on('notebook_renamed.Notebook', function () { | |
50 | that.update_notebook_name(); |
|
46 | that.update_notebook_name(); | |
51 | that.update_document_title(); |
|
47 | that.update_document_title(); | |
52 | that.update_address_bar(); |
|
48 | that.update_address_bar(); | |
53 | }); |
|
49 | }); | |
54 | this.events.on('notebook_save_failed.Notebook', function () { |
|
50 | this.events.on('notebook_save_failed.Notebook', function () { | |
55 | that.set_save_status('Autosave Failed!'); |
|
51 | that.set_save_status('Autosave Failed!'); | |
56 | }); |
|
52 | }); | |
57 | this.events.on('checkpoints_listed.Notebook', function (event, data) { |
|
53 | this.events.on('checkpoints_listed.Notebook', function (event, data) { | |
58 | that.set_last_checkpoint(data[0]); |
|
54 | that.set_last_checkpoint(data[0]); | |
59 | }); |
|
55 | }); | |
60 |
|
56 | |||
61 | this.events.on('checkpoint_created.Notebook', function (event, data) { |
|
57 | this.events.on('checkpoint_created.Notebook', function (event, data) { | |
62 | that.set_last_checkpoint(data); |
|
58 | that.set_last_checkpoint(data); | |
63 | }); |
|
59 | }); | |
64 | this.events.on('set_dirty.Notebook', function (event, data) { |
|
60 | this.events.on('set_dirty.Notebook', function (event, data) { | |
65 | that.set_autosaved(data.value); |
|
61 | that.set_autosaved(data.value); | |
66 | }); |
|
62 | }); | |
67 | }; |
|
63 | }; | |
68 |
|
64 | |||
69 |
|
65 | |||
70 | SaveWidget.prototype.rename_notebook = function (options) { |
|
66 | SaveWidget.prototype.rename_notebook = function (options) { | |
71 | options = options || {}; |
|
67 | options = options || {}; | |
72 | var that = this; |
|
68 | var that = this; | |
73 | var dialog_body = $('<div/>').append( |
|
69 | var dialog_body = $('<div/>').append( | |
74 | $("<p/>").addClass("rename-message") |
|
70 | $("<p/>").addClass("rename-message") | |
75 | .text('Enter a new notebook name:') |
|
71 | .text('Enter a new notebook name:') | |
76 | ).append( |
|
72 | ).append( | |
77 | $("<br/>") |
|
73 | $("<br/>") | |
78 | ).append( |
|
74 | ).append( | |
79 | $('<input/>').attr('type','text').attr('size','25').addClass('form-control') |
|
75 | $('<input/>').attr('type','text').attr('size','25').addClass('form-control') | |
80 | .val(that.notebook.get_notebook_name()) |
|
76 | .val(that.notebook.get_notebook_name()) | |
81 | ); |
|
77 | ); | |
82 | dialog.modal({ |
|
78 | dialog.modal({ | |
83 | title: "Rename Notebook", |
|
79 | title: "Rename Notebook", | |
84 | body: dialog_body, |
|
80 | body: dialog_body, | |
85 | notebook: options.notebook, |
|
81 | notebook: options.notebook, | |
86 | keyboard_manager: this.keyboard_manager, |
|
82 | keyboard_manager: this.keyboard_manager, | |
87 | buttons : { |
|
83 | buttons : { | |
88 | "Cancel": {}, |
|
84 | "Cancel": {}, | |
89 | "OK": { |
|
85 | "OK": { | |
90 | class: "btn-primary", |
|
86 | class: "btn-primary", | |
91 | click: function () { |
|
87 | click: function () { | |
92 | var new_name = $(this).find('input').val(); |
|
88 | var new_name = $(this).find('input').val(); | |
93 | if (!that.notebook.test_notebook_name(new_name)) { |
|
89 | if (!that.notebook.test_notebook_name(new_name)) { | |
94 | $(this).find('.rename-message').text( |
|
90 | $(this).find('.rename-message').text( | |
95 | "Invalid notebook name. Notebook names must "+ |
|
91 | "Invalid notebook name. Notebook names must "+ | |
96 | "have 1 or more characters and can contain any characters " + |
|
92 | "have 1 or more characters and can contain any characters " + | |
97 | "except :/\\. Please enter a new notebook name:" |
|
93 | "except :/\\. Please enter a new notebook name:" | |
98 | ); |
|
94 | ); | |
99 | return false; |
|
95 | return false; | |
100 | } else { |
|
96 | } else { | |
101 | that.notebook.rename(new_name); |
|
97 | that.notebook.rename(new_name); | |
102 | } |
|
98 | } | |
103 | }} |
|
99 | }} | |
104 | }, |
|
100 | }, | |
105 | open : function (event, ui) { |
|
101 | open : function (event, ui) { | |
106 | var that = $(this); |
|
102 | var that = $(this); | |
107 | // Upon ENTER, click the OK button. |
|
103 | // Upon ENTER, click the OK button. | |
108 | that.find('input[type="text"]').keydown(function (event, ui) { |
|
104 | that.find('input[type="text"]').keydown(function (event, ui) { | |
109 | if (event.which === keyboard.keycodes.enter) { |
|
105 | if (event.which === keyboard.keycodes.enter) { | |
110 | that.find('.btn-primary').first().click(); |
|
106 | that.find('.btn-primary').first().click(); | |
111 | return false; |
|
107 | return false; | |
112 | } |
|
108 | } | |
113 | }); |
|
109 | }); | |
114 | that.find('input[type="text"]').focus().select(); |
|
110 | that.find('input[type="text"]').focus().select(); | |
115 | } |
|
111 | } | |
116 | }); |
|
112 | }); | |
117 | }; |
|
113 | }; | |
118 |
|
114 | |||
119 |
|
115 | |||
120 | SaveWidget.prototype.update_notebook_name = function () { |
|
116 | SaveWidget.prototype.update_notebook_name = function () { | |
121 | var nbname = this.notebook.get_notebook_name(); |
|
117 | var nbname = this.notebook.get_notebook_name(); | |
122 | this.element.find('span#notebook_name').text(nbname); |
|
118 | this.element.find('span#notebook_name').text(nbname); | |
123 | }; |
|
119 | }; | |
124 |
|
120 | |||
125 |
|
121 | |||
126 | SaveWidget.prototype.update_document_title = function () { |
|
122 | SaveWidget.prototype.update_document_title = function () { | |
127 | var nbname = this.notebook.get_notebook_name(); |
|
123 | var nbname = this.notebook.get_notebook_name(); | |
128 | document.title = nbname; |
|
124 | document.title = nbname; | |
129 | }; |
|
125 | }; | |
130 |
|
126 | |||
131 | SaveWidget.prototype.update_address_bar = function(){ |
|
127 | SaveWidget.prototype.update_address_bar = function(){ | |
132 | var base_url = this.notebook.base_url; |
|
128 | var base_url = this.notebook.base_url; | |
133 | var nbname = this.notebook.notebook_name; |
|
129 | var nbname = this.notebook.notebook_name; | |
134 | var path = this.notebook.notebook_path; |
|
130 | var path = this.notebook.notebook_path; | |
135 | var state = {path : path, name: nbname}; |
|
131 | var state = {path : path, name: nbname}; | |
136 | window.history.replaceState(state, "", utils.url_join_encode( |
|
132 | window.history.replaceState(state, "", utils.url_join_encode( | |
137 | base_url, |
|
133 | base_url, | |
138 | "notebooks", |
|
134 | "notebooks", | |
139 | path, |
|
135 | path, | |
140 | nbname) |
|
136 | nbname) | |
141 | ); |
|
137 | ); | |
142 | }; |
|
138 | }; | |
143 |
|
139 | |||
144 |
|
140 | |||
145 | SaveWidget.prototype.set_save_status = function (msg) { |
|
141 | SaveWidget.prototype.set_save_status = function (msg) { | |
146 | this.element.find('span#autosave_status').text(msg); |
|
142 | this.element.find('span#autosave_status').text(msg); | |
147 | }; |
|
143 | }; | |
148 |
|
144 | |||
149 | SaveWidget.prototype.set_checkpoint_status = function (msg) { |
|
145 | SaveWidget.prototype.set_checkpoint_status = function (msg) { | |
150 | this.element.find('span#checkpoint_status').text(msg); |
|
146 | this.element.find('span#checkpoint_status').text(msg); | |
151 | }; |
|
147 | }; | |
152 |
|
148 | |||
153 | SaveWidget.prototype.set_last_checkpoint = function (checkpoint) { |
|
149 | SaveWidget.prototype.set_last_checkpoint = function (checkpoint) { | |
154 | if (!checkpoint) { |
|
150 | if (!checkpoint) { | |
155 | this.set_checkpoint_status(""); |
|
151 | this.set_checkpoint_status(""); | |
156 | return; |
|
152 | return; | |
157 | } |
|
153 | } | |
158 | var d = new Date(checkpoint.last_modified); |
|
154 | var d = new Date(checkpoint.last_modified); | |
159 | this.set_checkpoint_status( |
|
155 | this.set_checkpoint_status( | |
160 | "Last Checkpoint: " + d.format('mmm dd HH:MM') |
|
156 | "Last Checkpoint: " + d.format('mmm dd HH:MM') | |
161 | ); |
|
157 | ); | |
162 | }; |
|
158 | }; | |
163 |
|
159 | |||
164 | SaveWidget.prototype.set_autosaved = function (dirty) { |
|
160 | SaveWidget.prototype.set_autosaved = function (dirty) { | |
165 | if (dirty) { |
|
161 | if (dirty) { | |
166 | this.set_save_status("(unsaved changes)"); |
|
162 | this.set_save_status("(unsaved changes)"); | |
167 | } else { |
|
163 | } else { | |
168 | this.set_save_status("(autosaved)"); |
|
164 | this.set_save_status("(autosaved)"); | |
169 | } |
|
165 | } | |
170 | }; |
|
166 | }; | |
171 |
|
167 | |||
172 | // Backwards compatability. |
|
168 | // Backwards compatability. | |
173 | IPython.SaveWidget = SaveWidget; |
|
169 | IPython.SaveWidget = SaveWidget; | |
174 |
|
170 | |||
175 | return {'SaveWidget': SaveWidget}; |
|
171 | return {'SaveWidget': SaveWidget}; | |
176 |
|
172 | |||
177 | }); |
|
173 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now