widget_string.js
288 lines
| 9.2 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Jonathan Frederic
|
r14366 | |||
Jonathan Frederic
|
r17198 | define([ | ||
"widgets/js/widget", | ||||
Jonathan Frederic
|
r17216 | "jquery", | ||
MinRK
|
r17312 | "bootstrap", | ||
Jonathan Frederic
|
r17216 | ], function(widget, $){ | ||
Jonathan Frederic
|
r14366 | |||
Jonathan Frederic
|
r17198 | var HTMLView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14263 | render : function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14263 | this.update(); // Set defaults. | ||
}, | ||||
update : function(){ | ||||
Jonathan Frederic
|
r19176 | /** | ||
* 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
|
r14252 | |||
Jonathan Frederic
|
r14447 | |||
Jonathan Frederic
|
r17198 | var LatexView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14447 | render : function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14447 | this.update(); // Set defaults. | ||
}, | ||||
update : function(){ | ||||
Jonathan Frederic
|
r19176 | /** | ||
* 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. | ||||
*/ | ||||
Nicholas Bollweg (Nick)
|
r19196 | this.typeset(this.$el, this.model.get('value')); | ||
Jonathan Frederic
|
r14583 | return LatexView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14609 | }, | ||
Jonathan Frederic
|
r14447 | }); | ||
Jonathan Frederic
|
r14609 | |||
Jonathan Frederic
|
r17198 | var TextareaView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14403 | render: function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14263 | this.$el | ||
Jason Grout
|
r18120 | .addClass('widget-hbox widget-textarea'); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
.appendTo(this.$el) | ||||
Jonathan Frederic
|
r17929 | .addClass('widget-label') | ||
Jonathan Frederic
|
r14292 | .hide(); | ||
Jonathan Frederic
|
r14263 | this.$textbox = $('<textarea />') | ||
.attr('rows', 5) | ||||
Jonathan Frederic
|
r16952 | .addClass('widget-text form-control') | ||
Jonathan Frederic
|
r14263 | .appendTo(this.$el); | ||
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
|
r19176 | /** | ||
* 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
|
r19176 | /** | ||
* 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
|
r19176 | /** | ||
* 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 { | ||||
Nicholas Bollweg (Nick)
|
r19196 | this.typeset(this.$label, description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14834 | return TextareaView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r17724 | |||
update_attr: function(name, value) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Set a css attr of the widget view. | ||||
*/ | ||||
Jonathan Frederic
|
r19366 | if (name == 'padding' || name == 'margin') { | ||
this.$el.css(name, value); | ||||
} else { | ||||
this.$textbox.css(name, value); | ||||
} | ||||
Jonathan Frederic
|
r17724 | }, | ||
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
|
r19176 | /** | ||
* Handles and validates user input. | ||||
* | ||||
* 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
|
r14252 | |||
Jonathan Frederic
|
r14609 | |||
Jonathan Frederic
|
r17198 | var TextView = widget.DOMWidgetView.extend({ | ||
Jonathan Frederic
|
r14403 | render: function(){ | ||
Jonathan Frederic
|
r19176 | /** | ||
* Called when view is rendered. | ||||
*/ | ||||
Jonathan Frederic
|
r14263 | this.$el | ||
Jason Grout
|
r18120 | .addClass('widget-hbox widget-text'); | ||
Jonathan Frederic
|
r14292 | this.$label = $('<div />') | ||
Jonathan Frederic
|
r17929 | .addClass('widget-label') | ||
Jonathan Frederic
|
r14292 | .appendTo(this.$el) | ||
.hide(); | ||||
Jonathan Frederic
|
r14263 | this.$textbox = $('<input type="text" />') | ||
.addClass('input') | ||||
Jonathan Frederic
|
r16952 | .addClass('widget-text form-control') | ||
Jonathan Frederic
|
r14263 | .appendTo(this.$el); | ||
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
|
r19176 | /** | ||
* 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 { | ||||
Nicholas Bollweg (Nick)
|
r19196 | this.typeset(this.$label, description); | ||
Jonathan Frederic
|
r14570 | this.$label.show(); | ||
} | ||||
Jonathan Frederic
|
r14292 | } | ||
Jonathan Frederic
|
r14834 | return TextView.__super__.update.apply(this); | ||
Jonathan Frederic
|
r14263 | }, | ||
Jonathan Frederic
|
r17724 | |||
update_attr: function(name, value) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Set a css attr of the widget view. | ||||
*/ | ||||
Jonathan Frederic
|
r19366 | if (name == 'padding' || name == 'margin') { | ||
this.$el.css(name, value); | ||||
} else { | ||||
this.$textbox.css(name, value); | ||||
} | ||||
Jonathan Frederic
|
r17724 | }, | ||
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
|
r19176 | /** | ||
* Handles user input. | ||||
* | ||||
* 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
|
r19176 | /** | ||
* Handles text submition | ||||
*/ | ||||
Jonathan Frederic
|
r14391 | if (e.keyCode == 13) { // Return key | ||
Jonathan Frederic
|
r14403 | this.send({event: 'submit'}); | ||
weichm
|
r17803 | e.stopPropagation(); | ||
e.preventDefault(); | ||||
Jonathan Frederic
|
r14746 | return false; | ||
} | ||||
}, | ||||
handleBlur: function(e) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* 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? | ||||
*/ | ||||
Jonathan Frederic
|
r14746 | if (e.relatedTarget === null) { | ||
weichm
|
r17803 | e.stopPropagation(); | ||
e.preventDefault(); | ||||
Jonathan Frederic
|
r14746 | return false; | ||
} | ||||
}, | ||||
handleFocusOut: function(e) { | ||||
Jonathan Frederic
|
r19176 | /** | ||
* Prevent a blur from firing if the blur was not user intended. | ||||
* This is a workaround for the return-key focus loss bug. | ||||
*/ | ||||
Jonathan Frederic
|
r14746 | if (e.relatedTarget === null) { | ||
weichm
|
r17803 | e.stopPropagation(); | ||
e.preventDefault(); | ||||
Jonathan Frederic
|
r14746 | return false; | ||
Jonathan Frederic
|
r14391 | } | ||
}, | ||||
Jonathan Frederic
|
r14263 | }); | ||
Jonathan Frederic
|
r17198 | |||
return { | ||||
'HTMLView': HTMLView, | ||||
'LatexView': LatexView, | ||||
'TextareaView': TextareaView, | ||||
'TextView': TextView, | ||||
}; | ||||
Jonathan Frederic
|
r14263 | }); | ||