##// END OF EJS Templates
Making the input text area watch for `ENTER` in nb renames.
Brian Granger -
Show More
@@ -1,148 +1,148
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // SaveWidget
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var utils = IPython.utils;
15 15
16 16 var SaveWidget = function (selector) {
17 17 this.selector = selector;
18 18 if (this.selector !== undefined) {
19 19 this.element = $(selector);
20 20 this.style();
21 21 this.bind_events();
22 22 }
23 23 };
24 24
25 25
26 26 SaveWidget.prototype.style = function () {
27 27 this.element.find('span#save_widget').addClass('ui-widget');
28 28 this.element.find('span#notebook_name').addClass('ui-widget ui-widget-content');
29 29 this.element.find('span#save_status').addClass('ui-widget ui-widget-content')
30 30 .css({border: 'none', 'margin-left': '20px'});
31 31 };
32 32
33 33
34 34 SaveWidget.prototype.bind_events = function () {
35 35 var that = this;
36 36 this.element.find('span#notebook_name').click(function () {
37 37 that.rename_notebook();
38 38 });
39 39 this.element.find('span#notebook_name').hover(function () {
40 40 $(this).addClass("ui-state-hover");
41 41 }, function () {
42 42 $(this).removeClass("ui-state-hover");
43 43 });
44 44 $([IPython.events]).on('notebook_loaded.Notebook', function () {
45 45 that.set_last_saved();
46 46 that.update_notebook_name();
47 47 that.update_document_title();
48 48 });
49 49 $([IPython.events]).on('notebook_saved.Notebook', function () {
50 50 that.set_last_saved();
51 51 that.update_notebook_name();
52 52 that.update_document_title();
53 53 });
54 54 $([IPython.events]).on('notebook_save_failed.Notebook', function () {
55 55 that.set_save_status('');
56 56 });
57 57 };
58 58
59 59
60 60 SaveWidget.prototype.rename_notebook = function () {
61 61 var that = this;
62 62 var dialog = $('<div/>');
63 63 dialog.append(
64 64 $('<h3/>').html('Enter a new notebook name:')
65 65 .css({'margin-bottom': '10px'})
66 66 );
67 67 dialog.append(
68 68 $('<input/>').attr('type','text').attr('size','25')
69 69 .addClass('ui-widget ui-widget-content')
70 70 .attr('value',IPython.notebook.get_notebook_name())
71 71 );
72 72 // $(document).append(dialog);
73 73 dialog.dialog({
74 74 resizable: false,
75 75 modal: true,
76 76 title: "Rename Notebook",
77 77 closeText: "",
78 78 close: function(event, ui) {$(this).dialog('destroy').remove();},
79 79 buttons : {
80 80 "OK": function () {
81 81 var new_name = $(this).find('input').attr('value');
82 82 if (!IPython.notebook.test_notebook_name(new_name)) {
83 83 $(this).find('h3').html(
84 84 "Invalid notebook name. Notebook names must "+
85 85 "have 1 or more characters and can contain any characters " +
86 86 "except / and \\. Please enter a new notebook name:"
87 87 );
88 88 } else {
89 89 IPython.notebook.set_notebook_name(new_name);
90 90 IPython.notebook.save_notebook();
91 91 $(this).dialog('close');
92 92 }
93 93 },
94 94 "Cancel": function () {
95 95 $(this).dialog('close');
96 96 }
97 97 },
98 98 open : function (event, ui) {
99 99 var that = $(this);
100 100 // Upon ENTER, click the OK button.
101 that.keydown(function (event, ui) {
101 that.find('input[type="text"]').keydown(function (event, ui) {
102 102 if (event.which === 13) {
103 103 that.parent().find('button').first().click();
104 104 }
105 105 });
106 106 }
107 107 });
108 108 }
109 109
110 110
111 111 SaveWidget.prototype.update_notebook_name = function () {
112 112 var nbname = IPython.notebook.get_notebook_name();
113 113 this.element.find('span#notebook_name').html(nbname);
114 114 };
115 115
116 116
117 117 SaveWidget.prototype.update_document_title = function () {
118 118 var nbname = IPython.notebook.get_notebook_name();
119 119 document.title = nbname;
120 120 };
121 121
122 122
123 123 SaveWidget.prototype.update_url = function () {
124 124 var notebook_id = IPython.notebook.get_notebook_id();
125 125 if (notebook_id !== null) {
126 126 var new_url = $('body').data('baseProjectUrl') + notebook_id;
127 127 window.history.replaceState({}, '', new_url);
128 128 };
129 129 };
130 130
131 131
132 132 SaveWidget.prototype.set_save_status = function (msg) {
133 133 this.element.find('span#save_status').html(msg);
134 134 }
135 135
136 136
137 137 SaveWidget.prototype.set_last_saved = function () {
138 138 var d = new Date();
139 139 this.set_save_status('Last saved: '+d.format('mmm dd h:MM TT'));
140 140 };
141 141
142 142
143 143 IPython.SaveWidget = SaveWidget;
144 144
145 145 return IPython;
146 146
147 147 }(IPython));
148 148
General Comments 0
You need to be logged in to leave comments. Login now