Show More
string.js
167 lines
| 5.5 KiB
| application/javascript
|
JavascriptLexer
|
r14366 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2013 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// StringWidget | ||||
//============================================================================ | ||||
/** | ||||
* @module IPython | ||||
* @namespace IPython | ||||
**/ | ||||
|
r14374 | define(["notebook/js/widget"], function(widget_manager){ | ||
|
r14263 | var StringWidgetModel = IPython.WidgetModel.extend({}); | ||
|
r14374 | widget_manager.register_widget_model('StringWidgetModel', StringWidgetModel); | ||
|
r14252 | |||
|
r14263 | var LabelView = IPython.WidgetView.extend({ | ||
// Called when view is rendered. | ||||
render : function(){ | ||||
this.update(); // Set defaults. | ||||
}, | ||||
// Handles: Backend -> Frontend Sync | ||||
// Frontent -> Frontend Sync | ||||
update : function(){ | ||||
|
r14268 | this.$el.html(this.model.get('value')); | ||
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
|
r14263 | }, | ||
}); | ||||
|
r14252 | |||
|
r14374 | widget_manager.register_widget_view('LabelView', LabelView); | ||
|
r14252 | |||
|
r14291 | var TextAreaView = IPython.WidgetView.extend({ | ||
|
r14263 | |||
// Called when view is rendered. | ||||
|
r14403 | render: function(){ | ||
|
r14263 | this.$el | ||
|
r14295 | .addClass('widget-hbox') | ||
|
r14278 | .html(''); | ||
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
|
r14297 | .addClass('widget-hlabel') | ||
|
r14292 | .hide(); | ||
|
r14263 | this.$textbox = $('<textarea />') | ||
.attr('rows', 5) | ||||
|
r14295 | .addClass('widget-text') | ||
|
r14263 | .appendTo(this.$el); | ||
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
|
r14263 | this.update(); // Set defaults. | ||
|
r14403 | |||
|
r14406 | this.model.on_msg($.proxy(this._handle_textarea_msg, this)); | ||
|
r14403 | }, | ||
_handle_textarea_msg: function (content){ | ||||
if (content.method == "scroll_to_bottom") { | ||||
this.scroll_to_bottom(); | ||||
} | ||||
}, | ||||
scroll_to_bottom: function (){ | ||||
this.$textbox.scrollTop(this.$textbox[0].scrollHeight); | ||||
|
r14263 | }, | ||
|
r14403 | |||
|
r14263 | |||
// Handles: Backend -> Frontend Sync | ||||
// Frontent -> Frontend Sync | ||||
|
r14403 | update: function(){ | ||
|
r14263 | if (!this.user_invoked_update) { | ||
this.$textbox.val(this.model.get('value')); | ||||
} | ||||
|
r14292 | |||
|
r14302 | var disabled = this.model.get('disabled'); | ||
this.$textbox.prop('disabled', disabled); | ||||
|
r14292 | var description = this.model.get('description'); | ||
if (description.length == 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
|
r14263 | }, | ||
|
r14403 | events: {"keyup textarea": "handleChanging", | ||
"paste textarea": "handleChanging", | ||||
"cut textarea": "handleChanging"}, | ||||
|
r14263 | |||
// Handles and validates user input. | ||||
handleChanging: function(e) { | ||||
this.user_invoked_update = true; | ||||
this.model.set('value', e.target.value); | ||||
|
r14278 | this.model.update_other_views(this); | ||
|
r14263 | this.user_invoked_update = false; | ||
}, | ||||
}); | ||||
|
r14374 | widget_manager.register_widget_view('TextAreaView', TextAreaView); | ||
|
r14252 | |||
|
r14291 | var TextBoxView = IPython.WidgetView.extend({ | ||
|
r14263 | |||
// Called when view is rendered. | ||||
|
r14403 | render: function(){ | ||
|
r14263 | this.$el | ||
|
r14295 | .addClass('widget-hbox-single') | ||
|
r14278 | .html(''); | ||
|
r14292 | this.$label = $('<div />') | ||
|
r14297 | .addClass('widget-hlabel') | ||
|
r14292 | .appendTo(this.$el) | ||
.hide(); | ||||
|
r14263 | this.$textbox = $('<input type="text" />') | ||
.addClass('input') | ||||
|
r14295 | .addClass('widget-text') | ||
|
r14263 | .appendTo(this.$el); | ||
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
// Handles: Backend -> Frontend Sync | ||||
// Frontent -> Frontend Sync | ||||
|
r14403 | update: function(){ | ||
|
r14392 | if (this.$textbox.val() != this.model.get('value')) { | ||
|
r14263 | this.$textbox.val(this.model.get('value')); | ||
} | ||||
|
r14292 | |||
|
r14302 | var disabled = this.model.get('disabled'); | ||
this.$textbox.prop('disabled', disabled); | ||||
|
r14292 | var description = this.model.get('description'); | ||
if (description.length == 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
this.$label.html(description); | ||||
this.$label.show(); | ||||
} | ||||
|
r14279 | return IPython.WidgetView.prototype.update.call(this); | ||
|
r14263 | }, | ||
|
r14403 | events: {"keyup input": "handleChanging", | ||
"paste input": "handleChanging", | ||||
"cut input": "handleChanging", | ||||
"keypress input": "handleKeypress"}, | ||||
|
r14263 | |||
// Handles and validates user input. | ||||
handleChanging: function(e) { | ||||
this.model.set('value', e.target.value); | ||||
|
r14278 | this.model.update_other_views(this); | ||
|
r14263 | }, | ||
|
r14391 | |||
// Handles text submition | ||||
handleKeypress: function(e) { | ||||
if (e.keyCode == 13) { // Return key | ||||
|
r14403 | this.send({event: 'submit'}); | ||
|
r14391 | } | ||
}, | ||||
|
r14263 | }); | ||
|
r14374 | widget_manager.register_widget_view('TextBoxView', TextBoxView); | ||
|
r14263 | }); | ||