widget_string.js
246 lines
| 8.8 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
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 | ||||
**/ | ||||
Jonathan Frederic
|
r15427 | define(["widgets/js/widget"], function(WidgetManager){ | ||
Jonathan Frederic
|
r14609 | |||
var HTMLView = IPython.DOMWidgetView.extend({ | ||||
Jonathan Frederic
|
r14263 | render : function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
update : function(){ | ||||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14663 | this.$el.html(this.model.get('value')); // CAUTION! .html(...) CALL MANDITORY!!! | ||
Jonathan Frederic
|
r14583 | return HTMLView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
}); | ||||
Jonathan Frederic
|
r14627 | WidgetManager.register_widget_view('HTMLView', HTMLView); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14447 | |||
Jonathan Frederic
|
r14609 | var LatexView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14447 | render : function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r14447 | this.update(); // Set defaults. | ||
}, | ||||
update : function(){ | ||||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14663 | this.$el.text(this.model.get('value')); | ||
Jonathan Frederic
|
r14448 | MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]); | ||
Jonathan Frederic
|
r14583 | return LatexView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14609 | }, | ||
Jonathan Frederic
|
r14447 | }); | ||
Jonathan Frederic
|
r14627 | WidgetManager.register_widget_view('LatexView', LatexView); | ||
Jonathan Frederic
|
r14447 | |||
Jonathan Frederic
|
r14609 | |||
Jonathan Frederic
|
r14834 | var TextareaView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14403 | render: function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r14263 | this.$el | ||
Jonathan Frederic
|
r14663 | .addClass('widget-hbox'); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$textbox = $('<textarea />') | ||
.attr('rows', 5) | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-text') | ||
Jonathan Frederic
|
r14263 | .appendTo(this.$el); | ||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
Jonathan Frederic
|
r14403 | |||
Jason Grout
|
r14487 | this.model.on('msg:custom', $.proxy(this._handle_textarea_msg, this)); | ||
Jessica B. Hamrick
|
r16323 | this.model.on('change:placeholder', function(model, value, options) { | ||
this.update_placeholder(value); | ||||
}, this); | ||||
this.update_placeholder(); | ||||
Jonathan Frederic
|
r14403 | }, | ||
_handle_textarea_msg: function (content){ | ||||
Jonathan Frederic
|
r14609 | // Handle when a custom msg is recieved from the back-end. | ||
Jonathan Frederic
|
r14403 | if (content.method == "scroll_to_bottom") { | ||
this.scroll_to_bottom(); | ||||
} | ||||
}, | ||||
Jessica B. Hamrick
|
r16323 | update_placeholder: function(value) { | ||
if (!value) { | ||||
value = this.model.get('placeholder'); | ||||
} | ||||
this.$textbox.attr('placeholder', value); | ||||
}, | ||||
Jonathan Frederic
|
r14403 | scroll_to_bottom: function (){ | ||
Jonathan Frederic
|
r14609 | // Scroll the text-area view to the bottom. | ||
Jonathan Frederic
|
r14403 | this.$textbox.scrollTop(this.$textbox[0].scrollHeight); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14403 | |||
Jonathan Frederic
|
r14570 | update: function(options){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
Jonathan Frederic
|
r14263 | this.$textbox.val(this.model.get('value')); | ||
Jonathan Frederic
|
r14292 | |||
Jonathan Frederic
|
r14570 | var disabled = this.model.get('disabled'); | ||
this.$textbox.prop('disabled', disabled); | ||||
Jonathan Frederic
|
r14302 | |||
Jonathan Frederic
|
r14570 | var description = this.model.get('description'); | ||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
Jonathan Frederic
|
r14663 | this.$label.text(description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14834 | return TextareaView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14609 | events: { | ||
// Dictionary of events and their handlers. | ||||
Jonathan Frederic
|
r14610 | "keyup textarea" : "handleChanging", | ||
"paste textarea" : "handleChanging", | ||||
"cut textarea" : "handleChanging" | ||||
Jonathan Frederic
|
r14609 | }, | ||
Jonathan Frederic
|
r14263 | |||
handleChanging: function(e) { | ||||
Jonathan Frederic
|
r14609 | // Handles and validates user input. | ||
Jonathan Frederic
|
r14569 | |||
// Calling model.set will trigger all of the other views of the | ||||
// model to update. | ||||
Jonathan Frederic
|
r14570 | this.model.set('value', e.target.value, {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14263 | }, | ||
}); | ||||
Jonathan Frederic
|
r14834 | WidgetManager.register_widget_view('TextareaView', TextareaView); | ||
Jonathan Frederic
|
r14252 | |||
Jonathan Frederic
|
r14609 | |||
Jonathan Frederic
|
r14834 | var TextView = IPython.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14403 | render: function(){ | ||
Jonathan Frederic
|
r14609 | // Called when view is rendered. | ||
Jonathan Frederic
|
r14263 | this.$el | ||
Jonathan Frederic
|
r14663 | .addClass('widget-hbox-single'); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
Jonathan Frederic
|
r14297 | .addClass('widget-hlabel') | ||
Jonathan Frederic
|
r14292 | .appendTo(this.$el) | ||
.hide(); | ||||
Jonathan Frederic
|
r14263 | this.$textbox = $('<input type="text" />') | ||
.addClass('input') | ||||
Jonathan Frederic
|
r14295 | .addClass('widget-text') | ||
Jonathan Frederic
|
r14263 | .appendTo(this.$el); | ||
Jonathan Frederic
|
r14314 | this.$el_to_style = this.$textbox; // Set default element to style | ||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
Jessica B. Hamrick
|
r16321 | this.model.on('change:placeholder', function(model, value, options) { | ||
this.update_placeholder(value); | ||||
}, this); | ||||
Jessica B. Hamrick
|
r16322 | |||
this.update_placeholder(); | ||||
Jessica B. Hamrick
|
r16321 | }, | ||
update_placeholder: function(value) { | ||||
if (!value) { | ||||
value = this.model.get('placeholder'); | ||||
} | ||||
this.$textbox.attr('placeholder', value); | ||||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14570 | update: function(options){ | ||
Jonathan Frederic
|
r14568 | // Update the contents of this view | ||
// | ||||
// Called when the model is changed. The model may have been | ||||
// changed by another view or by a state update from the back-end. | ||||
Jonathan Frederic
|
r14570 | if (options === undefined || options.updated_view != this) { | ||
if (this.$textbox.val() != this.model.get('value')) { | ||||
this.$textbox.val(this.model.get('value')); | ||||
} | ||||
var disabled = this.model.get('disabled'); | ||||
this.$textbox.prop('disabled', disabled); | ||||
var description = this.model.get('description'); | ||||
if (description.length === 0) { | ||||
this.$label.hide(); | ||||
} else { | ||||
Jonathan Frederic
|
r14663 | this.$label.text(description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14834 | return TextView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14609 | events: { | ||
// Dictionary of events and their handlers. | ||||
Jonathan Frederic
|
r14610 | "keyup input" : "handleChanging", | ||
"paste input" : "handleChanging", | ||||
"cut input" : "handleChanging", | ||||
Jonathan Frederic
|
r14746 | "keypress input" : "handleKeypress", | ||
"blur input" : "handleBlur", | ||||
"focusout input" : "handleFocusOut" | ||||
Jonathan Frederic
|
r14609 | }, | ||
Jonathan Frederic
|
r14263 | |||
handleChanging: function(e) { | ||||
Jonathan Frederic
|
r14741 | // Handles user input. | ||
Jonathan Frederic
|
r14569 | // Calling model.set will trigger all of the other views of the | ||
// model to update. | ||||
Jonathan Frederic
|
r14570 | this.model.set('value', e.target.value, {updated_view: this}); | ||
Jonathan Frederic
|
r14482 | this.touch(); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r14391 | |||
handleKeypress: function(e) { | ||||
Jonathan Frederic
|
r14609 | // Handles text submition | ||
Jonathan Frederic
|
r14391 | if (e.keyCode == 13) { // Return key | ||
Jonathan Frederic
|
r14403 | this.send({event: 'submit'}); | ||
Jonathan Frederic
|
r14746 | event.stopPropagation(); | ||
event.preventDefault(); | ||||
return false; | ||||
} | ||||
}, | ||||
handleBlur: function(e) { | ||||
// Prevent a blur from firing if the blur was not user intended. | ||||
// This is a workaround for the return-key focus loss bug. | ||||
// TODO: Is the original bug actually a fault of the keyboard | ||||
// manager? | ||||
if (e.relatedTarget === null) { | ||||
event.stopPropagation(); | ||||
event.preventDefault(); | ||||
return false; | ||||
} | ||||
}, | ||||
handleFocusOut: function(e) { | ||||
// Prevent a blur from firing if the blur was not user intended. | ||||
// This is a workaround for the return-key focus loss bug. | ||||
if (e.relatedTarget === null) { | ||||
event.stopPropagation(); | ||||
event.preventDefault(); | ||||
return false; | ||||
Jonathan Frederic
|
r14391 | } | ||
}, | ||||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r14834 | WidgetManager.register_widget_view('TextView', TextView); | ||
Jonathan Frederic
|
r14263 | }); | ||