##// END OF EJS Templates
Add placeholder to textarea as well as text
Jessica B. Hamrick -
Show More
@@ -1,234 +1,246
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2013 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 // StringWidget
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 **/
16 16
17 17 define(["widgets/js/widget"], function(WidgetManager){
18 18
19 19 var HTMLView = IPython.DOMWidgetView.extend({
20 20 render : function(){
21 21 // Called when view is rendered.
22 22 this.update(); // Set defaults.
23 23 },
24 24
25 25 update : function(){
26 26 // Update the contents of this view
27 27 //
28 28 // Called when the model is changed. The model may have been
29 29 // changed by another view or by a state update from the back-end.
30 30 this.$el.html(this.model.get('value')); // CAUTION! .html(...) CALL MANDITORY!!!
31 31 return HTMLView.__super__.update.apply(this);
32 32 },
33 33 });
34 34 WidgetManager.register_widget_view('HTMLView', HTMLView);
35 35
36 36
37 37 var LatexView = IPython.DOMWidgetView.extend({
38 38 render : function(){
39 39 // Called when view is rendered.
40 40 this.update(); // Set defaults.
41 41 },
42 42
43 43 update : function(){
44 44 // Update the contents of this view
45 45 //
46 46 // Called when the model is changed. The model may have been
47 47 // changed by another view or by a state update from the back-end.
48 48 this.$el.text(this.model.get('value'));
49 49 MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]);
50 50
51 51 return LatexView.__super__.update.apply(this);
52 52 },
53 53 });
54 54 WidgetManager.register_widget_view('LatexView', LatexView);
55 55
56 56
57 57 var TextareaView = IPython.DOMWidgetView.extend({
58 58 render: function(){
59 59 // Called when view is rendered.
60 60 this.$el
61 61 .addClass('widget-hbox');
62 62 this.$label = $('<div />')
63 63 .appendTo(this.$el)
64 64 .addClass('widget-hlabel')
65 65 .hide();
66 66 this.$textbox = $('<textarea />')
67 67 .attr('rows', 5)
68 68 .addClass('widget-text')
69 69 .appendTo(this.$el);
70 70 this.$el_to_style = this.$textbox; // Set default element to style
71 71 this.update(); // Set defaults.
72 72
73 73 this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this));
74 this.model.on('change:placeholder', function(model, value, options) {
75 this.update_placeholder(value);
76 }, this);
77
78 this.update_placeholder();
74 79 },
75 80
76 81 _handle_textarea_msg: function (content){
77 82 // Handle when a custom msg is recieved from the back-end.
78 83 if (content.method == "scroll_to_bottom") {
79 84 this.scroll_to_bottom();
80 85 }
81 86 },
82 87
88 update_placeholder: function(value) {
89 if (!value) {
90 value = this.model.get('placeholder');
91 }
92 this.$textbox.attr('placeholder', value);
93 },
94
83 95 scroll_to_bottom: function (){
84 96 // Scroll the text-area view to the bottom.
85 97 this.$textbox.scrollTop(this.$textbox[0].scrollHeight);
86 98 },
87 99
88 100 update: function(options){
89 101 // Update the contents of this view
90 102 //
91 103 // Called when the model is changed. The model may have been
92 104 // changed by another view or by a state update from the back-end.
93 105 if (options === undefined || options.updated_view != this) {
94 106 this.$textbox.val(this.model.get('value'));
95 107
96 108 var disabled = this.model.get('disabled');
97 109 this.$textbox.prop('disabled', disabled);
98 110
99 111 var description = this.model.get('description');
100 112 if (description.length === 0) {
101 113 this.$label.hide();
102 114 } else {
103 115 this.$label.text(description);
104 116 this.$label.show();
105 117 }
106 118 }
107 119 return TextareaView.__super__.update.apply(this);
108 120 },
109 121
110 122 events: {
111 123 // Dictionary of events and their handlers.
112 124 "keyup textarea" : "handleChanging",
113 125 "paste textarea" : "handleChanging",
114 126 "cut textarea" : "handleChanging"
115 127 },
116 128
117 129 handleChanging: function(e) {
118 130 // Handles and validates user input.
119 131
120 132 // Calling model.set will trigger all of the other views of the
121 133 // model to update.
122 134 this.model.set('value', e.target.value, {updated_view: this});
123 135 this.touch();
124 136 },
125 137 });
126 138 WidgetManager.register_widget_view('TextareaView', TextareaView);
127 139
128 140
129 141 var TextView = IPython.DOMWidgetView.extend({
130 142 render: function(){
131 143 // Called when view is rendered.
132 144 this.$el
133 145 .addClass('widget-hbox-single');
134 146 this.$label = $('<div />')
135 147 .addClass('widget-hlabel')
136 148 .appendTo(this.$el)
137 149 .hide();
138 150 this.$textbox = $('<input type="text" />')
139 151 .addClass('input')
140 152 .addClass('widget-text')
141 153 .appendTo(this.$el);
142 154 this.$el_to_style = this.$textbox; // Set default element to style
143 155 this.update(); // Set defaults.
144 156 this.model.on('change:placeholder', function(model, value, options) {
145 157 this.update_placeholder(value);
146 158 }, this);
147 159
148 160 this.update_placeholder();
149 161 },
150 162
151 163 update_placeholder: function(value) {
152 164 if (!value) {
153 165 value = this.model.get('placeholder');
154 166 }
155 167 this.$textbox.attr('placeholder', value);
156 168 },
157 169
158 170 update: function(options){
159 171 // Update the contents of this view
160 172 //
161 173 // Called when the model is changed. The model may have been
162 174 // changed by another view or by a state update from the back-end.
163 175 if (options === undefined || options.updated_view != this) {
164 176 if (this.$textbox.val() != this.model.get('value')) {
165 177 this.$textbox.val(this.model.get('value'));
166 178 }
167 179
168 180 var disabled = this.model.get('disabled');
169 181 this.$textbox.prop('disabled', disabled);
170 182
171 183 var description = this.model.get('description');
172 184 if (description.length === 0) {
173 185 this.$label.hide();
174 186 } else {
175 187 this.$label.text(description);
176 188 this.$label.show();
177 189 }
178 190 }
179 191 return TextView.__super__.update.apply(this);
180 192 },
181 193
182 194 events: {
183 195 // Dictionary of events and their handlers.
184 196 "keyup input" : "handleChanging",
185 197 "paste input" : "handleChanging",
186 198 "cut input" : "handleChanging",
187 199 "keypress input" : "handleKeypress",
188 200 "blur input" : "handleBlur",
189 201 "focusout input" : "handleFocusOut"
190 202 },
191 203
192 204 handleChanging: function(e) {
193 205 // Handles user input.
194 206
195 207 // Calling model.set will trigger all of the other views of the
196 208 // model to update.
197 209 this.model.set('value', e.target.value, {updated_view: this});
198 210 this.touch();
199 211 },
200 212
201 213 handleKeypress: function(e) {
202 214 // Handles text submition
203 215 if (e.keyCode == 13) { // Return key
204 216 this.send({event: 'submit'});
205 217 event.stopPropagation();
206 218 event.preventDefault();
207 219 return false;
208 220 }
209 221 },
210 222
211 223 handleBlur: function(e) {
212 224 // Prevent a blur from firing if the blur was not user intended.
213 225 // This is a workaround for the return-key focus loss bug.
214 226 // TODO: Is the original bug actually a fault of the keyboard
215 227 // manager?
216 228 if (e.relatedTarget === null) {
217 229 event.stopPropagation();
218 230 event.preventDefault();
219 231 return false;
220 232 }
221 233 },
222 234
223 235 handleFocusOut: function(e) {
224 236 // Prevent a blur from firing if the blur was not user intended.
225 237 // This is a workaround for the return-key focus loss bug.
226 238 if (e.relatedTarget === null) {
227 239 event.stopPropagation();
228 240 event.preventDefault();
229 241 return false;
230 242 }
231 243 },
232 244 });
233 245 WidgetManager.register_widget_view('TextView', TextView);
234 246 });
General Comments 0
You need to be logged in to leave comments. Login now